From 5de255b55bdfaada0337550ecf11c2a470cf839a Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 23 Nov 2018 15:10:26 -0800 Subject: [PATCH] printing test progress live, timing things --- tests/src/map_conversion.rs | 2 +- tests/src/runner.rs | 52 ++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/tests/src/map_conversion.rs b/tests/src/map_conversion.rs index bf53c8597c..1b6965be75 100644 --- a/tests/src/map_conversion.rs +++ b/tests/src/map_conversion.rs @@ -3,7 +3,7 @@ use convert_osm; use runner::TestRunner; pub fn run(t: &mut TestRunner) { - t.run("convert twice", Box::new(|_| { + t.run("convert_twice", Box::new(|_| { let flags = convert_osm::Flags { osm: "../data/input/montlake.osm".to_string(), elevation: "../data/input/N47W122.hgt".to_string(), diff --git a/tests/src/runner.rs b/tests/src/runner.rs index 14de195244..78d18619ea 100644 --- a/tests/src/runner.rs +++ b/tests/src/runner.rs @@ -1,8 +1,10 @@ // https://github.com/rust-lang/rust/issues/50297 would hopefully obsolete this approach. +use abstutil; use gag::Redirect; use std; use yansi::Paint; +use std::io::Write; pub struct TestRunner { results: Vec, @@ -11,6 +13,7 @@ pub struct TestRunner { struct TestResult { test_name: String, pass: bool, + duration: String, output_path: String, } @@ -22,35 +25,42 @@ impl TestRunner { } pub fn run(&mut self, test_name: &str, test: Box) { - println!("Running {}...", test_name); + print!("Running {}...", test_name); + std::io::stdout().flush().unwrap(); // TODO Make a temporary directory inside /tmp, remove successful files + let start = std::time::Instant::now(); + let mut helper = TestHelper {}; let output_path = format!("/tmp/{}.log", test_name); std::fs::create_dir_all(std::path::Path::new(&output_path).parent().unwrap()) .expect("Creating parent dir failed"); - let _stdout_redirect = Redirect::stdout( - std::fs::OpenOptions::new() - .create(true) - .append(true) - .open(output_path.clone()) - .unwrap(), - ).unwrap(); - let _stderr_redirect = Redirect::stderr( - std::fs::OpenOptions::new() - .create(true) - .append(true) - .open(output_path.clone()) - .unwrap(), - ).unwrap(); - let mut helper = TestHelper {}; + let pass = { + let _stdout_redirect = Redirect::stdout( + std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(output_path.clone()) + .unwrap(), + ).unwrap(); + let _stderr_redirect = Redirect::stderr( + std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(output_path.clone()) + .unwrap(), + ).unwrap(); - let pass = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { - test(&mut helper); - })).is_ok(); + std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + test(&mut helper); + })).is_ok() + }; + let duration = format!("{:.02}s", abstutil::elapsed_seconds(start)); + print!("\rRunning {}... {}\n", test_name, duration); self.results.push(TestResult { test_name: test_name.to_string(), pass, + duration, output_path, }); } @@ -62,10 +72,10 @@ impl TestRunner { for result in self.results.into_iter() { if result.pass { passed += 1; - println!("- {}: {}", result.test_name, Paint::green("PASS")); + println!("- {} ({}): {}", result.test_name, result.duration, Paint::green("PASS")); } else { failed += 1; - println!("- {}: {}", result.test_name, Paint::red("FAIL")); + println!("- {} ({}): {}", result.test_name, result.duration, Paint::red("FAIL")); println!(" {}", Paint::cyan(result.output_path)); } }