From 96d96a7ac48cd8becf3133035ab0cf0717a4cf75 Mon Sep 17 00:00:00 2001
From: "Samuel E. Moelius III" <sam@moeli.us>
Date: Sun, 30 Jan 2022 10:25:40 -0500
Subject: [PATCH] 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.
---
 library/test/src/cli.rs                            | 14 +++-----------
 library/test/src/formatters/pretty.rs              |  2 +-
 library/test/src/tests.rs                          |  1 -
 library/test/src/time.rs                           |  5 ++---
 src/doc/rustc/src/tests/index.md                   |  2 +-
 .../src/compiler-flags/report-time.md              |  7 ++-----
 6 files changed, 9 insertions(+), 22 deletions(-)

diff --git a/library/test/src/cli.rs b/library/test/src/cli.rs
index cb40b4e965b..b39701a3d42 100644
--- a/library/test/src/cli.rs
+++ b/library/test/src/cli.rs
@@ -109,12 +109,10 @@ fn optgroups() -> getopts::Options {
             unstable-options = Allow use of experimental features",
             "unstable-options",
         )
-        .optflagopt(
+        .optflag(
             "",
             "report-time",
-            "Show execution time of each test. Available values:
-            plain   = do not colorize the execution time (default);
-            colored = colorize output according to the `color` parameter value;
+            "Show execution time of each test.
 
             Threshold values for colorized output can be configured via
             `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.
 
             Not available for --format=terse",
-            "plain|colored",
         )
         .optflag(
             "",
@@ -319,17 +316,12 @@ fn get_time_options(
     allow_unstable: bool,
 ) -> OptPartRes<Option<TestTimeOptions>> {
     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");
 
     // If `ensure-test-time` option is provided, time output is enforced,
     // so user won't be confused if any of tests will silently fail.
     let options = if report_time || ensure_test_time {
-        if ensure_test_time && !report_time {
-            report_time_colored = true;
-        }
-        Some(TestTimeOptions::new_from_env(ensure_test_time, report_time_colored))
+        Some(TestTimeOptions::new_from_env(ensure_test_time))
     } else {
         None
     };
diff --git a/library/test/src/formatters/pretty.rs b/library/test/src/formatters/pretty.rs
index 4a03b4b9147..5da275d4af7 100644
--- a/library/test/src/formatters/pretty.rs
+++ b/library/test/src/formatters/pretty.rs
@@ -102,7 +102,7 @@ impl<T: Write> PrettyFormatter<T> {
         if let (Some(opts), Some(time)) = (self.time_options, exec_time) {
             let time_str = format!(" <{}>", time);
 
-            let color = if opts.colored {
+            let color = if self.use_color {
                 if opts.is_critical(desc, time) {
                     Some(term::color::RED)
                 } else if opts.is_warn(desc, time) {
diff --git a/library/test/src/tests.rs b/library/test/src/tests.rs
index 7f0b6193d09..f563d490d11 100644
--- a/library/test/src/tests.rs
+++ b/library/test/src/tests.rs
@@ -382,7 +382,6 @@ fn test_time_options_threshold() {
 
     let options = TestTimeOptions {
         error_on_excess: false,
-        colored: false,
         unit_threshold: unit.clone(),
         integration_threshold: integration.clone(),
         doctest_threshold: doc.clone(),
diff --git a/library/test/src/time.rs b/library/test/src/time.rs
index e0b6eadffa1..8c64e5d1b73 100644
--- a/library/test/src/time.rs
+++ b/library/test/src/time.rs
@@ -137,14 +137,13 @@ pub struct TestTimeOptions {
     /// Denotes if the test critical execution time limit excess should be considered
     /// a test failure.
     pub error_on_excess: bool,
-    pub colored: bool,
     pub unit_threshold: TimeThreshold,
     pub integration_threshold: TimeThreshold,
     pub doctest_threshold: TimeThreshold,
 }
 
 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)
             .unwrap_or_else(Self::default_unit);
 
@@ -155,7 +154,7 @@ impl TestTimeOptions {
         let doctest_threshold = TimeThreshold::from_env_var(time_constants::DOCTEST_ENV_NAME)
             .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 {
diff --git a/src/doc/rustc/src/tests/index.md b/src/doc/rustc/src/tests/index.md
index 23a9f31e8e7..0e0eb85db74 100644
--- a/src/doc/rustc/src/tests/index.md
+++ b/src/doc/rustc/src/tests/index.md
@@ -267,7 +267,7 @@ Controls the format of the output. Valid options:
 
 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
 unstable-options` flag. See [tracking issue
diff --git a/src/doc/unstable-book/src/compiler-flags/report-time.md b/src/doc/unstable-book/src/compiler-flags/report-time.md
index ac0093f77ae..9e6a1fb0005 100644
--- a/src/doc/unstable-book/src/compiler-flags/report-time.md
+++ b/src/doc/unstable-book/src/compiler-flags/report-time.md
@@ -21,11 +21,8 @@ Sample usage command:
 Available options:
 
 ```sh
---report-time [plain|colored]
-                Show execution time of each test. Available values:
-                plain = do not colorize the execution time (default);
-                colored = colorize output according to the `color`
-                parameter value;
+--report-time
+                Show execution time of each test.
                 Threshold values for colorized output can be
                 configured via
                 `RUST_TEST_TIME_UNIT`, `RUST_TEST_TIME_INTEGRATION`