Use optflag for --report-time

Essentially, what is described here:
https://github.com/rust-lang/rust/issues/64888#issuecomment-1008047228

There is one difference. The comment proposes to add a
`--report-time-color` option. This change instead uses libtest's
existing `--color` option for that purpose.
This commit is contained in:
Samuel E. Moelius III 2022-01-30 10:25:40 -05:00
parent a00e130dae
commit 96d96a7ac4
6 changed files with 9 additions and 22 deletions

View File

@ -109,12 +109,10 @@ fn optgroups() -> getopts::Options {
unstable-options = Allow use of experimental features", unstable-options = Allow use of experimental features",
"unstable-options", "unstable-options",
) )
.optflagopt( .optflag(
"", "",
"report-time", "report-time",
"Show execution time of each test. Available values: "Show execution time of each test.
plain = do not colorize the execution time (default);
colored = colorize output according to the `color` parameter value;
Threshold values for colorized output can be configured via Threshold values for colorized output can be configured via
`RUST_TEST_TIME_UNIT`, `RUST_TEST_TIME_INTEGRATION` and `RUST_TEST_TIME_UNIT`, `RUST_TEST_TIME_INTEGRATION` and
@ -125,7 +123,6 @@ fn optgroups() -> getopts::Options {
is 0.5 seconds, and the critical time is 2 seconds. is 0.5 seconds, and the critical time is 2 seconds.
Not available for --format=terse", Not available for --format=terse",
"plain|colored",
) )
.optflag( .optflag(
"", "",
@ -319,17 +316,12 @@ fn get_time_options(
allow_unstable: bool, allow_unstable: bool,
) -> OptPartRes<Option<TestTimeOptions>> { ) -> OptPartRes<Option<TestTimeOptions>> {
let report_time = unstable_optflag!(matches, allow_unstable, "report-time"); let report_time = unstable_optflag!(matches, allow_unstable, "report-time");
let colored_opt_str = matches.opt_str("report-time");
let mut report_time_colored = report_time && colored_opt_str == Some("colored".into());
let ensure_test_time = unstable_optflag!(matches, allow_unstable, "ensure-time"); let ensure_test_time = unstable_optflag!(matches, allow_unstable, "ensure-time");
// If `ensure-test-time` option is provided, time output is enforced, // If `ensure-test-time` option is provided, time output is enforced,
// so user won't be confused if any of tests will silently fail. // so user won't be confused if any of tests will silently fail.
let options = if report_time || ensure_test_time { let options = if report_time || ensure_test_time {
if ensure_test_time && !report_time { Some(TestTimeOptions::new_from_env(ensure_test_time))
report_time_colored = true;
}
Some(TestTimeOptions::new_from_env(ensure_test_time, report_time_colored))
} else { } else {
None None
}; };

View File

@ -102,7 +102,7 @@ impl<T: Write> PrettyFormatter<T> {
if let (Some(opts), Some(time)) = (self.time_options, exec_time) { if let (Some(opts), Some(time)) = (self.time_options, exec_time) {
let time_str = format!(" <{}>", time); let time_str = format!(" <{}>", time);
let color = if opts.colored { let color = if self.use_color {
if opts.is_critical(desc, time) { if opts.is_critical(desc, time) {
Some(term::color::RED) Some(term::color::RED)
} else if opts.is_warn(desc, time) { } else if opts.is_warn(desc, time) {

View File

@ -382,7 +382,6 @@ fn test_time_options_threshold() {
let options = TestTimeOptions { let options = TestTimeOptions {
error_on_excess: false, error_on_excess: false,
colored: false,
unit_threshold: unit.clone(), unit_threshold: unit.clone(),
integration_threshold: integration.clone(), integration_threshold: integration.clone(),
doctest_threshold: doc.clone(), doctest_threshold: doc.clone(),

View File

@ -137,14 +137,13 @@ pub struct TestTimeOptions {
/// Denotes if the test critical execution time limit excess should be considered /// Denotes if the test critical execution time limit excess should be considered
/// a test failure. /// a test failure.
pub error_on_excess: bool, pub error_on_excess: bool,
pub colored: bool,
pub unit_threshold: TimeThreshold, pub unit_threshold: TimeThreshold,
pub integration_threshold: TimeThreshold, pub integration_threshold: TimeThreshold,
pub doctest_threshold: TimeThreshold, pub doctest_threshold: TimeThreshold,
} }
impl TestTimeOptions { impl TestTimeOptions {
pub fn new_from_env(error_on_excess: bool, colored: bool) -> Self { pub fn new_from_env(error_on_excess: bool) -> Self {
let unit_threshold = TimeThreshold::from_env_var(time_constants::UNIT_ENV_NAME) let unit_threshold = TimeThreshold::from_env_var(time_constants::UNIT_ENV_NAME)
.unwrap_or_else(Self::default_unit); .unwrap_or_else(Self::default_unit);
@ -155,7 +154,7 @@ impl TestTimeOptions {
let doctest_threshold = TimeThreshold::from_env_var(time_constants::DOCTEST_ENV_NAME) let doctest_threshold = TimeThreshold::from_env_var(time_constants::DOCTEST_ENV_NAME)
.unwrap_or_else(Self::default_doctest); .unwrap_or_else(Self::default_doctest);
Self { error_on_excess, colored, unit_threshold, integration_threshold, doctest_threshold } Self { error_on_excess, unit_threshold, integration_threshold, doctest_threshold }
} }
pub fn is_warn(&self, test: &TestDesc, exec_time: &TestExecTime) -> bool { pub fn is_warn(&self, test: &TestDesc, exec_time: &TestExecTime) -> bool {

View File

@ -267,7 +267,7 @@ Controls the format of the output. Valid options:
Writes the results of the tests to the given file. Writes the results of the tests to the given file.
#### `--report-time` _FORMAT_ #### `--report-time`
⚠️ 🚧 This option is [unstable](#unstable-options), and requires the `-Z ⚠️ 🚧 This option is [unstable](#unstable-options), and requires the `-Z
unstable-options` flag. See [tracking issue unstable-options` flag. See [tracking issue

View File

@ -21,11 +21,8 @@ Sample usage command:
Available options: Available options:
```sh ```sh
--report-time [plain|colored] --report-time
Show execution time of each test. Available values: Show execution time of each test.
plain = do not colorize the execution time (default);
colored = colorize output according to the `color`
parameter value;
Threshold values for colorized output can be Threshold values for colorized output can be
configured via configured via
`RUST_TEST_TIME_UNIT`, `RUST_TEST_TIME_INTEGRATION` `RUST_TEST_TIME_UNIT`, `RUST_TEST_TIME_INTEGRATION`