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");
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());
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());
}
#[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();
}
#[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();
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")
));
std::mem::forget(job);
}