返回 CodeWhale
with-hermetic-test-home.test.sh
根目录 / scripts / with-hermetic-test-home.test.sh
1 #!/bin/sh
2 # Exercise the real boundary using only synthetic homes and a fake toolchain.
3 # Quoted child programs and the literal argv probe expand only in the child.
4 # shellcheck disable=SC2016
5 set -eu
6 repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
7 fixture=$(mktemp -d "${TMPDIR:-/tmp}/hermetic-home-proof.XXXXXX")
8 trap 'rm -rf -- "$fixture"' EXIT
9
10 mkdir -p "$fixture/bin" "$fixture/toolchain" "$fixture/tmp with spaces"
11 mkdir -p "$fixture/outer/home/.codewhale/fleets" "$fixture/cargo" "$fixture/rustup"
12 printf '%s\n' 'My fleet' > "$fixture/outer/home/.codewhale/fleets/selected"
13 printf '%s\n' '[invalid fixture' > "$fixture/outer/home/.codewhale/fleets/my-fleet.toml"
14 printf '%s\n' '#!/bin/sh' 'printf "%s\n" "$TEST_TOOLCHAIN/rustc"' > "$fixture/bin/rustup"
15 chmod +x "$fixture/bin/rustup"
16 fixture_stack=20971520
17
18 run_fixture() {
19 env -i HOME="$fixture/outer/home" \
20 CODEWHALE_HOME="$fixture/outer/home/.codewhale" \
21 CODEWHALE_CONFIG_PATH="$fixture/outer/poison.toml" \
22 DEEPSEEK_CONFIG_PATH="$fixture/outer/legacy-poison.toml" \
23 DEEPSEEK_HOME="$fixture/outer/legacy-home" \
24 CODEX_HOME="$fixture/outer/codex" \
25 OPENAI_API_KEY=synthetic-outer-key \
26 RUST_MIN_STACK="$fixture_stack" \
27 CARGO_HOME="$fixture/cargo" RUSTUP_HOME="$fixture/rustup" \
28 TEST_FIXTURE="$fixture" TEST_TOOLCHAIN="$fixture/toolchain" \
29 TMPDIR="$fixture/tmp with spaces" \
30 CODEWHALE_DEV_CACHE_QUIET=1 CODEWHALE_SCCACHE=0 \
31 PATH="$fixture/bin:/usr/bin:/bin" "$@"
32 }
33
34 run_isolated() {
35 run_fixture "$repo_root/scripts/with-hermetic-test-home.sh" "$@"
36 }
37
38 run_isolated sh -c '
39 set -eu
40 test "$HOME" != "$1/outer/home"
41 test "$USERPROFILE" = "$HOME"
42 test -d "$HOME/.codewhale"
43 test -d "$HOME/AppData/Roaming"
44 test -d "$HOME/AppData/Local"
45 test "$APPDATA" = "$HOME/AppData/Roaming"
46 test "$LOCALAPPDATA" = "$HOME/AppData/Local"
47 test ! -e "$HOME/.codewhale/fleets/selected"
48 test -z "${CODEWHALE_HOME+x}"
49 test -z "${CODEWHALE_CONFIG_PATH+x}"
50 test -z "${DEEPSEEK_CONFIG_PATH+x}"
51 test -z "${DEEPSEEK_HOME+x}"
52 test "$CODEX_HOME" != "$1/outer/codex"
53 test -d "$XDG_CONFIG_HOME"
54 test -d "$CODEX_HOME"
55 test -z "$OPENAI_API_KEY"
56 test "$RUST_MIN_STACK" = 20971520
57 test "$CARGO_HOME" = "$1/cargo"
58 test "$RUSTUP_HOME" = "$1/rustup"
59 case "$PATH" in "$1/toolchain:"*) ;; *) exit 1 ;; esac
60 test "$2" = '\''one argument $(not run)'\''
61 printf "%s\n" "$HOME" > "$1/child-home"
62 ' sh "$fixture" 'one argument $(not run)'
63 child_home=$(cat "$fixture/child-home")
64 test ! -d "${child_home%/*}"
65 test "$(cat "$fixture/outer/home/.codewhale/fleets/selected")" = 'My fleet'
66 test "$(cat "$fixture/outer/home/.codewhale/fleets/my-fleet.toml")" = '[invalid fixture'
67 printf '%s\n' 'ok 1 - isolated homes, credentials, argv, toolchain and outer state'
68
69 status=0
70 fixture_stack=
71 run_isolated sh -c 'set -e; test "$RUST_MIN_STACK" = 16777216; printf "%s\n" "$HOME" > "$1/failing-home"; exit 37' sh "$fixture" || status=$?
72 test "$status" -eq 37
73 child_home=$(cat "$fixture/failing-home")
74 test ! -d "${child_home%/*}"
75 test -f "$fixture/outer/home/.codewhale/fleets/selected"
76 printf '%s\n' 'ok 2 - child failure status and owned-home cleanup'
77
78 status=0
79 run_isolated > "$fixture/usage" 2>&1 || status=$?
80 test "$status" -eq 2
81 printf '%s\n' 'ok 3 - missing command is rejected'
82
83 # Exercise the actual developer entry point without invoking a Rust tool.
84 # The cache remains outside the disposable HOME, including Cargo's literal
85 # build-dir template and --config argument needed for template expansion.
86 cat > "$fixture/toolchain/cargo" <<'EOF'
87 #!/bin/sh
88 set -eu
89 if [ "${1:-}" = --version ]; then
90 printf '%s\n' 'cargo 1.97.0 (synthetic)'
91 exit 0
92 fi
93 test "$HOME" != "$TEST_FIXTURE/outer/home" || {
94 printf '%s\n' 'dev-test left ambient HOME visible' >&2
95 exit 1
96 }
97 test "$USERPROFILE" = "$HOME"
98 test "$APPDATA" = "$HOME/AppData/Roaming"
99 test "$LOCALAPPDATA" = "$HOME/AppData/Local"
100 test -d "$APPDATA"
101 test -d "$LOCALAPPDATA"
102 test ! -e "$HOME/.codewhale/fleets/selected"
103 test -z "${CODEWHALE_HOME+x}"
104 test -z "${CODEWHALE_CONFIG_PATH+x}"
105 test -z "${DEEPSEEK_CONFIG_PATH+x}"
106 test -z "${DEEPSEEK_HOME+x}"
107 test -z "$OPENAI_API_KEY"
108 test "$CARGO_HOME" = "$TEST_FIXTURE/cargo"
109 test "$RUSTUP_HOME" = "$TEST_FIXTURE/rustup"
110 test "$RUST_MIN_STACK" = 16777216
111 test "$CARGO_BUILD_BUILD_DIR" = "$TEST_FIXTURE/outer/home/.cache/codewhale/build/{workspace-path-hash}"
112 test "$CARGO_BUILD_BUILD_DIR" = "$CODEWHALE_CACHE_ROOT/build/{workspace-path-hash}"
113 printf '%s\n' "$HOME" > "$TEST_FIXTURE/dev-home"
114 printf '%s\n' "$@" > "$TEST_FIXTURE/dev-argv"
115 exit "${TEST_CARGO_STATUS:-0}"
116 EOF
117 printf '%s\n' '#!/bin/sh' 'printf "%s\n" "commit-hash: synthetic"' > "$fixture/toolchain/rustc"
118 printf '%s\n' '#!/bin/sh' 'exit 0' > "$fixture/bin/cargo-nextest"
119 chmod +x "$fixture/toolchain/cargo" "$fixture/toolchain/rustc" "$fixture/bin/cargo-nextest"
120 ln -s "$fixture/toolchain/cargo" "$fixture/bin/cargo"
121 ln -s "$fixture/toolchain/rustc" "$fixture/bin/rustc"
122
123 count=3
124 for runner in 0 1; do
125 for area in config tui-integration; do
126 run_fixture env CODEWHALE_DEV_NEXTEST="$runner" \
127 "$repo_root/scripts/dev-test.sh" "$area" 'one argument $(not run)' > "$fixture/dev-output"
128 {
129 printf '%s\n' --config "build.build-dir = \"$fixture/outer/home/.cache/codewhale/build/{workspace-path-hash}\""
130 if [ "$runner" -eq 1 ]; then
131 printf '%s\n' nextest run
132 else
133 printf '%s\n' test
134 fi
135 if [ "$area" = config ]; then
136 printf '%s\n' -p codewhale-config --lib
137 else
138 printf '%s\n' -p codewhale-tui --test integration
139 fi
140 printf '%s\n' --locked 'one argument $(not run)'
141 } > "$fixture/expected-argv"
142 cmp "$fixture/expected-argv" "$fixture/dev-argv"
143 child_home=$(cat "$fixture/dev-home")
144 test ! -d "${child_home%/*}"
145 test -d "$fixture/outer/home/.cache/codewhale/build"
146 count=$((count + 1))
147 printf 'ok %s - dev-test runner=%s area=%s isolates config and preserves persistent cache and argv\n' "$count" "$runner" "$area"
148 done
149 done
150
151 status=0
152 run_fixture env CODEWHALE_DEV_NEXTEST=0 TEST_CARGO_STATUS=37 \
153 "$repo_root/scripts/dev-test.sh" config > "$fixture/dev-output" || status=$?
154 test "$status" -eq 37
155 child_home=$(cat "$fixture/dev-home")
156 test ! -d "${child_home%/*}"
157 test "$(cat "$fixture/outer/home/.codewhale/fleets/selected")" = 'My fleet'
158 printf '%s\n' 'ok 8 - dev-test preserves failure status and cleans only its temporary home'
159
160 # Exercise Windows cygpath path normalization, backslash toolchain resolution,
161 # and AppData provisioning required by sccache and Windows known-folder lookups.
162 mkdir -p "$fixture/win-bin" "$fixture/win-toolchain/bin"
163 cat > "$fixture/win-bin/cygpath" <<'EOF'
164 #!/bin/sh
165 set -eu
166 case "${1:-}" in
167 -m|-u) shift ;;
168 *) exit 2 ;;
169 esac
170 printf '%s\n' "$1" | tr '\\' '/'
171 EOF
172 cat > "$fixture/win-bin/rustup" <<'EOF'
173 #!/bin/sh
174 set -eu
175 printf '%s\n' "$TEST_TOOLCHAIN\\bin\\rustc.exe"
176 EOF
177 chmod +x "$fixture/win-bin/cygpath" "$fixture/win-bin/rustup"
178
179 run_win_fixture() {
180 env -i HOME="$fixture/outer/home" \
181 USERPROFILE="$fixture/outer/home" \
182 APPDATA="$fixture/outer/home/AppData/Roaming" \
183 LOCALAPPDATA="$fixture/outer/home/AppData/Local" \
184 CARGO_HOME="$fixture/cargo" RUSTUP_HOME="$fixture/rustup" \
185 TEST_FIXTURE="$fixture" TEST_TOOLCHAIN="$fixture/win-toolchain" \
186 TMPDIR="$fixture/tmp with spaces" \
187 PATH="$fixture/win-bin:/usr/bin:/bin" "$@"
188 }
189
190 run_win_fixture "$repo_root/scripts/with-hermetic-test-home.sh" sh -c '
191 set -eu
192 test "$HOME" != "$1/outer/home"
193 test "$USERPROFILE" = "$HOME"
194 test "$APPDATA" = "$HOME/AppData/Roaming"
195 test "$LOCALAPPDATA" = "$HOME/AppData/Local"
196 test -d "$HOME/AppData/Roaming"
197 test -d "$HOME/AppData/Local"
198 test -d "$HOME/.codewhale"
199 test "$APPDATA" != "$1/outer/home/AppData/Roaming"
200 test "$LOCALAPPDATA" != "$1/outer/home/AppData/Local"
201 # Verify toolchain_bin stripped the backslash executable properly
202 case "$PATH" in "$1/win-toolchain/bin:"*) ;; *) exit 1 ;; esac
203 # Verify sccache config-dir resolution target (RoamingAppData/Mozilla/sccache)
204 sccache_cfg_parent="$APPDATA/Mozilla/sccache"
205 mkdir -p "$sccache_cfg_parent"
206 printf "%s\n" "test-config = true" > "$sccache_cfg_parent/config"
207 test -f "$HOME/AppData/Roaming/Mozilla/sccache/config"
208 test ! -e "$1/outer/home/AppData/Roaming/Mozilla/sccache/config"
209 printf "%s\n" "$HOME" > "$1/win-child-home"
210 ' sh "$fixture"
211 win_child_home=$(cat "$fixture/win-child-home")
212 test ! -d "${win_child_home%/*}"
213 printf '%s\n' 'ok 9 - cygpath path normalization, backslash toolchain resolution and hermetic AppData'
214
215 printf '%s\n' 'test result: 9 passed; 0 failed'
216
216 lines BASH