Skip to content

Commit 0e2e012

Browse files
refactor: add early exit to nested test file discovery
1 parent 9805d3c commit 0e2e012

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

src/cargo-fmt/main.rs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ fn get_targets_root_only(
421421
&package_targets,
422422
&mut targets,
423423
include_nested_test_files,
424-
);
424+
)?;
425425

426426
Ok(())
427427
}
@@ -441,7 +441,7 @@ fn get_targets_recursive(
441441
&package.targets,
442442
&mut targets,
443443
include_nested_test_files,
444-
);
444+
)?;
445445

446446
// Look for local dependencies.
447447
for dependency in package.dependencies {
@@ -497,7 +497,7 @@ fn get_targets_with_hitlist(
497497
&package.targets,
498498
&mut targets,
499499
include_nested_test_files,
500-
);
500+
)?;
501501
}
502502
}
503503

@@ -517,57 +517,62 @@ fn add_targets(
517517
target_paths: &[cargo_metadata::Target],
518518
targets: &mut BTreeSet<Target>,
519519
include_nested_test_files: bool,
520-
) {
520+
) -> Result<(), io::Error> {
521521
let mut test_files_added = false;
522522
for target in target_paths {
523523
// Packages often have more than one `test` target,
524524
// 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
526526
&& !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+
));
536543
}
544+
targets.insert(Target::from_target(&target, test_files));
537545
} 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+
}
541552
}
553+
554+
Ok(())
542555
}
543556

544557
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
545558
// of the `tests` directory for a given package.
546559
// https://github.com/rust-lang/rustfmt/issues/1820
547560
fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Option<Vec<PathBuf>> {
561+
if !path.is_dir() {
562+
return Some(vec![]);
563+
}
548564
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);
571576
}
572577
}
573578
Some(files)

0 commit comments

Comments
 (0)