#[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");
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(())
}
fn entry_present(path: &Path) -> bool {
fs::symlink_metadata(path).is_ok()
}
#[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")));
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();
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));
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));
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();
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();
}