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