Skip to content

Commit 55d583f

Browse files
refactor: remove expects in nested test file detection
1 parent 1eb1446 commit 55d583f

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/cargo-fmt/main.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,7 @@ impl Target {
270270
) -> Self {
271271
let path = PathBuf::from(&target.src_path);
272272
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![]);
277274

278275
Target {
279276
path: canonicalized,
@@ -524,7 +521,7 @@ fn add_targets(
524521
Some(package_dir) => {
525522
let target_dir = package_dir.join("tests");
526523
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)
528525
}
529526
}
530527
} else {
@@ -537,27 +534,33 @@ fn add_targets(
537534
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
538535
// of the `tests` directory for a given package.
539536
// 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>> {
541538
let mut files = vec![];
542539
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+
}
557558
}
559+
} else {
560+
return None;
558561
}
559562
}
560-
files
563+
Some(files)
561564
}
562565

563566
fn run_rustfmt(
@@ -870,7 +873,7 @@ mod cargo_fmt_tests {
870873
fn returns_no_files_if_root_not_dir() {
871874
let target_dir = PathBuf::from("tests/nested-test-files/no-test-dir/Cargo.toml");
872875
assert_eq!(
873-
Vec::new() as Vec<PathBuf>,
876+
Some(Vec::new() as Vec<PathBuf>),
874877
get_nested_integration_test_files(&target_dir, &target_dir),
875878
)
876879
}
@@ -879,7 +882,7 @@ mod cargo_fmt_tests {
879882
fn returns_no_files_if_tests_has_no_nested_files() {
880883
let target_dir = Path::new("tests/nested-test-files/only-root-level-tests/tests");
881884
assert_eq!(
882-
Vec::new() as Vec<PathBuf>,
885+
Some(Vec::new() as Vec<PathBuf>),
883886
get_nested_integration_test_files(&target_dir, &target_dir),
884887
)
885888
}
@@ -897,7 +900,7 @@ mod cargo_fmt_tests {
897900
"tests/nested-test-files/root-and-nested-tests/tests/nested/other.rs",
898901
);
899902
assert_eq!(
900-
vec![exp_baz, exp_foo_bar, exp_other],
903+
Some(vec![exp_baz, exp_foo_bar, exp_other]),
901904
get_nested_integration_test_files(&target_dir, &target_dir),
902905
)
903906
}

0 commit comments

Comments
 (0)