use super::*;
#[test]
fn lookup_uses_path_order_relative_to_child_cwd_and_never_implicit_local_search() {
let root = tempfile::tempdir().unwrap();
for directory in ["first", "second"] {
std::fs::create_dir(root.path().join(directory)).unwrap();
lookup_program(&root.path().join(directory), "shim", None);
}
let program = executable_name();
let path = std::env::join_paths(["second", "first"]).unwrap();
assert_eq!(
resolve_with(Path::new(&program), root.path(), |name| (name
== SEARCH_PATH)
.then(|| path.clone()))
.unwrap(),
root.path().join("second").join(&program)
);
lookup_program(root.path(), "shim", None);
assert!(resolve_with(Path::new(&program), root.path(), |_| None).is_err());
}
#[cfg(windows)]
#[test]
fn pathext_candidates_keep_the_variable_order_and_skip_unsafe_suffixes() {
assert_eq!(
candidates(Path::new("dir/shim"), |key| (key == "PathExt")
.then(|| ".CMD;bad;.a/b;.EXE".into())),
[PathBuf::from("dir/shim.CMD"), PathBuf::from("dir/shim.EXE")]
);
assert_eq!(
candidates(Path::new("shim"), |_| None),
[".COM", ".EXE", ".BAT", ".CMD"].map(|suffix| PathBuf::from(format!("shim{suffix}")))
);
}
#[cfg(windows)]
#[test]
fn extensionless_names_follow_pathext_order_not_exe_preference() {
let root = tempfile::tempdir().unwrap();
for extension in ["cmd", "exe", "bat"] {
lookup_program(root.path(), "shim", Some(extension));
}
let path = std::env::join_paths([root.path()]).unwrap();
assert_eq!(
resolve_with(Path::new("shim"), root.path(), |key| match key {
"Path" => Some(path.clone()),
"PathExt" => Some(".CMD;.EXE".into()),
_ => None,
})
.unwrap(),
root.path().join("shim.CMD")
);
assert_eq!(
resolve_with(Path::new("shim"), root.path(), |key| match key {
"pAtH" => Some(path.clone()),
"PATHEXT" => Some(".EXE".into()),
"Pathext" => Some(".BAT;.CMD".into()),
_ => None,
})
.unwrap(),
root.path().join("shim.BAT")
);
}
#[cfg(unix)] fn lookup_program(directory: &Path, name: &str, _: Option<&str>) {
use std::{fs, os::unix::fs::PermissionsExt};
let path = directory.join(name);
fs::write(&path, "").unwrap();
fs::set_permissions(&path, fs::Permissions::from_mode(0o700)).unwrap();
}
#[cfg(windows)] fn lookup_program(directory: &Path, name: &str, extension: Option<&str>) {
std::fs::write(
directory
.join(name)
.with_extension(extension.unwrap_or("exe")),
"",
)
.unwrap();
}
fn executable_name() -> String {
format!("shim{}", std::env::consts::EXE_SUFFIX)
}
#[cfg(windows)]
#[test]
fn drive_relative_paths_are_anchored_only_to_the_matching_child_drive() {
let cwd = Path::new(r"C:\child");
assert_eq!(
lookup_paths(Path::new(r"c:tool.exe"), cwd, None, None),
[cwd.join("tool.exe")]
);
assert_eq!(
lookup_paths(
Path::new("tool.exe"),
cwd,
Some(OsStr::new(r"c:bin;D:bin")),
None
),
[cwd.join(r"bin\tool.exe")]
);
assert!(lookup_paths(Path::new(r"D:tool.exe"), cwd, None, None).is_empty());
}