summaryrefslogtreecommitdiffstats
path: root/library/test/src/helpers/exit_code.rs
blob: f762f88819da530c8206c92736bb0bc7304c7bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//! Helper module to detect subprocess exit code.

use std::process::ExitStatus;

#[cfg(not(unix))]
pub fn get_exit_code(status: ExitStatus) -> Result<i32, String> {
    status.code().ok_or_else(|| "received no exit code from child process".into())
}

#[cfg(unix)]
pub fn get_exit_code(status: ExitStatus) -> Result<i32, String> {
    use std::os::unix::process::ExitStatusExt;
    match status.code() {
        Some(code) => Ok(code),
        None => match status.signal() {
            Some(signal) => Err(format!("child process exited with signal {signal}")),
            None => Err("child process exited with unknown signal".into()),
        },
    }
}