返回 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 printf '%s\n' "$@" >> "$TEST_FIXTURE/dev-argv-log"
116 # A real libtest run prints this; dev-test.sh refuses a filtered green without
117 # it. TEST_CARGO_SILENT drops it so the refusal itself can be exercised.
118 if [ -z "${TEST_CARGO_SILENT:-}" ]; then
119 printf '%s\n' 'test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s'
120 fi
121 exit "${TEST_CARGO_STATUS:-0}"
122 EOF
123 printf '%s\n' '#!/bin/sh' 'printf "%s\n" "commit-hash: synthetic"' > "$fixture/toolchain/rustc"
124 printf '%s\n' '#!/bin/sh' 'exit 0' > "$fixture/bin/cargo-nextest"
125 chmod +x "$fixture/toolchain/cargo" "$fixture/toolchain/rustc" "$fixture/bin/cargo-nextest"
126 ln -s "$fixture/toolchain/cargo" "$fixture/bin/cargo"
127 ln -s "$fixture/toolchain/rustc" "$fixture/bin/rustc"
128
129 count=3
130 for runner in 0 1; do
131 for area in config tui-integration; do
132 : > "$fixture/dev-argv-log"
133 run_fixture env CODEWHALE_DEV_NEXTEST="$runner" \
134 "$repo_root/scripts/dev-test.sh" "$area" 'one argument $(not run)' > "$fixture/dev-output"
135 if [ "$runner" -eq 1 ] && command -v python3 >/dev/null 2>&1; then
136 {
137 printf '%s\n' nextest run \
138 --cargo-metadata '<tmp>/cw-nextest/cargo-metadata.json' \
139 --binaries-metadata '<tmp>/cw-nextest/binaries-metadata.json' \
140 --config "build.build-dir = \"$fixture/outer/home/.cache/codewhale/build/{workspace-path-hash}\"" \
141 'one argument $(not run)'
142 } > "$fixture/expected-argv"
143 sed 's|^.*/cw-nextest-[^/]*/|<tmp>/cw-nextest/|' "$fixture/dev-argv" > "$fixture/dev-argv-norm"
144 cmp "$fixture/expected-argv" "$fixture/dev-argv-norm"
145 grep -Fx -- '--locked' "$fixture/dev-argv-log" >/dev/null
146 if [ "$area" = config ]; then
147 grep -Fx -- 'codewhale-config' "$fixture/dev-argv-log" >/dev/null
148 grep -Fx -- '--lib' "$fixture/dev-argv-log" >/dev/null
149 else
150 grep -Fx -- 'codewhale-tui' "$fixture/dev-argv-log" >/dev/null
151 grep -Fx -- 'integration' "$fixture/dev-argv-log" >/dev/null
152 fi
153 else
154 {
155 if [ "$runner" -eq 1 ]; then
156 printf '%s\n' nextest run --config "build.build-dir = \"$fixture/outer/home/.cache/codewhale/build/{workspace-path-hash}\""
157 else
158 printf '%s\n' --config "build.build-dir = \"$fixture/outer/home/.cache/codewhale/build/{workspace-path-hash}\"" test
159 fi
160 if [ "$area" = config ]; then
161 printf '%s\n' -p codewhale-config --lib
162 else
163 printf '%s\n' -p codewhale-tui --test integration
164 fi
165 printf '%s\n' --locked 'one argument $(not run)'
166 } > "$fixture/expected-argv"
167 cmp "$fixture/expected-argv" "$fixture/dev-argv"
168 fi
169 child_home=$(cat "$fixture/dev-home")
170 test ! -d "${child_home%/*}"
171 test -d "$fixture/outer/home/.cache/codewhale/build"
172 count=$((count + 1))
173 printf 'ok %s - dev-test runner=%s area=%s isolates config and preserves persistent cache and argv\n' "$count" "$runner" "$area"
174 done
175 done
176
177 status=0
178 run_fixture env CODEWHALE_DEV_NEXTEST=0 TEST_CARGO_STATUS=37 \
179 "$repo_root/scripts/dev-test.sh" config > "$fixture/dev-output" || status=$?
180 test "$status" -eq 37
181 child_home=$(cat "$fixture/dev-home")
182 test ! -d "${child_home%/*}"
183 test "$(cat "$fixture/outer/home/.codewhale/fleets/selected")" = 'My fleet'
184 printf '%s\n' 'ok 8 - dev-test preserves failure status and cleans only its temporary home'
185
186 # Exercise Windows cygpath path normalization, backslash toolchain resolution,
187 # and AppData provisioning required by sccache and Windows known-folder lookups.
188 mkdir -p "$fixture/win-bin" "$fixture/win-toolchain/bin"
189 cat > "$fixture/win-bin/cygpath" <<'EOF'
190 #!/bin/sh
191 set -eu
192 case "${1:-}" in
193 -m|-u) shift ;;
194 *) exit 2 ;;
195 esac
196 printf '%s\n' "$1" | tr '\\' '/'
197 EOF
198 cat > "$fixture/win-bin/rustup" <<'EOF'
199 #!/bin/sh
200 set -eu
201 printf '%s\n' "$TEST_TOOLCHAIN\\bin\\rustc.exe"
202 EOF
203 chmod +x "$fixture/win-bin/cygpath" "$fixture/win-bin/rustup"
204
205 run_win_fixture() {
206 env -i HOME="$fixture/outer/home" \
207 USERPROFILE="$fixture/outer/home" \
208 APPDATA="$fixture/outer/home/AppData/Roaming" \
209 LOCALAPPDATA="$fixture/outer/home/AppData/Local" \
210 CARGO_HOME="$fixture/cargo" RUSTUP_HOME="$fixture/rustup" \
211 TEST_FIXTURE="$fixture" TEST_TOOLCHAIN="$fixture/win-toolchain" \
212 TMPDIR="$fixture/tmp with spaces" \
213 PATH="$fixture/win-bin:/usr/bin:/bin" "$@"
214 }
215
216 run_win_fixture "$repo_root/scripts/with-hermetic-test-home.sh" sh -c '
217 set -eu
218 test "$HOME" != "$1/outer/home"
219 test "$USERPROFILE" = "$HOME"
220 test "$APPDATA" = "$HOME/AppData/Roaming"
221 test "$LOCALAPPDATA" = "$HOME/AppData/Local"
222 test -d "$HOME/AppData/Roaming"
223 test -d "$HOME/AppData/Local"
224 test -d "$HOME/.codewhale"
225 test "$APPDATA" != "$1/outer/home/AppData/Roaming"
226 test "$LOCALAPPDATA" != "$1/outer/home/AppData/Local"
227 # Verify toolchain_bin stripped the backslash executable properly
228 case "$PATH" in "$1/win-toolchain/bin:"*) ;; *) exit 1 ;; esac
229 # Verify sccache config-dir resolution target (RoamingAppData/Mozilla/sccache)
230 sccache_cfg_parent="$APPDATA/Mozilla/sccache"
231 mkdir -p "$sccache_cfg_parent"
232 printf "%s\n" "test-config = true" > "$sccache_cfg_parent/config"
233 test -f "$HOME/AppData/Roaming/Mozilla/sccache/config"
234 test ! -e "$1/outer/home/AppData/Roaming/Mozilla/sccache/config"
235 printf "%s\n" "$HOME" > "$1/win-child-home"
236 ' sh "$fixture"
237 win_child_home=$(cat "$fixture/win-child-home")
238 test ! -d "${win_child_home%/*}"
239 printf '%s\n' 'ok 9 - cygpath path normalization, backslash toolchain resolution and hermetic AppData'
240
241 # The guard 8b6dad20e introduced: libtest exits 0 when a filter matches
242 # nothing, which has been mistaken for a pass here before. With an explicit
243 # filter and no `test result:` line, dev-test.sh must refuse that green.
244 status=0
245 run_fixture env CODEWHALE_DEV_NEXTEST=0 TEST_CARGO_SILENT=1 \
246 "$repo_root/scripts/dev-test.sh" config 'filter $(matching nothing)' \
247 > "$fixture/dev-output" 2>&1 || status=$?
248 test "$status" -ne 0
249 grep -q 'refusing green' "$fixture/dev-output"
250 # nextest fails loud on an empty selection, so the guard must not wrap it.
251 run_fixture env CODEWHALE_DEV_NEXTEST=1 TEST_CARGO_SILENT=1 \
252 "$repo_root/scripts/dev-test.sh" config 'filter $(matching nothing)' \
253 > "$fixture/dev-output" 2>&1
254 printf '%s\n' 'ok 10 - a filtered libtest run with no test result refuses green'
255
256 for test_dir in \
257 crates/tui/tests crates/tui/tests/ \
258 crates/tui/tests/integration crates/tui/tests/integration/ \
259 crates/tui/tests/cucumber crates/tui/tests/cucumber/
260 do
261 case $test_dir in
262 */cucumber|*/cucumber/) harness=cucumber ;;
263 *) harness=integration ;;
264 esac
265 run_fixture env CODEWHALE_DEV_NEXTEST=0 \
266 "$repo_root/scripts/dev-test.sh" "$test_dir" > "$fixture/dev-output"
267 printf '%s\n' --config "build.build-dir = \"$fixture/outer/home/.cache/codewhale/build/{workspace-path-hash}\"" \
268 test -p codewhale-tui --test "$harness" --locked > "$fixture/expected-argv"
269 cmp "$fixture/expected-argv" "$fixture/dev-argv"
270 done
271 printf '%s\n' 'ok 11 - dev-test runs the full harness for TUI test directories'
272
273 (
274 cd "$fixture"
275 for test_path in crates/command-contract/src/elapsed.rs crates/tui/tests/cucumber/; do
276 run_fixture env CODEWHALE_DEV_NEXTEST=0 \
277 "$repo_root/scripts/dev-test.sh" "$test_path" > "$fixture/dev-output"
278 cp "$fixture/dev-argv" "$fixture/relative-argv"
279 run_fixture env CODEWHALE_DEV_NEXTEST=0 \
280 "$repo_root/scripts/dev-test.sh" "$repo_root/$test_path" > "$fixture/dev-output"
281 cmp "$fixture/relative-argv" "$fixture/dev-argv"
282 done
283 )
284 printf '%s\n' 'ok 12 - dev-test accepts absolute source and test directory paths from outside the repository'
285
286 printf '%s\n' 'test result: 12 passed; 0 failed'
287
287 lines BASH