From 9bae1a7dbc8939bc30252df22ff5e5bbd418a7f9 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 2 Oct 2024 02:21:33 +0200 Subject: [PATCH] added a testcase against `.snap.new` files existing --- tests/integration_snapshots.rs | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/integration_snapshots.rs b/tests/integration_snapshots.rs index f6584f19..e273e687 100644 --- a/tests/integration_snapshots.rs +++ b/tests/integration_snapshots.rs @@ -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) -> Vec { + 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::>(); + assert_eq!(new_snaps,Vec::::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?"); +}