Skip to content

Commit 9adf26a

Browse files
committed
add option to run all tests
add --all flag to libtest that runs ignored and not ignored tests
1 parent 435b0ab commit 9adf26a

File tree

1 file changed

+96
-46
lines changed

1 file changed

+96
-46
lines changed

src/libtest/lib.rs

+96-46
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const QUIET_MODE_MAX_COLUMN: usize = 100; // insert a '\n' after 100 tests in qu
8080
// to be used by rustc to compile tests in libtest
8181
pub mod test {
8282
pub use {assert_test_result, filter_tests, parse_opts, run_test, test_main, test_main_static,
83-
Bencher, DynTestFn, DynTestName, Metric, MetricMap, Options, ShouldPanic,
83+
Bencher, DynTestFn, DynTestName, Metric, MetricMap, Options, RunIgnored, ShouldPanic,
8484
StaticBenchFn, StaticTestFn, StaticTestName, TestDesc, TestDescAndFn, TestName,
8585
TestOpts, TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk};
8686
}
@@ -348,12 +348,19 @@ pub enum OutputFormat {
348348
Json,
349349
}
350350

351+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
352+
pub enum RunIgnored {
353+
Yes,
354+
No,
355+
Only,
356+
}
357+
351358
#[derive(Debug)]
352359
pub struct TestOpts {
353360
pub list: bool,
354361
pub filter: Option<String>,
355362
pub filter_exact: bool,
356-
pub run_ignored: bool,
363+
pub run_ignored: RunIgnored,
357364
pub run_tests: bool,
358365
pub bench_benchmarks: bool,
359366
pub logfile: Option<PathBuf>,
@@ -372,7 +379,7 @@ impl TestOpts {
372379
list: false,
373380
filter: None,
374381
filter_exact: false,
375-
run_ignored: false,
382+
run_ignored: RunIgnored::No,
376383
run_tests: false,
377384
bench_benchmarks: false,
378385
logfile: None,
@@ -391,7 +398,8 @@ pub type OptRes = Result<TestOpts, String>;
391398

392399
fn optgroups() -> getopts::Options {
393400
let mut opts = getopts::Options::new();
394-
opts.optflag("", "ignored", "Run ignored tests")
401+
opts.optflag("", "all", "Run ignored and not ignored tests")
402+
.optflag("", "ignored", "Run only ignored tests")
395403
.optflag("", "test", "Run tests and not benchmarks")
396404
.optflag("", "bench", "Run benchmarks instead of tests")
397405
.optflag("", "list", "List all tests and benchmarks")
@@ -490,8 +498,8 @@ Test Attributes:
490498
contain: #[should_panic(expected = "foo")].
491499
#[ignore] - When applied to a function which is already attributed as a
492500
test, then the test runner will ignore these tests during
493-
normal test runs. Running with --ignored will run these
494-
tests."#,
501+
normal test runs. Running with --ignored or --all will run
502+
these tests."#,
495503
usage = options.usage(&message)
496504
);
497505
}
@@ -544,7 +552,14 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
544552
None
545553
};
546554

547-
let run_ignored = matches.opt_present("ignored");
555+
let run_ignored = match (matches.opt_present("all"), matches.opt_present("ignored")) {
556+
(true, true) => return Some(Err(
557+
"the options --all and --ignored are mutually exclusive".into()
558+
)),
559+
(true, false) => RunIgnored::Yes,
560+
(false, true) => RunIgnored::Only,
561+
(false, false) => RunIgnored::No,
562+
};
548563
let quiet = matches.opt_present("quiet");
549564
let exact = matches.opt_present("exact");
550565
let list = matches.opt_present("list");
@@ -1315,16 +1330,17 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescA
13151330
!opts.skip.iter().any(|sf| matches_filter(test, sf))
13161331
});
13171332

1318-
// Maybe pull out the ignored test and unignore them
1319-
if opts.run_ignored {
1320-
filtered = filtered.into_iter()
1321-
.filter(|test| test.desc.ignore)
1322-
.map(|mut test| {
1323-
test.desc.ignore = false;
1324-
test
1325-
})
1326-
.collect();
1327-
};
1333+
// maybe unignore tests
1334+
match opts.run_ignored {
1335+
RunIgnored::Yes => {
1336+
filtered.iter_mut().for_each(|test| test.desc.ignore = false);
1337+
},
1338+
RunIgnored::Only => {
1339+
filtered.retain(|test| test.desc.ignore);
1340+
filtered.iter_mut().for_each(|test| test.desc.ignore = false);
1341+
}
1342+
RunIgnored::No => {}
1343+
}
13281344

13291345
// Sort the tests alphabetically
13301346
filtered.sort_by(|t1, t2| t1.desc.name.as_slice().cmp(t2.desc.name.as_slice()));
@@ -1713,13 +1729,37 @@ pub mod bench {
17131729

17141730
#[cfg(test)]
17151731
mod tests {
1716-
use test::{filter_tests, parse_opts, run_test, DynTestFn, DynTestName, MetricMap, ShouldPanic,
1717-
StaticTestName, TestDesc, TestDescAndFn, TestOpts, TrFailed, TrFailedMsg,
1718-
TrIgnored, TrOk};
1732+
use test::{filter_tests, parse_opts, run_test, DynTestFn, DynTestName, MetricMap, RunIgnored,
1733+
ShouldPanic, StaticTestName, TestDesc, TestDescAndFn, TestOpts, TrFailed,
1734+
TrFailedMsg, TrIgnored, TrOk};
17191735
use std::sync::mpsc::channel;
17201736
use bench;
17211737
use Bencher;
17221738

1739+
1740+
fn one_ignored_one_unignored_test() -> Vec<TestDescAndFn> {
1741+
vec![
1742+
TestDescAndFn {
1743+
desc: TestDesc {
1744+
name: StaticTestName("1"),
1745+
ignore: true,
1746+
should_panic: ShouldPanic::No,
1747+
allow_fail: false,
1748+
},
1749+
testfn: DynTestFn(Box::new(move || {})),
1750+
},
1751+
TestDescAndFn {
1752+
desc: TestDesc {
1753+
name: StaticTestName("2"),
1754+
ignore: false,
1755+
should_panic: ShouldPanic::No,
1756+
allow_fail: false,
1757+
},
1758+
testfn: DynTestFn(Box::new(move || {})),
1759+
},
1760+
]
1761+
}
1762+
17231763
#[test]
17241764
pub fn do_not_run_ignored_tests() {
17251765
fn f() {
@@ -1845,11 +1885,19 @@ mod tests {
18451885
"filter".to_string(),
18461886
"--ignored".to_string(),
18471887
];
1848-
let opts = match parse_opts(&args) {
1849-
Some(Ok(o)) => o,
1850-
_ => panic!("Malformed arg in parse_ignored_flag"),
1851-
};
1852-
assert!((opts.run_ignored));
1888+
let opts = parse_opts(&args).unwrap().unwrap();
1889+
assert_eq!(opts.run_ignored, RunIgnored::Only);
1890+
}
1891+
1892+
#[test]
1893+
fn parse_all_flag() {
1894+
let args = vec![
1895+
"progname".to_string(),
1896+
"filter".to_string(),
1897+
"--all".to_string(),
1898+
];
1899+
let opts = parse_opts(&args).unwrap().unwrap();
1900+
assert_eq!(opts.run_ignored, RunIgnored::Yes);
18531901
}
18541902

18551903
#[test]
@@ -1859,35 +1907,33 @@ mod tests {
18591907

18601908
let mut opts = TestOpts::new();
18611909
opts.run_tests = true;
1862-
opts.run_ignored = true;
1910+
opts.run_ignored = RunIgnored::Only;
18631911

1864-
let tests = vec![
1865-
TestDescAndFn {
1866-
desc: TestDesc {
1867-
name: StaticTestName("1"),
1868-
ignore: true,
1869-
should_panic: ShouldPanic::No,
1870-
allow_fail: false,
1871-
},
1872-
testfn: DynTestFn(Box::new(move || {})),
1873-
},
1874-
TestDescAndFn {
1875-
desc: TestDesc {
1876-
name: StaticTestName("2"),
1877-
ignore: false,
1878-
should_panic: ShouldPanic::No,
1879-
allow_fail: false,
1880-
},
1881-
testfn: DynTestFn(Box::new(move || {})),
1882-
},
1883-
];
1912+
let tests = one_ignored_one_unignored_test();
18841913
let filtered = filter_tests(&opts, tests);
18851914

18861915
assert_eq!(filtered.len(), 1);
18871916
assert_eq!(filtered[0].desc.name.to_string(), "1");
18881917
assert!(!filtered[0].desc.ignore);
18891918
}
18901919

1920+
#[test]
1921+
pub fn run_all_option() {
1922+
// When we run "--all" tests, the ignore flag should be set to false on
1923+
// all tests and no test filtered out
1924+
1925+
let mut opts = TestOpts::new();
1926+
opts.run_tests = true;
1927+
opts.run_ignored = RunIgnored::Yes;
1928+
1929+
let tests = one_ignored_one_unignored_test();
1930+
let filtered = filter_tests(&opts, tests);
1931+
1932+
assert_eq!(filtered.len(), 2);
1933+
assert!(!filtered[0].desc.ignore);
1934+
assert!(!filtered[1].desc.ignore);
1935+
}
1936+
18911937
#[test]
18921938
pub fn exact_filter_match() {
18931939
fn tests() -> Vec<TestDescAndFn> {
@@ -1995,7 +2041,9 @@ mod tests {
19952041
"test::ignored_tests_result_in_ignored".to_string(),
19962042
"test::first_free_arg_should_be_a_filter".to_string(),
19972043
"test::parse_ignored_flag".to_string(),
2044+
"test::parse_all_flag".to_string(),
19982045
"test::filter_for_ignored_option".to_string(),
2046+
"test::run_all_option".to_string(),
19992047
"test::sort_tests".to_string(),
20002048
];
20012049
let tests = {
@@ -2025,7 +2073,9 @@ mod tests {
20252073
"test::filter_for_ignored_option".to_string(),
20262074
"test::first_free_arg_should_be_a_filter".to_string(),
20272075
"test::ignored_tests_result_in_ignored".to_string(),
2076+
"test::parse_all_flag".to_string(),
20282077
"test::parse_ignored_flag".to_string(),
2078+
"test::run_all_option".to_string(),
20292079
"test::sort_tests".to_string(),
20302080
];
20312081

0 commit comments

Comments
 (0)