Skip to content

Commit 849ed3d

Browse files
tests: add tests for nested int test files
1 parent 46dadb3 commit 849ed3d

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
@@ -536,8 +536,8 @@ fn add_targets(
536536
}
537537
}
538538

539-
// Returns a `Vec` containing `PathBuf`s of files nested .rs files within a subdirectory
540-
// under the `tests` directory for a given package.
539+
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
540+
// of the `tests` directory for a given package.
541541
// https://github.com/rust-lang/rustfmt/issues/1820
542542
fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Vec<PathBuf> {
543543
let mut files = vec![];
@@ -546,13 +546,16 @@ fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Vec<PathBu
546546
"couldn't read directory {}",
547547
path.to_str().unwrap()
548548
)) {
549-
let entry = entry.expect("couldn't get `DirEntry`");
550-
let path = entry.path();
551-
let parent = path.parent().expect("couldn't get parent directory");
552-
if path.is_dir() {
553-
files.append(&mut get_nested_integration_test_files(&path, &root_dir));
554-
} else if path.extension().map_or(false, |f| f == "rs") && parent != root_dir {
555-
files.push(path);
549+
let entry = entry.expect("couldn't get nested `DirEntry` in tests");
550+
let parent = path;
551+
let entry_path = entry.path();
552+
if entry_path.is_dir() {
553+
files.append(&mut get_nested_integration_test_files(
554+
&entry_path,
555+
&root_dir,
556+
));
557+
} else if entry_path.extension().map_or(false, |f| f == "rs") && parent != root_dir {
558+
files.push(entry_path);
556559
}
557560
}
558561
}
@@ -860,4 +863,37 @@ mod cargo_fmt_tests {
860863
);
861864
}
862865
}
866+
867+
mod get_nested_integration_test_files_tests {
868+
use super::*;
869+
870+
#[test]
871+
fn returns_no_files_if_root_not_dir() {
872+
let target_dir = PathBuf::from("tests/nested-test-files/no-test-dir/Cargo.toml");
873+
assert_eq!(
874+
Vec::new() as Vec<PathBuf>,
875+
get_nested_integration_test_files(&target_dir, &target_dir),
876+
)
877+
}
878+
879+
#[test]
880+
fn returns_no_files_if_tests_has_no_nested_files() {
881+
let target_dir = Path::new("tests/nested-test-files/only-root-level-tests/tests");
882+
assert_eq!(
883+
Vec::new() as Vec<PathBuf>,
884+
get_nested_integration_test_files(&target_dir, &target_dir),
885+
)
886+
}
887+
888+
#[test]
889+
fn returns_nested_files() {
890+
let target_dir = Path::new("tests/nested-test-files/root-and-nested-tests/tests");
891+
assert_eq!(
892+
vec![PathBuf::from(
893+
"tests/nested-test-files/root-and-nested-tests/tests/nested/foo_bar.rs"
894+
)],
895+
get_nested_integration_test_files(&target_dir, &target_dir),
896+
)
897+
}
898+
}
863899
}
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 <[email protected]>"]
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 <[email protected]>"]
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 <[email protected]>"]
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 <[email protected]>"]
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)