Skip to content

Commit

Permalink
Increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed Sep 26, 2024
1 parent 74ecc51 commit 77a489b
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/shell/src/commands/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl ShellCommand for TouchCommand {
Box::pin(futures::future::ready(match execute_touch(&mut context) {
Ok(_) => ExecuteResult::from_exit_code(0),
Err(e) => {
let _ = context.stderr.write_all(format!("{:#}", e).as_bytes());
let _ = context.stderr.write_all(format!("{:?}", e).as_bytes());
ExecuteResult::from_exit_code(1)
}
}))
Expand Down
1 change: 1 addition & 0 deletions crates/tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ shell = { path = "../shell" }
anyhow = "1.0.87"
futures = "0.3.30"
tokio = { version = "1.40.0", features = ["full"] }
dirs = "5.0.1"

[dev-dependencies]
pretty_assertions = "1.0.0"
Expand Down
58 changes: 51 additions & 7 deletions crates/tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,6 @@ async fn uname() {
TestBuilder::new()
.command("uname")
.assert_exit_code(0)
.check_stderr(false)
.check_stdout(false)
.run()
.await;
Expand Down Expand Up @@ -939,12 +938,6 @@ async fn touch() {
.run()
.await;

TestBuilder::new()
.command("touch -d '2024-02-20 14:30:00' yyyymmddhhmms.txt")
.assert_exists("yyyymmddhhmms.txt")
.run()
.await;

TestBuilder::new()
.command("touch -d '2024-02-20 14:30' yyyy_mm_dd_hh_mm.txt")
.assert_exists("yyyy_mm_dd_hh_mm.txt")
Expand All @@ -968,6 +961,57 @@ async fn touch() {
.assert_exists("reference.txt")
.run()
.await;
// Test for non-existent file with -c option
TestBuilder::new()
.command("touch -c nonexistent.txt")
.assert_not_exists("nonexistent.txt")
.run()
.await;

// Test for invalid date format
TestBuilder::new()
.command("touch -d 'invalid date' invalid_date.txt")
.assert_stderr_contains("Unable to parse date: invalid date\n")
.assert_exit_code(1)
.run()
.await;

// Test for invalid timestamp format
TestBuilder::new()
.command("touch -t 9999999999 invalid_timestamp.txt")
.assert_stderr_contains("invalid date format '9999999999'\n")
.assert_exit_code(1)
.run()
.await;

TestBuilder::new()
.command("touch ~/absolute_path.txt")
.assert_exists("~/absolute_path.txt")
.run()
.await;

TestBuilder::new()
.command("touch /non_existent_dir/non_existent.txt")
.assert_stderr_contains("No such file or directory")
.assert_exit_code(1)
.run()
.await;

// Test with -h option on a symlink
TestBuilder::new()
.command("touch original.txt && ln -s original.txt symlink.txt && touch -h symlink.txt")
.assert_exists("symlink.txt")
.run()
.await;

// Test with multiple files, including one that doesn't exist
TestBuilder::new()
.command("touch existing.txt && touch existing.txt nonexistent.txt another_existing.txt")
.assert_exists("existing.txt")
.assert_exists("nonexistent.txt")
.assert_exists("another_existing.txt")
.run()
.await;
}

#[cfg(test)]
Expand Down
37 changes: 29 additions & 8 deletions crates/tests/src/test_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub struct TestBuilder {
expected_exit_code: i32,
expected_stderr: String,
expected_stdout: String,
expected_stderr_contains: String,
assertions: Vec<TestAssertion>,
assert_stdout: bool,
assert_stderr: bool,
Expand Down Expand Up @@ -101,9 +102,10 @@ impl TestBuilder {
expected_exit_code: 0,
expected_stderr: Default::default(),
expected_stdout: Default::default(),
expected_stderr_contains: Default::default(),
assertions: Default::default(),
assert_stdout: true,
assert_stderr: true,
assert_stderr: false,
}
}

Expand Down Expand Up @@ -163,6 +165,15 @@ impl TestBuilder {

pub fn assert_stderr(&mut self, output: &str) -> &mut Self {
self.expected_stderr.push_str(output);
self.assert_stderr = true;
self.expected_stderr_contains.clear();
self
}

pub fn assert_stderr_contains(&mut self, output: &str) -> &mut Self {
self.expected_stderr_contains.push_str(output);
self.assert_stderr = false;
self.expected_stderr.clear();
self
}

Expand All @@ -176,11 +187,6 @@ impl TestBuilder {
self
}

pub fn check_stderr(&mut self, check_stderr: bool) -> &mut Self {
self.assert_stderr = check_stderr;
self
}

pub fn assert_exists(&mut self, path: &str) -> &mut Self {
self.ensure_temp_dir();
self.assertions
Expand Down Expand Up @@ -231,13 +237,21 @@ impl TestBuilder {
} else {
"NO_TEMP_DIR".to_string()
};
let stderr_output = stderr_handle.await.unwrap();
if self.assert_stderr {
assert_eq!(
stderr_handle.await.unwrap(),
stderr_output,
self.expected_stderr.replace("$TEMP_DIR", &temp_dir),
"\n\nFailed for: {}",
self.command
);
} else if !self.expected_stderr_contains.is_empty() {
assert!(
stderr_output.contains(&self.expected_stderr_contains),
"\n\nFailed for: {}\nExpected stderr to contain: {}",
self.command,
self.expected_stderr_contains
);
}
if self.assert_stdout {
assert_eq!(
Expand All @@ -256,8 +270,15 @@ 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)
};
assert!(
cwd.join(path).exists(),
path_to_check.exists(),
"\n\nFailed for: {}\nExpected '{}' to exist.",
self.command,
path,
Expand Down

0 comments on commit 77a489b

Please sign in to comment.