Skip to content

Commit 861b328

Browse files
committed
Respect --nocapture in panic=abort test mode
1 parent 442ae7f commit 861b328

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

src/libtest/lib.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ use std::{
6363
env, io,
6464
io::prelude::Write,
6565
panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo},
66-
process,
67-
process::{Command, Termination},
66+
process::{self, Command, Termination},
6867
sync::mpsc::{channel, Sender},
6968
sync::{Arc, Mutex},
7069
thread,
@@ -457,9 +456,13 @@ pub fn run_test(
457456
monitor_ch,
458457
opts.time,
459458
),
460-
RunStrategy::SpawnPrimary => {
461-
spawn_test_subprocess(desc, opts.time.is_some(), monitor_ch, opts.time)
462-
}
459+
RunStrategy::SpawnPrimary => spawn_test_subprocess(
460+
desc,
461+
opts.nocapture,
462+
opts.time.is_some(),
463+
monitor_ch,
464+
opts.time,
465+
),
463466
};
464467

465468
// If the platform is single-threaded we're just going to run
@@ -558,6 +561,7 @@ fn run_test_in_process(
558561

559562
fn spawn_test_subprocess(
560563
desc: TestDesc,
564+
nocapture: bool,
561565
report_time: bool,
562566
monitor_ch: Sender<CompletedTest>,
563567
time_opts: Option<time::TestTimeOptions>,
@@ -566,11 +570,15 @@ fn spawn_test_subprocess(
566570
let args = env::args().collect::<Vec<_>>();
567571
let current_exe = &args[0];
568572

573+
let mut command = Command::new(current_exe);
574+
command.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice());
575+
if nocapture {
576+
command.stdout(process::Stdio::inherit());
577+
command.stderr(process::Stdio::inherit());
578+
}
579+
569580
let start = report_time.then(Instant::now);
570-
let output = match Command::new(current_exe)
571-
.env(SECONDARY_TEST_INVOKER_VAR, desc.name.as_slice())
572-
.output()
573-
{
581+
let output = match command.output() {
574582
Ok(out) => out,
575583
Err(e) => {
576584
let err = format!("Failed to spawn {} as child for test: {:?}", args[0], e);
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// no-prefer-dynamic
2+
// compile-flags: --test -Cpanic=abort -Zpanic_abort_tests
3+
// run-flags: --test-threads=1 --nocapture
4+
// run-fail
5+
// check-run-results
6+
// exec-env:RUST_BACKTRACE=0
7+
8+
// ignore-wasm no panic or subprocess support
9+
// ignore-emscripten no panic or subprocess support
10+
11+
#![cfg(test)]
12+
13+
use std::io::Write;
14+
15+
#[test]
16+
fn it_works() {
17+
println!("about to succeed");
18+
assert_eq!(1 + 1, 2);
19+
}
20+
21+
#[test]
22+
#[should_panic]
23+
fn it_panics() {
24+
println!("about to panic");
25+
assert_eq!(1 + 1, 4);
26+
}
27+
28+
#[test]
29+
fn it_fails() {
30+
println!("about to fail");
31+
assert_eq!(1 + 1, 4);
32+
}
33+
34+
#[test]
35+
fn it_writes_to_stdio() {
36+
println!("hello, world");
37+
writeln!(std::io::stdout(), "testing123").unwrap();
38+
writeln!(std::io::stderr(), "testing321").unwrap();
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
thread 'main' panicked at 'assertion failed: `(left == right)`
2+
left: `2`,
3+
right: `4`', $DIR/test-panic-abort-nocapture.rs:31:5
4+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
5+
thread 'main' panicked at 'assertion failed: `(left == right)`
6+
left: `2`,
7+
right: `4`', $DIR/test-panic-abort-nocapture.rs:25:5
8+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
9+
testing321
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
running 4 tests
3+
test it_fails ... about to fail
4+
FAILED
5+
test it_panics ... about to panic
6+
ok
7+
test it_works ... about to succeed
8+
ok
9+
test it_writes_to_stdio ... hello, world
10+
testing123
11+
ok
12+
13+
failures:
14+
15+
---- it_fails stdout ----
16+
---- it_fails stderr ----
17+
18+
19+
failures:
20+
it_fails
21+
22+
test result: FAILED. 3 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
23+

0 commit comments

Comments
 (0)