skuld::default_labels!(crate::test_harness::ROOT);
use crate::command::Command;
use crate::error::Error;
use crate::test_child::Console;
use crate::test_child::{cwd_and_path_tools, CWD_TOOL_EXIT, PATH_TOOL_EXIT};
fn exit_code(c: &mut Command) -> Option<i32> {
c.spawn().expect("spawn").wait().expect("wait").code()
}
#[skuld::test]
fn a_bare_exact_name_loads_the_file_in_the_childs_cwd_not_one_on_path() {
let (cwd, on_path) = cwd_and_path_tools();
let mut c = crate::test_child::command(Console::Hidden);
c.raw_executable("tool")
.args(["tool"])
.current_dir(cwd.path())
.env("PATH", on_path.path());
assert_eq!(exit_code(&mut c), Some(CWD_TOOL_EXIT));
}
#[skuld::test]
fn a_bare_exact_name_missing_from_the_childs_cwd_is_not_found_on_path() {
let (_cwd, on_path) = cwd_and_path_tools();
let empty = tempfile::tempdir().expect("tempdir");
let mut c = crate::test_child::command(Console::Hidden);
c.raw_executable("tool")
.args(["tool"])
.current_dir(empty.path())
.env("PATH", on_path.path());
match c.spawn() {
Err(Error::Io(e)) if e.kind() == std::io::ErrorKind::NotFound => {}
other => panic!("expected Io(NotFound), got {other:?}"),
}
}
#[skuld::test]
fn an_exact_name_that_names_no_file_is_refused_on_posix() {
let (cwd, on_path) = cwd_and_path_tools();
std::fs::create_dir(cwd.path().join("dir")).expect("mkdir");
for n in ["", ".", "..", "dir/", "tool/..", "/"] {
let mut c = crate::test_child::command(Console::Hidden);
c.raw_executable(n)
.args(["tool"])
.current_dir(cwd.path())
.env("PATH", on_path.path());
match c.spawn() {
Err(Error::Io(e)) if e.kind() == std::io::ErrorKind::InvalidInput => {}
other => panic!("{n:?} names no file and must be Io(InvalidInput), got {other:?}"),
}
}
}
#[skuld::test]
fn a_bare_search_name_still_loads_the_file_on_path() {
let (cwd, on_path) = cwd_and_path_tools();
let mut c = crate::test_child::command(Console::Hidden);
c.executable("tool")
.args(["tool"])
.current_dir(cwd.path())
.env("PATH", on_path.path());
assert_eq!(exit_code(&mut c), Some(PATH_TOOL_EXIT));
}
#[skuld::test]
fn a_bare_exact_name_with_a_commandline_loads_the_childs_cwd_file() {
let (cwd, on_path) = cwd_and_path_tools();
let mut c = crate::test_child::command(Console::Hidden);
c.raw_executable("tool")
.commandline("tool")
.current_dir(cwd.path())
.env("PATH", on_path.path());
assert_eq!(exit_code(&mut c), Some(CWD_TOOL_EXIT));
}
fn marker_tool(dir: &std::path::Path, marker: &str, code: i32) {
use std::os::unix::fs::{DirBuilderExt, PermissionsExt};
std::fs::DirBuilder::new().mode(0o700).create(dir).expect("mkdir");
std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o755)).expect("chmod dir");
std::fs::write(dir.join(marker), "").expect("write marker");
let tool = dir.join("tool");
let _guard = crate::child::spawn::spawn_lock();
std::fs::write(&tool, format!("#!/bin/sh\n[ -f ./{marker} ] || exit 3\nexit {code}\n")).expect("write tool");
std::fs::set_permissions(&tool, std::fs::Permissions::from_mode(0o755)).expect("chmod tool");
}
const FIXTURE_UNREACHABLE_CWD_TEST: &str =
"child::spawn::exact_posix_tests::fixture_spawn_exact_tool_in_an_unreachable_cwd";
const FIXTURE_UNREACHABLE_CWD_ENV: &str = "COSCA_FIXTURE_UNREACHABLE_CWD";
const FIXTURE_CURRENT_DIR_ENV: &str = "COSCA_FIXTURE_UNREACHABLE_CWD_CURRENT_DIR";
const PRECONDITION_FAILED: i32 = 90;
const SPAWN_FAILED: i32 = 91;
#[skuld::test]
fn fixture_spawn_exact_tool_in_an_unreachable_cwd() {
use std::io::Read;
let Some(own_path) = std::env::var_os(FIXTURE_UNREACHABLE_CWD_ENV) else {
return;
};
if !crate::test_child::is_fixture_reexec() {
return;
}
let mut gate = [0u8; 1];
std::io::stdin().read_exact(&mut gate).expect("gate byte");
if std::fs::metadata(&own_path).is_ok() {
report(&format!("precondition: {own_path:?} is still reachable by path"));
std::process::exit(PRECONDITION_FAILED);
}
let std_code = crate::test_spawn::status(&mut std::process::Command::new("./tool")).map(|s| s.code());
if !matches!(std_code, Ok(Some(CWD_TOOL_EXIT))) {
report(&format!("precondition: std's ./tool gave {std_code:?}"));
std::process::exit(PRECONDITION_FAILED);
}
let mut c = crate::test_child::command(Console::Hidden);
c.raw_executable("tool").args(["tool"]);
if let Some(dir) = std::env::var_os(FIXTURE_CURRENT_DIR_ENV) {
c.current_dir(dir);
}
let code = match c.spawn() {
Ok(child) => child.wait().expect("wait").code().unwrap_or(SPAWN_FAILED),
Err(e) => {
report(&format!("spawn: {e}"));
SPAWN_FAILED
}
};
std::process::exit(code);
}
fn report(line: &str) {
use std::io::Write;
_ = writeln!(std::io::stderr(), "{line}");
}
fn spawn_exact_tool_in_an_unreachable_cwd(current_dir: Option<&str>) -> (Option<i32>, String) {
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
let root = tempfile::tempdir().expect("tempdir");
let (p, d) = (root.path().join("p"), root.path().join("p").join("d"));
std::fs::create_dir(&p).expect("mkdir p");
marker_tool(&d, "d-marker", CWD_TOOL_EXIT);
marker_tool(&d.join("sub"), "sub-marker", PATH_TOOL_EXIT);
let (mut fixture, _dirs) = crate::test_child::fixture_command_without_dac_bypass(FIXTURE_UNREACHABLE_CWD_TEST);
fixture
.env(FIXTURE_UNREACHABLE_CWD_ENV, &d)
.current_dir(&d)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::null());
if let Some(dir) = current_dir {
fixture.env(FIXTURE_CURRENT_DIR_ENV, dir);
}
let mut child = crate::test_spawn::spawn(&mut fixture).expect("spawn the fixture");
let _restore = crate::test_child::RestoreMode::new(&p, 0o755);
std::fs::set_permissions(&p, std::fs::Permissions::from_mode(0o000)).expect("chmod 000");
let gate = child.stdin.take().expect("stdin").write_all(b"x");
let out = child.wait_with_output().expect("wait");
(
out.status.code(),
format!("gate write: {gate:?}\n{}", String::from_utf8_lossy(&out.stderr)),
)
}
#[skuld::test]
fn an_exact_program_runs_in_a_cwd_that_has_no_path() {
let (code, stderr) = spawn_exact_tool_in_an_unreachable_cwd(None);
assert_eq!(code, Some(CWD_TOOL_EXIT), "{stderr}");
}
#[skuld::test]
fn a_relative_current_dir_is_entered_from_a_cwd_that_has_no_path() {
let (code, stderr) = spawn_exact_tool_in_an_unreachable_cwd(Some("sub"));
assert_eq!(code, Some(PATH_TOOL_EXIT), "{stderr}");
}
#[skuld::test]
fn an_empty_cwd_fails_an_exact_program_as_it_fails_a_search_one() {
let (cwd, _on_path) = cwd_and_path_tools();
let kind = |c: &mut Command| match c.spawn() {
Err(Error::Io(e)) => e.kind(),
other => panic!("expected Io, got {other:?}"),
};
let mut exact = crate::test_child::command(Console::Hidden);
exact.raw_executable("tool").args(["tool"]).current_dir("");
let mut search = crate::test_child::command(Console::Hidden);
search
.executable(cwd.path().join("tool"))
.args([cwd.path().join("tool")])
.current_dir("");
assert_eq!(kind(&mut search), std::io::ErrorKind::NotFound);
assert_eq!(kind(&mut exact), std::io::ErrorKind::NotFound);
}
#[cfg(target_os = "macos")]
fn std_command_of(c: &Command) -> std::process::Command {
let mut steps = crate::child::spawn::in_child::InChild::default();
let (std_cmd, ()) = crate::child::spawn::build_std_command_with(c, &mut steps, |_| ()).expect("build");
std_cmd
}
#[cfg(target_os = "macos")]
#[skuld::test]
fn a_relative_exact_program_hands_std_no_current_dir() {
for dir in ["sub", "/work"] {
let mut c = crate::test_child::command(Console::Hidden);
c.raw_executable("tool").args(["tool"]).current_dir(dir);
let std_cmd = std_command_of(&c);
assert_eq!(std_cmd.get_current_dir(), None, "{dir}");
assert_eq!(std_cmd.get_program(), "./tool", "{dir}");
}
}
#[cfg(target_os = "macos")]
#[skuld::test]
fn a_located_search_program_enters_its_directory_in_the_child() {
let dir = tempfile::tempdir().expect("tempdir");
std::fs::write(dir.path().join("tool"), b"x").expect("write");
let mut c = crate::test_child::command(Console::Hidden);
c.executable("./tool").args(["tool"]).current_dir(dir.path());
let std_cmd = std_command_of(&c);
assert_eq!(std_cmd.get_current_dir(), None);
assert_eq!(std_cmd.get_program(), "./tool");
}
#[skuld::test]
fn an_exact_program_with_an_empty_argv_gets_the_written_name_as_argv0() {
let mut c = crate::test_child::command(Console::Hidden);
c.raw_executable("sh").args(Vec::<&str>::new()).current_dir("/bin");
c.stdin(crate::Stdio::pipe()).expect("stdin");
c.stdout(crate::Stdio::pipe()).expect("stdout");
let out = c
.spawn()
.expect("spawn")
.communicate(Some(b"echo \"$0\"\n"))
.expect("communicate");
assert_eq!(String::from_utf8_lossy(&out.stdout), "sh\n");
}