Skip to content

Commit

Permalink
Add test_path_filter and deprecate test_file_filter.
Browse files Browse the repository at this point in the history
`test_file_filter` filters out non-file things, which makes it
impossible to, for example, use directories as tests.

Rather than change the semantics of `test_file_filter`, this commit adds
`test_path_filter` which doesn't filter out non-file things, but is
otherwise identical to `test_file_filter`. It also marks
`test_file_filter` as deprecated, with a generic way to migrate code to
`test_path_filter`, allowing users to mechanically migrate their code.
  • Loading branch information
ltratt committed Jan 22, 2024
1 parent 40045bd commit a642ccb
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/fm_options/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use regex::Regex;
fn main() {
LangTester::new()
.test_dir("examples/fm_options/lang_tests")
.test_file_filter(|p| p.extension().unwrap().to_str().unwrap() == "py")
.test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("py"))
.test_extract(|p| {
read_to_string(p)
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion examples/rust_lang_tester/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
LangTester::new()
.test_dir("examples/rust_lang_tester/lang_tests")
// Only use files named `*.rs` as test files.
.test_file_filter(|p| p.extension().unwrap().to_str().unwrap() == "rs")
.test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("rs"))
// Extract the first sequence of commented line(s) as the tests.
.test_extract(|p| {
read_to_string(p)
Expand Down
2 changes: 1 addition & 1 deletion lang_tests/rerun/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main() {
LangTester::new()
.rerun_at_most(5)
.test_dir("lang_tests/rerun/")
.test_file_filter(|p| p.extension().unwrap().to_str().unwrap() == "py")
.test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("py"))
.test_extract(|p| {
read_to_string(p)
.unwrap()
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! LangTester::new()
//! .test_dir("examples/rust_lang_tester/lang_tests")
//! // Only use files named `*.rs` as test files.
//! .test_file_filter(|p| p.extension().unwrap().to_str().unwrap() == "rs")
//! .test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("rs"))
//! // Extract the first sequence of commented line(s) as the tests.
//! .test_extract(|p| {
//! read_to_string(p)
Expand Down
35 changes: 29 additions & 6 deletions src/tester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const DEFAULT_RERUN_AT_MOST: u64 = 3;

pub struct LangTester {
use_cmdline_args: bool,
test_file_filter: Option<Box<dyn Fn(&Path) -> bool + RefUnwindSafe>>,
test_path_filter: Option<Box<dyn Fn(&Path) -> bool + RefUnwindSafe>>,
cmdline_filters: Option<Vec<String>>,
inner: Arc<LangTesterPooler>,
}
Expand Down Expand Up @@ -87,7 +87,7 @@ impl LangTester {
/// [`test_cmds`](#method.test_cmds).
pub fn new() -> Self {
LangTester {
test_file_filter: None,
test_path_filter: None,
use_cmdline_args: true,
cmdline_filters: None,
inner: Arc::new(LangTesterPooler {
Expand Down Expand Up @@ -140,11 +140,35 @@ impl LangTester {
/// ```
///
/// Note that `lang_tester` recursively searches directories for files.
pub fn test_file_filter<F>(&mut self, test_file_filter: F) -> &mut Self
#[deprecated(
since = "0.7.5",
note = "Convert `test_file_filter(|p| ...)` to `test_path_fiter(|p| p.is_file() & ...)`"
)]
pub fn test_file_filter<F>(&mut self, test_path_filter: F) -> &mut Self
where
F: 'static + Fn(&Path) -> bool + RefUnwindSafe,
{
self.test_file_filter = Some(Box::new(test_file_filter));
self.test_path_filter = Some(Box::new(move |p| p.is_file() && test_path_filter(p)));
self
}

/// If `test_path_filter` is specified, only paths for which it returns `true` will be
/// considered tests. A common use of this is to filter tests based on filename extensions
/// e.g.:
///
/// ```rust,ignore
/// LangTester::new()
/// ...
/// .test_path_filter(|p| p.extension().and_then(|x| x.to_str()) == Some("rs"))
/// ...
/// ```
///
/// Note that `lang_tester` recursively searches directories.
pub fn test_path_filter<F>(&mut self, test_path_filter: F) -> &mut Self
where
F: 'static + Fn(&Path) -> bool + RefUnwindSafe,
{
self.test_path_filter = Some(Box::new(test_path_filter));
self
}

Expand Down Expand Up @@ -304,10 +328,9 @@ impl LangTester {
let paths = WalkDir::new(self.inner.test_dir.as_ref().unwrap())
.into_iter()
.filter_map(|x| x.ok())
.filter(|x| x.file_type().is_file())
.map(|x| canonicalize(x.into_path()).unwrap())
// Filter out non-test files
.filter(|x| match self.test_file_filter.as_ref() {
.filter(|x| match self.test_path_filter.as_ref() {
Some(f) => match catch_unwind(|| f(x)) {
Ok(b) => b,
Err(_) => {
Expand Down

0 comments on commit a642ccb

Please sign in to comment.