diff options
Diffstat (limited to 'src/libtest/lib.rs')
| -rw-r--r-- | src/libtest/lib.rs | 135 |
1 files changed, 53 insertions, 82 deletions
diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 0097558eae6..884db85efe0 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -32,34 +32,35 @@ #![feature(test)] // Public reexports -pub use self::ColorConfig::*; -pub use self::types::*; -pub use self::types::TestName::*; -pub use self::options::{ColorConfig, Options, OutputFormat, RunIgnored, ShouldPanic}; -pub use self::bench::{Bencher, black_box}; +pub use self::bench::{black_box, Bencher}; pub use self::console::run_tests_console; +pub use self::options::{ColorConfig, Options, OutputFormat, RunIgnored, ShouldPanic}; +pub use self::types::TestName::*; +pub use self::types::*; +pub use self::ColorConfig::*; pub use cli::TestOpts; // Module to be used by rustc to compile tests in libtest pub mod test { pub use crate::{ + assert_test_result, bench::Bencher, cli::{parse_opts, TestOpts}, + filter_tests, helpers::metrics::{Metric, MetricMap}, - options::{ShouldPanic, Options, RunIgnored, RunStrategy}, + options::{Options, RunIgnored, RunStrategy, ShouldPanic}, + run_test, test_main, test_main_static, test_result::{TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk}, - time::{TestTimeOptions, TestExecTime}, + time::{TestExecTime, TestTimeOptions}, types::{ - DynTestFn, DynTestName, StaticBenchFn, StaticTestFn, StaticTestName, - TestDesc, TestDescAndFn, TestName, TestType, + DynTestFn, DynTestName, StaticBenchFn, StaticTestFn, StaticTestName, TestDesc, + TestDescAndFn, TestName, TestType, }, - assert_test_result, filter_tests, run_test, test_main, test_main_static, }; } use std::{ - env, - io, + env, io, io::prelude::Write, panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo}, process, @@ -70,28 +71,28 @@ use std::{ time::{Duration, Instant}, }; -pub mod stats; pub mod bench; -mod formatters; mod cli; mod console; mod event; +mod formatters; mod helpers; -mod time; -mod types; mod options; +pub mod stats; mod test_result; +mod time; +mod types; #[cfg(test)] mod tests; -use test_result::*; -use time::TestExecTime; -use options::{RunStrategy, Concurrent}; use event::{CompletedTest, TestEvent}; -use helpers::sink::Sink; use helpers::concurrency::get_concurrency; use helpers::exit_code::get_exit_code; +use helpers::sink::Sink; +use options::{Concurrent, RunStrategy}; +use test_result::*; +use time::TestExecTime; // Process exit code to be used to indicate test failures. const ERROR_EXIT_CODE: i32 = 101; @@ -177,14 +178,8 @@ pub fn test_main_static_abort(tests: &[&TestDescAndFn]) { /// This will panic when fed any dynamic tests, because they cannot be cloned. fn make_owned_test(test: &&TestDescAndFn) -> TestDescAndFn { match test.testfn { - StaticTestFn(f) => TestDescAndFn { - testfn: StaticTestFn(f), - desc: test.desc.clone(), - }, - StaticBenchFn(f) => TestDescAndFn { - testfn: StaticBenchFn(f), - desc: test.desc.clone(), - }, + StaticTestFn(f) => TestDescAndFn { testfn: StaticTestFn(f), desc: test.desc.clone() }, + StaticBenchFn(f) => TestDescAndFn { testfn: StaticBenchFn(f), desc: test.desc.clone() }, _ => panic!("non-static tests passed to test::test_main_static"), } } @@ -205,7 +200,7 @@ pub fn assert_test_result<T: Termination>(result: T) { pub fn run_tests<F>( opts: &TestOpts, tests: Vec<TestDescAndFn>, - mut notify_about_test_event: F + mut notify_about_test_event: F, ) -> io::Result<()> where F: FnMut(TestEvent) -> io::Result<()>, @@ -267,13 +262,7 @@ where let now = Instant::now(); let timed_out = running_tests .iter() - .filter_map(|(desc, timeout)| { - if &now >= timeout { - Some(desc.clone()) - } else { - None - } - }) + .filter_map(|(desc, timeout)| if &now >= timeout { Some(desc.clone()) } else { None }) .collect(); for test in &timed_out { running_tests.remove(test); @@ -284,11 +273,7 @@ where fn calc_timeout(running_tests: &TestMap) -> Option<Duration> { running_tests.values().min().map(|next_timeout| { let now = Instant::now(); - if *next_timeout >= now { - *next_timeout - now - } else { - Duration::new(0, 0) - } + if *next_timeout >= now { *next_timeout - now } else { Duration::new(0, 0) } }) }; @@ -391,15 +376,11 @@ pub fn filter_tests(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> Vec<TestDescA // maybe unignore tests match opts.run_ignored { RunIgnored::Yes => { - filtered - .iter_mut() - .for_each(|test| test.desc.ignore = false); + filtered.iter_mut().for_each(|test| test.desc.ignore = false); } RunIgnored::Only => { filtered.retain(|test| test.desc.ignore); - filtered - .iter_mut() - .for_each(|test| test.desc.ignore = false); + filtered.iter_mut().for_each(|test| test.desc.ignore = false); } RunIgnored::No => {} } @@ -424,10 +405,7 @@ pub fn convert_benchmarks_to_tests(tests: Vec<TestDescAndFn>) -> Vec<TestDescAnd })), f => f, }; - TestDescAndFn { - desc: x.desc, - testfn, - } + TestDescAndFn { desc: x.desc, testfn } }) .collect() } @@ -444,7 +422,8 @@ pub fn run_test( // Emscripten can catch panics but other wasm targets cannot let ignore_because_no_process_support = desc.should_panic != ShouldPanic::No - && cfg!(target_arch = "wasm32") && !cfg!(target_os = "emscripten"); + && cfg!(target_arch = "wasm32") + && !cfg!(target_os = "emscripten"); if force_ignore || desc.ignore || ignore_because_no_process_support { let message = CompletedTest::new(desc, TrIgnored, None, Vec::new()); @@ -468,19 +447,17 @@ pub fn run_test( let concurrency = opts.concurrency; let name = desc.name.clone(); - let runtest = move || { - match opts.strategy { - RunStrategy::InProcess => - run_test_in_process( - desc, - opts.nocapture, - opts.time.is_some(), - testfn, - monitor_ch, - opts.time - ), - RunStrategy::SpawnPrimary => - spawn_test_subprocess(desc, opts.time.is_some(), monitor_ch, opts.time), + let runtest = move || match opts.strategy { + RunStrategy::InProcess => run_test_in_process( + desc, + opts.nocapture, + opts.time.is_some(), + testfn, + monitor_ch, + opts.time, + ), + RunStrategy::SpawnPrimary => { + spawn_test_subprocess(desc, opts.time.is_some(), monitor_ch, opts.time) } }; @@ -496,12 +473,8 @@ pub fn run_test( } } - let test_run_opts = TestRunOpts { - strategy, - nocapture: opts.nocapture, - concurrency, - time: opts.time_options - }; + let test_run_opts = + TestRunOpts { strategy, nocapture: opts.nocapture, concurrency, time: opts.time_options }; match testfn { DynBenchFn(bencher) => { @@ -597,13 +570,14 @@ fn spawn_test_subprocess( let start = report_time.then(Instant::now); let output = match Command::new(current_exe) .env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice()) - .output() { - Ok(out) => out, - Err(e) => { - let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e); - return (TrFailed, err.into_bytes(), None); - } - }; + .output() + { + Ok(out) => out, + Err(e) => { + let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e); + return (TrFailed, err.into_bytes(), None); + } + }; let exec_time = start.map(|start| { let duration = start.elapsed(); TestExecTime(duration) @@ -632,10 +606,7 @@ fn spawn_test_subprocess( monitor_ch.send(message).unwrap(); } -fn run_test_in_spawned_subprocess( - desc: TestDesc, - testfn: Box<dyn FnOnce() + Send>, -) -> ! { +fn run_test_in_spawned_subprocess(desc: TestDesc, testfn: Box<dyn FnOnce() + Send>) -> ! { let builtin_panic_hook = panic::take_hook(); let record_result = Arc::new(move |panic_info: Option<&'_ PanicInfo<'_>>| { let test_result = match panic_info { |
