@@ -71,6 +71,7 @@ use tempfile::TempDir;
71
71
mod binary;
72
72
mod delete_pending;
73
73
mod inline;
74
+ mod unreferenced;
74
75
mod workspace;
75
76
76
77
/// Wraps a formatting function to be used as a `Stdio`
@@ -617,92 +618,6 @@ fn foo_always_missing() {
617
618
// Check for the name clash error message
618
619
assert ! ( error_output. contains( "Insta snapshot name clash detected between 'foo_always_missing' and 'test_foo_always_missing' in 'snapshot_name_clash_test'. Rename one function." ) ) ;
619
620
}
620
-
621
- #[ test]
622
- fn test_unreferenced_delete ( ) {
623
- let test_project = TestFiles :: new ( )
624
- . add_cargo_toml ( "test_unreferenced_delete" )
625
- . add_file (
626
- "src/lib.rs" ,
627
- r#"
628
- #[test]
629
- fn test_snapshot() {
630
- insta::assert_snapshot!("Hello, world!");
631
- }
632
- "#
633
- . to_string ( ) ,
634
- )
635
- . create_project ( ) ;
636
-
637
- // Run tests to create snapshots
638
- let output = test_project
639
- . insta_cmd ( )
640
- . args ( [ "test" , "--accept" ] )
641
- . output ( )
642
- . unwrap ( ) ;
643
-
644
- assert ! ( & output. status. success( ) ) ;
645
-
646
- // Manually add an unreferenced snapshot
647
- let unreferenced_snapshot_path = test_project
648
- . workspace_dir
649
- . join ( "src/snapshots/test_unreferenced_delete__unused_snapshot.snap" ) ;
650
- std:: fs:: create_dir_all ( unreferenced_snapshot_path. parent ( ) . unwrap ( ) ) . unwrap ( ) ;
651
- std:: fs:: write (
652
- & unreferenced_snapshot_path,
653
- r#"---
654
- source: src/lib.rs
655
- expression: "Unused snapshot"
656
- ---
657
- Unused snapshot
658
- "# ,
659
- )
660
- . unwrap ( ) ;
661
-
662
- assert_snapshot ! ( test_project. file_tree_diff( ) , @r"
663
- --- Original file tree
664
- +++ Updated file tree
665
- @@ -1,4 +1,8 @@
666
-
667
- + Cargo.lock
668
- Cargo.toml
669
- src
670
- src/lib.rs
671
- + src/snapshots
672
- + src/snapshots/test_unreferenced_delete__snapshot.snap
673
- + src/snapshots/test_unreferenced_delete__unused_snapshot.snap
674
- " ) ;
675
-
676
- // Run cargo insta test with --unreferenced=delete
677
- let output = test_project
678
- . insta_cmd ( )
679
- . args ( [
680
- "test" ,
681
- "--unreferenced=delete" ,
682
- "--accept" ,
683
- "--" ,
684
- "--nocapture" ,
685
- ] )
686
- . output ( )
687
- . unwrap ( ) ;
688
-
689
- assert ! ( & output. status. success( ) ) ;
690
-
691
- // We should now see the unreferenced snapshot deleted
692
- assert_snapshot ! ( test_project. file_tree_diff( ) , @r"
693
- --- Original file tree
694
- +++ Updated file tree
695
- @@ -1,4 +1,7 @@
696
-
697
- + Cargo.lock
698
- Cargo.toml
699
- src
700
- src/lib.rs
701
- + src/snapshots
702
- + src/snapshots/test_unreferenced_delete__snapshot.snap
703
- " ) ;
704
- }
705
-
706
621
#[ test]
707
622
fn test_hidden_snapshots ( ) {
708
623
let test_project = TestFiles :: new ( )
@@ -889,137 +804,3 @@ src/
889
804
stderr
890
805
) ;
891
806
}
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