@@ -270,10 +270,7 @@ impl Target {
270
270
) -> Self {
271
271
let path = PathBuf :: from ( & target. src_path ) ;
272
272
let canonicalized = fs:: canonicalize ( & path) . unwrap_or ( path) ;
273
- let test_files = match nested_int_test_files {
274
- Some ( files) => files,
275
- None => vec ! [ ] ,
276
- } ;
273
+ let test_files = nested_int_test_files. unwrap_or ( vec ! [ ] ) ;
277
274
278
275
Target {
279
276
path : canonicalized,
@@ -524,7 +521,7 @@ fn add_targets(
524
521
Some ( package_dir) => {
525
522
let target_dir = package_dir. join ( "tests" ) ;
526
523
test_files_added = true ;
527
- Some ( get_nested_integration_test_files ( & target_dir, & target_dir) )
524
+ get_nested_integration_test_files ( & target_dir, & target_dir)
528
525
}
529
526
}
530
527
} else {
@@ -537,27 +534,33 @@ fn add_targets(
537
534
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
538
535
// of the `tests` directory for a given package.
539
536
// https://github.com/rust-lang/rustfmt/issues/1820
540
- fn get_nested_integration_test_files ( path : & Path , root_dir : & Path ) -> Vec < PathBuf > {
537
+ fn get_nested_integration_test_files ( path : & Path , root_dir : & Path ) -> Option < Vec < PathBuf > > {
541
538
let mut files = vec ! [ ] ;
542
539
if path. is_dir ( ) {
543
- for entry in fs:: read_dir ( path) . expect ( & format ! (
544
- "couldn't read directory {}" ,
545
- path. to_str( ) . unwrap( )
546
- ) ) {
547
- let entry = entry. expect ( "couldn't get nested `DirEntry` in tests" ) ;
548
- let parent = path;
549
- let entry_path = entry. path ( ) ;
550
- if entry_path. is_dir ( ) {
551
- files. append ( & mut get_nested_integration_test_files (
552
- & entry_path,
553
- & root_dir,
554
- ) ) ;
555
- } else if entry_path. extension ( ) . map_or ( false , |f| f == "rs" ) && parent != root_dir {
556
- files. push ( entry_path) ;
540
+ if let Ok ( dir) = fs:: read_dir ( path) {
541
+ for dir_entry in dir {
542
+ if let Ok ( entry) = dir_entry {
543
+ let parent = path;
544
+ let entry_path = entry. path ( ) ;
545
+ if entry_path. is_dir ( ) {
546
+ files. append (
547
+ & mut get_nested_integration_test_files ( & entry_path, & root_dir)
548
+ . unwrap_or ( vec ! [ ] ) ,
549
+ ) ;
550
+ } else if entry_path. extension ( ) . map_or ( false , |f| f == "rs" )
551
+ && parent != root_dir
552
+ {
553
+ files. push ( entry_path) ;
554
+ }
555
+ } else {
556
+ return None ;
557
+ }
557
558
}
559
+ } else {
560
+ return None ;
558
561
}
559
562
}
560
- files
563
+ Some ( files)
561
564
}
562
565
563
566
fn run_rustfmt (
@@ -870,7 +873,7 @@ mod cargo_fmt_tests {
870
873
fn returns_no_files_if_root_not_dir ( ) {
871
874
let target_dir = PathBuf :: from ( "tests/nested-test-files/no-test-dir/Cargo.toml" ) ;
872
875
assert_eq ! (
873
- Vec :: new( ) as Vec <PathBuf >,
876
+ Some ( Vec :: new( ) as Vec <PathBuf >) ,
874
877
get_nested_integration_test_files( & target_dir, & target_dir) ,
875
878
)
876
879
}
@@ -879,7 +882,7 @@ mod cargo_fmt_tests {
879
882
fn returns_no_files_if_tests_has_no_nested_files ( ) {
880
883
let target_dir = Path :: new ( "tests/nested-test-files/only-root-level-tests/tests" ) ;
881
884
assert_eq ! (
882
- Vec :: new( ) as Vec <PathBuf >,
885
+ Some ( Vec :: new( ) as Vec <PathBuf >) ,
883
886
get_nested_integration_test_files( & target_dir, & target_dir) ,
884
887
)
885
888
}
@@ -897,7 +900,7 @@ mod cargo_fmt_tests {
897
900
"tests/nested-test-files/root-and-nested-tests/tests/nested/other.rs" ,
898
901
) ;
899
902
assert_eq ! (
900
- vec![ exp_baz, exp_foo_bar, exp_other] ,
903
+ Some ( vec![ exp_baz, exp_foo_bar, exp_other] ) ,
901
904
get_nested_integration_test_files( & target_dir, & target_dir) ,
902
905
)
903
906
}
0 commit comments