printing test progress live, timing things

This commit is contained in:
Dustin Carlino 2018-11-23 15:10:26 -08:00
parent e8dc01fd98
commit 5de255b55b
2 changed files with 32 additions and 22 deletions

View File

@ -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(),

View File

@ -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<TestResult>,
@ -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<Fn(&mut TestHelper)>) {
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));
}
}