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

Don't add snapshot_kind: text to snapshots #690

Merged
merged 7 commits into from
Dec 13, 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
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
Loading