@@ -280,10 +280,7 @@ impl Target {
280
280
) -> Self {
281
281
let path = PathBuf :: from ( & target. src_path ) ;
282
282
let canonicalized = fs:: canonicalize ( & path) . unwrap_or ( path) ;
283
- let test_files = match nested_int_test_files {
284
- Some ( files) => files,
285
- None => vec ! [ ] ,
286
- } ;
283
+ let test_files = nested_int_test_files. unwrap_or ( vec ! [ ] ) ;
287
284
288
285
Target {
289
286
path : canonicalized,
@@ -534,7 +531,7 @@ fn add_targets(
534
531
Some ( package_dir) => {
535
532
let target_dir = package_dir. join ( "tests" ) ;
536
533
test_files_added = true ;
537
- Some ( get_nested_integration_test_files ( & target_dir, & target_dir) )
534
+ get_nested_integration_test_files ( & target_dir, & target_dir)
538
535
}
539
536
}
540
537
} else {
@@ -547,27 +544,33 @@ fn add_targets(
547
544
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
548
545
// of the `tests` directory for a given package.
549
546
// https://github.com/rust-lang/rustfmt/issues/1820
550
- fn get_nested_integration_test_files ( path : & Path , root_dir : & Path ) -> Vec < PathBuf > {
547
+ fn get_nested_integration_test_files ( path : & Path , root_dir : & Path ) -> Option < Vec < PathBuf > > {
551
548
let mut files = vec ! [ ] ;
552
549
if path. is_dir ( ) {
553
- for entry in fs:: read_dir ( path) . expect ( & format ! (
554
- "couldn't read directory {}" ,
555
- path. to_str( ) . unwrap( )
556
- ) ) {
557
- let entry = entry. expect ( "couldn't get nested `DirEntry` in tests" ) ;
558
- let parent = path;
559
- let entry_path = entry. path ( ) ;
560
- if entry_path. is_dir ( ) {
561
- files. append ( & mut get_nested_integration_test_files (
562
- & entry_path,
563
- & root_dir,
564
- ) ) ;
565
- } else if entry_path. extension ( ) . map_or ( false , |f| f == "rs" ) && parent != root_dir {
566
- files. push ( entry_path) ;
550
+ if let Ok ( dir) = fs:: read_dir ( path) {
551
+ for dir_entry in dir {
552
+ if let Ok ( entry) = dir_entry {
553
+ let parent = path;
554
+ let entry_path = entry. path ( ) ;
555
+ if entry_path. is_dir ( ) {
556
+ files. append (
557
+ & mut get_nested_integration_test_files ( & entry_path, & root_dir)
558
+ . unwrap_or ( vec ! [ ] ) ,
559
+ ) ;
560
+ } else if entry_path. extension ( ) . map_or ( false , |f| f == "rs" )
561
+ && parent != root_dir
562
+ {
563
+ files. push ( entry_path) ;
564
+ }
565
+ } else {
566
+ return None ;
567
+ }
567
568
}
569
+ } else {
570
+ return None ;
568
571
}
569
572
}
570
- files
573
+ Some ( files)
571
574
}
572
575
573
576
fn run_rustfmt (
@@ -883,7 +886,7 @@ mod cargo_fmt_tests {
883
886
fn returns_no_files_if_root_not_dir ( ) {
884
887
let target_dir = PathBuf :: from ( "tests/nested-test-files/no-test-dir/Cargo.toml" ) ;
885
888
assert_eq ! (
886
- Vec :: new( ) as Vec <PathBuf >,
889
+ Some ( Vec :: new( ) as Vec <PathBuf >) ,
887
890
get_nested_integration_test_files( & target_dir, & target_dir) ,
888
891
)
889
892
}
@@ -892,7 +895,7 @@ mod cargo_fmt_tests {
892
895
fn returns_no_files_if_tests_has_no_nested_files ( ) {
893
896
let target_dir = Path :: new ( "tests/nested-test-files/only-root-level-tests/tests" ) ;
894
897
assert_eq ! (
895
- Vec :: new( ) as Vec <PathBuf >,
898
+ Some ( Vec :: new( ) as Vec <PathBuf >) ,
896
899
get_nested_integration_test_files( & target_dir, & target_dir) ,
897
900
)
898
901
}
@@ -910,7 +913,7 @@ mod cargo_fmt_tests {
910
913
"tests/nested-test-files/root-and-nested-tests/tests/nested/other.rs" ,
911
914
) ;
912
915
assert_eq ! (
913
- vec![ exp_baz, exp_foo_bar, exp_other] ,
916
+ Some ( vec![ exp_baz, exp_foo_bar, exp_other] ) ,
914
917
get_nested_integration_test_files( & target_dir, & target_dir) ,
915
918
)
916
919
}
0 commit comments