Skip to content

Commit

Permalink
added a testcase against .snap.new files existing
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 2, 2024
1 parent 7a00fa4 commit 9bae1a7
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions tests/integration_snapshots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,53 @@ fn executable_path() -> PathBuf {
.canonicalize()
.expect("error canonicalizing")
}

/// lists all files in the directory recursively
/// errors lead to panics as this is only used in a testcase
fn recurse_list_files(path: impl AsRef<Path>) -> Vec<std::fs::DirEntry> {
let mut buf = vec![];
let entries = std::fs::read_dir(path).unwrap();

for entry in entries {
let entry = entry.unwrap();
let meta = entry.metadata().unwrap();

if meta.is_dir() {
let mut subdir = recurse_list_files(entry.path());
buf.append(&mut subdir);
}
if meta.is_file() {
buf.push(entry);
}
}

buf
}

/// makes sure that no `.snap.new` snapshots exist
///
/// This testcase exists as [`insta`] behaves as following if seeing both a having both a `.snap.new` and a `.snap` file:
/// - the content of both files is equal, it will pass without failure and remove `.snap.new`-file
/// - if not, it will fail and show the diff
///
/// This behavior is problematic as
/// - we might not pick up on a `.snap.new` file being added as the testcase pass in CI.
/// - future contributors would be confused where this change comes from
/// => we are asserting that no such files exist
#[test]
fn no_new_snapshots() {
let files = recurse_list_files("test_outputs/");
let new_snaps=files.into_iter()
.map(|f| f.path())
.filter(|f| {
if f.as_path().to_str().unwrap().contains("snap.new") {
println!("{:?}",f.display());
}
if let Some(name)=f.file_name().unwrap().to_str() {
return name.ends_with("snap.new");
}
return false;
})
.collect::<Vec<_>>();
assert_eq!(new_snaps,Vec::<PathBuf>::new(), "`.snap.new` files exit, but should not. Did you\n- forget to run `cargo insta review` or\n- forget to move the `.snap.new` file to `.snap` after verifying the content is exactly as expected?");
}

0 comments on commit 9bae1a7

Please sign in to comment.