about summary refs log tree commit diff
diff options
context:
space:
mode:
authorIan Jackson <ijackson@chiark.greenend.org.uk>2023-08-22 17:29:17 +0100
committerIan Jackson <ijackson@chiark.greenend.org.uk>2023-08-22 17:46:59 +0100
commit06567ad7fafcf0a2b380a8908d004a3ca8c88333 (patch)
treeeb852f7f17cfbc20a33c97198b3e296f8edb9b01
parent013d2d212351e0553c0b1b129ad811b6c8bdfddc (diff)
downloadrust-06567ad7fafcf0a2b380a8908d004a3ca8c88333.tar.gz
rust-06567ad7fafcf0a2b380a8908d004a3ca8c88333.zip
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.)
-rw-r--r--library/std/src/sys/unix/process/process_common/tests.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/library/std/src/sys/unix/process/process_common/tests.rs b/library/std/src/sys/unix/process/process_common/tests.rs
index 03631e4e33b..082c564700f 100644
--- a/library/std/src/sys/unix/process/process_common/tests.rs
+++ b/library/std/src/sys/unix/process/process_common/tests.rs
@@ -159,3 +159,36 @@ fn test_program_kind() {
         );
     }
 }
+
+// Test that Rust std handles wait status values (`ExitStatus`) the way that Unix does,
+// at least for the values which represent a Unix exit status (`ExitCode`).
+// Should work on every #[cfg(unix)] platform.  However:
+#[cfg(not(any(
+    // Fuchsia is not Unix and has totally broken std::os::unix.
+    // https://github.com/rust-lang/rust/issues/58590#issuecomment-836535609
+    target_os = "fuchsia",
+)))]
+#[test]
+fn unix_exit_statuses() {
+    use crate::num::NonZeroI32;
+    use crate::os::unix::process::ExitStatusExt;
+    use crate::process::*;
+
+    for exit_code in 0..=0xff {
+        // TODO impl From<ExitCode> for ExitStatus and then test that here too;
+        // the two ExitStatus values should be the same
+        let raw_wait_status = exit_code << 8;
+        let exit_status = ExitStatus::from_raw(raw_wait_status);
+
+        assert_eq!(exit_status.code(), Some(exit_code));
+
+        if let Ok(nz) = NonZeroI32::try_from(exit_code) {
+            assert!(!exit_status.success());
+            let es_error = exit_status.exit_ok().unwrap_err();
+            assert_eq!(es_error.code().unwrap(), i32::from(nz));
+        } else {
+            assert!(exit_status.success());
+            assert_eq!(exit_status.exit_ok(), Ok(()));
+        }
+    }
+}