diff --git a/crates/tests/src/lib.rs b/crates/tests/src/lib.rs index 1b5dbc5..8efe800 100644 --- a/crates/tests/src/lib.rs +++ b/crates/tests/src/lib.rs @@ -985,13 +985,13 @@ async fn touch() { .await; TestBuilder::new() - .command("touch ~/absolute_path.txt") - .assert_exists("~/absolute_path.txt") + .command("touch $TEMP_DIR/absolute_path.txt") + .assert_exists("$TEMP_DIR/absolute_path.txt") .run() .await; TestBuilder::new() - .command("touch ~/non_existent_dir/non_existent.txt") + .command("touch $TEMP_DIR/non_existent_dir/non_existent.txt") .assert_stderr_contains("No such file or directory") .assert_exit_code(1) .run() diff --git a/crates/tests/src/test_builder.rs b/crates/tests/src/test_builder.rs index c46fd3b..14215d7 100644 --- a/crates/tests/src/test_builder.rs +++ b/crates/tests/src/test_builder.rs @@ -117,6 +117,8 @@ impl TestBuilder { fn get_temp_dir(&mut self) -> &mut TempDir { if self.temp_dir.is_none() { self.temp_dir = Some(TempDir::new()); + let temp_dir_string = self.temp_dir_path().display().to_string(); + self.env_vars.insert("TEMP_DIR".to_string(), temp_dir_string); } self.temp_dir.as_mut().unwrap() } @@ -189,8 +191,13 @@ impl TestBuilder { pub fn assert_exists(&mut self, path: &str) -> &mut Self { self.ensure_temp_dir(); + let temp_dir = if let Some(temp_dir) = &self.temp_dir { + temp_dir.cwd.display().to_string() + } else { + "NO_TEMP_DIR".to_string() + }; self.assertions - .push(TestAssertion::FileExists(path.to_string())); + .push(TestAssertion::FileExists(path.to_string().replace("$TEMP_DIR", &temp_dir))); self } @@ -247,7 +254,7 @@ impl TestBuilder { ); } else if !self.expected_stderr_contains.is_empty() { assert!( - stderr_output.contains(&self.expected_stderr_contains), + stderr_output.contains(&self.expected_stderr_contains.replace("$TEMP_DIR", &temp_dir)), "\n\nFailed for: {}\nExpected stderr to contain: {}", self.command, self.expected_stderr_contains @@ -270,15 +277,8 @@ impl TestBuilder { for assertion in &self.assertions { match assertion { TestAssertion::FileExists(path) => { - let path_to_check = if path.starts_with('/') { - PathBuf::from(path) - } else if path.starts_with("~/") { - dirs::home_dir() - .unwrap() - .join(path.strip_prefix("~/").unwrap()) - } else { - cwd.join(path) - }; + let path_to_check = cwd.join(path); + assert!( path_to_check.exists(), "\n\nFailed for: {}\nExpected '{}' to exist.",