@@ -421,7 +421,7 @@ fn get_targets_root_only(
421
421
& package_targets,
422
422
& mut targets,
423
423
include_nested_test_files,
424
- ) ;
424
+ ) ? ;
425
425
426
426
Ok ( ( ) )
427
427
}
@@ -441,7 +441,7 @@ fn get_targets_recursive(
441
441
& package. targets ,
442
442
& mut targets,
443
443
include_nested_test_files,
444
- ) ;
444
+ ) ? ;
445
445
446
446
// Look for local dependencies.
447
447
for dependency in package. dependencies {
@@ -497,7 +497,7 @@ fn get_targets_with_hitlist(
497
497
& package. targets ,
498
498
& mut targets,
499
499
include_nested_test_files,
500
- ) ;
500
+ ) ? ;
501
501
}
502
502
}
503
503
@@ -517,57 +517,62 @@ fn add_targets(
517
517
target_paths : & [ cargo_metadata:: Target ] ,
518
518
targets : & mut BTreeSet < Target > ,
519
519
include_nested_test_files : bool ,
520
- ) {
520
+ ) -> Result < ( ) , io :: Error > {
521
521
let mut test_files_added = false ;
522
522
for target in target_paths {
523
523
// Packages often have more than one `test` target,
524
524
// so only add the nested files for the first one.
525
- let test_files = if include_nested_test_files
525
+ let check_for_nested_test_files = include_nested_test_files
526
526
&& !test_files_added
527
- && target. kind . iter ( ) . any ( |t| t == "test" )
528
- {
529
- match manifest_path. parent ( ) {
530
- None => None ,
531
- Some ( package_dir) => {
532
- let target_dir = package_dir. join ( "tests" ) ;
533
- test_files_added = true ;
534
- get_nested_integration_test_files ( & target_dir, & target_dir)
535
- }
527
+ && target. kind . iter ( ) . any ( |t| t == "test" ) ;
528
+
529
+ if !check_for_nested_test_files {
530
+ targets. insert ( Target :: from_target ( & target, None ) ) ;
531
+ continue ;
532
+ }
533
+
534
+ if let Some ( package_dir) = manifest_path. parent ( ) {
535
+ let target_dir = package_dir. join ( "tests" ) ;
536
+ test_files_added = true ;
537
+ let test_files = get_nested_integration_test_files ( & target_dir, & target_dir) ;
538
+ if test_files. is_none ( ) {
539
+ return Err ( io:: Error :: new (
540
+ io:: ErrorKind :: Other ,
541
+ "Error encountered while searching for nested integration test files" ,
542
+ ) ) ;
536
543
}
544
+ targets. insert ( Target :: from_target ( & target, test_files) ) ;
537
545
} else {
538
- None
539
- } ;
540
- targets. insert ( Target :: from_target ( target, test_files) ) ;
546
+ return Err ( io:: Error :: new (
547
+ io:: ErrorKind :: InvalidData ,
548
+ "Unable to determine root `tests` directory for /
549
+ nested integration test file discovery" ,
550
+ ) ) ;
551
+ }
541
552
}
553
+
554
+ Ok ( ( ) )
542
555
}
543
556
544
557
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
545
558
// of the `tests` directory for a given package.
546
559
// https://github.com/rust-lang/rustfmt/issues/1820
547
560
fn get_nested_integration_test_files ( path : & Path , root_dir : & Path ) -> Option < Vec < PathBuf > > {
561
+ if !path. is_dir ( ) {
562
+ return Some ( vec ! [ ] ) ;
563
+ }
548
564
let mut files = vec ! [ ] ;
549
- if path. is_dir ( ) {
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
- }
568
- }
569
- } else {
570
- return None ;
565
+ let dir = fs:: read_dir ( path) . ok ( ) ?;
566
+
567
+ for dir_entry in dir {
568
+ let entry_path = dir_entry. ok ( ) ?. path ( ) ;
569
+ if entry_path. is_dir ( ) {
570
+ files. append ( & mut get_nested_integration_test_files (
571
+ & entry_path,
572
+ & root_dir,
573
+ ) ?) ;
574
+ } else if entry_path. extension ( ) . map_or ( false , |f| f == "rs" ) && path != root_dir {
575
+ files. push ( entry_path) ;
571
576
}
572
577
}
573
578
Some ( files)
0 commit comments