Skip to content

Commit

Permalink
Add tests for clean subcommand
Browse files Browse the repository at this point in the history
commit-id:ca58bd28
  • Loading branch information
ksew1 committed Dec 12, 2024
1 parent 9fa5fad commit 1c7ce06
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions crates/cairo-coverage/src/commands/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,70 @@ pub fn run(args: CleanArgs) -> Result<()> {
println!("Cleanup complete.");
Ok(())
}

#[cfg(test)]
mod tests {
use super::super::clean;
use super::CleanArgs;
use assert_fs::fixture::PathChild;
use assert_fs::TempDir;
use test_utils::{CreateFile, Utf8PathBufConversion};

#[test]
fn test_clean_removes_target_files() {
let temp_dir = TempDir::new().unwrap();

let sub_dir = temp_dir.child("sub_dir");
let sub_sub_dir = sub_dir.child("nested_dir");

let file_to_delete_1 = temp_dir.create_file("coverage.lcov");
let file_to_delete_2 = sub_dir.create_file("coverage.lcov");
let file_to_delete_3 = sub_sub_dir.create_file("coverage.lcov");
let non_target_file = temp_dir.create_file("keep_this.txt");

let clean_args = CleanArgs {
root_dir: temp_dir.to_utf8_path_buf(),
files_to_delete: "coverage.lcov".into(),
};

clean::run(clean_args).unwrap();

assert!(!file_to_delete_1.exists());
assert!(!file_to_delete_2.exists());
assert!(!file_to_delete_3.exists());
assert!(non_target_file.exists());
}

#[test]
fn test_clean_does_nothing_for_nonexistent_files() {
let temp_dir = TempDir::new().unwrap();

let sub_dir = temp_dir.child("sub_dir");
let file_1 = temp_dir.create_file("some_file.txt");
let file_2 = sub_dir.create_file("another_file.rs");

let clean_args = CleanArgs {
root_dir: temp_dir.to_utf8_path_buf(),
files_to_delete: "nonexistent_file.txt".into(),
};

clean::run(clean_args).unwrap();

assert!(file_1.exists());
assert!(file_2.exists());
}

#[test]
fn test_clean_handles_empty_directory() {
let temp_dir = TempDir::new().unwrap();

let clean_args = CleanArgs {
root_dir: temp_dir.to_utf8_path_buf(),
files_to_delete: "coverage.lcov".into(),
};

clean::run(clean_args).unwrap();

// No assertion needed—just ensuring no panics or errors occur
}
}

0 comments on commit 1c7ce06

Please sign in to comment.