Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable inline snapshots to be force-updated #569

Merged
merged 5 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ All notable changes to insta and cargo-insta are documented here.
themselves contain `###`. If there are no existing `#` characters in the
snapshot value, a single `#` will be used. #540

- Inline snapshots can now be updated with `--force-update-snapshots`. #569

- `cargo insta test` accepts multiple `--exclude` flags. #520

- `test` `runner` in insta's yaml config works. #544
Expand Down
57 changes: 57 additions & 0 deletions cargo-insta/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ fn assert_success(output: &std::process::Output) {
// we would otherwise lose any output from the command such as `dbg!`
// statements.
eprint!("{}", String::from_utf8_lossy(&output.stderr));
eprint!("{}", String::from_utf8_lossy(&output.stdout));
assert!(
output.status.success(),
"Tests failed: {}\n{}",
Expand Down Expand Up @@ -716,6 +717,62 @@ fn test_virtual_manifest_single_crate() {
"### );
}

#[test]
fn test_force_update_inline_snapshot() {
let test_project = TestFiles::new()
.add_file(
"Cargo.toml",
r#"
[package]
name = "force-update-inline"
version = "0.1.0"
edition = "2021"

[dependencies]
insta = { path = '$PROJECT_PATH' }
"#
.to_string(),
)
.add_file(
"src/lib.rs",
r#####"
#[test]
fn test_excessive_hashes() {
insta::assert_snapshot!("foo", @r####"foo"####);
}
"#####
.to_string(),
)
.create_project();

// Run the test with --force-update-snapshots and --accept
let output = test_project
.cmd()
.args([
"test",
"--force-update-snapshots",
"--accept",
"--",
"--nocapture",
])
.output()
.unwrap();

assert_success(&output);

assert_snapshot!(test_project.diff("src/lib.rs"), @r#####"
--- Original: src/lib.rs
+++ Updated: src/lib.rs
@@ -1,5 +1,5 @@

#[test]
fn test_excessive_hashes() {
- insta::assert_snapshot!("foo", @r####"foo"####);
+ insta::assert_snapshot!("foo", @"foo");
}
"#####);
}

#[test]
fn test_hashtag_escape_in_inline_snapshot() {
let test_project = TestFiles::new()
Expand Down
15 changes: 2 additions & 13 deletions insta/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ impl<'a> SnapshotAssertionContext<'a> {
}
SnapshotUpdateBehavior::NewFile => {
if let Some(ref snapshot_file) = self.snapshot_file {
// File snapshot
if let Some(new_path) = new_snapshot.save_new(snapshot_file)? {
if should_print {
elog!(
Expand All @@ -415,19 +416,7 @@ impl<'a> SnapshotAssertionContext<'a> {
.bold(),
);
}

// special case for pending inline snapshots. Here we really only want
// to write the contents if the snapshot contents changed as the metadata
// is not retained for inline snapshots. This used to have different
// behavior in the past where we did indeed want to rewrite the snapshots
// entirely since we used to change the canonical snapshot format, but now
// this is significantly less likely to happen and seeing hundreds of unchanged
// inline snapshots in the review screen is not a lot of fun.
} else if self
.old_snapshot
.as_ref()
.map_or(true, |x| x.contents() != new_snapshot.contents())
{
} else {
PendingInlineSnapshot::new(
Some(new_snapshot),
self.old_snapshot.clone(),
Expand Down
Loading