Skip to content

Commit

Permalink
Create a hacky fail-fast mode that stops tests at the first failure
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Dec 1, 2022
1 parent 744a97b commit e590258
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
5 changes: 5 additions & 0 deletions library/test/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pub struct TestOpts {
pub test_threads: Option<usize>,
pub skip: Vec<String>,
pub time_options: Option<TestTimeOptions>,
/// Stop at first failing test.
/// May run a few more tests due to threading, but will
/// abort as soon as possible.
pub fail_fast: bool,
pub options: Options,
}

Expand Down Expand Up @@ -296,6 +300,7 @@ fn parse_opts_impl(matches: getopts::Matches) -> OptRes {
skip,
time_options,
options,
fail_fast: false,
};

Ok(test_opts)
Expand Down
2 changes: 1 addition & 1 deletion library/test/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ pub fn run_tests_console(opts: &TestOpts, tests: Vec<TestDescAndFn>) -> io::Resu
run_tests(opts, tests, |x| on_test_event(&x, &mut st, &mut *out))?;
st.exec_time = start_time.map(|t| TestSuiteExecTime(t.elapsed()));

assert!(st.current_test_count() == st.total);
assert!(opts.fail_fast || st.current_test_count() == st.total);

out.write_run_finish(&st)
}
Expand Down
20 changes: 20 additions & 0 deletions library/test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,17 @@ where
let mut completed_test = rx.recv().unwrap();
RunningTest { join_handle }.join(&mut completed_test);

let fail_fast = match completed_test.result {
TrIgnored | TrOk | TrBench(_) => false,
TrFailed | TrFailedMsg(_) | TrTimedFail => opts.fail_fast,
};

let event = TestEvent::TeResult(completed_test);
notify_about_test_event(event)?;

if fail_fast {
return Ok(());
}
}
} else {
while pending > 0 || !remaining.is_empty() {
Expand Down Expand Up @@ -431,9 +440,20 @@ where
let running_test = running_tests.remove(&completed_test.id).unwrap();
running_test.join(&mut completed_test);

let fail_fast = match completed_test.result {
TrIgnored | TrOk | TrBench(_) => false,
TrFailed | TrFailedMsg(_) | TrTimedFail => opts.fail_fast,
};

let event = TestEvent::TeResult(completed_test);
notify_about_test_event(event)?;
pending -= 1;

if fail_fast {
// Prevent remaining test threads from panicking
std::mem::forget(rx);
return Ok(());
}
}
}

Expand Down
1 change: 1 addition & 0 deletions library/test/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ impl TestOpts {
skip: vec![],
time_options: None,
options: Options::new(),
fail_fast: false,
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/compiletest/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ pub fn test_opts(config: &Config) -> test::TestOpts {
options: test::Options::new(),
time_options: None,
force_run_in_process: false,
fail_fast: std::env::var_os("RUSTC_TEST_FAIL_FAST").is_some(),
}
}

Expand Down

0 comments on commit e590258

Please sign in to comment.