Skip to content

Commit 11d0c08

Browse files
tests: add tests for nested int test files
1 parent 5c9f22b commit 11d0c08

File tree

14 files changed

+93
-9
lines changed

14 files changed

+93
-9
lines changed

src/cargo-fmt/main.rs

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,8 @@ fn add_targets(
546546
}
547547
}
548548

549-
// Returns a `Vec` containing `PathBuf`s of files nested .rs files within a subdirectory
550-
// under the `tests` directory for a given package.
549+
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
550+
// of the `tests` directory for a given package.
551551
// https://github.com/rust-lang/rustfmt/issues/1820
552552
fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Vec<PathBuf> {
553553
let mut files = vec![];
@@ -556,13 +556,16 @@ fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Vec<PathBu
556556
"couldn't read directory {}",
557557
path.to_str().unwrap()
558558
)) {
559-
let entry = entry.expect("couldn't get `DirEntry`");
560-
let path = entry.path();
561-
let parent = path.parent().expect("couldn't get parent directory");
562-
if path.is_dir() {
563-
files.append(&mut get_nested_integration_test_files(&path, &root_dir));
564-
} else if path.extension().map_or(false, |f| f == "rs") && parent != root_dir {
565-
files.push(path);
559+
let entry = entry.expect("couldn't get nested `DirEntry` in tests");
560+
let parent = path;
561+
let entry_path = entry.path();
562+
if entry_path.is_dir() {
563+
files.append(&mut get_nested_integration_test_files(
564+
&entry_path,
565+
&root_dir,
566+
));
567+
} else if entry_path.extension().map_or(false, |f| f == "rs") && parent != root_dir {
568+
files.push(entry_path);
566569
}
567570
}
568571
}
@@ -874,4 +877,37 @@ mod cargo_fmt_tests {
874877
);
875878
}
876879
}
880+
881+
mod get_nested_integration_test_files_tests {
882+
use super::*;
883+
884+
#[test]
885+
fn returns_no_files_if_root_not_dir() {
886+
let target_dir = PathBuf::from("tests/nested-test-files/no-test-dir/Cargo.toml");
887+
assert_eq!(
888+
Vec::new() as Vec<PathBuf>,
889+
get_nested_integration_test_files(&target_dir, &target_dir),
890+
)
891+
}
892+
893+
#[test]
894+
fn returns_no_files_if_tests_has_no_nested_files() {
895+
let target_dir = Path::new("tests/nested-test-files/only-root-level-tests/tests");
896+
assert_eq!(
897+
Vec::new() as Vec<PathBuf>,
898+
get_nested_integration_test_files(&target_dir, &target_dir),
899+
)
900+
}
901+
902+
#[test]
903+
fn returns_nested_files() {
904+
let target_dir = Path::new("tests/nested-test-files/root-and-nested-tests/tests");
905+
assert_eq!(
906+
vec![PathBuf::from(
907+
"tests/nested-test-files/root-and-nested-tests/tests/nested/foo_bar.rs"
908+
)],
909+
get_nested_integration_test_files(&target_dir, &target_dir),
910+
)
911+
}
912+
}
877913
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "empty-tests-dir"
3+
version = "0.1.0"
4+
authors = ["rustfmt devs <awesome@gmail.com>"]
5+
6+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn foo() {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "no-tests"
3+
version = "0.1.0"
4+
authors = ["rustfmt devs <awesome@gmail.com>"]
5+
6+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn foo() {}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "foo"
3+
version = "0.1.0"
4+
authors = ["rustfmt devs <awesome@gmail.com>"]
5+
6+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn foo() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test2() {
3+
assert!(true);
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test1() {
3+
assert_eq!(1, 1);
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "empty-tests-dir"
3+
version = "0.1.0"
4+
authors = ["rustfmt devs <awesome@gmail.com>"]
5+
6+
[dependencies]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fn foo() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test2() {
3+
assert!(true);
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test1() {
3+
assert_eq!(1, 1);
4+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[test]
2+
fn test_false() {
3+
assert_ne!(false, true);
4+
}

0 commit comments

Comments
 (0)