use std::path::Path;
pub(crate) fn same_dir(left: &Path, right: &Path) -> bool {
match (left.canonicalize(), right.canonicalize()) {
(Ok(left), Ok(right)) => left == right,
_ => left == right,
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
#[test]
fn a_directory_is_itself() {
let dir = tempfile::tempdir().expect("tempdir");
assert!(same_dir(dir.path(), dir.path()));
}
#[test]
fn two_spellings_of_one_directory_match() {
let dir = tempfile::tempdir().expect("tempdir");
let indirect = dir.path().join("child").join("..");
std::fs::create_dir(dir.path().join("child")).expect("create child");
assert!(
same_dir(dir.path(), &indirect),
"a path that resolves to the same place is the same place"
);
}
#[test]
fn different_directories_do_not_match() {
let left = tempfile::tempdir().expect("tempdir");
let right = tempfile::tempdir().expect("tempdir");
assert!(!same_dir(left.path(), right.path()));
}
#[test]
fn unresolvable_paths_fall_back_to_comparing_as_written() {
let missing = PathBuf::from("/definitely/not/a/real/path");
assert!(same_dir(&missing, &missing));
assert!(!same_dir(&missing, &PathBuf::from("/also/not/real")));
}
}