@@ -529,8 +529,6 @@ fn add_targets(
529
529
&& !test_files_added
530
530
&& target. kind . iter ( ) . any ( |t| t == "test" )
531
531
{
532
- // If any errors are encountered, just fallback to ignoring
533
- // nested files in subdirectories of `tests`
534
532
match manifest_path. parent ( ) {
535
533
None => None ,
536
534
Some ( package_dir) => {
@@ -902,12 +900,172 @@ mod cargo_fmt_tests {
902
900
#[ test]
903
901
fn returns_nested_files ( ) {
904
902
let target_dir = Path :: new ( "tests/nested-test-files/root-and-nested-tests/tests" ) ;
903
+ let exp_baz = PathBuf :: from (
904
+ "tests/nested-test-files/root-and-nested-tests/tests/nested/deeply-nested/baz.rs" ,
905
+ ) ;
906
+ let exp_foo_bar = PathBuf :: from (
907
+ "tests/nested-test-files/root-and-nested-tests/tests/nested/foo_bar.rs" ,
908
+ ) ;
909
+ let exp_other = PathBuf :: from (
910
+ "tests/nested-test-files/root-and-nested-tests/tests/nested/other.rs" ,
911
+ ) ;
905
912
assert_eq ! (
906
- vec![ PathBuf :: from(
907
- "tests/nested-test-files/root-and-nested-tests/tests/nested/foo_bar.rs"
908
- ) ] ,
913
+ vec![ exp_baz, exp_foo_bar, exp_other] ,
909
914
get_nested_integration_test_files( & target_dir, & target_dir) ,
910
915
)
911
916
}
912
917
}
918
+
919
+ mod get_targets_tests {
920
+ use super :: * ;
921
+
922
+ fn create_stub_target (
923
+ src_path : & str ,
924
+ test_files : Vec < PathBuf > ,
925
+ kind : & str ,
926
+ edition : & str ,
927
+ ) -> Target {
928
+ let path = PathBuf :: from ( src_path) ;
929
+ let canonicalized = fs:: canonicalize ( & path) . unwrap_or ( path) ;
930
+ Target {
931
+ path : canonicalized,
932
+ kind : String :: from ( kind) ,
933
+ edition : String :: from ( edition) ,
934
+ nested_int_test_files : test_files,
935
+ }
936
+ }
937
+
938
+ mod nested_test_files {
939
+ use super :: * ;
940
+
941
+ #[ test]
942
+ fn does_not_include_nested_test_files_when_disabled ( ) {
943
+ let edition = "2018" ;
944
+ let project_dir_base = "tests/nested-test-files/root-and-nested-tests" ;
945
+ let manifest_path = PathBuf :: from ( format ! ( "{}/Cargo.toml" , project_dir_base) ) ;
946
+ let mut exp_targets: BTreeSet < Target > = BTreeSet :: new ( ) ;
947
+ exp_targets. insert ( create_stub_target (
948
+ & format ! ( "{}/src/lib.rs" , project_dir_base) ,
949
+ vec ! [ ] ,
950
+ "lib" ,
951
+ edition,
952
+ ) ) ;
953
+ exp_targets. insert ( create_stub_target (
954
+ & format ! ( "{}/tests/bar.rs" , project_dir_base) ,
955
+ vec ! [ ] ,
956
+ "test" ,
957
+ edition,
958
+ ) ) ;
959
+ exp_targets. insert ( create_stub_target (
960
+ & format ! ( "{}/tests/foo.rs" , project_dir_base) ,
961
+ vec ! [ ] ,
962
+ "test" ,
963
+ edition,
964
+ ) ) ;
965
+ let strategy = CargoFmtStrategy :: Root ;
966
+ let act_targets = get_targets ( & strategy, Some ( & manifest_path) , false ) ;
967
+ assert_eq ! ( act_targets. unwrap( ) , exp_targets) ;
968
+ }
969
+
970
+ #[ test]
971
+ fn does_include_nested_test_files_when_enabled ( ) {
972
+ let edition = "2018" ;
973
+ let project_dir_base = "tests/nested-test-files/root-and-nested-tests" ;
974
+ let manifest_path = PathBuf :: from ( format ! ( "{}/Cargo.toml" , project_dir_base) ) ;
975
+ let exp_baz = PathBuf :: from ( format ! (
976
+ "{}/tests/nested/deeply-nested/baz.rs" ,
977
+ project_dir_base
978
+ ) ) ;
979
+ let exp_foo_bar =
980
+ PathBuf :: from ( format ! ( "{}/tests/nested/foo_bar.rs" , project_dir_base) ) ;
981
+ let exp_other =
982
+ PathBuf :: from ( format ! ( "{}/tests/nested/other.rs" , project_dir_base) ) ;
983
+ let mut exp_targets: BTreeSet < Target > = BTreeSet :: new ( ) ;
984
+ exp_targets. insert ( create_stub_target (
985
+ & format ! ( "{}/src/lib.rs" , project_dir_base) ,
986
+ vec ! [ ] ,
987
+ "lib" ,
988
+ edition,
989
+ ) ) ;
990
+ exp_targets. insert ( create_stub_target (
991
+ & format ! ( "{}/tests/bar.rs" , project_dir_base) ,
992
+ vec ! [ exp_baz, exp_foo_bar, exp_other] ,
993
+ "test" ,
994
+ edition,
995
+ ) ) ;
996
+ exp_targets. insert ( create_stub_target (
997
+ & format ! ( "{}/tests/foo.rs" , project_dir_base) ,
998
+ vec ! [ ] ,
999
+ "test" ,
1000
+ edition,
1001
+ ) ) ;
1002
+ let strategy = CargoFmtStrategy :: Root ;
1003
+ let act_targets = get_targets ( & strategy, Some ( & manifest_path) , true ) ;
1004
+ assert_eq ! ( act_targets. unwrap( ) , exp_targets) ;
1005
+ }
1006
+
1007
+ #[ test]
1008
+ fn returns_correct_targets_with_empty_tests_dir ( ) {
1009
+ let edition = "2015" ;
1010
+ let project_dir_base = "tests/nested-test-files/empty-tests-dir" ;
1011
+ let manifest_path = PathBuf :: from ( format ! ( "{}/Cargo.toml" , project_dir_base) ) ;
1012
+ let mut exp_targets: BTreeSet < Target > = BTreeSet :: new ( ) ;
1013
+ exp_targets. insert ( create_stub_target (
1014
+ & format ! ( "{}/src/lib.rs" , project_dir_base) ,
1015
+ vec ! [ ] ,
1016
+ "lib" ,
1017
+ edition,
1018
+ ) ) ;
1019
+ let strategy = CargoFmtStrategy :: Root ;
1020
+ let act_targets = get_targets ( & strategy, Some ( & manifest_path) , true ) ;
1021
+ assert_eq ! ( act_targets. unwrap( ) , exp_targets) ;
1022
+ }
1023
+
1024
+ #[ test]
1025
+ fn returns_correct_targets_with_no_tests_dir ( ) {
1026
+ let edition = "2015" ;
1027
+ let project_dir_base = "tests/nested-test-files/no-tests-dir" ;
1028
+ let manifest_path = PathBuf :: from ( format ! ( "{}/Cargo.toml" , project_dir_base) ) ;
1029
+ let mut exp_targets: BTreeSet < Target > = BTreeSet :: new ( ) ;
1030
+ exp_targets. insert ( create_stub_target (
1031
+ & format ! ( "{}/src/lib.rs" , project_dir_base) ,
1032
+ vec ! [ ] ,
1033
+ "lib" ,
1034
+ edition,
1035
+ ) ) ;
1036
+ let strategy = CargoFmtStrategy :: Root ;
1037
+ let act_targets = get_targets ( & strategy, Some ( & manifest_path) , true ) ;
1038
+ assert_eq ! ( act_targets. unwrap( ) , exp_targets) ;
1039
+ }
1040
+
1041
+ #[ test]
1042
+ fn returns_correct_targets_with_only_root_level_tests ( ) {
1043
+ let edition = "2015" ;
1044
+ let project_dir_base = "tests/nested-test-files/only-root-level-tests-dir" ;
1045
+ let manifest_path = PathBuf :: from ( format ! ( "{}/Cargo.toml" , project_dir_base) ) ;
1046
+ let mut exp_targets: BTreeSet < Target > = BTreeSet :: new ( ) ;
1047
+ exp_targets. insert ( create_stub_target (
1048
+ & format ! ( "{}/src/lib.rs" , project_dir_base) ,
1049
+ vec ! [ ] ,
1050
+ "lib" ,
1051
+ edition,
1052
+ ) ) ;
1053
+ exp_targets. insert ( create_stub_target (
1054
+ & format ! ( "{}/tests/bar.rs" , project_dir_base) ,
1055
+ vec ! [ ] ,
1056
+ "test" ,
1057
+ edition,
1058
+ ) ) ;
1059
+ exp_targets. insert ( create_stub_target (
1060
+ & format ! ( "{}/tests/foo.rs" , project_dir_base) ,
1061
+ vec ! [ ] ,
1062
+ "test" ,
1063
+ edition,
1064
+ ) ) ;
1065
+ let strategy = CargoFmtStrategy :: Root ;
1066
+ let act_targets = get_targets ( & strategy, Some ( & manifest_path) , true ) ;
1067
+ assert_eq ! ( act_targets. unwrap( ) , exp_targets) ;
1068
+ }
1069
+ }
1070
+ }
913
1071
}
0 commit comments