summaryrefslogtreecommitdiff
path: root/cli/tools/test.rs
diff options
context:
space:
mode:
Diffstat (limited to 'cli/tools/test.rs')
-rw-r--r--cli/tools/test.rs38
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)");
+ }
+}