@@ -536,8 +536,8 @@ fn add_targets(
536
536
}
537
537
}
538
538
539
- // Returns a `Vec` containing `PathBuf`s of files nested .rs files within a subdirectory
540
- // under the `tests` directory for a given package.
539
+ // Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
540
+ // of the `tests` directory for a given package.
541
541
// https://github.com/rust-lang/rustfmt/issues/1820
542
542
fn get_nested_integration_test_files ( path : & Path , root_dir : & Path ) -> Vec < PathBuf > {
543
543
let mut files = vec ! [ ] ;
@@ -546,13 +546,16 @@ fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Vec<PathBu
546
546
"couldn't read directory {}" ,
547
547
path. to_str( ) . unwrap( )
548
548
) ) {
549
- let entry = entry. expect ( "couldn't get `DirEntry`" ) ;
550
- let path = entry. path ( ) ;
551
- let parent = path. parent ( ) . expect ( "couldn't get parent directory" ) ;
552
- if path. is_dir ( ) {
553
- files. append ( & mut get_nested_integration_test_files ( & path, & root_dir) ) ;
554
- } else if path. extension ( ) . map_or ( false , |f| f == "rs" ) && parent != root_dir {
555
- files. push ( path) ;
549
+ let entry = entry. expect ( "couldn't get nested `DirEntry` in tests" ) ;
550
+ let parent = path;
551
+ let entry_path = entry. path ( ) ;
552
+ if entry_path. is_dir ( ) {
553
+ files. append ( & mut get_nested_integration_test_files (
554
+ & entry_path,
555
+ & root_dir,
556
+ ) ) ;
557
+ } else if entry_path. extension ( ) . map_or ( false , |f| f == "rs" ) && parent != root_dir {
558
+ files. push ( entry_path) ;
556
559
}
557
560
}
558
561
}
@@ -860,4 +863,40 @@ mod cargo_fmt_tests {
860
863
) ;
861
864
}
862
865
}
866
+
867
+ mod get_nested_integration_test_files_tests {
868
+ use super :: * ;
869
+
870
+ #[ test]
871
+ fn returns_no_files_if_root_not_dir ( ) {
872
+ let target_dir = PathBuf :: from ( "tests/nested-test-files/no-test-dir/Cargo.toml" ) ;
873
+ println ! ( "target_dir: {:?}" , target_dir) ;
874
+ assert_eq ! (
875
+ Vec :: new( ) as Vec <PathBuf >,
876
+ get_nested_integration_test_files( & target_dir, & target_dir) ,
877
+ )
878
+ }
879
+
880
+ #[ test]
881
+ fn returns_no_files_if_tests_has_no_nested_files ( ) {
882
+ let target_dir = Path :: new ( "tests/nested-test-files/only-root-level-tests/tests" ) ;
883
+ println ! ( "target_dir: {:?}" , target_dir) ;
884
+ assert_eq ! (
885
+ Vec :: new( ) as Vec <PathBuf >,
886
+ get_nested_integration_test_files( & target_dir, & target_dir) ,
887
+ )
888
+ }
889
+
890
+ #[ test]
891
+ fn returns_nested_files ( ) {
892
+ let target_dir = Path :: new ( "tests/nested-test-files/root-and-nested-tests/tests" ) ;
893
+ println ! ( "target_dir: {:?}" , target_dir) ;
894
+ assert_eq ! (
895
+ vec![ PathBuf :: from(
896
+ "tests/nested-test-files/root-and-nested-tests/tests/nested/foo_bar.rs"
897
+ ) ] ,
898
+ get_nested_integration_test_files( & target_dir, & target_dir) ,
899
+ )
900
+ }
901
+ }
863
902
}
0 commit comments