Skip to content

Commit c29db01

Browse files
authored
Enable inline snapshots to be force-updated (#569)
1 parent abb6ba5 commit c29db01

File tree

3 files changed

+61
-13
lines changed

3 files changed

+61
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ All notable changes to insta and cargo-insta are documented here.
1919
themselves contain `###`. If there are no existing `#` characters in the
2020
snapshot value, a single `#` will be used. #540
2121

22+
- Inline snapshots can now be updated with `--force-update-snapshots`. #569
23+
2224
- `cargo insta test` accepts multiple `--exclude` flags. #520
2325

2426
- `test` `runner` in insta's yaml config works. #544

cargo-insta/tests/main.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ fn assert_success(output: &std::process::Output) {
7171
// we would otherwise lose any output from the command such as `dbg!`
7272
// statements.
7373
eprint!("{}", String::from_utf8_lossy(&output.stderr));
74+
eprint!("{}", String::from_utf8_lossy(&output.stdout));
7475
assert!(
7576
output.status.success(),
7677
"Tests failed: {}\n{}",
@@ -716,6 +717,62 @@ fn test_virtual_manifest_single_crate() {
716717
"### );
717718
}
718719

720+
#[test]
721+
fn test_force_update_inline_snapshot() {
722+
let test_project = TestFiles::new()
723+
.add_file(
724+
"Cargo.toml",
725+
r#"
726+
[package]
727+
name = "force-update-inline"
728+
version = "0.1.0"
729+
edition = "2021"
730+
731+
[dependencies]
732+
insta = { path = '$PROJECT_PATH' }
733+
"#
734+
.to_string(),
735+
)
736+
.add_file(
737+
"src/lib.rs",
738+
r#####"
739+
#[test]
740+
fn test_excessive_hashes() {
741+
insta::assert_snapshot!("foo", @r####"foo"####);
742+
}
743+
"#####
744+
.to_string(),
745+
)
746+
.create_project();
747+
748+
// Run the test with --force-update-snapshots and --accept
749+
let output = test_project
750+
.cmd()
751+
.args([
752+
"test",
753+
"--force-update-snapshots",
754+
"--accept",
755+
"--",
756+
"--nocapture",
757+
])
758+
.output()
759+
.unwrap();
760+
761+
assert_success(&output);
762+
763+
assert_snapshot!(test_project.diff("src/lib.rs"), @r#####"
764+
--- Original: src/lib.rs
765+
+++ Updated: src/lib.rs
766+
@@ -1,5 +1,5 @@
767+
768+
#[test]
769+
fn test_excessive_hashes() {
770+
- insta::assert_snapshot!("foo", @r####"foo"####);
771+
+ insta::assert_snapshot!("foo", @"foo");
772+
}
773+
"#####);
774+
}
775+
719776
#[test]
720777
fn test_hashtag_escape_in_inline_snapshot() {
721778
let test_project = TestFiles::new()

insta/src/runtime.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ impl<'a> SnapshotAssertionContext<'a> {
397397
}
398398
SnapshotUpdateBehavior::NewFile => {
399399
if let Some(ref snapshot_file) = self.snapshot_file {
400+
// File snapshot
400401
if let Some(new_path) = new_snapshot.save_new(snapshot_file)? {
401402
if should_print {
402403
elog!(
@@ -415,19 +416,7 @@ impl<'a> SnapshotAssertionContext<'a> {
415416
.bold(),
416417
);
417418
}
418-
419-
// special case for pending inline snapshots. Here we really only want
420-
// to write the contents if the snapshot contents changed as the metadata
421-
// is not retained for inline snapshots. This used to have different
422-
// behavior in the past where we did indeed want to rewrite the snapshots
423-
// entirely since we used to change the canonical snapshot format, but now
424-
// this is significantly less likely to happen and seeing hundreds of unchanged
425-
// inline snapshots in the review screen is not a lot of fun.
426-
} else if self
427-
.old_snapshot
428-
.as_ref()
429-
.map_or(true, |x| x.contents() != new_snapshot.contents())
430-
{
419+
} else {
431420
PendingInlineSnapshot::new(
432421
Some(new_snapshot),
433422
self.old_snapshot.clone(),

0 commit comments

Comments
 (0)