Skip to main content

merry_process/
host_path.rs

1use std::path::{Path, PathBuf};
2
3/// Resolves a host path through the last existing prefix.
4///
5/// Delegates to [`merry_runtime::resolve_path_with_existing_ancestor`] so
6/// process sandboxes, in-process file tools, and workspace persistence all
7/// share one lexical-and-symlink path resolution contract.
8#[must_use]
9pub fn resolve_host_path(path: &Path) -> PathBuf {
10    merry_runtime::resolve_path_with_existing_ancestor(path)
11}
12
13#[cfg(all(test, unix))]
14mod tests {
15    use super::resolve_host_path;
16    use std::{fs, os::unix::fs::symlink, path::Path};
17
18    #[test]
19    fn resolves_existing_symlink_components() {
20        let temp = tempfile::tempdir().expect("temporary path");
21        let real = temp.path().join("real");
22        let link = temp.path().join("link");
23        fs::create_dir_all(&real).expect("real directory");
24        symlink(&real, &link).expect("directory symlink");
25
26        assert_eq!(resolve_host_path(&link), fs::canonicalize(real).unwrap());
27    }
28
29    #[test]
30    fn resolves_existing_prefix_for_missing_leaf() {
31        let temp = tempfile::tempdir().expect("temporary path");
32        let real = temp.path().join("real");
33        let link = temp.path().join("link");
34        fs::create_dir_all(&real).expect("real directory");
35        symlink(&real, &link).expect("directory symlink");
36        let missing = link.join("future").join("file");
37
38        assert_eq!(
39            resolve_host_path(&missing),
40            fs::canonicalize(&real)
41                .expect("canonical real")
42                .join("future")
43                .join("file")
44        );
45    }
46
47    #[test]
48    fn leaves_dangling_leaf_symlink_unresolved() {
49        let temp = tempfile::tempdir().expect("temporary path");
50        let link = temp.path().join("link");
51        symlink(Path::new("missing"), &link).expect("dangling symlink");
52
53        assert_eq!(
54            resolve_host_path(&link),
55            fs::canonicalize(temp.path())
56                .expect("canonical temp dir")
57                .join("link")
58        );
59    }
60}