1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00

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
This commit is contained in:
Bartek Iwańczuk 2021-10-30 22:49:46 +02:00 committed by GitHub
parent 95b9e5f30f
commit e06515c5a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 4 deletions

View file

@ -1,3 +1,3 @@
[WILDCARD]
test result: ok. [WILDCARD] passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD]ms)
test result: ok. [WILDCARD] passed; 0 failed; 0 ignored; 0 measured; 0 filtered out ([WILDCARD])

View file

@ -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)");
}
}