| 1 | #!/bin/sh |
| 2 | # Shared test-home boundary for local and CI workspace tests. |
| 3 | # Rust toolchain homes stay real. |
| 4 | set -eu |
| 5 | |
| 6 | if [ "$#" -eq 0 ]; then |
| 7 | printf '%s\n' 'usage: with-hermetic-test-home.sh command [args...]' >&2 |
| 8 | exit 2 |
| 9 | fi |
| 10 | |
| 11 | real_cargo_home=${CARGO_HOME:-${HOME}/.cargo} |
| 12 | real_rustup_home=${RUSTUP_HOME:-${HOME}/.rustup} |
| 13 | # Git Bash hands MSYS paths to native Windows processes, which cannot |
| 14 | # resolve them. `-m` keeps forward slashes, so MSYS tools still work. |
| 15 | if command -v cygpath >/dev/null 2>&1; then |
| 16 | real_cargo_home=$(cygpath -m "$real_cargo_home") |
| 17 | real_rustup_home=$(cygpath -m "$real_rustup_home") |
| 18 | fi |
| 19 | |
| 20 | rustc_bin=$(RUSTUP_HOME="$real_rustup_home" rustup which rustc) |
| 21 | if command -v cygpath >/dev/null 2>&1; then |
| 22 | # PATH is a POSIX list in Git Bash: a C:/ drive prefix would add a |
| 23 | # spurious separator. Only exported native home values use mixed paths. |
| 24 | rustc_bin=$(cygpath -u "$rustc_bin") |
| 25 | fi |
| 26 | rustc_bin_norm=$(printf '%s' "$rustc_bin" | tr "\\\\" '/') |
| 27 | toolchain_bin=${rustc_bin_norm%/*} |
| 28 | |
| 29 | test_home_root=$(mktemp -d "${TMPDIR:-/tmp}/codewhale-test-home.XXXXXX") |
| 30 | test_home_root_raw=$test_home_root |
| 31 | if command -v cygpath >/dev/null 2>&1; then |
| 32 | test_home_root=$(cygpath -m "$test_home_root") |
| 33 | fi |
| 34 | |
| 35 | # Materialized builtin plugins leave read-only runtime trees behind; make the |
| 36 | # tree writable first so cleanup never masks the command's own exit status. |
| 37 | trap 'chmod -R u+w -- "$test_home_root_raw" 2>/dev/null || true; rm -rf -- "$test_home_root_raw" 2>/dev/null || true' EXIT |
| 38 | trap 'exit 129' HUP |
| 39 | trap 'exit 130' INT |
| 40 | trap 'exit 143' TERM |
| 41 | |
| 42 | mkdir -p "$test_home_root/home/.codewhale" "$test_home_root/xdg" |
| 43 | # Windows tools and known-folder resolvers (e.g. sccache / directories crate) |
| 44 | # expand %USERPROFILE%\AppData\Roaming and Local, and SHGetKnownFolderPath |
| 45 | # verifies directory existence before returning success. |
| 46 | mkdir -p "$test_home_root/home/AppData/Roaming" "$test_home_root/home/AppData/Local" |
| 47 | mkdir -p "$test_home_root/codex" "$test_home_root/grok" "$test_home_root/kimi-code" |
| 48 | mkdir -p "$test_home_root/kimi-share" "$test_home_root/claude" |
| 49 | |
| 50 | # Use the isolated HOME default, while allowing each test to choose its own |
| 51 | # home or config fixture. Canonical overrides would shadow legacy fixtures. |
| 52 | unset CODEWHALE_HOME CODEWHALE_CONFIG_PATH DEEPSEEK_CONFIG_PATH DEEPSEEK_HOME |
| 53 | |
| 54 | env \ |
| 55 | HOME="$test_home_root/home" \ |
| 56 | USERPROFILE="$test_home_root/home" \ |
| 57 | APPDATA="$test_home_root/home/AppData/Roaming" \ |
| 58 | LOCALAPPDATA="$test_home_root/home/AppData/Local" \ |
| 59 | XDG_CONFIG_HOME="$test_home_root/xdg" \ |
| 60 | CODEX_HOME="$test_home_root/codex" \ |
| 61 | GROK_HOME="$test_home_root/grok" \ |
| 62 | GROK_AUTH_PATH="$test_home_root/grok/auth.json" \ |
| 63 | KIMI_CODE_HOME="$test_home_root/kimi-code" \ |
| 64 | KIMI_SHARE_DIR="$test_home_root/kimi-share" \ |
| 65 | CLAUDE_CONFIG_DIR="$test_home_root/claude" \ |
| 66 | DEEPSEEK_API_KEY= \ |
| 67 | OPENAI_API_KEY= \ |
| 68 | ANTHROPIC_API_KEY= \ |
| 69 | XAI_API_KEY= \ |
| 70 | GROK_API_KEY= \ |
| 71 | MOONSHOT_API_KEY= \ |
| 72 | KIMI_API_KEY= \ |
| 73 | XIAOMI_MIMO_API_KEY= \ |
| 74 | XIAOMI_MIMO_TOKEN_PLAN_API_KEY= \ |
| 75 | MIMO_API_KEY= \ |
| 76 | MIMO_TOKEN_PLAN_API_KEY= \ |
| 77 | RUST_MIN_STACK="${RUST_MIN_STACK:-16777216}" \ |
| 78 | CARGO_HOME="$real_cargo_home" \ |
| 79 | RUSTUP_HOME="$real_rustup_home" \ |
| 80 | PATH="$toolchain_bin:$PATH" \ |
| 81 | "$@" |
| 82 |