Skip to content

Commit 6827b5b

Browse files
max-sixtyclaude
andcommitted
Add test for INSTA_UPDATE=unseen mode behavior
The bug affected not just messages but also the core behavior of INSTA_UPDATE=unseen mode (used by --accept-unseen). With the inverted logic, the behavior was completely backwards: Before fix (broken): - Existing snapshots → incorrectly created .snap.new files - New snapshots → incorrectly updated in-place After fix (correct): - Existing snapshots → update in-place ✓ - New snapshots → create .snap.new files for review ✓ This test verifies the fix correctly handles the unseen detection for both update behavior logic and display messages. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent ffabf9c commit 6827b5b

File tree

1 file changed

+148
-0
lines changed
  • cargo-insta/tests/functional

1 file changed

+148
-0
lines changed

cargo-insta/tests/functional/main.rs

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,3 +977,151 @@ fn test_new_snapshot() {
977977
"Should not show 'updated snapshot' for new snapshot, got:\n{stderr}"
978978
);
979979
}
980+
981+
#[test]
982+
fn test_unseen_mode_behavior() {
983+
use std::process::Stdio;
984+
985+
// Test that INSTA_UPDATE=unseen correctly distinguishes between existing and new snapshots
986+
// This mode is used by --accept-unseen and should:
987+
// - Update existing snapshots in-place
988+
// - Create .snap.new files for new snapshots
989+
990+
// Test 1: Existing snapshot should be updated in-place with INSTA_UPDATE=unseen
991+
let test_existing = TestFiles::new()
992+
.add_file(
993+
"Cargo.toml",
994+
r#"
995+
[package]
996+
name = "test_unseen_mode_existing"
997+
version = "0.1.0"
998+
edition = "2021"
999+
1000+
[dependencies]
1001+
insta = { path = '$PROJECT_PATH' }
1002+
"#
1003+
.to_string(),
1004+
)
1005+
.add_file(
1006+
"src/lib.rs",
1007+
r#"
1008+
#[test]
1009+
fn test_existing() {
1010+
insta::assert_snapshot!("existing", "Updated value");
1011+
}
1012+
"#
1013+
.to_string(),
1014+
)
1015+
.add_file(
1016+
"src/snapshots/test_unseen_mode_existing__existing.snap",
1017+
r#"---
1018+
source: src/lib.rs
1019+
expression: "\"Old value\""
1020+
---
1021+
Old value
1022+
"#
1023+
.to_string(),
1024+
)
1025+
.create_project();
1026+
1027+
let output = test_existing
1028+
.insta_cmd()
1029+
.env("INSTA_UPDATE", "unseen")
1030+
.args(["test", "--", "--nocapture"])
1031+
.stderr(Stdio::piped())
1032+
.output()
1033+
.unwrap();
1034+
1035+
assert!(
1036+
output.status.success(),
1037+
"Command should succeed\nstderr: {}",
1038+
String::from_utf8_lossy(&output.stderr)
1039+
);
1040+
1041+
let stderr = String::from_utf8_lossy(&output.stderr);
1042+
assert!(
1043+
stderr.contains("updated snapshot"),
1044+
"Expected 'updated snapshot' message for existing snapshot with INSTA_UPDATE=unseen, got:\n{stderr}"
1045+
);
1046+
1047+
// Verify the snapshot was updated in-place, not as a .new file
1048+
let snapshot_path = test_existing
1049+
.workspace_dir
1050+
.join("src/snapshots/test_unseen_mode_existing__existing.snap");
1051+
let snapshot_content = fs::read_to_string(&snapshot_path).unwrap();
1052+
assert!(
1053+
snapshot_content.contains("Updated value"),
1054+
"Existing snapshot should be updated in-place with INSTA_UPDATE=unseen"
1055+
);
1056+
let new_file_path = test_existing
1057+
.workspace_dir
1058+
.join("src/snapshots/test_unseen_mode_existing__existing.snap.new");
1059+
assert!(
1060+
!new_file_path.exists(),
1061+
"Should not create .snap.new file for existing snapshot with INSTA_UPDATE=unseen"
1062+
);
1063+
1064+
// Test 2: New snapshot should create .snap.new file with INSTA_UPDATE=unseen
1065+
let test_new = TestFiles::new()
1066+
.add_file(
1067+
"Cargo.toml",
1068+
r#"
1069+
[package]
1070+
name = "test_unseen_mode_new"
1071+
version = "0.1.0"
1072+
edition = "2021"
1073+
1074+
[dependencies]
1075+
insta = { path = '$PROJECT_PATH' }
1076+
"#
1077+
.to_string(),
1078+
)
1079+
.add_file(
1080+
"src/lib.rs",
1081+
r#"
1082+
#[test]
1083+
fn test_new() {
1084+
insta::assert_snapshot!("new", "New value");
1085+
}
1086+
"#
1087+
.to_string(),
1088+
)
1089+
.create_project();
1090+
1091+
let output = test_new
1092+
.insta_cmd()
1093+
.env("INSTA_UPDATE", "unseen")
1094+
.args(["test", "--", "--nocapture"])
1095+
.stderr(Stdio::piped())
1096+
.output()
1097+
.unwrap();
1098+
1099+
// The command should fail because there are pending snapshots to review
1100+
assert!(
1101+
!output.status.success(),
1102+
"Command should fail with pending snapshots\nstderr: {}",
1103+
String::from_utf8_lossy(&output.stderr)
1104+
);
1105+
1106+
let stderr = String::from_utf8_lossy(&output.stderr);
1107+
assert!(
1108+
stderr.contains("stored new snapshot"),
1109+
"Expected 'stored new snapshot' message for new snapshot with INSTA_UPDATE=unseen, got:\n{stderr}"
1110+
);
1111+
1112+
// Verify a .snap.new file was created for the new snapshot
1113+
let new_file_path = test_new
1114+
.workspace_dir
1115+
.join("src/snapshots/test_unseen_mode_new__new.snap.new");
1116+
assert!(
1117+
new_file_path.exists(),
1118+
"Should create .snap.new file for new snapshot with INSTA_UPDATE=unseen"
1119+
);
1120+
let snapshot_path = test_new
1121+
.workspace_dir
1122+
.join("src/snapshots/test_unseen_mode_new__new.snap");
1123+
assert!(
1124+
!snapshot_path.exists(),
1125+
"Should not create .snap file directly for new snapshot with INSTA_UPDATE=unseen"
1126+
);
1127+
}

0 commit comments

Comments
 (0)