Skip to content

Commit 5d1f100

Browse files
committed
Add unstable option to ignore should_panic tests.
1 parent aadbc45 commit 5d1f100

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/libtest/lib.rs

+45
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ pub struct TestOpts {
366366
pub list: bool,
367367
pub filter: Option<String>,
368368
pub filter_exact: bool,
369+
pub exclude_should_panic: bool,
369370
pub run_ignored: RunIgnored,
370371
pub run_tests: bool,
371372
pub bench_benchmarks: bool,
@@ -385,6 +386,7 @@ impl TestOpts {
385386
list: false,
386387
filter: None,
387388
filter_exact: false,
389+
exclude_should_panic: false,
388390
run_ignored: RunIgnored::No,
389391
run_tests: false,
390392
bench_benchmarks: false,
@@ -406,6 +408,7 @@ fn optgroups() -> getopts::Options {
406408
let mut opts = getopts::Options::new();
407409
opts.optflag("", "include-ignored", "Run ignored and not ignored tests")
408410
.optflag("", "ignored", "Run only ignored tests")
411+
.optflag("", "exclude-should-panic", "Sets #[should_panic] tests to imply #[ignore]")
409412
.optflag("", "test", "Run tests and not benchmarks")
410413
.optflag("", "bench", "Run benchmarks instead of tests")
411414
.optflag("", "list", "List all tests and benchmarks")
@@ -558,6 +561,13 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
558561
None
559562
};
560563

564+
let exclude_should_panic = matches.opt_present("exclude-should-panic");
565+
if !allow_unstable && exclude_should_panic {
566+
return Some(Err(
567+
"The \"exclude-should-panic\" flag is only accepted on the nightly compiler".into(),
568+
));
569+
}
570+
561571
let include_ignored = matches.opt_present("include-ignored");
562572
if !allow_unstable && include_ignored {
563573
return Some(Err(
@@ -648,6 +658,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
648658
list,
649659
filter,
650660
filter_exact: exact,
661+
exclude_should_panic,
651662
run_ignored,
652663
run_tests,
653664
bench_benchmarks,
@@ -1365,6 +1376,14 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescA
13651376
// Skip tests that match any of the skip filters
13661377
filtered.retain(|test| !opts.skip.iter().any(|sf| matches_filter(test, sf)));
13671378

1379+
// Set #[should_panic] tests to ignore
1380+
if opts.exclude_should_panic {
1381+
filtered
1382+
.iter_mut()
1383+
.filter(|test| test.desc.should_panic != ShouldPanic::No)
1384+
.for_each(|test| test.desc.ignore = true);
1385+
}
1386+
13681387
// maybe unignore tests
13691388
match opts.run_ignored {
13701389
RunIgnored::Yes => {
@@ -1983,6 +2002,32 @@ mod tests {
19832002
assert!(!filtered[1].desc.ignore);
19842003
}
19852004

2005+
#[test]
2006+
pub fn exclude_should_panic_option() {
2007+
let mut opts = TestOpts::new();
2008+
opts.run_tests = true;
2009+
opts.exclude_should_panic = true;
2010+
2011+
let mut tests = one_ignored_one_unignored_test();
2012+
2013+
tests.push(TestDescAndFn {
2014+
desc: TestDesc {
2015+
name: StaticTestName("3"),
2016+
ignore: false,
2017+
should_panic: ShouldPanic::YesWithMessage("should panic with message"),
2018+
allow_fail: false,
2019+
},
2020+
testfn: DynTestFn(Box::new(move || {})),
2021+
});
2022+
2023+
let filtered = filter_tests(&opts, tests);
2024+
2025+
assert_eq!(filtered.len(), 3);
2026+
assert!(filtered[0].desc.ignore);
2027+
assert!(!filtered[1].desc.ignore);
2028+
assert!(filtered[2].desc.ignore);
2029+
}
2030+
19862031
#[test]
19872032
pub fn exact_filter_match() {
19882033
fn tests() -> Vec<TestDescAndFn> {

0 commit comments

Comments
 (0)