diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-10-30 22:49:46 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-30 22:49:46 +0200 |
commit | e06515c5a904f92946bf20070a554094336f71ae (patch) | |
tree | ac775a96e84e69c8bd21a2c01dee57f5a4b4fd44 /cli/tools/test.rs | |
parent | 95b9e5f30f9b5e925b9102430eaee10713cb0aa6 (diff) |
feat(test): better formatting for test elapsed time (#12610)
This commit changes formatting of elapsed time in test
runner output.
Instead of "XXXms", reporter outputs one of:
- "XXXms" for <1000ms
- "XXs" for <60s
- "XXXmYYs" for >=60s
Diffstat (limited to 'cli/tools/test.rs')
-rw-r--r-- | cli/tools/test.rs | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/cli/tools/test.rs b/cli/tools/test.rs index bccd6d731..c7ddf209f 100644 --- a/cli/tools/test.rs +++ b/cli/tools/test.rs @@ -249,7 +249,7 @@ impl PrettyTestReporter { println!( "{} {}", status, - colors::gray(format!("({}ms)", elapsed)).to_string() + colors::gray(human_elapsed(elapsed.into())).to_string() ); if let Some(error_text) = result.error() { @@ -260,6 +260,22 @@ impl PrettyTestReporter { } } +/// A function that converts a milisecond elapsed time to a string that +/// represents a human readable version of that time. +fn human_elapsed(elapsed: u128) -> String { + if elapsed < 1_000 { + return format!("({}ms)", elapsed); + } + if elapsed < 1_000 * 60 { + return format!("({}s)", elapsed / 1000); + } + + let seconds = elapsed / 1_000; + let minutes = seconds / 60; + let seconds_reminder = seconds % 60; + format!("({}m{}s)", minutes, seconds_reminder) +} + impl TestReporter for PrettyTestReporter { fn report_plan(&mut self, plan: &TestPlan) { let inflection = if plan.total == 1 { "test" } else { "tests" }; @@ -323,7 +339,7 @@ impl TestReporter for PrettyTestReporter { println!( "{} {}", status, - colors::gray(format!("({}ms)", elapsed)).to_string() + colors::gray(human_elapsed(elapsed.into())).to_string() ); } @@ -389,7 +405,7 @@ impl TestReporter for PrettyTestReporter { summary.ignored, summary.measured, summary.filtered_out, - colors::gray(format!("({}ms)", elapsed.as_millis())), + colors::gray(human_elapsed(elapsed.as_millis())), ); } } @@ -1202,3 +1218,19 @@ pub async fn run_tests_with_watch( Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_human_elapsed() { + assert_eq!(human_elapsed(1), "(1ms)"); + assert_eq!(human_elapsed(256), "(256ms)"); + assert_eq!(human_elapsed(1000), "(1s)"); + assert_eq!(human_elapsed(1001), "(1s)"); + assert_eq!(human_elapsed(1020), "(1s)"); + assert_eq!(human_elapsed(70 * 1000), "(1m10s)"); + assert_eq!(human_elapsed(86 * 1000 + 100), "(1m26s)"); + } +} |