From 02837742793bd797c94b05c5415fd3e558978683 Mon Sep 17 00:00:00 2001 From: Maximilian Roos <5635139+max-sixty@users.noreply.github.com> Date: Fri, 13 Dec 2024 10:07:49 -0800 Subject: [PATCH] Don't add `snapshot_kind: text` to snapshots (#690) This was extremely simple, sorry to leave it hanging. Will do a self-review tomorrow to confirm I haven't missed anything --- cargo-insta/tests/functional/main.rs | 60 ++++++++++++++++++++++- cargo-insta/tests/functional/workspace.rs | 1 - insta/src/snapshot.rs | 10 ++-- 3 files changed, 62 insertions(+), 9 deletions(-) diff --git a/cargo-insta/tests/functional/main.rs b/cargo-insta/tests/functional/main.rs index 67438f93..59856fc5 100644 --- a/cargo-insta/tests/functional/main.rs +++ b/cargo-insta/tests/functional/main.rs @@ -359,13 +359,12 @@ Hello, world! assert_snapshot!(test_current_insta.diff("src/snapshots/test_force_update_current__force_update.snap"), @r#" --- Original: src/snapshots/test_force_update_current__force_update.snap +++ Updated: src/snapshots/test_force_update_current__force_update.snap - @@ -1,8 +1,6 @@ + @@ -1,8 +1,5 @@ - --- source: src/lib.rs -expression: +expression: "\"Hello, world!\"" - +snapshot_kind: text --- Hello, world! - @@ -763,6 +762,63 @@ Hidden snapshot ); } +#[test] +fn test_snapshot_kind_behavior() { + let test_project = TestFiles::new() + .add_cargo_toml("test_snapshot_kind") + .add_file( + "src/lib.rs", + r#" +#[test] +fn test_snapshots() { + insta::assert_snapshot!("new snapshot"); + insta::assert_snapshot!("existing snapshot"); +} +"# + .to_string(), + ) + .add_file( + "src/snapshots/test_snapshot_kind__existing.snap", + r#"--- +source: src/lib.rs +expression: "\"existing snapshot\"" +snapshot_kind: text +--- +existing snapshot +"# + .to_string(), + ) + .create_project(); + + // Run the test with --accept to create the new snapshot + let output = test_project + .insta_cmd() + .args(["test", "--accept"]) + .output() + .unwrap(); + + assert!(output.status.success()); + + // Verify the new snapshot was created without snapshot_kind + let new_snapshot = std::fs::read_to_string( + test_project + .workspace_dir + .join("src/snapshots/test_snapshot_kind__snapshots.snap"), + ) + .unwrap(); + + assert!(!new_snapshot.contains("snapshot_kind:")); + + // Verify both snapshots work with --require-full-match + let output = test_project + .insta_cmd() + .args(["test", "--require-full-match"]) + .output() + .unwrap(); + + assert!(output.status.success()); +} + #[test] fn test_ignored_snapshots() { let test_project = TestFiles::new() diff --git a/cargo-insta/tests/functional/workspace.rs b/cargo-insta/tests/functional/workspace.rs index 0b4841e7..b3976fed 100644 --- a/cargo-insta/tests/functional/workspace.rs +++ b/cargo-insta/tests/functional/workspace.rs @@ -576,7 +576,6 @@ fn test_hello() { --- source: "../tests/lib.rs" expression: hello() - snapshot_kind: text --- Hello, world! "#); diff --git a/insta/src/snapshot.rs b/insta/src/snapshot.rs index ec80602e..9275f458 100644 --- a/insta/src/snapshot.rs +++ b/insta/src/snapshot.rs @@ -291,15 +291,13 @@ impl MetaData { fields.push(("input_file", Content::from(input_file))); } - let snapshot_type = Content::from(match self.snapshot_kind { - SnapshotKind::Text => "text", + match self.snapshot_kind { + SnapshotKind::Text => {} SnapshotKind::Binary { ref extension } => { fields.push(("extension", Content::from(extension.clone()))); - "binary" + fields.push(("snapshot_kind", Content::from("binary"))); } - }); - - fields.push(("snapshot_kind", snapshot_type)); + } Content::Struct("MetaData", fields) }