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

libtest: add glob support to test name filters #46417

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/libtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ crate-type = ["dylib", "rlib"]

[dependencies]
getopts = "0.2"
glob = "0.2"
term = { path = "../libterm" }
110 changes: 73 additions & 37 deletions src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#![feature(panic_unwind)]
#![feature(staged_api)]

extern crate glob;
extern crate getopts;
extern crate term;
#[cfg(unix)]
Expand Down Expand Up @@ -78,7 +79,7 @@ pub mod test {
pub use {Bencher, TestName, TestResult, TestDesc, TestDescAndFn, TestOpts, TrFailed,
TrFailedMsg, TrIgnored, TrOk, Metric, MetricMap, StaticTestFn, StaticTestName,
DynTestName, DynTestFn, run_test, test_main, test_main_static, filter_tests,
parse_opts, StaticBenchFn, ShouldPanic, Options};
parse_opts, StaticBenchFn, ShouldPanic, Options, TestNamePattern};
}

pub mod stats;
Expand Down Expand Up @@ -334,11 +335,40 @@ pub enum ColorConfig {
NeverColor,
}

#[derive(Debug)]
pub enum TestNamePattern {
Exact(String),
Substring(String),
Glob(glob::Pattern),
}

impl TestNamePattern {
pub fn new(pattern: String, force_exact: bool) -> Self {
if force_exact {
return TestNamePattern::Exact(pattern);
}
if pattern.chars().any(|c| ['*', '?', '['].contains(&c)) {
if let Ok(g) = glob::Pattern::new(&pattern) {
return TestNamePattern::Glob(g);
}
}
TestNamePattern::Substring(pattern)
}

fn matches(&self, test: &TestDescAndFn) -> bool {
let name = test.desc.name.as_slice();
match *self {
TestNamePattern::Exact(ref filter) => name == filter,
TestNamePattern::Substring(ref filter) => name.contains(&filter[..]),
TestNamePattern::Glob(ref g) => g.matches(name),
}
}
}

#[derive(Debug)]
pub struct TestOpts {
pub list: bool,
pub filter: Option<String>,
pub filter_exact: bool,
pub filter: Option<TestNamePattern>,
pub run_ignored: bool,
pub run_tests: bool,
pub bench_benchmarks: bool,
Expand All @@ -347,7 +377,7 @@ pub struct TestOpts {
pub color: ColorConfig,
pub quiet: bool,
pub test_threads: Option<usize>,
pub skip: Vec<String>,
pub skip: Vec<TestNamePattern>,
pub options: Options,
}

Expand All @@ -357,7 +387,6 @@ impl TestOpts {
TestOpts {
list: false,
filter: None,
filter_exact: false,
run_ignored: false,
run_tests: false,
bench_benchmarks: false,
Expand Down Expand Up @@ -445,15 +474,15 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
return None;
}

let exact = matches.opt_present("exact");
let filter = if !matches.free.is_empty() {
Some(matches.free[0].clone())
Some(TestNamePattern::new(matches.free[0].clone(), exact))
} else {
None
};

let run_ignored = matches.opt_present("ignored");
let quiet = matches.opt_present("quiet");
let exact = matches.opt_present("exact");
let list = matches.opt_present("list");

let logfile = matches.opt_str("logfile");
Expand Down Expand Up @@ -496,10 +525,14 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
}
};

let skip = matches
.opt_strs("skip")
.into_iter().map(|s| TestNamePattern::new(s, exact))
.collect();

let test_opts = TestOpts {
list,
filter,
filter_exact: exact,
run_ignored,
run_tests,
bench_benchmarks,
Expand All @@ -508,7 +541,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
color,
quiet,
test_threads,
skip: matches.opt_strs("skip"),
skip,
options: Options::new(),
};

Expand Down Expand Up @@ -1325,26 +1358,14 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescA
None => filtered,
Some(ref filter) => {
filtered.into_iter()
.filter(|test| {
if opts.filter_exact {
test.desc.name.as_slice() == &filter[..]
} else {
test.desc.name.as_slice().contains(&filter[..])
}
})
.filter(|test| filter.matches(test))
.collect()
}
};

// Skip tests that match any of the skip filters
filtered = filtered.into_iter()
.filter(|t| !opts.skip.iter().any(|sf| {
if opts.filter_exact {
t.desc.name.as_slice() == &sf[..]
} else {
t.desc.name.as_slice().contains(&sf[..])
}
}))
.filter(|t| !opts.skip.iter().any(|sf| sf.matches(t)))
.collect();

// Maybe pull out the ignored test and unignore them
Expand Down Expand Up @@ -1752,7 +1773,7 @@ pub mod bench {
mod tests {
use test::{TrFailed, TrFailedMsg, TrIgnored, TrOk, filter_tests, parse_opts, TestDesc,
TestDescAndFn, TestOpts, run_test, MetricMap, StaticTestName, DynTestName,
DynTestFn, ShouldPanic};
DynTestFn, ShouldPanic, TestNamePattern};
use std::sync::mpsc::channel;
use bench;
use Bencher;
Expand Down Expand Up @@ -1920,7 +1941,7 @@ mod tests {
}

#[test]
pub fn exact_filter_match() {
pub fn filter_type_match() {
fn tests() -> Vec<TestDescAndFn> {
vec!["base",
"base::test",
Expand All @@ -1940,52 +1961,67 @@ mod tests {
}

let substr = filter_tests(&TestOpts {
filter: Some("base".into()),
filter: Some(TestNamePattern::Substring("base".into())),
..TestOpts::new()
}, tests());
assert_eq!(substr.len(), 4);

let substr = filter_tests(&TestOpts {
filter: Some("bas".into()),
filter: Some(TestNamePattern::Substring("bas".into())),
..TestOpts::new()
}, tests());
assert_eq!(substr.len(), 4);

let substr = filter_tests(&TestOpts {
filter: Some("::test".into()),
filter: Some(TestNamePattern::Substring("::test".into())),
..TestOpts::new()
}, tests());
assert_eq!(substr.len(), 3);

let substr = filter_tests(&TestOpts {
filter: Some("base::test".into()),
filter: Some(TestNamePattern::Substring("base::test".into())),
..TestOpts::new()
}, tests());
assert_eq!(substr.len(), 3);

let exact = filter_tests(&TestOpts {
filter: Some("base".into()),
filter_exact: true, ..TestOpts::new()
filter: Some(TestNamePattern::Exact("base".into())),
..TestOpts::new()
}, tests());
assert_eq!(exact.len(), 1);

let exact = filter_tests(&TestOpts {
filter: Some("bas".into()),
filter_exact: true,
filter: Some(TestNamePattern::Exact("bas".into())),
..TestOpts::new()
}, tests());
assert_eq!(exact.len(), 0);

let exact = filter_tests(&TestOpts {
filter: Some("::test".into()),
filter_exact: true,
filter: Some(TestNamePattern::Exact("::test".into())),
..TestOpts::new()
}, tests());
assert_eq!(exact.len(), 0);

let exact = filter_tests(&TestOpts {
filter: Some("base::test".into()),
filter_exact: true,
filter: Some(TestNamePattern::Exact("base::test".into())),
..TestOpts::new()
}, tests());
assert_eq!(exact.len(), 1);

let exact = filter_tests(&TestOpts {
filter: Some(TestNamePattern::new("base*".into(), false)),
..TestOpts::new()
}, tests());
assert_eq!(exact.len(), 4);

let exact = filter_tests(&TestOpts {
filter: Some(TestNamePattern::new("base::test?".into(), false)),
..TestOpts::new()
}, tests());
assert_eq!(exact.len(), 2);

let exact = filter_tests(&TestOpts {
filter: Some(TestNamePattern::new("base::test[2-9]".into(), false)),
..TestOpts::new()
}, tests());
assert_eq!(exact.len(), 1);
Expand Down
10 changes: 8 additions & 2 deletions src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,15 @@ pub fn run_tests(config: &Config) {
}

pub fn test_opts(config: &Config) -> test::TestOpts {
let filter = config.filter.clone().map(|filter| {
test::TestNamePattern::new(
filter,
config.filter_exact,
)
});

test::TestOpts {
filter: config.filter.clone(),
filter_exact: config.filter_exact,
filter: filter,
run_ignored: config.run_ignored,
quiet: config.quiet,
logfile: config.logfile.clone(),
Expand Down