Skip to content

Commit 06567ad

Browse files
committed
std: unix process: Test exit statuses / wait statuses
This is a pretty basic test but should spot any other platforms which are `#[cfg(unix)]` but not Unix and where the wait status representation is wrong. (And any actual Unix platforms where it's not as expected, but I don't think they exist.)
1 parent 013d2d2 commit 06567ad

File tree

1 file changed

+33
-0
lines changed
  • library/std/src/sys/unix/process/process_common

1 file changed

+33
-0
lines changed

library/std/src/sys/unix/process/process_common/tests.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,36 @@ fn test_program_kind() {
159159
);
160160
}
161161
}
162+
163+
// Test that Rust std handles wait status values (`ExitStatus`) the way that Unix does,
164+
// at least for the values which represent a Unix exit status (`ExitCode`).
165+
// Should work on every #[cfg(unix)] platform. However:
166+
#[cfg(not(any(
167+
// Fuchsia is not Unix and has totally broken std::os::unix.
168+
// https://github.com/rust-lang/rust/issues/58590#issuecomment-836535609
169+
target_os = "fuchsia",
170+
)))]
171+
#[test]
172+
fn unix_exit_statuses() {
173+
use crate::num::NonZeroI32;
174+
use crate::os::unix::process::ExitStatusExt;
175+
use crate::process::*;
176+
177+
for exit_code in 0..=0xff {
178+
// TODO impl From<ExitCode> for ExitStatus and then test that here too;
179+
// the two ExitStatus values should be the same
180+
let raw_wait_status = exit_code << 8;
181+
let exit_status = ExitStatus::from_raw(raw_wait_status);
182+
183+
assert_eq!(exit_status.code(), Some(exit_code));
184+
185+
if let Ok(nz) = NonZeroI32::try_from(exit_code) {
186+
assert!(!exit_status.success());
187+
let es_error = exit_status.exit_ok().unwrap_err();
188+
assert_eq!(es_error.code().unwrap(), i32::from(nz));
189+
} else {
190+
assert!(exit_status.success());
191+
assert_eq!(exit_status.exit_ok(), Ok(()));
192+
}
193+
}
194+
}

0 commit comments

Comments
 (0)