Skip to content

Commit e91a7ac

Browse files
authored
fix unreferenced not being read from config file (#759)
closes #757
1 parent 7aa23e0 commit e91a7ac

File tree

3 files changed

+150
-6
lines changed

3 files changed

+150
-6
lines changed

.typos.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[default]
2-
extend-words = { "Foled" = "Foled" }
2+
extend-words = { "Foled" = "Foled" }

cargo-insta/src/cli.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ struct TestCommand {
197197
#[arg(long)]
198198
force_update_snapshots: bool,
199199
/// Handle unreferenced snapshots after a successful test run.
200-
#[arg(long, default_value = "ignore")]
201-
unreferenced: UnreferencedSnapshots,
200+
#[arg(long)]
201+
unreferenced: Option<UnreferencedSnapshots>,
202202
/// Filters to apply to the insta glob feature.
203203
#[arg(long)]
204204
glob_filter: Vec<String>,
@@ -742,9 +742,14 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
742742
// Legacy command
743743
if cmd.delete_unreferenced_snapshots {
744744
eprintln!("Warning: `--delete-unreferenced-snapshots` is deprecated. Use `--unreferenced=delete` instead.");
745-
cmd.unreferenced = UnreferencedSnapshots::Delete;
745+
cmd.unreferenced = Some(UnreferencedSnapshots::Delete);
746746
}
747747

748+
// If unreferenced wasn't specified, use the config file setting
749+
cmd.unreferenced = cmd
750+
.unreferenced
751+
.or_else(|| Some(loc.tool_config.test_unreferenced()));
752+
748753
// Prioritize the command line over the tool config
749754
let test_runner = match cmd.test_runner {
750755
TestRunner::Auto => loc.tool_config.test_runner(),
@@ -797,7 +802,12 @@ fn test_run(mut cmd: TestCommand, color: ColorWhen) -> Result<(), Box<dyn Error>
797802
// tests ran successfully
798803
if success {
799804
if let Some(ref snapshot_ref_path) = snapshot_ref_file {
800-
handle_unreferenced_snapshots(snapshot_ref_path.borrow(), &loc, cmd.unreferenced)?;
805+
handle_unreferenced_snapshots(
806+
snapshot_ref_path.borrow(),
807+
&loc,
808+
// we set this to `Some` above so can't be `None`
809+
cmd.unreferenced.unwrap(),
810+
)?;
801811
}
802812
}
803813

@@ -996,7 +1006,7 @@ fn prepare_test_runner<'snapshot_ref>(
9961006
proc.env("INSTA_CARGO_INSTA", "1");
9971007
proc.env("INSTA_CARGO_INSTA_VERSION", cargo_insta_version());
9981008

999-
let snapshot_ref_file = if cmd.unreferenced != UnreferencedSnapshots::Ignore {
1009+
let snapshot_ref_file = if cmd.unreferenced != Some(UnreferencedSnapshots::Ignore) {
10001010
match snapshot_ref_file {
10011011
Some(path) => Some(Cow::Borrowed(path)),
10021012
None => {

cargo-insta/tests/functional/main.rs

+134
Original file line numberDiff line numberDiff line change
@@ -889,3 +889,137 @@ src/
889889
stderr
890890
);
891891
}
892+
893+
#[test]
894+
fn test_unreferenced_config_reject() {
895+
// This test verifies that the `test.unreferenced: reject` setting in insta.yaml
896+
// is respected when no command-line argument is provided.
897+
//
898+
// Specifically, it tests the fix for issue #757, which ensures that:
899+
// 1. Config file settings are properly applied when not overridden by command-line flags
900+
// 2. Error handling for unreferenced snapshots properly updates the success flag
901+
let test_project = TestFiles::new()
902+
.add_cargo_toml("test_unreferenced_config_reject")
903+
.add_file(
904+
"src/lib.rs",
905+
r#"
906+
#[test]
907+
fn test_snapshot() {
908+
insta::assert_snapshot!("Hello, world!");
909+
}
910+
"#
911+
.to_string(),
912+
)
913+
.create_project();
914+
915+
// Run tests to create snapshots first (without the config file)
916+
let output = test_project
917+
.insta_cmd()
918+
.args(["test", "--accept"])
919+
.output()
920+
.unwrap();
921+
922+
assert!(output.status.success());
923+
924+
// Now add the config file after snapshot is created
925+
test_project.update_file(
926+
"insta.yaml",
927+
r#"
928+
test:
929+
unreferenced: reject
930+
"#
931+
.to_string(),
932+
);
933+
934+
// Manually add an unreferenced snapshot
935+
let unreferenced_snapshot_path = test_project
936+
.workspace_dir
937+
.join("src/snapshots/test_unreferenced_config_reject__unused_snapshot.snap");
938+
std::fs::create_dir_all(unreferenced_snapshot_path.parent().unwrap()).unwrap();
939+
std::fs::write(
940+
&unreferenced_snapshot_path,
941+
r#"---
942+
source: src/lib.rs
943+
expression: "Unused snapshot"
944+
---
945+
Unused snapshot
946+
"#,
947+
)
948+
.unwrap();
949+
950+
// Verify files exist
951+
let snapshot_path = test_project
952+
.workspace_dir
953+
.join("src/snapshots/test_unreferenced_config_reject__snapshot.snap");
954+
let unreferenced_path = test_project
955+
.workspace_dir
956+
.join("src/snapshots/test_unreferenced_config_reject__unused_snapshot.snap");
957+
958+
assert!(snapshot_path.exists(), "Normal snapshot file should exist");
959+
assert!(
960+
unreferenced_path.exists(),
961+
"Unreferenced snapshot file should exist"
962+
);
963+
964+
// First verify explicitly passing --unreferenced=reject does fail correctly
965+
let output = test_project
966+
.insta_cmd()
967+
.args(["test", "--unreferenced=reject", "--", "--nocapture"])
968+
.stderr(Stdio::piped())
969+
.output()
970+
.unwrap();
971+
972+
// The test should fail with explicit flag
973+
assert!(
974+
!output.status.success(),
975+
"Command should fail with explicit --unreferenced=reject flag"
976+
);
977+
978+
let stderr = String::from_utf8_lossy(&output.stderr);
979+
assert!(
980+
stderr.contains("encountered unreferenced snapshots"),
981+
"Expected error message about unreferenced snapshots, got: {}",
982+
stderr
983+
);
984+
985+
// Now run without flags - this should also fail due to the config file setting
986+
let output = test_project
987+
.insta_cmd()
988+
.args(["test", "--", "--nocapture"])
989+
.stderr(Stdio::piped())
990+
.output()
991+
.unwrap();
992+
993+
// The command should fail because of the config file setting
994+
assert!(
995+
!output.status.success(),
996+
"Command should fail when config file has test.unreferenced: reject"
997+
);
998+
999+
// Verify the error message mentions unreferenced snapshots
1000+
let stderr = String::from_utf8_lossy(&output.stderr);
1001+
assert!(
1002+
stderr.contains("encountered unreferenced snapshots"),
1003+
"Expected error message about unreferenced snapshots, got: {}",
1004+
stderr
1005+
);
1006+
1007+
// Run with --unreferenced=delete to clean up
1008+
let output = test_project
1009+
.insta_cmd()
1010+
.args(["test", "--unreferenced=delete", "--", "--nocapture"])
1011+
.output()
1012+
.unwrap();
1013+
1014+
assert!(output.status.success());
1015+
1016+
// Verify the unreferenced snapshot was deleted
1017+
assert!(
1018+
snapshot_path.exists(),
1019+
"Normal snapshot file should still exist"
1020+
);
1021+
assert!(
1022+
!unreferenced_path.exists(),
1023+
"Unreferenced snapshot file should have been deleted"
1024+
);
1025+
}

0 commit comments

Comments
 (0)