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 unstable option to ignore should_panic tests #58689

Merged
merged 2 commits into from
Mar 1, 2019
Merged
Changes from 1 commit
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
45 changes: 45 additions & 0 deletions src/libtest/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ pub struct TestOpts {
pub list: bool,
pub filter: Option<String>,
pub filter_exact: bool,
pub exclude_should_panic: bool,
pub run_ignored: RunIgnored,
pub run_tests: bool,
pub bench_benchmarks: bool,
Expand All @@ -385,6 +386,7 @@ impl TestOpts {
list: false,
filter: None,
filter_exact: false,
exclude_should_panic: false,
run_ignored: RunIgnored::No,
run_tests: false,
bench_benchmarks: false,
Expand All @@ -406,6 +408,7 @@ fn optgroups() -> getopts::Options {
let mut opts = getopts::Options::new();
opts.optflag("", "include-ignored", "Run ignored and not ignored tests")
.optflag("", "ignored", "Run only ignored tests")
.optflag("", "exclude-should-panic", "Sets #[should_panic] tests to imply #[ignore]")
.optflag("", "test", "Run tests and not benchmarks")
.optflag("", "bench", "Run benchmarks instead of tests")
.optflag("", "list", "List all tests and benchmarks")
Expand Down Expand Up @@ -558,6 +561,13 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
None
};

let exclude_should_panic = matches.opt_present("exclude-should-panic");
if !allow_unstable && exclude_should_panic {
return Some(Err(
"The \"exclude-should-panic\" flag is only accepted on the nightly compiler".into(),
));
}

let include_ignored = matches.opt_present("include-ignored");
if !allow_unstable && include_ignored {
return Some(Err(
Expand Down Expand Up @@ -648,6 +658,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
list,
filter,
filter_exact: exact,
exclude_should_panic,
run_ignored,
run_tests,
bench_benchmarks,
Expand Down Expand Up @@ -1365,6 +1376,14 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescA
// Skip tests that match any of the skip filters
filtered.retain(|test| !opts.skip.iter().any(|sf| matches_filter(test, sf)));

// Set #[should_panic] tests to ignore
if opts.exclude_should_panic {
filtered
.iter_mut()
.filter(|test| test.desc.should_panic != ShouldPanic::No)
.for_each(|test| test.desc.ignore = true);
memoryruins marked this conversation as resolved.
Show resolved Hide resolved
}

// maybe unignore tests
match opts.run_ignored {
RunIgnored::Yes => {
Expand Down Expand Up @@ -1983,6 +2002,32 @@ mod tests {
assert!(!filtered[1].desc.ignore);
}

#[test]
pub fn exclude_should_panic_option() {
let mut opts = TestOpts::new();
opts.run_tests = true;
opts.exclude_should_panic = true;

let mut tests = one_ignored_one_unignored_test();

tests.push(TestDescAndFn {
desc: TestDesc {
name: StaticTestName("3"),
ignore: false,
should_panic: ShouldPanic::YesWithMessage("should panic with message"),
allow_fail: false,
},
testfn: DynTestFn(Box::new(move || {})),
});

let filtered = filter_tests(&opts, tests);

assert_eq!(filtered.len(), 3);
assert!(filtered[0].desc.ignore);
assert!(!filtered[1].desc.ignore);
assert!(filtered[2].desc.ignore);
}

#[test]
pub fn exact_filter_match() {
fn tests() -> Vec<TestDescAndFn> {
Expand Down