Skip to main content

cortexkit_test_support/
lib.rs

1//! Portable helpers for CortexKit tests. Use this crate as a **dev-dependency only**.
2//!
3//! Scratch fixtures preserve failure evidence, executables are published as
4//! immutable `ckdev-*` copies, and daemon builders enforce an ambient-root fence.
5#![forbid(unsafe_code)]
6
7mod binaries;
8mod daemon;
9mod fence;
10mod scratch;
11mod sibling_cache;
12mod spawn_guard;
13mod stable;
14
15pub use binaries::*;
16pub use daemon::{TestDaemon, TestDaemonCommand};
17pub use fence::*;
18pub use scratch::{
19    process_alive, process_liveness, scratch_root, shared_scratch_root, wait_until_gone, Liveness,
20    ScratchDir, STABLE_BIN_DIR,
21};
22pub use sibling_cache::*;
23pub use spawn_guard::assert_test_binary_spawns;
24#[cfg(unix)]
25pub use stable::{stable_executable, stable_executable_dir};
26
27/// Opt-in variable for tests requiring passwordless sudo, namespaces, or root.
28pub const PRIVILEGED_TESTS_ENV: &str = "CORTEXKIT_PRIVILEGED_TESTS";
29
30/// Returns true only for `CORTEXKIT_PRIVILEGED_TESTS=1`; otherwise prints a skip reason.
31pub fn privileged_tests_enabled(test: &str, needs: &str) -> bool {
32    if std::env::var(PRIVILEGED_TESTS_ENV).as_deref() == Ok("1") {
33        return true;
34    }
35    eprintln!("skipped: {test} needs {needs}; set {PRIVILEGED_TESTS_ENV}=1 to run it");
36    false
37}
38
39#[cfg(test)]
40#[test]
41fn privileged_gate_only_accepts_exact_one() {
42    let previous = std::env::var_os(PRIVILEGED_TESTS_ENV);
43    for value in [None, Some("0"), Some("true"), Some("1")] {
44        match value {
45            Some(value) => std::env::set_var(PRIVILEGED_TESTS_ENV, value),
46            None => std::env::remove_var(PRIVILEGED_TESTS_ENV),
47        }
48        assert_eq!(
49            privileged_tests_enabled("probe", "privilege"),
50            value == Some("1")
51        );
52    }
53    match previous {
54        Some(value) => std::env::set_var(PRIVILEGED_TESTS_ENV, value),
55        None => std::env::remove_var(PRIVILEGED_TESTS_ENV),
56    }
57}