artifactize-core 0.27.2

The library behind artifactize: Artifacts, evals, reviews, the monitor and review models, and the shared operations every interface calls.
Documentation
use std::time::Duration;
use windows_sys::Win32::Foundation::ERROR_CANCELLED;

use artifactize_foundation::platform::process::*;
use std::io;

#[tokio::test]
async fn an_unadmitted_gate_never_runs_the_child_and_kills_it() {
    let directory = crate::test_os::tempdir();
    let (gate, spawning) = spawn_gated(crate::test_os::windows_marker(directory.path())).unwrap();
    let gate = gate.identify().await.unwrap();
    let pid = gate.pid();
    assert!(process_start_time(pid).is_ok(), "the held child exists");
    // identify() acknowledges the suspended gate: the child cannot run until admitted.
    assert!(!directory.path().join("marker").exists());
    drop(gate);
    let error = tokio::time::timeout(Duration::from_secs(5), spawning)
        .await
        .unwrap()
        .unwrap()
        .err()
        .expect("the child must not run");
    assert_eq!(error.raw_os_error(), Some(ERROR_CANCELLED as i32));
    assert_eq!(
        process_start_time(pid).unwrap_err().kind(),
        io::ErrorKind::NotFound
    );
    assert!(!directory.path().join("marker").exists());

    // Admitted, the same child runs: the marker would have shown an early start.
    let (gate, spawning) = spawn_gated(crate::test_os::windows_marker(directory.path())).unwrap();
    gate.identify().await.unwrap().admit().unwrap();
    let mut child = spawning.await.unwrap().unwrap();
    assert!(child.wait().await.unwrap().success());
    assert!(directory.path().join("marker").exists());
}

/// The marker fixture ([`crate::test_os::windows_marker`]): creates `marker` in its working
/// directory once it runs.
#[test]
fn marker_probe() {
    if std::env::var_os("ARTIFACTIZE_MARKER_PROBE").is_none() {
        return;
    }
    std::fs::File::create("marker").unwrap();
}

#[test]
#[ignore = "child fixture started by the Windows process tests"]
fn stdin_waiter_probe() {
    std::io::copy(&mut std::io::stdin(), &mut std::io::sink()).unwrap();
}

/// A job that forbids breakaway refuses CREATE_BREAKAWAY_FROM_JOB; the handoff then starts
/// inside it. A process cannot leave a job, so the probe runs in a test process of its own.
#[test]
fn a_detached_launch_inside_a_job_without_breakaway_still_starts() {
    let output = crate::test_os::run_test(
        "platform::windows::process::tests::detached_launch_probe",
        &[("ARTIFACTIZE_DETACHED_PROBE", "1".into())],
    );
    assert!(output.status.success(), "{output:?}");
    assert!(
        String::from_utf8_lossy(&output.stdout).contains("1 passed"),
        "{output:?}"
    );
}

#[tokio::test]
async fn detached_launch_probe() {
    if std::env::var_os("ARTIFACTIZE_DETACHED_PROBE").is_none() {
        return;
    }
    let job = crate::test_os::windows_job_without_breakaway();
    let command = crate::test_os::stdin_waiter();
    let mut child = spawn_detached(command).unwrap();
    assert!(
        crate::test_os::process_in_job(&child, &job),
        "the fallback starts the handoff inside the job"
    );
    child.kill().await.unwrap();

    // A Run worker uses the same restricted Job, but with owner-only cwd and stderr
    // beneath a parent whose Everyone ACE would otherwise be inherited.
    let parent = crate::test_os::tempdir();
    crate::test_os::share_dir(parent.path());
    let directory = crate::platform::private_tempdir_in("run-", parent.path()).unwrap();
    let stderr_path = directory.path().join("worker.stderr");
    let stderr = std::fs::File::create(&stderr_path).unwrap();
    let mut command = crate::test_os::windows_marker(directory.path());
    command.stderr(std::process::Stdio::from(stderr));
    let mut worker = spawn_run(command).unwrap();
    assert!(worker.wait().await.unwrap().success());
    assert!(directory.path().join("marker").exists());
    assert!(crate::test_os::private_dir(directory.path()));
    assert!(!crate::test_os::has_inherited_aces(directory.path()));
    assert!(crate::test_os::private_file(&stderr_path));
    assert!(crate::test_os::private_file(
        &directory.path().join("marker")
    ));

    // Closing the job's last handle would end this process too; it closes at exit.
    std::mem::forget(job);
}