Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test_path_filter and deprecate test_file_filter. #118

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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