Skip to content

Commit e6015e4

Browse files
committed
Include signal from terminated child process on unix
1 parent df24055 commit e6015e4

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/libtest/formatters/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ pub(crate) trait OutputFormatter {
2121
) -> io::Result<()>;
2222
fn write_run_finish(&mut self, state: &ConsoleTestState) -> io::Result<bool>;
2323
}
24+
25+
pub(crate) fn write_stderr_delimiter(test_output: &mut Vec<u8>, test_name: &TestName) {
26+
match test_output.last() {
27+
Some(b'\n') => (),
28+
Some(_) => test_output.push(b'\n'),
29+
None => (),
30+
}
31+
write!(test_output, "---- {} stderr ----\n", test_name).unwrap();
32+
}

src/libtest/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ use std::io::prelude::*;
5656
use std::panic::{self, catch_unwind, AssertUnwindSafe, PanicInfo};
5757
use std::path::PathBuf;
5858
use std::process;
59-
use std::process::{Command, Termination};
59+
use std::process::{ExitStatus, Command, Termination};
6060
use std::sync::mpsc::{channel, Sender};
6161
use std::sync::{Arc, Mutex};
6262
use std::thread;
@@ -1613,10 +1613,11 @@ fn spawn_test_subprocess(desc: TestDesc, monitor_ch: Sender<MonitorMsg>) {
16131613

16141614
let std::process::Output { stdout, stderr, status } = output;
16151615
let mut test_output = stdout;
1616+
formatters::write_stderr_delimiter(&mut test_output, &desc.name);
16161617
test_output.extend_from_slice(&stderr);
16171618

16181619
let result = match (move || {
1619-
let exit_code = status.code().ok_or("child process was terminated")?;
1620+
let exit_code = get_exit_code(status)?;
16201621
TestResult::try_from(exit_code).map_err(|_| {
16211622
format!("child process returned unexpected exit code {}", exit_code)
16221623
})
@@ -1664,6 +1665,23 @@ fn run_test_in_spawned_subprocess(desc: TestDesc, testfn: Box<dyn FnOnce() + Sen
16641665
unreachable!("panic=abort callback should have exited the process")
16651666
}
16661667

1668+
#[cfg(not(unix))]
1669+
fn get_exit_code(status: ExitStatus) -> Result<i32, String> {
1670+
status.code().ok_or("received no exit code from child process".into())
1671+
}
1672+
1673+
#[cfg(unix)]
1674+
fn get_exit_code(status: ExitStatus) -> Result<i32, String> {
1675+
use std::os::unix::process::ExitStatusExt;
1676+
match status.code() {
1677+
Some(code) => Ok(code),
1678+
None => match status.signal() {
1679+
Some(signal) => Err(format!("child process exited with signal {}", signal)),
1680+
None => Err("child process exited with unknown signal".into()),
1681+
}
1682+
}
1683+
}
1684+
16671685
#[derive(Clone, PartialEq)]
16681686
pub struct MetricMap(BTreeMap<String, Metric>);
16691687

0 commit comments

Comments
 (0)