#!/usr/bin/env bash # Fixture coverage for the isolated-HOME observed-run wrapper. set -euo pipefail cd "$(dirname "$0")/.." || exit 1 tmp=$(mktemp -d) trap 'rm -rf "$tmp"' EXIT # A fake "real" HOME holding a precious save at the platform data-dir path. fake_home="$tmp/home" case "$(uname -s)" in Darwin) data_dir="$fake_home/Library/Application Support/misaligned" ;; *) data_dir="$fake_home/.local/share/misaligned" ;; esac mkdir -p "$data_dir" printf 'precious playthrough\n' > "$data_dir/misaligned_save.txt" run() { HOME="$fake_home" XDG_DATA_HOME="" bash tools/observed-run.sh "$@" } # 1. A child that writes a save into its own HOME lands in the sandbox and # leaves the real save untouched. log="$tmp/sandboxed.log" run sh -c ' case "$(uname -s)" in Darwin) dir="$HOME/Library/Application Support/misaligned" ;; *) dir="${XDG_DATA_HOME:-$HOME/.local/share}/misaligned" ;; esac mkdir -p "$dir" printf "sandbox save\n" > "$dir/misaligned_save.txt" ' 2> "$log" grep -q "real save directory untouched" "$log" || { echo "FAIL: observed-run did not report the real save untouched" >&2 exit 1 } sandbox=$(sed -n 's/^observed-run: sandbox HOME: //p' "$log") found=$(find "$sandbox" -name misaligned_save.txt | head -1) if [ -z "$found" ] || ! grep -q "sandbox save" "$found"; then echo "FAIL: the child write did not land in the sandbox HOME" >&2 exit 1 fi rm -rf "$sandbox" [ "$(cat "$data_dir/misaligned_save.txt")" = "precious playthrough" ] || { echo "FAIL: the real save changed under a clean sandboxed run" >&2 exit 1 } # 2. A child that reaches the real save directory anyway must fail the run. log="$tmp/breakout.log" if run sh -c "echo junk >> '$data_dir/misaligned_save.txt'" 2> "$log"; then echo "FAIL: observed-run accepted a run that modified the real save" >&2 exit 1 fi grep -q "real save directory changed" "$log" || { echo "FAIL: observed-run failed without naming the real save change" >&2 exit 1 } sandbox=$(sed -n 's/^observed-run: sandbox HOME: //p' "$log") rm -rf "$sandbox" printf 'precious playthrough\n' > "$data_dir/misaligned_save.txt" # 3. The child's exit status propagates when the real save is untouched. log="$tmp/status.log" status=0 run sh -c 'exit 3' 2> "$log" || status=$? [ "$status" -eq 3 ] || { echo "FAIL: observed-run rewrote the child exit status ($status != 3)" >&2 exit 1 } grep -q "real save directory untouched" "$log" || { echo "FAIL: a failing child still needs the untouched verdict" >&2 exit 1 } sandbox=$(sed -n 's/^observed-run: sandbox HOME: //p' "$log") rm -rf "$sandbox" echo "observed-run fixtures: OK"