#![cfg(unix)]
use std::process::Command;
const OUT_ENV: &str = "SU_RESTART_OUT";
const MARKER: &str = "su_restart_grandchild_marker";
const FORWARDED: [&str; 3] = ["restart_child_entry", MARKER, "--nocapture"];
#[test]
fn restart_child_entry() {
let Ok(out_path) = std::env::var(OUT_ENV) else {
return;
};
let is_post_exec = std::env::args().any(|a| a == MARKER);
if is_post_exec {
let args: Vec<String> = std::env::args().skip(1).collect();
std::fs::write(&out_path, args.join("\n")).expect("write forwarded args");
} else {
let err = self_update::restart::restart_with(FORWARDED)
.expect_err("restart_with returns only on failure");
panic!("exec failed instead of replacing the process: {err}");
}
}
#[test]
fn restart_with_reexecs_current_exe_with_given_args() {
let dir = tempfile::TempDir::new().unwrap();
let out_path = dir.path().join("forwarded-args.txt");
let status = Command::new(std::env::current_exe().unwrap())
.args(["restart_child_entry", "--exact", "--nocapture"])
.env(OUT_ENV, &out_path)
.status()
.expect("spawn test binary");
assert!(
status.success(),
"spawned restart generation exited unsuccessfully: {status:?}"
);
let recorded = std::fs::read_to_string(&out_path)
.expect("post-exec generation should have written the forwarded args");
let got: Vec<&str> = recorded.lines().collect();
assert_eq!(
got, FORWARDED,
"restart_with must re-exec current_exe with the given args, verbatim"
);
}