use super::*;
use std::path::PathBuf;
fn touch(dir: &Path, name: &str) -> PathBuf {
let p = dir.join(name);
std::fs::write(&p, b"x").unwrap();
p
}
fn go(program: &str, cwd: &Path, path_var: Option<&OsStr>, loadable_only: bool) -> Result<PathBuf, Error> {
resolve(ResolveInput {
program: Path::new(program),
cwd: Some(cwd),
system_dirs: &no_system_dirs,
path_var,
windows: true,
loadable_only,
normalise: &as_written,
child_drive_dir: &no_drive_dir,
same_drive: &ascii_same_drive,
})
}
fn kind(got: Result<PathBuf, Error>) -> std::io::ErrorKind {
match got {
Err(Error::Io(e)) => e.kind(),
other => panic!("expected an io error, got {other:?}"),
}
}
fn bin_with(files: &[&str]) -> (tempfile::TempDir, PathBuf) {
let cwd = tempfile::tempdir().unwrap();
let bin = cwd.path().join("bin");
std::fs::create_dir(&bin).unwrap();
for f in files {
touch(&bin, f);
}
(cwd, bin)
}
#[skuld::test]
fn an_extensionless_sibling_does_not_block_the_exe() {
let (cwd, bin) = bin_with(&["tool", "tool.exe"]);
assert_eq!(go("bin/tool", cwd.path(), None, true).unwrap(), bin.join("tool.exe"));
}
#[skuld::test]
fn only_an_extensionless_file_is_not_found() {
let (cwd, _bin) = bin_with(&["tool"]);
assert_eq!(
kind(go("bin/tool", cwd.path(), None, true)),
std::io::ErrorKind::NotFound
);
}
#[skuld::test]
fn only_the_exe_resolves_to_it() {
let (cwd, bin) = bin_with(&["tool.exe"]);
assert_eq!(go("bin/tool", cwd.path(), None, true).unwrap(), bin.join("tool.exe"));
}
#[skuld::test]
fn a_loadable_name_written_by_the_caller_is_kept() {
let (cwd, bin) = bin_with(&["TOOL.EXE", "more.com"]);
assert_eq!(
go("bin/TOOL.EXE", cwd.path(), None, true).unwrap(),
bin.join("TOOL.EXE")
);
assert_eq!(
go("bin/more.com", cwd.path(), None, true).unwrap(),
bin.join("more.com")
);
}
#[skuld::test]
fn a_located_name_with_no_loadable_candidate_is_refused_on_shape() {
for present in [true, false] {
let (cwd, _bin) = bin_with(if present {
&["tool.bat", "tool.", "tool.bin"]
} else {
&[]
});
for name in ["bin/tool.bat", "bin/tool.", "bin/tool.bin"] {
let got = go(name, cwd.path(), None, true);
assert_eq!(
kind(got),
std::io::ErrorKind::InvalidInput,
"{name:?} (present: {present})"
);
}
}
}
#[skuld::test]
fn a_bare_name_resolves_as_it_does_unelevated() {
let cwd = tempfile::tempdir().unwrap();
let dir = tempfile::tempdir().unwrap();
touch(dir.path(), "tool");
let want = touch(dir.path(), "tool.exe");
let path = dir.path().as_os_str();
assert_eq!(go("tool", cwd.path(), Some(path), true).unwrap(), want);
assert_eq!(go("tool", cwd.path(), Some(path), false).unwrap(), want);
}
#[skuld::test]
fn unelevated_resolution_is_unchanged() {
let (cwd, bin) = bin_with(&["tool", "tool.exe", "tool.bat"]);
assert_eq!(go("bin/tool", cwd.path(), None, false).unwrap(), bin.join("tool"));
assert_eq!(
go("bin/tool.bat", cwd.path(), None, false).unwrap(),
bin.join("tool.bat")
);
let (cwd, bin) = bin_with(&["tool"]);
assert_eq!(go("bin/tool", cwd.path(), None, false).unwrap(), bin.join("tool"));
}