cosca 0.7.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
//! Tests for the pure parts of `exit_only`.

#[cfg(unix)]
mod unix {
    use std::os::unix::process::ExitStatusExt;
    use std::process::ExitStatus;

    use crate::os_process::child::ExitRecord;
    use crate::wait::exit_only::{exit_record_of, is_exit_record, Record};

    fn status(code: i32, si_status: i32) -> ExitRecord {
        exit_record_of(Record {
            si_code: code,
            si_status,
        })
    }

    /// `CLD_EXITED` gives the low byte as the code, dropping every other bit of `si_status`
    /// (XNU ORs `p_xhighbits` into its high byte, `kern_exit.c:3255`).
    ///
    /// Mutant: a mask-free mapping keeps the high bits in the raw status.
    #[skuld::test]
    fn an_exited_record_gives_the_low_byte_as_the_code() {
        assert_eq!(
            status(libc::CLD_EXITED, 0x0A00_0107),
            ExitRecord::Status(ExitStatus::from_raw(7 << 8))
        );
    }

    /// `CLD_KILLED` and `CLD_DUMPED` give the signal, and a core dump sets the core bit.
    ///
    /// Mutant: a mask-free mapping; `CLD_DUMPED` mapped like `CLD_KILLED`.
    #[skuld::test]
    fn killed_and_dumped_records_give_the_signal() {
        assert_eq!(
            status(libc::CLD_KILLED, 0x0A00_0009),
            ExitRecord::Status(ExitStatus::from_raw(9))
        );
        assert_eq!(
            status(libc::CLD_DUMPED, 0x0A00_000B),
            ExitRecord::Status(ExitStatus::from_raw(0x0B | 0x80))
        );
    }

    /// A ptrace stop is not an exit record, and the mapping is total: it becomes `Unreadable`,
    /// never a panic.
    ///
    /// Mutant: a trap read as an exit.
    #[skuld::test]
    fn a_trapped_record_is_not_an_exit_record() {
        for code in [libc::CLD_TRAPPED, libc::CLD_STOPPED, libc::CLD_CONTINUED, 0, 99] {
            assert!(!is_exit_record(code), "si_code {code}");
            assert_eq!(status(code, 5), ExitRecord::Unreadable { si_code: code });
        }
        for code in [libc::CLD_EXITED, libc::CLD_KILLED, libc::CLD_DUMPED] {
            assert!(is_exit_record(code), "si_code {code}");
        }
    }
}

mod step_hooks {
    use std::cell::Cell;
    use std::rc::Rc;

    use crate::wait::exit_only::seams::{self, HolderStep};

    /// A dropped guard takes its unfired hook with it: the next `step` runs nothing.
    ///
    /// Mutant: the guard's `Drop` does nothing.
    #[skuld::test]
    fn a_dropped_step_hook_guard_removes_its_hook() {
        let fired = Rc::new(Cell::new(false));
        let guard = seams::on_holder_step(HolderStep::FinalPeek, {
            let fired = Rc::clone(&fired);
            move || fired.set(true)
        });
        drop(guard);
        seams::step(HolderStep::FinalPeek);
        assert!(!fired.get(), "the hook outlived its guard");
        seams::holder_steps();
    }

    /// A guard removes only its own hook: dropping the guard of one that already fired leaves a
    /// later hook for the same step armed.
    ///
    /// Mutant: the guard removes every hook of its step.
    #[skuld::test]
    fn a_step_hook_guard_removes_only_its_own_hook() {
        let first = seams::on_holder_step(HolderStep::FinalPeek, || ());
        seams::step(HolderStep::FinalPeek);
        let fired = Rc::new(Cell::new(false));
        let _second = seams::on_holder_step(HolderStep::FinalPeek, {
            let fired = Rc::clone(&fired);
            move || fired.set(true)
        });
        drop(first);
        seams::step(HolderStep::FinalPeek);
        assert!(fired.get(), "an earlier guard removed a later hook");
        seams::holder_steps();
    }
}

#[cfg(windows)]
mod windows {
    use std::os::windows::io::AsHandle as _;

    use crate::wait::exit_only::{peek, Target};

    /// A wait on our own process handle that fails is a contract breach, not an answer: debug
    /// builds assert, release builds return the OS error.
    ///
    /// Mutant: the `debug_assert!` removed from the failed arm of `peek`.
    #[skuld::test]
    fn a_failed_wait_on_our_own_handle_is_a_contract_breach() {
        let unwaitable = crate::test_child::unwaitable::unwaitable_handle();
        let outcome = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
            peek(&Target::Handle(unwaitable.as_handle()))
        }));
        if cfg!(debug_assertions) {
            assert!(outcome.is_err(), "a debug build asserts the contract");
        } else {
            outcome.expect("release returns").expect_err("a breach is an error");
        }
    }
}