Skip to content

Commit 32303aa

Browse files
refactor: remove expects in nested test file detection
1 parent 5d7a370 commit 32303aa

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/cargo-fmt/main.rs

+27-24
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,7 @@ impl Target {
280280
) -> Self {
281281
let path = PathBuf::from(&target.src_path);
282282
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![]);
287284

288285
Target {
289286
path: canonicalized,
@@ -534,7 +531,7 @@ fn add_targets(
534531
Some(package_dir) => {
535532
let target_dir = package_dir.join("tests");
536533
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)
538535
}
539536
}
540537
} else {
@@ -547,27 +544,33 @@ fn add_targets(
547544
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
548545
// of the `tests` directory for a given package.
549546
// 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>> {
551548
let mut files = vec![];
552549
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+
}
567568
}
569+
} else {
570+
return None;
568571
}
569572
}
570-
files
573+
Some(files)
571574
}
572575

573576
fn run_rustfmt(
@@ -883,7 +886,7 @@ mod cargo_fmt_tests {
883886
fn returns_no_files_if_root_not_dir() {
884887
let target_dir = PathBuf::from("tests/nested-test-files/no-test-dir/Cargo.toml");
885888
assert_eq!(
886-
Vec::new() as Vec<PathBuf>,
889+
Some(Vec::new() as Vec<PathBuf>),
887890
get_nested_integration_test_files(&target_dir, &target_dir),
888891
)
889892
}
@@ -892,7 +895,7 @@ mod cargo_fmt_tests {
892895
fn returns_no_files_if_tests_has_no_nested_files() {
893896
let target_dir = Path::new("tests/nested-test-files/only-root-level-tests/tests");
894897
assert_eq!(
895-
Vec::new() as Vec<PathBuf>,
898+
Some(Vec::new() as Vec<PathBuf>),
896899
get_nested_integration_test_files(&target_dir, &target_dir),
897900
)
898901
}
@@ -910,7 +913,7 @@ mod cargo_fmt_tests {
910913
"tests/nested-test-files/root-and-nested-tests/tests/nested/other.rs",
911914
);
912915
assert_eq!(
913-
vec![exp_baz, exp_foo_bar, exp_other],
916+
Some(vec![exp_baz, exp_foo_bar, exp_other]),
914917
get_nested_integration_test_files(&target_dir, &target_dir),
915918
)
916919
}

0 commit comments

Comments
 (0)