use windows_namespace_request_sys::ResolveFullPath;
use wtf_string::Wtf16String;
const CHILD_MARKER: &str = "WNRS_UNC_CURRENT_DIRECTORY_CHILD";
const UNC_ROOT: &str = r"\\localhost\C$";
const UNC_SUBDIRECTORY_CANDIDATES: &[&str] = &["Windows", "Users", "ProgramData"];
const TEST_NAME: &str = "a_root_relative_path_takes_the_share_root_under_a_unc_current_directory";
#[test]
#[ignore = "needs a reachable administrative share for a UNC current directory; run in CI with --include-ignored"]
fn a_root_relative_path_takes_the_share_root_under_a_unc_current_directory() {
if let Some(placed_at) = std::env::var_os(CHILD_MARKER) {
let placed_at = placed_at
.to_str()
.expect("the parent passes a UTF-8 directory")
.to_owned();
assert_root_relative_takes_the_share_root(&placed_at);
return;
}
assert!(
std::path::Path::new(UNC_ROOT).is_dir(),
"{UNC_ROOT} is not reachable, so no UNC current directory can be \
established -- an environment limitation, not a failure of the \
behaviour under test"
);
let working_directory = UNC_SUBDIRECTORY_CANDIDATES
.iter()
.map(|name| format!(r"{UNC_ROOT}\{name}"))
.find(|path| std::path::Path::new(path).is_dir())
.unwrap_or_else(|| {
panic!(
"none of {UNC_SUBDIRECTORY_CANDIDATES:?} exists under {UNC_ROOT}, \
so the child cannot be placed BELOW the share root -- and at the \
root the two rooting rules give the same answer, which is what \
this test exists to separate"
)
});
let exe = std::env::current_exe().expect("the test binary knows its own path");
let status = std::process::Command::new(exe)
.arg("--exact")
.arg(TEST_NAME)
.arg("--nocapture")
.arg("--include-ignored")
.env(CHILD_MARKER, &working_directory)
.current_dir(&working_directory)
.status()
.expect("re-execute this test binary with a UNC current directory");
assert!(
status.success(),
"the child, running with {working_directory} as its current directory, \
did not pass: {status}"
);
}
fn assert_root_relative_takes_the_share_root(placed_at: &str) {
let cwd = std::env::current_dir().expect("the child has a current directory");
let cwd = cwd.to_str().expect("the current directory is UTF-8");
assert!(
cwd.eq_ignore_ascii_case(placed_at.trim_end_matches('\\')),
"the child is at {cwd}, not the {placed_at} the parent chose. If no \
parent placed it there, {CHILD_MARKER} is set in this environment and \
should be unset -- that is a collision, not a defect in the crate"
);
assert!(
cwd.starts_with(r"\\"),
"precondition: the child must actually hold a UNC current directory, \
not a drive-rooted one: {cwd}"
);
assert_ne!(
cwd.trim_end_matches('\\'),
UNC_ROOT,
"precondition: the child must be BELOW the share root. At the root, \
rooting at the root of the current directory and rooting at the whole \
current directory agree, so neither assertion below can tell them apart"
);
let resolved = ResolveFullPath::new(Wtf16String::from(r"\foo"))
.perform()
.expect("a root-relative path resolves");
assert_eq!(
resolved.to_string_lossy(),
format!(r"{UNC_ROOT}\foo"),
"a root-relative path takes the ROOT of the current directory, which \
under a UNC current directory is the share root -- so \"the current \
drive\", as a draft of the module doc said, has no referent here"
);
let relative = ResolveFullPath::new(Wtf16String::from("foo"))
.perform()
.expect("a relative path resolves");
assert_eq!(
relative.to_string_lossy(),
format!(r"{cwd}\foo"),
"an ordinary relative path takes the whole current directory, not its \
root -- the two rules are distinguishable here and identical at the \
share root"
);
}