From a8b6cc221c07df25fca3bc1578fedfc0670027b6 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Sat, 12 Oct 2024 11:06:06 -0700 Subject: [PATCH] Small refactors (#664) --- cargo-insta/src/cli.rs | 31 ++++++++++++++-------------- cargo-insta/tests/functional/main.rs | 2 +- insta/src/snapshot.rs | 2 +- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/cargo-insta/src/cli.rs b/cargo-insta/src/cli.rs index e377c2ef..661af6a4 100644 --- a/cargo-insta/src/cli.rs +++ b/cargo-insta/src/cli.rs @@ -832,23 +832,22 @@ fn handle_unreferenced_snapshots( UnreferencedSnapshots::Ignore => return Ok(()), }; - let mut files = HashSet::new(); - match fs::read_to_string(snapshot_ref_path) { - Ok(s) => { - for line in s.lines() { - if let Ok(path) = fs::canonicalize(line) { - files.insert(path); - } - } - } - Err(err) => { - // if the file was not created, no test referenced - // snapshots. - if err.kind() != io::ErrorKind::NotFound { - return Err(err.into()); + let files = fs::read_to_string(snapshot_ref_path) + .map(|s| { + s.lines() + .filter_map(|line| fs::canonicalize(line).ok()) + .collect() + }) + .or_else(|err| { + if err.kind() == io::ErrorKind::NotFound { + // if the file was not created, no test referenced + // snapshots (though we also check for this in the calling + // function, so maybe duplicative...) + Ok(HashSet::new()) + } else { + Err(err) } - } - } + })?; let mut encountered_any = false; diff --git a/cargo-insta/tests/functional/main.rs b/cargo-insta/tests/functional/main.rs index ade947bc..67438f93 100644 --- a/cargo-insta/tests/functional/main.rs +++ b/cargo-insta/tests/functional/main.rs @@ -226,7 +226,7 @@ impl TestProject { format!("{} {}", style(&stdout_name).green(), line) })) .stderr(OutputFormatter(move |line| { - format!("{} {}", style(&stderr_name).red(), line) + format!("{} {}", style(&stderr_name).yellow(), line) })); command diff --git a/insta/src/snapshot.rs b/insta/src/snapshot.rs index a11ca207..d815a559 100644 --- a/insta/src/snapshot.rs +++ b/insta/src/snapshot.rs @@ -385,7 +385,7 @@ impl Snapshot { } } } - elog!("A snapshot uses a legacy snapshot format; please update it to the new format with `cargo insta test --force-update-snapshots --accept`.\n\nSnapshot is at: {}", p.to_string_lossy()); + elog!("A snapshot uses a legacy snapshot format; please update it to the new format with `cargo insta test --force-update-snapshots --accept`.\nSnapshot is at: {}", p.to_string_lossy()); rv };