Skip to content

Commit

Permalink
Using TEMPDIR for absolute path instead of ~
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 27, 2024
1 parent 5de6001 commit 76c5c9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
6 changes: 3 additions & 3 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
22 changes: 11 additions & 11 deletions crates/tests/src/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand All @@ -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.",
Expand Down

0 comments on commit 76c5c9a

Please sign in to comment.