From 059e23c59dbd07feab96c743514cf2e6a07f6f04 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Wed, 10 Feb 2021 14:49:27 +0100 Subject: [PATCH] Allow multiple test filters --- src/cli.rs | 19 +++++++------------ src/lib.rs | 4 ++-- src/tests.rs | 34 +++++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 97a659f..65e05e5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -10,7 +10,7 @@ use super::time::TestTimeOptions; #[derive(Debug)] pub struct TestOpts { pub list: bool, - pub filter: Option, + pub filters: Vec, pub filter_exact: bool, pub force_run_in_process: bool, pub exclude_should_panic: bool, @@ -148,12 +148,13 @@ fn optgroups() -> getopts::Options { } fn usage(binary: &str, options: &getopts::Options) { - let message = format!("Usage: {} [OPTIONS] [FILTER]", binary); + let message = format!("Usage: {} [OPTIONS] [FILTERS...]", binary); println!( r#"{usage} -The FILTER string is tested against the name of all tests, and only those -tests whose names contain the filter are run. +The FILTERS string is tested against the name of all tests, and only those +tests whose names contain the filter are run. Multiple filter strings may +be passed, which will run all tests matching any of the filters. By default, all tests are run in parallel. This can be altered with the --test-threads flag or the RUST_TEST_THREADS environment variable when running @@ -243,7 +244,7 @@ fn parse_opts_impl(matches: getopts::Matches) -> OptRes { let logfile = get_log_file(&matches)?; let run_ignored = get_run_ignored(&matches, include_ignored)?; - let filter = get_filter(&matches)?; + let filters = matches.free.clone(); let nocapture = get_nocapture(&matches)?; let test_threads = get_test_threads(&matches)?; let color = get_color_config(&matches)?; @@ -253,7 +254,7 @@ fn parse_opts_impl(matches: getopts::Matches) -> OptRes { let test_opts = TestOpts { list, - filter, + filters, filter_exact: exact, force_run_in_process, exclude_should_panic, @@ -397,12 +398,6 @@ fn get_run_ignored(matches: &getopts::Matches, include_ignored: bool) -> OptPart Ok(run_ignored) } -fn get_filter(matches: &getopts::Matches) -> OptPartRes> { - let filter = if !matches.free.is_empty() { Some(matches.free[0].clone()) } else { None }; - - Ok(filter) -} - fn get_allow_unstable(matches: &getopts::Matches) -> OptPartRes { let mut allow_unstable = false; diff --git a/src/lib.rs b/src/lib.rs index 79f1f79..b0aefbb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -359,8 +359,8 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec) -> Vec TestOpts { TestOpts { list: false, - filter: None, + filters: vec![], filter_exact: false, force_run_in_process: false, exclude_should_panic: false, @@ -469,43 +469,59 @@ pub fn exact_filter_match() { } let substr = - filter_tests(&TestOpts { filter: Some("base".into()), ..TestOpts::new() }, tests()); + filter_tests(&TestOpts { filters: vec!["base".into()], ..TestOpts::new() }, tests()); assert_eq!(substr.len(), 4); - let substr = filter_tests(&TestOpts { filter: Some("bas".into()), ..TestOpts::new() }, tests()); + let substr = filter_tests(&TestOpts { filters: vec!["bas".into()], ..TestOpts::new() }, tests()); assert_eq!(substr.len(), 4); let substr = - filter_tests(&TestOpts { filter: Some("::test".into()), ..TestOpts::new() }, tests()); + filter_tests(&TestOpts { filters: vec!["::test".into()], ..TestOpts::new() }, tests()); assert_eq!(substr.len(), 3); let substr = - filter_tests(&TestOpts { filter: Some("base::test".into()), ..TestOpts::new() }, tests()); + filter_tests(&TestOpts { filters: vec!["base::test".into()], ..TestOpts::new() }, tests()); assert_eq!(substr.len(), 3); + let substr = filter_tests( + &TestOpts { filters: vec!["test1".into(), "test2".into()], ..TestOpts::new() }, + tests(), + ); + assert_eq!(substr.len(), 2); + let exact = filter_tests( - &TestOpts { filter: Some("base".into()), filter_exact: true, ..TestOpts::new() }, + &TestOpts { filters: vec!["base".into()], filter_exact: true, ..TestOpts::new() }, tests(), ); assert_eq!(exact.len(), 1); let exact = filter_tests( - &TestOpts { filter: Some("bas".into()), filter_exact: true, ..TestOpts::new() }, + &TestOpts { filters: vec!["bas".into()], filter_exact: true, ..TestOpts::new() }, tests(), ); assert_eq!(exact.len(), 0); let exact = filter_tests( - &TestOpts { filter: Some("::test".into()), filter_exact: true, ..TestOpts::new() }, + &TestOpts { filters: vec!["::test".into()], filter_exact: true, ..TestOpts::new() }, tests(), ); assert_eq!(exact.len(), 0); let exact = filter_tests( - &TestOpts { filter: Some("base::test".into()), filter_exact: true, ..TestOpts::new() }, + &TestOpts { filters: vec!["base::test".into()], filter_exact: true, ..TestOpts::new() }, tests(), ); assert_eq!(exact.len(), 1); + + let exact = filter_tests( + &TestOpts { + filters: vec!["base".into(), "base::test".into()], + filter_exact: true, + ..TestOpts::new() + }, + tests(), + ); + assert_eq!(exact.len(), 2); } #[test]