Rollup merge of #68909 - tmandry:panic-abort-nocapture, r=alexcrichton

Respect --nocapture in panic=abort test mode

r? @alexcrichton
This commit is contained in:
Dylan DPC 2020-02-07 17:00:21 +01:00 committed by GitHub
commit f734e47516
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 88 additions and 9 deletions

View File

@ -63,8 +63,7 @@ use std::{
env, io, env, io,
io::prelude::Write, io::prelude::Write,
panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo}, panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo},
process, process::{self, Command, Termination},
process::{Command, Termination},
sync::mpsc::{channel, Sender}, sync::mpsc::{channel, Sender},
sync::{Arc, Mutex}, sync::{Arc, Mutex},
thread, thread,
@ -457,9 +456,13 @@ pub fn run_test(
monitor_ch, monitor_ch,
opts.time, opts.time,
), ),
RunStrategy::SpawnPrimary => { RunStrategy::SpawnPrimary => spawn_test_subprocess(
spawn_test_subprocess(desc, opts.time.is_some(), monitor_ch, opts.time) desc,
} opts.nocapture,
opts.time.is_some(),
monitor_ch,
opts.time,
),
}; };
// If the platform is single-threaded we're just going to run // If the platform is single-threaded we're just going to run
@ -558,6 +561,7 @@ fn run_test_in_process(
fn spawn_test_subprocess( fn spawn_test_subprocess(
desc: TestDesc, desc: TestDesc,
nocapture: bool,
report_time: bool, report_time: bool,
monitor_ch: Sender<CompletedTest>, monitor_ch: Sender<CompletedTest>,
time_opts: Option<time::TestTimeOptions>, time_opts: Option<time::TestTimeOptions>,
@ -566,11 +570,15 @@ fn spawn_test_subprocess(
let args = env::args().collect::<Vec<_>>(); let args = env::args().collect::<Vec<_>>();
let current_exe = &args[0]; let current_exe = &args[0];
let mut command = Command::new(current_exe);
command.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice());
if nocapture {
command.stdout(process::Stdio::inherit());
command.stderr(process::Stdio::inherit());
}
let start = report_time.then(Instant::now); let start = report_time.then(Instant::now);
let output = match Command::new(current_exe) let output = match command.output() {
.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice())
.output()
{
Ok(out) => out, Ok(out) => out,
Err(e) => { Err(e) => {
let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e); let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e);

View File

@ -0,0 +1,39 @@
// no-prefer-dynamic
// compile-flags: --test -Cpanic=abort -Zpanic_abort_tests
// run-flags: --test-threads=1 --nocapture
// run-fail
// check-run-results
// exec-env:RUST_BACKTRACE=0
// ignore-wasm no panic or subprocess support
// ignore-emscripten no panic or subprocess support
#![cfg(test)]
use std::io::Write;
#[test]
fn it_works() {
println!("about to succeed");
assert_eq!(1 + 1, 2);
}
#[test]
#[should_panic]
fn it_panics() {
println!("about to panic");
assert_eq!(1 + 1, 4);
}
#[test]
fn it_fails() {
println!("about to fail");
assert_eq!(1 + 1, 4);
}
#[test]
fn it_writes_to_stdio() {
println!("hello, world");
writeln!(std::io::stdout(), "testing123").unwrap();
writeln!(std::io::stderr(), "testing321").unwrap();
}

View File

@ -0,0 +1,9 @@
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `2`,
right: `4`', $DIR/test-panic-abort-nocapture.rs:31:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `2`,
right: `4`', $DIR/test-panic-abort-nocapture.rs:25:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
testing321

View File

@ -0,0 +1,23 @@
running 4 tests
test it_fails ... about to fail
FAILED
test it_panics ... about to panic
ok
test it_works ... about to succeed
ok
test it_writes_to_stdio ... hello, world
testing123
ok
failures:
---- it_fails stdout ----
---- it_fails stderr ----
failures:
it_fails
test result: FAILED. 3 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out