Skip to content

Commit

Permalink
tests: add tests for nested int test files
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright committed Oct 8, 2019
1 parent 46dadb3 commit 849ed3d
Show file tree
Hide file tree
Showing 14 changed files with 93 additions and 9 deletions.
54 changes: 45 additions & 9 deletions src/cargo-fmt/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ fn add_targets(
}
}

// Returns a `Vec` containing `PathBuf`s of files nested .rs files within a subdirectory
// under the `tests` directory for a given package.
// Returns a `Vec` containing `PathBuf`s of nested .rs files within subdirectories
// of the `tests` directory for a given package.
// https://github.com/rust-lang/rustfmt/issues/1820
fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Vec<PathBuf> {
let mut files = vec![];
Expand All @@ -546,13 +546,16 @@ fn get_nested_integration_test_files(path: &Path, root_dir: &Path) -> Vec<PathBu
"couldn't read directory {}",
path.to_str().unwrap()
)) {
let entry = entry.expect("couldn't get `DirEntry`");
let path = entry.path();
let parent = path.parent().expect("couldn't get parent directory");
if path.is_dir() {
files.append(&mut get_nested_integration_test_files(&path, &root_dir));
} else if path.extension().map_or(false, |f| f == "rs") && parent != root_dir {
files.push(path);
let entry = entry.expect("couldn't get nested `DirEntry` in tests");
let parent = path;
let entry_path = entry.path();
if entry_path.is_dir() {
files.append(&mut get_nested_integration_test_files(
&entry_path,
&root_dir,
));
} else if entry_path.extension().map_or(false, |f| f == "rs") && parent != root_dir {
files.push(entry_path);
}
}
}
Expand Down Expand Up @@ -860,4 +863,37 @@ mod cargo_fmt_tests {
);
}
}

mod get_nested_integration_test_files_tests {
use super::*;

#[test]
fn returns_no_files_if_root_not_dir() {
let target_dir = PathBuf::from("tests/nested-test-files/no-test-dir/Cargo.toml");
assert_eq!(
Vec::new() as Vec<PathBuf>,
get_nested_integration_test_files(&target_dir, &target_dir),
)
}

#[test]
fn returns_no_files_if_tests_has_no_nested_files() {
let target_dir = Path::new("tests/nested-test-files/only-root-level-tests/tests");
assert_eq!(
Vec::new() as Vec<PathBuf>,
get_nested_integration_test_files(&target_dir, &target_dir),
)
}

#[test]
fn returns_nested_files() {
let target_dir = Path::new("tests/nested-test-files/root-and-nested-tests/tests");
assert_eq!(
vec![PathBuf::from(
"tests/nested-test-files/root-and-nested-tests/tests/nested/foo_bar.rs"
)],
get_nested_integration_test_files(&target_dir, &target_dir),
)
}
}
}
6 changes: 6 additions & 0 deletions tests/nested-test-files/empty-tests-dir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "empty-tests-dir"
version = "0.1.0"
authors = ["rustfmt devs <awesome@gmail.com>"]

[dependencies]
1 change: 1 addition & 0 deletions tests/nested-test-files/empty-tests-dir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn foo() {}
6 changes: 6 additions & 0 deletions tests/nested-test-files/no-tests-dir/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "no-tests"
version = "0.1.0"
authors = ["rustfmt devs <awesome@gmail.com>"]

[dependencies]
1 change: 1 addition & 0 deletions tests/nested-test-files/no-tests-dir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn foo() {}
6 changes: 6 additions & 0 deletions tests/nested-test-files/only-root-level-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "foo"
version = "0.1.0"
authors = ["rustfmt devs <awesome@gmail.com>"]

[dependencies]
1 change: 1 addition & 0 deletions tests/nested-test-files/only-root-level-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn foo() {}
4 changes: 4 additions & 0 deletions tests/nested-test-files/only-root-level-tests/tests/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test2() {
assert!(true);
}
4 changes: 4 additions & 0 deletions tests/nested-test-files/only-root-level-tests/tests/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test1() {
assert_eq!(1, 1);
}
6 changes: 6 additions & 0 deletions tests/nested-test-files/root-and-nested-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "empty-tests-dir"
version = "0.1.0"
authors = ["rustfmt devs <awesome@gmail.com>"]

[dependencies]
1 change: 1 addition & 0 deletions tests/nested-test-files/root-and-nested-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn foo() {}
4 changes: 4 additions & 0 deletions tests/nested-test-files/root-and-nested-tests/tests/bar.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test2() {
assert!(true);
}
4 changes: 4 additions & 0 deletions tests/nested-test-files/root-and-nested-tests/tests/foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test1() {
assert_eq!(1, 1);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[test]
fn test_false() {
assert_ne!(false, true);
}

0 comments on commit 849ed3d

Please sign in to comment.