use super::{canonical_target, resolve_target};
#[cfg(windows)]
#[test]
fn verbatim_components_are_not_reparsed_as_plain_win32_names() {
let root = tempfile::tempdir().unwrap();
let native = std::fs::canonicalize(root.path()).unwrap();
let plain = native.join("report");
let punctuated = native.join("report.");
std::fs::write(&plain, b"plain").unwrap();
std::fs::write(&punctuated, b"punctuated").unwrap();
assert_eq!(
std::fs::read(canonical_target(&punctuated).unwrap()).unwrap(),
b"punctuated"
);
assert_eq!(
std::fs::read(canonical_target(&plain).unwrap()).unwrap(),
b"plain"
);
}
#[test]
fn missing_suffix_uses_the_existing_ancestors_native_case_policy() {
let root = tempfile::tempdir().unwrap();
let present = root.path().join("Existing");
std::fs::create_dir(&present).unwrap();
let alternate = root.path().join("EXISTING");
let insensitive = alternate.is_dir();
assert_eq!(
canonical_target(&present.join("NewDirectory/NéwFile")).unwrap()
== canonical_target(&present.join("NEWDIRECTORY/NÉWFILE")).unwrap(),
insensitive,
);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_casefold_names_match_expansions_and_canonical_equivalents() {
use std::{ffi::OsStr, os::unix::ffi::OsStrExt};
let fold = |name| super::NamePolicy::Folded.key(name).unwrap();
assert_eq!(
fold(OsStr::new("ÉStraße")),
fold(OsStr::new("E\u{301}STRASSE"))
);
let opaque = OsStr::from_bytes(b"A\xff");
assert_eq!(fold(opaque), opaque);
}
#[cfg(target_os = "macos")]
#[test]
fn macos_casefold_preserves_accents_and_unifies_canonical_equivalents() {
use std::ffi::OsStr;
let fold = |name| super::NamePolicy::Folded.key(OsStr::new(name)).unwrap();
assert_eq!(fold("Néw"), fold("NE\u{301}W"));
assert_ne!(fold("Néw"), fold("NEW"));
}
#[cfg(target_os = "macos")]
#[test]
fn macos_missing_unicode_names_follow_the_native_volume() {
let root = tempfile::tempdir().unwrap();
let composed = root.path().join("Néw");
let decomposed = root.path().join("Ne\u{301}w");
let before_creation =
canonical_target(&composed).unwrap() == canonical_target(&decomposed).unwrap();
std::fs::create_dir(&composed).unwrap();
assert_eq!(before_creation, decomposed.exists());
}
#[cfg(unix)]
#[test]
fn file_with_a_directory_suffix_is_not_a_file_target() {
let root = tempfile::tempdir().unwrap();
let file = root.path().join("file");
std::fs::write(&file, b"file").unwrap();
for suffix in ["file/", "file/.", "file/.///", "file/.."] {
let path = root.path().join(suffix);
let native = std::fs::metadata(&path).unwrap_err().kind();
assert_eq!(resolve_target(&path).unwrap_err().kind(), native);
assert_eq!(canonical_target(&path).unwrap_err().kind(), native);
assert!(!super::paths_equal(&path, &file));
}
let missing = resolve_target(&root.path().join("new/")).unwrap();
assert!(std::fs::File::create(missing).is_err());
for suffix in ["new/.", "new/.///"] {
let missing_dot = resolve_target(&root.path().join(suffix)).unwrap();
assert!(std::fs::create_dir(missing_dot).is_err());
}
assert!(!root.path().join("new").exists());
}
#[cfg(unix)]
#[test]
fn parent_component_cannot_hide_a_missing_directory() {
let root = tempfile::tempdir().unwrap();
std::fs::write(root.path().join("existing"), b"victim").unwrap();
for suffix in ["missing/..", "missing/../existing", "missing/../uncreated"] {
let path = root.path().join(suffix);
let native = std::fs::metadata(&path).unwrap_err().kind();
assert_eq!(native, std::io::ErrorKind::NotFound);
assert_eq!(canonical_target(&path).unwrap_err().kind(), native);
assert_eq!(resolve_target(&path).unwrap_err().kind(), native);
}
}
#[cfg(windows)]
#[test]
fn ordinary_parent_component_is_collapsed_before_lookup() {
let root = tempfile::tempdir().unwrap();
let existing = root.path().join("existing");
std::fs::write(&existing, b"victim").unwrap();
for name in ["existing", "uncreated"] {
let through_missing = root.path().join("missing").join("..").join(name);
let direct = root.path().join(name);
assert_eq!(
std::fs::canonicalize(&through_missing).map_err(|error| error.kind()),
std::fs::canonicalize(&direct).map_err(|error| error.kind()),
);
assert_eq!(
canonical_target(&through_missing).unwrap(),
canonical_target(&direct).unwrap(),
);
assert_eq!(
resolve_target(&through_missing).unwrap(),
resolve_target(&direct).unwrap(),
);
}
}
#[cfg(windows)]
#[test]
fn verbatim_parent_component_is_not_collapsed() {
let root = tempfile::tempdir().unwrap();
let parent = std::fs::canonicalize(root.path()).unwrap();
std::fs::create_dir(parent.join("dir")).unwrap();
std::fs::write(parent.join("file"), b"file").unwrap();
let mut literal = parent.into_os_string();
literal.push(r"\dir\..\file");
let literal = std::path::PathBuf::from(literal);
let native = std::fs::read(&literal).unwrap_err().kind();
for normalize in [canonical_target, resolve_target] {
match normalize(&literal) {
Ok(resolved) => assert_eq!(std::fs::read(resolved).unwrap_err().kind(), native),
Err(error) => assert_eq!(error.kind(), native),
}
}
}