Skip to content

Commit

Permalink
Don't add snapshot_kind: text to snapshots (#690)
Browse files Browse the repository at this point in the history
This was extremely simple, sorry to leave it hanging.

Will do a self-review tomorrow to confirm I haven't missed anything
  • Loading branch information
max-sixty authored Dec 13, 2024
1 parent f2783e5 commit 0283774
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
60 changes: 58 additions & 2 deletions cargo-insta/tests/functional/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!
-
Expand Down Expand Up @@ -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()
Expand Down
1 change: 0 additions & 1 deletion cargo-insta/tests/functional/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,6 @@ fn test_hello() {
---
source: "../tests/lib.rs"
expression: hello()
snapshot_kind: text
---
Hello, world!
"#);
Expand Down
10 changes: 4 additions & 6 deletions insta/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 0283774

Please sign in to comment.