mise-util 2026.11.14

Environment, directory, and filesystem helpers shared by mise's crates
Documentation
#[cfg(unix)]
use pretty_assertions::assert_eq;

use super::*;

#[cfg(unix)]
#[test]
fn un_dmg_accepts_license_and_extracts_app() -> Result<()> {
    check_dmg_extraction("licensed.dmg")
}

#[cfg(unix)]
#[test]
fn un_dmg_extracts_app_without_license() -> Result<()> {
    check_dmg_extraction("ordinary.dmg")
}

#[test]
fn dmg_attached_devices_finds_only_the_matching_image() {
    let info = "\
================================================
image-path      : /cache/other.dmg
image-alias     : /cache/other.dmg
/dev/disk6\tGUID_partition_scheme\t
/dev/disk6s1\t48465300-0000-11AA-AA11-00306543ECAC\t/Volumes/Other
================================================
image-path      : /cache/wanted.dmg
/dev/disk7\tGUID_partition_scheme\t
/dev/disk7s1\t48465300-0000-11AA-AA11-00306543ECAC\t/private/tmp/.tmpvS7AFm
";
    assert_eq!(
        dmg_attached_devices(info, Path::new("/cache/wanted.dmg")),
        vec!["/dev/disk7".to_string()]
    );
    assert!(dmg_attached_devices(info, Path::new("/cache/none.dmg")).is_empty());
}

#[cfg(unix)]
#[test]
fn un_dmg_detaches_image_left_attached_by_interrupted_run() -> Result<()> {
    check_dmg_extraction_with("ordinary.dmg", true)
}

#[cfg(unix)]
fn check_dmg_extraction(archive: &str) -> Result<()> {
    check_dmg_extraction_with(archive, false)
}

#[cfg(unix)]
fn check_dmg_extraction_with(archive: &str, stale: bool) -> Result<()> {
    let tmp = tempfile::tempdir()?;
    let hdiutil = tmp.path().join("hdiutil");
    // Emulate the external tool, including a bounded license prompt so a
    // regression fails instead of hanging the test runner.
    write(
        &hdiutil,
        r#"#!/bin/bash
set -eu
case "$1" in
  info)
if [[ -f "$0.stale" && ! -f "$0.detached" ]]; then
  printf 'image-path      : %s\n/dev/disk9\tGUID_partition_scheme\t\n' "$(cat "$0.stale")"
fi
;;
  attach)
if [[ -f "$0.stale" && ! -f "$0.detached" ]]; then
  echo "hdiutil: attach failed - Resource busy" >&2
  exit 16
fi
if [[ "$6" == *licensed.dmg ]]; then
  [[ "${PAGER:-}" == cat ]] || exit 45
  IFS= read -r -t 2 answer || exit 42
  [[ "$answer" == Y ]] || exit 43
fi
mkdir -p "$5/Example.app/Contents"
printf 'app payload' > "$5/Example.app/Contents/payload"
ln -s payload "$5/Example.app/Contents/link"
;;
  detach)
if [[ "$2" == -force ]]; then
  [[ "$3" == /dev/disk9 ]] || exit 47
  touch "$0.stale-detached"
fi
printf '%s' "${3:-$2}" > "$0.detached"
;;
  *) exit 44 ;;
esac
"#,
    )?;
    make_executable(&hdiutil)?;
    let mut env = crate::testing::EnvVarGuard::new();
    let mut paths = vec![tmp.path().to_path_buf()];
    paths.extend(std::env::split_paths(
        &std::env::var_os("PATH").unwrap_or_default(),
    ));
    env.set("PATH", std::env::join_paths(paths)?);
    env.set("PAGER", "less");
    let dest = tmp.path().join("extracted");
    if stale {
        write(
            tmp.path().join("hdiutil.stale"),
            tmp.path().join(archive).to_string_lossy().as_bytes(),
        )?;
    }

    un_dmg(&tmp.path().join(archive), &dest)?;

    assert_eq!(
        read_to_string(dest.join("Example.app/Contents/payload"))?,
        "app payload"
    );
    assert!(dest.join("Example.app/Contents/link").is_symlink());
    if stale {
        assert!(tmp.path().join("hdiutil.stale-detached").exists());
    }
    let mount = read_to_string(tmp.path().join("hdiutil.detached"))?;
    assert!(
        !Path::new(&mount).exists(),
        "temporary mount directory was not cleaned up"
    );
    Ok(())
}

/// Whether a directory entry is there at all, without resolving it.
///
/// `Path::exists` answers about the *target*, so it is false for a link whose target is gone —
/// which is the entry these tests are about. An assertion written with it would hold before
/// the removal as well as after, and prove nothing.
fn entry_present(path: &Path) -> bool {
    fs::symlink_metadata(path).is_ok()
}

/// Deliberately not `#[cfg(unix)]`: `make_symlink` writes a real symlink on unix and a
/// junction on Windows, and the two are removed by different system calls. The behaviour under
/// test is what happens to a link mise itself wrote, so it has to be checked on both.
/// Deliberately not `#[cfg(unix)]`, like the removal tests below: `make_symlink` writes a real
/// symlink on unix and a junction on Windows, and only `symlink_metadata` describes both
/// without resolving them.
#[test]
fn entry_exists_sees_what_path_exists_hides() {
    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("target");
    let live = dir.path().join("live");
    let broken = dir.path().join("broken");
    let plain = dir.path().join("plain.txt");
    create_dir_all(&target).unwrap();
    write(&plain, "x").unwrap();
    make_symlink(&target, &live).unwrap();
    make_symlink(&dir.path().join("nowhere"), &broken).unwrap();

    for p in [&target, &plain, &live, &broken] {
        assert!(entry_exists(p), "{}", p.display());
    }
    assert!(!entry_exists(dir.path().join("never-existed")));

    // The control, and the whole reason this function exists: `Path::exists` answers about the
    // link's target, so it disagrees for exactly the entry that needs finding.
    assert!(!broken.exists());
}

#[test]
fn dir_subdirs_drops_a_link_that_leads_nowhere() {
    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("target");
    create_dir_all(&target).unwrap();
    make_symlink(&target, &dir.path().join("live")).unwrap();
    make_symlink(&dir.path().join("nowhere"), &dir.path().join("broken")).unwrap();
    write(dir.path().join("plain.txt"), "x").unwrap();

    let subdirs = dir_subdirs(dir.path()).unwrap();
    assert!(
        subdirs.contains("target") && subdirs.contains("live"),
        "{subdirs:?}"
    );
    assert!(!subdirs.contains("broken"), "{subdirs:?}");
    assert!(!subdirs.contains("plain.txt"), "{subdirs:?}");
}

#[test]
fn remove_all_removes_a_link_whose_target_is_gone() {
    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("target");
    let link = dir.path().join("link");
    create_dir_all(&target).unwrap();
    write(target.join("keep.txt"), "important").unwrap();
    make_symlink(&target, &link).unwrap();

    remove_all(&target).unwrap();
    // Control: the entry really is a link that no longer resolves. Without this the assertion
    // below could be passing because there was nothing there to begin with.
    assert!(entry_present(&link));
    assert!(!link.exists());

    remove_all(&link).unwrap();
    assert!(!entry_present(&link));
}

#[test]
fn remove_all_removes_a_live_link_without_touching_its_target() {
    let dir = tempfile::tempdir().unwrap();
    let target = dir.path().join("target");
    let link = dir.path().join("link");
    create_dir_all(&target).unwrap();
    write(target.join("keep.txt"), "important").unwrap();
    make_symlink(&target, &link).unwrap();

    remove_all(&link).unwrap();
    assert!(!entry_present(&link));
    // The other half, and the reason the link arm cannot simply recurse: `mise link` points at
    // a directory the user owns, and removing the link must not take its contents with it.
    assert!(target.join("keep.txt").exists());
}

#[test]
fn remove_all_still_removes_ordinary_files_and_directories() {
    let dir = tempfile::tempdir().unwrap();
    let file = dir.path().join("file.txt");
    let subdir = dir.path().join("subdir");
    write(&file, "x").unwrap();
    create_dir_all(subdir.join("nested")).unwrap();
    write(subdir.join("nested/x.txt"), "x").unwrap();

    remove_all(&file).unwrap();
    assert!(!entry_present(&file));
    remove_all(&subdir).unwrap();
    assert!(!entry_present(&subdir));

    // A path that was never there is not an error: most callers remove opportunistically.
    remove_all(dir.path().join("never-existed")).unwrap();
}

#[cfg(unix)]
#[test]
fn remove_all_removes_a_tree_whose_directories_deny_their_owner_writes() {
    use std::os::unix::fs::PermissionsExt;

    let dir = tempfile::tempdir().unwrap();
    let install = dir.path().join("install");
    let nested = install.join("lib/python3.14/encodings");
    create_dir_all(&nested).unwrap();
    write(nested.join("aliases.py"), "x").unwrap();
    // A link out of the tree into a sealed directory the removal does not own.
    let outside = dir.path().join("outside");
    create_dir_all(&outside).unwrap();
    write(outside.join("keep.txt"), "important").unwrap();
    make_symlink(&outside, &install.join("lib/outside")).unwrap();
    for path in [
        &outside,
        &nested,
        &install.join("lib/python3.14"),
        &install.join("lib"),
        &install,
    ] {
        fs::set_permissions(path, fs::Permissions::from_mode(0o555)).unwrap();
    }

    remove_all(&install).unwrap();
    assert!(!entry_present(&install));
    let mode = fs::metadata(&outside).unwrap().permissions().mode();
    assert_eq!(mode & 0o777, 0o555);
    assert!(outside.join("keep.txt").exists());
    fs::set_permissions(&outside, fs::Permissions::from_mode(0o755)).unwrap();
}