use std::path::Path;
use std::process::Command;
use std::time::{Duration, Instant};
#[cfg(unix)] pub fn write_fake(path: &Path, script: &str) {
use std::os::unix::fs::PermissionsExt;
let (shebang, rest) = script
.split_once('\n')
.expect("a fake script starts with a shebang line");
std::fs::write(
path,
format!("{shebang}\n[ \"$FAKE_PRIME\" = 1 ] && exit 0\n{rest}"),
)
.unwrap();
std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).unwrap();
let give_up = Instant::now() + Duration::from_secs(10);
loop {
match Command::new(path).env("FAKE_PRIME", "1").output() {
Err(e) if e.raw_os_error() == Some(26) && Instant::now() < give_up => {
std::thread::sleep(Duration::from_millis(20));
}
other => {
other.expect("the fake runs");
break;
}
}
}
}