@@ -889,3 +889,137 @@ src/
889
889
stderr
890
890
) ;
891
891
}
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