Skip to content

Commit dbf22fe

Browse files
authored
Move test scratch artifacts out of repo root (#75)
Route checkout-scoped test scratch directories through target/test-scratch so interrupted or failed test runs do not leave .tmp-style artifacts in the repository root. Keep sandbox-sensitive scratch outside OS temp where tests need read-only cwd writes to be blocked, while moving the worker-process unit-test scratch to OS temp.
1 parent 5fc510b commit dbf22fe

4 files changed

Lines changed: 62 additions & 3 deletions

File tree

src/worker_process.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7212,6 +7212,15 @@ mod tests {
72127212
.unwrap_or_else(|err| err.into_inner())
72137213
}
72147214

7215+
#[cfg(target_family = "unix")]
7216+
fn worker_process_test_temp_parent(label: &str) -> PathBuf {
7217+
let root = std::env::temp_dir()
7218+
.join("mcp-repl-test-scratch")
7219+
.join(label);
7220+
std::fs::create_dir_all(&root).expect("create worker process test temp parent");
7221+
root
7222+
}
7223+
72157224
#[test]
72167225
fn python_backend_prepares_windows_sandbox_launch() {
72177226
assert!(
@@ -9829,7 +9838,7 @@ mod tests {
98299838
let _guard = cwd_test_mutex().lock().expect("cwd mutex");
98309839
let temp = tempfile::Builder::new()
98319840
.prefix(".tmp-interrupt-tail-current-sandbox-")
9832-
.tempdir_in(env!("CARGO_MANIFEST_DIR"))
9841+
.tempdir_in(worker_process_test_temp_parent("worker-process"))
98339842
.expect("tempdir");
98349843
let sandbox_cwd = temp.path().to_path_buf();
98359844
let plan = SandboxCliPlan {

tests/common/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ const WINDOWS_TEST_TIMEOUT_CAP_SECS: f64 = 60.0;
3636
#[cfg(windows)]
3737
const WINDOWS_TEST_SERVER_MUTEX_NAME: &str = "Local\\mcp_repl_test_server_mutex";
3838

39+
pub(crate) fn checkout_test_temp_parent(label: &str) -> TestResult<PathBuf> {
40+
let root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
41+
.join("target")
42+
.join("test-scratch")
43+
.join(label);
44+
std::fs::create_dir_all(&root)?;
45+
Ok(root)
46+
}
47+
3948
#[cfg(windows)]
4049
struct WindowsSuiteServerMutexOwner {
4150
release_tx: std::sync::mpsc::Sender<()>,

tests/sandbox_state_updates.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,28 @@ fn home_scratch_dir(label: &str) -> TestResult<TempDir> {
200200
fn repo_scratch_dir(label: &str) -> TestResult<TempDir> {
201201
Ok(Builder::new()
202202
.prefix(&format!(".tmp-{label}-"))
203-
.tempdir_in(env!("CARGO_MANIFEST_DIR"))?)
203+
.tempdir_in(common::checkout_test_temp_parent("sandbox-state-updates")?)?)
204+
}
205+
206+
#[test]
207+
fn repo_scratch_dir_uses_non_temp_checkout_scratch_parent() -> TestResult<()> {
208+
let scratch = repo_scratch_dir("parent")?;
209+
assert_ne!(
210+
scratch.path().parent(),
211+
Some(Path::new(env!("CARGO_MANIFEST_DIR"))),
212+
"test scratch dirs must not be created directly in the repo root"
213+
);
214+
assert!(
215+
scratch
216+
.path()
217+
.starts_with(common::checkout_test_temp_parent("sandbox-state-updates")?),
218+
"sandbox cwd scratch dirs should stay under target/test-scratch"
219+
);
220+
assert!(
221+
!scratch.path().starts_with(std::env::temp_dir()),
222+
"sandbox cwd scratch dirs must stay outside OS temp so read-only sandbox tests can observe blocked cwd writes"
223+
);
224+
Ok(())
204225
}
205226

206227
fn write_file_code(path: &Path) -> TestResult<String> {

tests/write_stdin_behavior.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,27 @@ fn r_path_literal(path: &Path) -> TestResult<String> {
9696
fn workspace_tempdir() -> TestResult<tempfile::TempDir> {
9797
Ok(tempfile::Builder::new()
9898
.prefix("mcp-repl-write-stdin-")
99-
.tempdir_in(env!("CARGO_MANIFEST_DIR"))?)
99+
.tempdir_in(common::checkout_test_temp_parent("write-stdin")?)?)
100+
}
101+
102+
#[test]
103+
fn workspace_tempdir_uses_non_temp_checkout_scratch_parent() -> TestResult<()> {
104+
let temp = workspace_tempdir()?;
105+
assert_ne!(
106+
temp.path().parent(),
107+
Some(Path::new(env!("CARGO_MANIFEST_DIR"))),
108+
"test scratch dirs must not be created directly in the repo root"
109+
);
110+
assert!(
111+
temp.path()
112+
.starts_with(common::checkout_test_temp_parent("write-stdin")?),
113+
"workspace scratch dirs should stay under target/test-scratch"
114+
);
115+
assert!(
116+
!temp.path().starts_with(std::env::temp_dir()),
117+
"workspace scratch dirs must stay outside OS temp because some tests remove or override worker temp env"
118+
);
119+
Ok(())
100120
}
101121

102122
async fn wait_until_path_exists(path: &Path, label: &str) -> TestResult<()> {

0 commit comments

Comments
 (0)