Skip to content

Commit

Permalink
add tests for test discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
brownben committed Nov 12, 2024
1 parent 53d570d commit f88d3a8
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
# Python
**/__pycache__
*.pyc
**/.venv
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exclude = ["./tests/discovery/invalid_syntax.py"]
7 changes: 4 additions & 3 deletions src/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ pub fn find_tests(paths: &[PathBuf], exclude: &[PathBuf]) -> DiscoveredTests {
// Exclude specified from the search
let mut exclude_override = ignore::overrides::OverrideBuilder::new("");
for path in exclude {
exclude_override
.add(&format!("!{}", path.to_string_lossy()))
.unwrap();
let path = path.to_string_lossy();
// Unexpected behaviour when relative paths start with "./" so remove if exists
let path = path.trim_matches(&['.', '/']);
exclude_override.add(&format!("!{path}",)).unwrap();
}
builder.overrides(exclude_override.build().unwrap());

Expand Down
54 changes: 54 additions & 0 deletions tests/discovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use assert_cmd::Command;
use std::env;

fn count_tests_run(args: &[&str]) -> usize {
let cmd_output = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(args)
.arg("--output=json")
.output()
.unwrap();

let stdout = String::from_utf8(cmd_output.stdout).unwrap();
stdout.lines().count()
}

#[test]
fn find_all_tests_in_folder() {
assert_eq!(count_tests_run(&["./tests/discovery/"]), 6);
}

#[test]
fn invalid_path() {
assert_eq!(count_tests_run(&["./discovery/tests/"]), 0);
assert_eq!(count_tests_run(&["./discovery/tests/x.py"]), 0);
}

#[test]
fn files_run_individually() {
assert_eq!(count_tests_run(&["./tests/discovery/a.py"]), 1);
assert_eq!(count_tests_run(&["./tests/discovery/b.py"]), 2);
assert_eq!(count_tests_run(&["./tests/discovery/c.py"]), 3);
}

#[test]
fn exclude_single_file() {
env::set_current_dir("./tests").unwrap();

for (file, test_count) in [("a", 5), ("b", 4), ("c", 3)] {
let relative = format!("--exclude=./discovery/{file}.py");
let path = format!("--exclude=discovery/{file}.py");
let glob_file = format!("--exclude=**/{file}.py");
let file = format!("--exclude={file}.py");

assert_eq!(count_tests_run(&["./discovery", &relative]), test_count);
assert_eq!(count_tests_run(&["./discovery", &path]), test_count);
assert_eq!(count_tests_run(&["./discovery", &glob_file]), test_count);
assert_eq!(count_tests_run(&["./discovery", &file]), test_count);
}
}

#[test]
fn file_with_invalid_syntax() {
assert_eq!(count_tests_run(&["./tests/discovery/invalid_syntax.py"]), 0);
}
2 changes: 2 additions & 0 deletions tests/discovery/a.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_one():
pass
6 changes: 6 additions & 0 deletions tests/discovery/b.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def test_one():
pass


def test_two():
pass
10 changes: 10 additions & 0 deletions tests/discovery/c.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def test_one():
pass


def test_two():
pass


def test_three():
pass
11 changes: 11 additions & 0 deletions tests/discovery/invalid_syntax.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ruff: noqa

use std::{
ffi::CStr,
ops,
time::{Duration, Instant},
};


def test_one():
pass

0 comments on commit f88d3a8

Please sign in to comment.