use std::path::{Path, PathBuf};
#[must_use]
pub fn resolve_host_path(path: &Path) -> PathBuf {
merry_runtime::resolve_path_with_existing_ancestor(path)
}
#[cfg(all(test, unix))]
mod tests {
use super::resolve_host_path;
use std::{fs, os::unix::fs::symlink, path::Path};
#[test]
fn resolves_existing_symlink_components() {
let temp = tempfile::tempdir().expect("temporary path");
let real = temp.path().join("real");
let link = temp.path().join("link");
fs::create_dir_all(&real).expect("real directory");
symlink(&real, &link).expect("directory symlink");
assert_eq!(resolve_host_path(&link), fs::canonicalize(real).unwrap());
}
#[test]
fn resolves_existing_prefix_for_missing_leaf() {
let temp = tempfile::tempdir().expect("temporary path");
let real = temp.path().join("real");
let link = temp.path().join("link");
fs::create_dir_all(&real).expect("real directory");
symlink(&real, &link).expect("directory symlink");
let missing = link.join("future").join("file");
assert_eq!(
resolve_host_path(&missing),
fs::canonicalize(&real)
.expect("canonical real")
.join("future")
.join("file")
);
}
#[test]
fn leaves_dangling_leaf_symlink_unresolved() {
let temp = tempfile::tempdir().expect("temporary path");
let link = temp.path().join("link");
symlink(Path::new("missing"), &link).expect("dangling symlink");
assert_eq!(
resolve_host_path(&link),
fs::canonicalize(temp.path())
.expect("canonical temp dir")
.join("link")
);
}
}