From 878889133a20ebcacf4225de192e51cf68d56520 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 23 Nov 2018 15:37:45 -0800 Subject: [PATCH] running just fast/slow tests in runner, and making the helper script invoke the new runner --- exec_tests.sh | 17 +++++++++++++++ test_all.sh | 7 ------- tests/Cargo.toml | 1 + tests/src/main.rs | 13 +++++++++++- tests/src/map_conversion.rs | 2 +- tests/src/physics.rs | 6 +++--- tests/src/runner.rs | 41 +++++++++++++++++++++++++++++++++++-- 7 files changed, 73 insertions(+), 14 deletions(-) create mode 100755 exec_tests.sh delete mode 100755 test_all.sh diff --git a/exec_tests.sh b/exec_tests.sh new file mode 100755 index 0000000000..739b98eed4 --- /dev/null +++ b/exec_tests.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +release_mode="" +filter="" + +for arg in "$@"; do + if [ "$arg" == "--release" ]; then + release_mode="--release"; + elif [ "$arg" == "--fast" ]; then + filter="--filter=Fast"; + elif [ "$arg" == "--slow" ]; then + filter="--filter=Slow"; + fi +done + +cd tests; +RUST_BACKTRACE=1 cargo run $release_mode -- $filter diff --git a/test_all.sh b/test_all.sh deleted file mode 100755 index fb379b761f..0000000000 --- a/test_all.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -if [ "$1" = "--fast" ]; then - cargo test --no-fail-fast -else - cargo test --release --no-fail-fast -fi diff --git a/tests/Cargo.toml b/tests/Cargo.toml index 49a6500b5c..65bcf85464 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -10,4 +10,5 @@ dimensioned = { git = "https://github.com/paholg/dimensioned", rev = "0e1076ebfa gag = "0.1.10" geom = { path = "../geom" } sim = { path = "../sim" } +structopt = "0.2" yansi = "0.4.0" diff --git a/tests/src/main.rs b/tests/src/main.rs index 9673b7499c..cc29666cc1 100644 --- a/tests/src/main.rs +++ b/tests/src/main.rs @@ -4,14 +4,25 @@ extern crate dimensioned; extern crate gag; extern crate geom; extern crate sim; +extern crate structopt; extern crate yansi; mod map_conversion; mod physics; mod runner; +use structopt::StructOpt; + +#[derive(StructOpt)] +#[structopt(name = "tests")] +struct Flags { + /// Which tests to run? + #[structopt(long = "filter", default_value = "All")] + filter: runner::Filter, +} + fn main() { - let mut t = runner::TestRunner::new(); + let mut t = runner::TestRunner::new(Flags::from_args().filter); map_conversion::run(t.suite("map_conversion")); physics::run(t.suite("physics")); diff --git a/tests/src/map_conversion.rs b/tests/src/map_conversion.rs index 3b91a95972..b6d329208f 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( + t.run_slow( "convert_twice", Box::new(|_| { let flags = convert_osm::Flags { diff --git a/tests/src/physics.rs b/tests/src/physics.rs index d8886721c7..36c802760d 100644 --- a/tests/src/physics.rs +++ b/tests/src/physics.rs @@ -6,7 +6,7 @@ use sim::{CarID, Distance, Speed, VehicleType}; pub fn run(t: &mut TestRunner) { // TODO table driven test style? - t.run( + t.run_fast( "accel_to_stop_in_dist/easy", Box::new(|_| { let v = Vehicle { @@ -22,7 +22,7 @@ pub fn run(t: &mut TestRunner) { }), ); - t.run( + t.run_fast( "accel_to_stop_in_dist/hard", Box::new(|_| { let v = Vehicle { @@ -42,7 +42,7 @@ pub fn run(t: &mut TestRunner) { }), ); - t.run( + t.run_fast( "accel_to_stop_in_dist/bike", Box::new(|_| { let v = Vehicle { diff --git a/tests/src/runner.rs b/tests/src/runner.rs index b4db891881..90cb19ebb3 100644 --- a/tests/src/runner.rs +++ b/tests/src/runner.rs @@ -1,14 +1,17 @@ // https://github.com/rust-lang/rust/issues/50297 would hopefully obsolete this approach. use abstutil; +use abstutil::Error; use gag::Redirect; use std; use std::io::Write; +use std::str::FromStr; use yansi::Paint; pub struct TestRunner { current_suite: Option, results: Vec, + filter: Filter, } struct TestResult { @@ -19,10 +22,11 @@ struct TestResult { } impl TestRunner { - pub fn new() -> TestRunner { + pub fn new(filter: Filter) -> TestRunner { TestRunner { current_suite: None, results: Vec::new(), + filter, } } @@ -31,7 +35,15 @@ impl TestRunner { self } - pub fn run(&mut self, specific_test_name: &str, test: Box) { + pub fn run_fast(&mut self, specific_test_name: &str, test: Box) { + self.run(specific_test_name, true, test); + } + + pub fn run_slow(&mut self, specific_test_name: &str, test: Box) { + self.run(specific_test_name, false, test); + } + + fn run(&mut self, specific_test_name: &str, fast: bool, test: Box) { let test_name = format!( "{}/{}", self.current_suite @@ -40,6 +52,11 @@ impl TestRunner { specific_test_name ); + if (fast && self.filter == Filter::Slow) || (!fast && self.filter == Filter::Fast) { + println!("Skipping {}", test_name); + return; + } + print!("Running {}...", test_name); std::io::stdout().flush().unwrap(); @@ -110,3 +127,23 @@ impl TestRunner { } pub struct TestHelper {} + +#[derive(PartialEq)] +pub enum Filter { + All, + Slow, + Fast, +} + +impl FromStr for Filter { + type Err = Error; + + fn from_str(s: &str) -> Result { + match s { + "All" => Ok(Filter::All), + "Slow" => Ok(Filter::Slow), + "Fast" => Ok(Filter::Fast), + _ => Err(Error::new(format!("{} isn't a valid Filter", s))), + } + } +}