Rollup merge of #58118 - h-michael:libtest-theme-2018, r=Centril
Transition libtest to 2018 edition Transitions libtest to Rust 2018; cc #58099 r? @Centril
This commit is contained in:
commit
9b793702fe
4 changed files with 104 additions and 71 deletions
|
|
@ -2,6 +2,7 @@
|
|||
authors = ["The Rust Project Developers"]
|
||||
name = "test"
|
||||
version = "0.0.0"
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
name = "test"
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ impl<T: Write> OutputFormatter for JsonFormatter<T> {
|
|||
struct EscapedString<S: AsRef<str>>(S);
|
||||
|
||||
impl<S: AsRef<str>> ::std::fmt::Display for EscapedString<S> {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
|
||||
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
|
||||
let mut start = 0;
|
||||
|
||||
for (i, byte) in self.0.as_ref().bytes().enumerate() {
|
||||
|
|
|
|||
|
|
@ -17,11 +17,15 @@
|
|||
// this crate, which relies on this attribute (rather than the value of `--crate-name` passed by
|
||||
// cargo) to detect this crate.
|
||||
|
||||
#![deny(rust_2018_idioms)]
|
||||
#![crate_name = "test"]
|
||||
#![unstable(feature = "test", issue = "27812")]
|
||||
#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
||||
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
||||
html_root_url = "https://doc.rust-lang.org/nightly/", test(attr(deny(warnings))))]
|
||||
#![doc(
|
||||
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
||||
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
||||
html_root_url = "https://doc.rust-lang.org/nightly/",
|
||||
test(attr(deny(warnings)))
|
||||
)]
|
||||
#![feature(asm)]
|
||||
#![feature(fnbox)]
|
||||
#![cfg_attr(any(unix, target_os = "cloudabi"), feature(libc, rustc_private))]
|
||||
|
|
@ -32,10 +36,10 @@
|
|||
#![feature(termination_trait_lib)]
|
||||
#![feature(test)]
|
||||
|
||||
extern crate getopts;
|
||||
use getopts;
|
||||
#[cfg(any(unix, target_os = "cloudabi"))]
|
||||
extern crate libc;
|
||||
extern crate term;
|
||||
use term;
|
||||
|
||||
// FIXME(#54291): rustc and/or LLVM don't yet support building with panic-unwind
|
||||
// on aarch64-pc-windows-msvc, so we don't link libtest against
|
||||
|
|
@ -46,52 +50,57 @@ extern crate term;
|
|||
#[cfg(not(all(windows, target_arch = "aarch64")))]
|
||||
extern crate panic_unwind;
|
||||
|
||||
pub use self::TestFn::*;
|
||||
pub use self::ColorConfig::*;
|
||||
pub use self::TestResult::*;
|
||||
pub use self::TestName::*;
|
||||
use self::TestEvent::*;
|
||||
use self::NamePadding::*;
|
||||
use self::OutputLocation::*;
|
||||
use self::TestEvent::*;
|
||||
pub use self::TestFn::*;
|
||||
pub use self::TestName::*;
|
||||
pub use self::TestResult::*;
|
||||
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use std::any::Any;
|
||||
use std::borrow::Cow;
|
||||
use std::boxed::FnBox;
|
||||
use std::cmp;
|
||||
use std::collections::BTreeMap;
|
||||
use std::env;
|
||||
use std::fmt;
|
||||
use std::fs::File;
|
||||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
use std::io::prelude::*;
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
use std::process::Termination;
|
||||
use std::sync::mpsc::{channel, Sender};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::borrow::Cow;
|
||||
use std::process;
|
||||
|
||||
const TEST_WARN_TIMEOUT_S: u64 = 60;
|
||||
const QUIET_MODE_MAX_COLUMN: usize = 100; // insert a '\n' after 100 tests in quiet mode
|
||||
|
||||
// to be used by rustc to compile tests in libtest
|
||||
pub mod test {
|
||||
pub use {assert_test_result, filter_tests, parse_opts, run_test, test_main, test_main_static,
|
||||
Bencher, DynTestFn, DynTestName, Metric, MetricMap, Options, RunIgnored, ShouldPanic,
|
||||
StaticBenchFn, StaticTestFn, StaticTestName, TestDesc, TestDescAndFn, TestName,
|
||||
TestOpts, TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk};
|
||||
pub use crate::{
|
||||
assert_test_result, filter_tests, parse_opts, run_test, test_main, test_main_static,
|
||||
Bencher, DynTestFn, DynTestName, Metric, MetricMap, Options, RunIgnored, ShouldPanic,
|
||||
StaticBenchFn, StaticTestFn, StaticTestName, TestDesc, TestDescAndFn, TestName, TestOpts,
|
||||
TestResult, TrFailed, TrFailedMsg, TrIgnored, TrOk,
|
||||
};
|
||||
}
|
||||
|
||||
pub mod stats;
|
||||
mod formatters;
|
||||
pub mod stats;
|
||||
|
||||
use formatters::{JsonFormatter, OutputFormatter, PrettyFormatter, TerseFormatter};
|
||||
use crate::formatters::{JsonFormatter, OutputFormatter, PrettyFormatter, TerseFormatter};
|
||||
|
||||
/// Whether to execute tests concurrently or not
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Concurrent { Yes, No }
|
||||
pub enum Concurrent {
|
||||
Yes,
|
||||
No,
|
||||
}
|
||||
|
||||
// The name of a test. By convention this follows the rules for rust
|
||||
// paths; i.e., it should be a series of identifiers separated by double
|
||||
|
|
@ -131,7 +140,7 @@ impl TestName {
|
|||
}
|
||||
}
|
||||
impl fmt::Display for TestName {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(self.as_slice(), f)
|
||||
}
|
||||
}
|
||||
|
|
@ -185,7 +194,7 @@ impl TestFn {
|
|||
}
|
||||
|
||||
impl fmt::Debug for TestFn {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(match *self {
|
||||
StaticTestFn(..) => "StaticTestFn(..)",
|
||||
StaticBenchFn(..) => "StaticBenchFn(..)",
|
||||
|
|
@ -329,8 +338,7 @@ pub fn test_main_static(tests: &[&TestDescAndFn]) {
|
|||
pub fn assert_test_result<T: Termination>(result: T) {
|
||||
let code = result.report();
|
||||
assert_eq!(
|
||||
code,
|
||||
0,
|
||||
code, 0,
|
||||
"the test returned a termination value with a non-zero status code ({}) \
|
||||
which indicates a failure",
|
||||
code
|
||||
|
|
@ -558,14 +566,16 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
|||
let include_ignored = matches.opt_present("include-ignored");
|
||||
if !allow_unstable && include_ignored {
|
||||
return Some(Err(
|
||||
"The \"include-ignored\" flag is only accepted on the nightly compiler".into()
|
||||
"The \"include-ignored\" flag is only accepted on the nightly compiler".into(),
|
||||
));
|
||||
}
|
||||
|
||||
let run_ignored = match (include_ignored, matches.opt_present("ignored")) {
|
||||
(true, true) => return Some(Err(
|
||||
"the options --include-ignored and --ignored are mutually exclusive".into()
|
||||
)),
|
||||
(true, true) => {
|
||||
return Some(Err(
|
||||
"the options --include-ignored and --ignored are mutually exclusive".into(),
|
||||
));
|
||||
}
|
||||
(true, false) => RunIgnored::Yes,
|
||||
(false, true) => RunIgnored::Only,
|
||||
(false, false) => RunIgnored::No,
|
||||
|
|
@ -597,7 +607,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
|||
"argument for --test-threads must be a number > 0 \
|
||||
(error: {})",
|
||||
e
|
||||
)))
|
||||
)));
|
||||
}
|
||||
},
|
||||
None => None,
|
||||
|
|
@ -613,7 +623,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
|||
"argument for --color must be auto, always, or never (was \
|
||||
{})",
|
||||
v
|
||||
)))
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -635,7 +645,7 @@ pub fn parse_opts(args: &[String]) -> Option<OptRes> {
|
|||
"argument for --format must be pretty, terse, or json (was \
|
||||
{})",
|
||||
v
|
||||
)))
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -823,7 +833,7 @@ pub fn list_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Res
|
|||
let mut nbench = 0;
|
||||
|
||||
for test in filter_tests(&opts, tests) {
|
||||
use TestFn::*;
|
||||
use crate::TestFn::*;
|
||||
|
||||
let TestDescAndFn {
|
||||
desc: TestDesc { name, .. },
|
||||
|
|
@ -1012,10 +1022,12 @@ fn use_color(opts: &TestOpts) -> bool {
|
|||
}
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "cloudabi",
|
||||
target_os = "redox",
|
||||
all(target_arch = "wasm32", not(target_os = "emscripten")),
|
||||
all(target_vendor = "fortanix", target_env = "sgx")))]
|
||||
#[cfg(any(
|
||||
target_os = "cloudabi",
|
||||
target_os = "redox",
|
||||
all(target_arch = "wasm32", not(target_os = "emscripten")),
|
||||
all(target_vendor = "fortanix", target_env = "sgx")
|
||||
))]
|
||||
fn stdout_isatty() -> bool {
|
||||
// FIXME: Implement isatty on Redox and SGX
|
||||
false
|
||||
|
|
@ -1246,21 +1258,34 @@ fn get_concurrency() -> usize {
|
|||
1
|
||||
}
|
||||
|
||||
#[cfg(any(all(target_arch = "wasm32", not(target_os = "emscripten")),
|
||||
all(target_vendor = "fortanix", target_env = "sgx")))]
|
||||
#[cfg(any(
|
||||
all(target_arch = "wasm32", not(target_os = "emscripten")),
|
||||
all(target_vendor = "fortanix", target_env = "sgx")
|
||||
))]
|
||||
fn num_cpus() -> usize {
|
||||
1
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "android", target_os = "cloudabi", target_os = "emscripten",
|
||||
target_os = "fuchsia", target_os = "ios", target_os = "linux",
|
||||
target_os = "macos", target_os = "solaris"))]
|
||||
#[cfg(any(
|
||||
target_os = "android",
|
||||
target_os = "cloudabi",
|
||||
target_os = "emscripten",
|
||||
target_os = "fuchsia",
|
||||
target_os = "ios",
|
||||
target_os = "linux",
|
||||
target_os = "macos",
|
||||
target_os = "solaris"
|
||||
))]
|
||||
fn num_cpus() -> usize {
|
||||
unsafe { libc::sysconf(libc::_SC_NPROCESSORS_ONLN) as usize }
|
||||
}
|
||||
|
||||
#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "bitrig",
|
||||
target_os = "netbsd"))]
|
||||
#[cfg(any(
|
||||
target_os = "freebsd",
|
||||
target_os = "dragonfly",
|
||||
target_os = "bitrig",
|
||||
target_os = "netbsd"
|
||||
))]
|
||||
fn num_cpus() -> usize {
|
||||
use std::ptr;
|
||||
|
||||
|
|
@ -1343,18 +1368,20 @@ 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))
|
||||
});
|
||||
filtered.retain(|test| !opts.skip.iter().any(|sf| matches_filter(test, sf)));
|
||||
|
||||
// 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 => {}
|
||||
}
|
||||
|
|
@ -1396,7 +1423,8 @@ pub fn run_test(
|
|||
) {
|
||||
let TestDescAndFn { desc, testfn } = test;
|
||||
|
||||
let ignore_because_panic_abort = cfg!(target_arch = "wasm32") && !cfg!(target_os = "emscripten")
|
||||
let ignore_because_panic_abort = cfg!(target_arch = "wasm32")
|
||||
&& !cfg!(target_os = "emscripten")
|
||||
&& desc.should_panic != ShouldPanic::No;
|
||||
|
||||
if force_ignore || desc.ignore || ignore_because_panic_abort {
|
||||
|
|
@ -1454,12 +1482,12 @@ pub fn run_test(
|
|||
|
||||
match testfn {
|
||||
DynBenchFn(bencher) => {
|
||||
::bench::benchmark(desc, monitor_ch, opts.nocapture, |harness| {
|
||||
crate::bench::benchmark(desc, monitor_ch, opts.nocapture, |harness| {
|
||||
bencher.run(harness)
|
||||
});
|
||||
}
|
||||
StaticBenchFn(benchfn) => {
|
||||
::bench::benchmark(desc, monitor_ch, opts.nocapture, |harness| {
|
||||
crate::bench::benchmark(desc, monitor_ch, opts.nocapture, |harness| {
|
||||
(benchfn.clone())(harness)
|
||||
});
|
||||
}
|
||||
|
|
@ -1487,7 +1515,8 @@ fn calc_result(desc: &TestDesc, task_result: Result<(), Box<dyn Any + Send>>) ->
|
|||
match (&desc.should_panic, task_result) {
|
||||
(&ShouldPanic::No, Ok(())) | (&ShouldPanic::Yes, Err(_)) => TrOk,
|
||||
(&ShouldPanic::YesWithMessage(msg), Err(ref err)) => {
|
||||
if err.downcast_ref::<String>()
|
||||
if err
|
||||
.downcast_ref::<String>()
|
||||
.map(|e| &**e)
|
||||
.or_else(|| err.downcast_ref::<&'static str>().map(|e| *e))
|
||||
.map(|e| e.contains(msg))
|
||||
|
|
@ -1534,7 +1563,8 @@ impl MetricMap {
|
|||
}
|
||||
|
||||
pub fn fmt_metrics(&self) -> String {
|
||||
let v = self.0
|
||||
let v = self
|
||||
.0
|
||||
.iter()
|
||||
.map(|(k, v)| format!("{}: {} (+/- {})", *k, v.value, v.noise))
|
||||
.collect::<Vec<_>>();
|
||||
|
|
@ -1643,7 +1673,8 @@ where
|
|||
|
||||
// If we've run for 100ms and seem to have converged to a
|
||||
// stable median.
|
||||
if loop_run > Duration::from_millis(100) && summ.median_abs_dev_pct < 1.0
|
||||
if loop_run > Duration::from_millis(100)
|
||||
&& summ.median_abs_dev_pct < 1.0
|
||||
&& summ.median - summ5.median < summ5.median_abs_dev
|
||||
{
|
||||
return summ5;
|
||||
|
|
@ -1669,12 +1700,12 @@ where
|
|||
}
|
||||
|
||||
pub mod bench {
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use super::{BenchMode, BenchSamples, Bencher, MonitorMsg, Sender, Sink, TestDesc, TestResult};
|
||||
use crate::stats;
|
||||
use std::cmp;
|
||||
use std::io;
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use stats;
|
||||
use super::{BenchMode, BenchSamples, Bencher, MonitorMsg, Sender, Sink, TestDesc, TestResult};
|
||||
|
||||
pub fn benchmark<F>(desc: TestDesc, monitor_ch: Sender<MonitorMsg>, nocapture: bool, f: F)
|
||||
where
|
||||
|
|
@ -1749,14 +1780,15 @@ pub mod bench {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use test::{filter_tests, parse_opts, run_test, DynTestFn, DynTestName, MetricMap, RunIgnored,
|
||||
ShouldPanic, StaticTestName, TestDesc, TestDescAndFn, TestOpts, TrFailed,
|
||||
TrFailedMsg, TrIgnored, TrOk};
|
||||
use crate::bench;
|
||||
use crate::test::{
|
||||
filter_tests, parse_opts, run_test, DynTestFn, DynTestName, MetricMap, RunIgnored,
|
||||
ShouldPanic, StaticTestName, TestDesc, TestDescAndFn, TestOpts, TrFailed, TrFailedMsg,
|
||||
TrIgnored, TrOk,
|
||||
};
|
||||
use crate::Bencher;
|
||||
use crate::Concurrent;
|
||||
use std::sync::mpsc::channel;
|
||||
use bench;
|
||||
use Bencher;
|
||||
use Concurrent;
|
||||
|
||||
|
||||
fn one_ignored_one_unignored_test() -> Vec<TestDescAndFn> {
|
||||
vec![
|
||||
|
|
@ -2156,7 +2188,7 @@ mod tests {
|
|||
allow_fail: false,
|
||||
};
|
||||
|
||||
::bench::benchmark(desc, tx, true, f);
|
||||
crate::bench::benchmark(desc, tx, true, f);
|
||||
rx.recv().unwrap();
|
||||
}
|
||||
|
||||
|
|
@ -2175,7 +2207,7 @@ mod tests {
|
|||
allow_fail: false,
|
||||
};
|
||||
|
||||
::bench::benchmark(desc, tx, true, f);
|
||||
crate::bench::benchmark(desc, tx, true, f);
|
||||
rx.recv().unwrap();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -319,8 +319,8 @@ pub fn winsorize(samples: &mut [f64], pct: f64) {
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use stats::Stats;
|
||||
use stats::Summary;
|
||||
use crate::stats::Stats;
|
||||
use crate::stats::Summary;
|
||||
use std::f64;
|
||||
use std::io::prelude::*;
|
||||
use std::io;
|
||||
|
|
@ -899,7 +899,7 @@ mod tests {
|
|||
mod bench {
|
||||
extern crate test;
|
||||
use self::test::Bencher;
|
||||
use stats::Stats;
|
||||
use crate::stats::Stats;
|
||||
|
||||
#[bench]
|
||||
pub fn sum_three_items(b: &mut Bencher) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue