返回 CodeWhale
dev-cache.test.sh
根目录 / scripts / dev-cache.test.sh
1 #!/bin/sh
2 # Hermetic tests for scripts/dev-cache.sh. No cargo compile, no cache
3 # deletion. Exercises portable defaults, overrides, missing-sccache
4 # fallback, incremental gating, and new-vs-existing worktree policy.
5 set -eu
6
7 repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
8 DEV_CACHE=$repo_root/scripts/dev-cache.sh
9 DEV_CARGO=$repo_root/scripts/dev-cargo.sh
10
11 fail=0
12 pass=0
13
14 ok() {
15 pass=$((pass + 1))
16 printf 'ok - %s\n' "$1"
17 }
18
19 bad() {
20 fail=$((fail + 1))
21 printf 'FAIL - %s\n' "$1"
22 if [ -n "${2:-}" ]; then
23 printf '%s\n' "$2" | sed 's/^/ /'
24 fi
25 }
26
27 work=$(mktemp -d "${TMPDIR:-/tmp}/codewhale-dev-cache.XXXXXX")
28 cleanup() { rm -rf "$work"; }
29 trap cleanup EXIT INT HUP TERM
30
31 HOME_DIR=$work/home
32 mkdir -p "$HOME_DIR"
33 FAKEBIN=$work/bin
34 mkdir -p "$FAKEBIN"
35 NEW_WT=$work/new-wt
36 OLD_WT=$work/old-wt
37 mkdir -p "$NEW_WT" "$OLD_WT/target"
38
39 # cargo/rustc/sccache are injected per case. Keep the base PATH hermetic.
40 BASE_PATH=$FAKEBIN:/usr/bin:/bin
41
42 run_apply() {
43 # shellcheck disable=SC2016 # $DEV_CACHE expands in the child.
44 env -i \
45 PATH="$BASE_PATH" \
46 HOME="$HOME_DIR" \
47 CODEWHALE_DEV_CACHE_QUIET=1 \
48 DEV_CACHE="$DEV_CACHE" \
49 "$@" \
50 /bin/sh -c '
51 set -eu
52 . "$DEV_CACHE"
53 codewhale_dev_cache_apply
54 codewhale_dev_cache_status
55 '
56 }
57
58 contains() {
59 printf '%s' "$1" | grep -qF -- "$2"
60 }
61
62 # 1. Portable default root is $HOME/.cache/codewhale, never a desk path.
63 out=$(run_apply CODEWHALE_DEV_CACHE=0 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT")
64 if contains "$out" "cache_root=$HOME_DIR/.cache/codewhale"; then
65 ok "default cache root is HOME/.cache/codewhale"
66 else
67 bad "default cache root is HOME/.cache/codewhale" "$out"
68 fi
69 if printf '%s\n' "$out" | grep -v '^repo_root=' | grep -qE '/Volumes/[^/]+/'; then
70 bad "default paths must not mention a host-specific /Volumes/ path" "$out"
71 else
72 ok "default paths do not mention a host-specific /Volumes/ path"
73 fi
74 if grep -E '/Users/[^/]+/|/Volumes/[^/]+/' "$DEV_CACHE" "$DEV_CARGO" >/dev/null; then
75 bad "helper source must not hard-code a desk path"
76 else
77 ok "helper source has no desk-local absolute paths"
78 fi
79
80 # 2. XDG_CACHE_HOME and CODEWHALE_CACHE_ROOT win in that documented order.
81 out=$(run_apply CODEWHALE_DEV_CACHE=0 XDG_CACHE_HOME="$work/xdg")
82 if contains "$out" "cache_root=$work/xdg/codewhale"; then
83 ok "XDG_CACHE_HOME/codewhale is the default when set"
84 else
85 bad "XDG_CACHE_HOME/codewhale is the default when set" "$out"
86 fi
87 out=$(run_apply CODEWHALE_DEV_CACHE=0 \
88 XDG_CACHE_HOME="$work/xdg" \
89 CODEWHALE_CACHE_ROOT="$work/explicit-root")
90 if contains "$out" "cache_root=$work/explicit-root"; then
91 ok "CODEWHALE_CACHE_ROOT overrides XDG and HOME"
92 else
93 bad "CODEWHALE_CACHE_ROOT overrides XDG and HOME" "$out"
94 fi
95
96 # 3. Disabled mode leaves Cargo/sccache vars alone.
97 out=$(run_apply CODEWHALE_DEV_CACHE=0)
98 if contains "$out" "mode=disabled" \
99 && contains "$out" "CARGO_BUILD_BUILD_DIR=<unset>" \
100 && contains "$out" "RUSTC_WRAPPER=<unset>"; then
101 ok "CODEWHALE_DEV_CACHE=0 does not set cargo or sccache vars"
102 else
103 bad "CODEWHALE_DEV_CACHE=0 does not set cargo or sccache vars" "$out"
104 fi
105
106 # 4. New worktree (no ./target) gets isolated build-dir with the cargo template.
107 out=$(run_apply \
108 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT" \
109 CODEWHALE_CACHE_ROOT="$work/cache")
110 if contains "$out" "mode=isolated-build-dir" \
111 && contains "$out" "CARGO_BUILD_BUILD_DIR=$work/cache/build/{workspace-path-hash}"; then
112 ok "new worktree uses isolated build-dir template"
113 else
114 bad "new worktree uses isolated build-dir template" "$out"
115 fi
116 if contains "$out" "CARGO_INCREMENTAL=<unset>"; then
117 ok "isolated default does not turn incremental off"
118 else
119 bad "isolated default does not turn incremental off" "$out"
120 fi
121 if contains "$out" "sccache=skipped-incremental"; then
122 ok "sccache stays off while incremental is on"
123 else
124 bad "sccache stays off while incremental is on" "$out"
125 fi
126
127 # 5. A stub or leftover ./target does not abandon isolation (Cargo still
128 # writes CACHEDIR.TAG under ./target when build-dir is split).
129 out=$(run_apply \
130 CODEWHALE_DEV_CACHE_REPO_ROOT="$OLD_WT" \
131 CODEWHALE_CACHE_ROOT="$work/cache")
132 if contains "$out" "mode=isolated-build-dir" \
133 && contains "$out" "CARGO_BUILD_BUILD_DIR=$work/cache/build/{workspace-path-hash}"; then
134 ok "existing ./target does not abandon isolated build-dir"
135 else
136 bad "existing ./target does not abandon isolated build-dir" "$out"
137 fi
138
139 # 6. CODEWHALE_DEV_CACHE=local keeps ./target.
140 out=$(run_apply \
141 CODEWHALE_DEV_CACHE=local \
142 CODEWHALE_DEV_CACHE_REPO_ROOT="$OLD_WT" \
143 CODEWHALE_CACHE_ROOT="$work/cache")
144 if contains "$out" "mode=existing-target" \
145 && contains "$out" "CARGO_BUILD_BUILD_DIR=<unset>"; then
146 ok "CODEWHALE_DEV_CACHE=local keeps ./target"
147 else
148 bad "CODEWHALE_DEV_CACHE=local keeps ./target" "$out"
149 fi
150
151 # 7. Already-set CARGO_TARGET_DIR / CARGO_BUILD_BUILD_DIR are respected.
152 out=$(run_apply \
153 CODEWHALE_DEV_CACHE=1 \
154 CARGO_TARGET_DIR="$work/user-target" \
155 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT")
156 if contains "$out" "mode=inherited-target-dir" \
157 && contains "$out" "CARGO_TARGET_DIR=$work/user-target" \
158 && contains "$out" "CARGO_BUILD_BUILD_DIR=<unset>"; then
159 ok "existing CARGO_TARGET_DIR is not overwritten"
160 else
161 bad "existing CARGO_TARGET_DIR is not overwritten" "$out"
162 fi
163 out=$(run_apply \
164 CARGO_BUILD_BUILD_DIR="$work/user-build" \
165 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT")
166 if contains "$out" "mode=inherited-build-dir" \
167 && contains "$out" "CARGO_BUILD_BUILD_DIR=$work/user-build"; then
168 ok "existing CARGO_BUILD_BUILD_DIR is not overwritten"
169 else
170 bad "existing CARGO_BUILD_BUILD_DIR is not overwritten" "$out"
171 fi
172
173 # 8. Missing sccache is a fallback, not a failure, even when requested.
174 out=$(run_apply \
175 CODEWHALE_SCCACHE=1 \
176 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT" \
177 CODEWHALE_CACHE_ROOT="$work/cache")
178 if contains "$out" "sccache=not-found" \
179 && contains "$out" "RUSTC_WRAPPER=<unset>" \
180 && contains "$out" "CARGO_INCREMENTAL=0"; then
181 ok "missing sccache falls back without setting RUSTC_WRAPPER"
182 else
183 bad "missing sccache falls back without setting RUSTC_WRAPPER" "$out"
184 fi
185
186 # 9. sccache wraps only when incremental is off and the binary exists.
187 printf '%s\n' '#!/bin/sh' 'exit 0' >"$FAKEBIN/sccache"
188 chmod +x "$FAKEBIN/sccache"
189
190 out=$(run_apply \
191 CARGO_INCREMENTAL=0 \
192 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT" \
193 CODEWHALE_CACHE_ROOT="$work/cache" \
194 CODEWHALE_RUSTC_COMMIT=abc123deadbeef)
195 if contains "$out" "sccache=enabled" \
196 && contains "$out" "RUSTC_WRAPPER=$FAKEBIN/sccache" \
197 && contains "$out" "SCCACHE_DIR=$work/cache/sccache/abc123deadbeef"; then
198 ok "sccache wraps rustc when incremental is off"
199 else
200 bad "sccache wraps rustc when incremental is off" "$out"
201 fi
202
203 out=$(run_apply \
204 CARGO_INCREMENTAL=1 \
205 CODEWHALE_SCCACHE=1 \
206 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT" \
207 CODEWHALE_CACHE_ROOT="$work/cache")
208 if contains "$out" "sccache=skipped-incremental" \
209 && contains "$out" "RUSTC_WRAPPER=<unset>" \
210 && contains "$out" "CARGO_INCREMENTAL=1"; then
211 ok "explicit incremental=1 wins over CODEWHALE_SCCACHE=1"
212 else
213 bad "explicit incremental=1 wins over CODEWHALE_SCCACHE=1" "$out"
214 fi
215
216 out=$(run_apply \
217 CODEWHALE_SCCACHE=1 \
218 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT" \
219 CODEWHALE_CACHE_ROOT="$work/cache" \
220 CODEWHALE_RUSTC_COMMIT=abc123deadbeef)
221 if contains "$out" "sccache=enabled" \
222 && contains "$out" "CARGO_INCREMENTAL=0"; then
223 ok "CODEWHALE_SCCACHE=1 requests incremental=0 and wraps"
224 else
225 bad "CODEWHALE_SCCACHE=1 requests incremental=0 and wraps" "$out"
226 fi
227
228 out=$(run_apply \
229 RUSTC_WRAPPER=/usr/bin/true \
230 CARGO_INCREMENTAL=0 \
231 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT")
232 if contains "$out" "sccache=external-wrapper" \
233 && contains "$out" "RUSTC_WRAPPER=/usr/bin/true"; then
234 ok "existing RUSTC_WRAPPER is left alone"
235 else
236 bad "existing RUSTC_WRAPPER is left alone" "$out"
237 fi
238
239 # 10. Cargo older than 1.91 falls back to a per-workspace CARGO_TARGET_DIR.
240 printf '%s\n' '#!/bin/sh' 'echo "cargo 1.88.0 (test)"' >"$FAKEBIN/cargo"
241 chmod +x "$FAKEBIN/cargo"
242 out=$(run_apply \
243 CODEWHALE_DEV_CACHE=1 \
244 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT" \
245 CODEWHALE_CACHE_ROOT="$work/cache")
246 if contains "$out" "legacy-target-dir" \
247 && contains "$out" "CARGO_BUILD_BUILD_DIR=<unset>" \
248 && contains "$out" "CARGO_TARGET_DIR=$work/cache/target/"; then
249 ok "cargo 1.88 falls back to an isolated CARGO_TARGET_DIR"
250 else
251 bad "cargo 1.88 falls back to an isolated CARGO_TARGET_DIR" "$out"
252 fi
253 rm -f "$FAKEBIN/cargo"
254
255 # 11. CLI self-check passes for a new worktree with portable HOME.
256 cli_out=$(
257 env -i \
258 PATH="$BASE_PATH" \
259 HOME="$HOME_DIR" \
260 PWD="$NEW_WT" \
261 CODEWHALE_DEV_CACHE_QUIET=1 \
262 CODEWHALE_DEV_CACHE_REPO_ROOT="$NEW_WT" \
263 CODEWHALE_CACHE_ROOT="$work/cache" \
264 /bin/sh "$DEV_CACHE" --self-check
265 ) || cli_rc=$?
266 cli_rc=${cli_rc:-0}
267 if [ "$cli_rc" -eq 0 ] && contains "$cli_out" "dev-cache: self-check ok"; then
268 ok "CLI --self-check passes on a new worktree"
269 else
270 bad "CLI --self-check passes on a new worktree" "$cli_out"
271 fi
272
273 # 12. Two worktree paths produce different legacy fingerprints.
274 # shellcheck source=scripts/dev-cache.sh
275 . "$DEV_CACHE"
276 fp_a=$(codewhale_dev_cache_path_fingerprint "$NEW_WT")
277 fp_b=$(codewhale_dev_cache_path_fingerprint "$OLD_WT")
278 if [ -n "$fp_a" ] && [ -n "$fp_b" ] && [ "$fp_a" != "$fp_b" ]; then
279 ok "path fingerprints differ across worktrees"
280 else
281 bad "path fingerprints differ across worktrees" "a=$fp_a b=$fp_b"
282 fi
283
284 # 13. dev-cargo.sh --status is the same helper.
285 if /bin/sh "$DEV_CARGO" --help | grep -q 'dev-cache'; then
286 ok "dev-cargo.sh --help delegates to the cache helper"
287 else
288 bad "dev-cargo.sh --help delegates to the cache helper"
289 fi
290
291 # 14. The documented eval form preserves paths with spaces and apostrophes.
292 cache_target="$work/developer's target"
293 shared_cache="$work/shared cache"
294 exports=$(env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
295 CODEWHALE_DEV_CACHE_QUIET=1 CODEWHALE_SCCACHE=0 \
296 CARGO_TARGET_DIR="$cache_target" SCCACHE_DIR="$shared_cache" \
297 "$DEV_CACHE" --print-exports)
298 if out=$(env -i PATH="$BASE_PATH" HOME="$HOME_DIR" /bin/sh -euc '
299 eval "$1"
300 test "$CARGO_TARGET_DIR" = "$2"
301 test "$SCCACHE_DIR" = "$3"
302 ' sh "$exports" "$cache_target" "$shared_cache" 2>&1); then
303 ok "--print-exports preserves cache paths through a fresh shell"
304 else
305 bad "--print-exports preserves cache paths through a fresh shell" "$out"
306 fi
307
308 # 15. Test runs compile under the build lock and run their binaries without it.
309 cargo_log=$work/cargo-calls.log
310 cargo_fail=$work/cargo-fail-no-run
311 cat >"$FAKEBIN/cargo" <<'EOF'
312 #!/bin/sh
313 printf 'held=%s args=%s\n' "${CODEWHALE_BUILD_LOCK_HELD:-0}" "$*" >>"$CARGO_LOG"
314 case " $* " in *" --no-run "*) [ ! -e "$CARGO_FAIL" ] || exit 101 ;; esac
315 EOF
316 chmod +x "$FAKEBIN/cargo"
317 run_exec() {
318 rm -f "$cargo_log"
319 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
320 CARGO_LOG="$cargo_log" CARGO_FAIL="$cargo_fail" \
321 CODEWHALE_BUILD_LOCK_FILE="$work/lock/build.lock" \
322 repo_root="$repo_root" DEV_CACHE="$DEV_CACHE" \
323 /bin/sh -c ". \"\$DEV_CACHE\"; codewhale_dev_cache_exec_cargo \"\$@\"" sh "$@"
324 }
325 expect_calls() {
326 if [ "$(cat "$cargo_log" 2>/dev/null)" = "$2" ]; then
327 ok "$1"
328 else
329 bad "$1" "$(cat "$cargo_log" 2>/dev/null)"
330 fi
331 }
332 run_exec test -p codewhale-x --lib -- --nocapture
333 expect_calls "cargo test compiles locked, then runs unlocked" "$(printf '%s\n' \
334 'held=1 args=test -p codewhale-x --lib --no-run -- --nocapture' \
335 'held=0 args=test -p codewhale-x --lib -- --nocapture')"
336 run_exec nextest run -p codewhale-x -E 'test(a)'
337 norm_log=$(sed -E 's|[^ ]+/cw-nextest-[^/]+/|<TMP>/|g' "$cargo_log" 2>/dev/null || true)
338 expected_nx=$(printf '%s\n' \
339 'held=1 args=metadata --format-version 1' \
340 'held=1 args=nextest list --list-type binaries-only --message-format json --cargo-metadata <TMP>/cargo-metadata.json -p codewhale-x' \
341 'held=0 args=nextest run --cargo-metadata <TMP>/cargo-metadata.json --binaries-metadata <TMP>/binaries-metadata.json -E test(a)')
342 if [ "$norm_log" = "$expected_nx" ]; then
343 ok "nextest run compiles locked via binaries-metadata, then runs unlocked without cargo build"
344 else
345 bad "nextest run compiles locked via binaries-metadata, then runs unlocked without cargo build" "$norm_log"
346 fi
347 run_exec check -p codewhale-x
348 expect_calls "cargo check stays one locked call" 'held=1 args=check -p codewhale-x'
349 run_exec test --doc -p codewhale-x
350 expect_calls "doctests stay one locked call" 'held=1 args=test --doc -p codewhale-x'
351 touch "$cargo_fail"
352 status=0
353 run_exec test -p codewhale-x || status=$?
354 rm -f "$cargo_fail"
355 if [ "$status" -eq 101 ]; then
356 expect_calls "a failed test compile stops before the unlocked run" 'held=1 args=test -p codewhale-x --no-run'
357 else
358 bad "a failed test compile stops before the unlocked run" "exit $status"
359 fi
360
361 # 16. Companion .cw-owner marker write on isolated build-dir.
362 resolved_dir="$work/cache/build/ab/1234567890abcdef"
363 cat >"$FAKEBIN/cargo" <<'EOF'
364 #!/bin/sh
365 if [ "${1:-}" = metadata ]; then
366 printf '{"build_directory":"%s"}\n' "$MOCK_BUILD_DIR"
367 exit 0
368 fi
369 printf 'args=%s\n' "$*" >>"$CARGO_LOG"
370 EOF
371 chmod +x "$FAKEBIN/cargo"
372
373 owner_wt="$work/owner-wt"
374 mkdir -p "$owner_wt"
375 owner_canon=$(CDPATH='' cd -- "$owner_wt" && pwd -P)
376
377 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
378 MOCK_BUILD_DIR="$resolved_dir" \
379 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
380 CARGO_BUILD_BUILD_DIR="$work/cache/build/{workspace-path-hash}" \
381 DEV_CACHE="$DEV_CACHE" \
382 /bin/sh -c '. "$DEV_CACHE"; codewhale_dev_cache_write_owner'
383
384 if [ -f "$resolved_dir/.cw-owner" ] \
385 && [ "$(sed -n '1p' "$resolved_dir/.cw-owner")" = "$owner_canon" ] \
386 && [ -z "$(find "$resolved_dir" -name '.cw-owner.tmp.*' 2>/dev/null)" ]; then
387 ok ".cw-owner writes canonical workspace root atomically"
388 else
389 bad ".cw-owner writes canonical workspace root atomically" "$(cat "$resolved_dir/.cw-owner" 2>/dev/null || true)"
390 fi
391
392 printf '%s\nkeep-stamp\n' "$owner_canon" >"$resolved_dir/.cw-owner"
393 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
394 MOCK_BUILD_DIR="$resolved_dir" \
395 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
396 CARGO_BUILD_BUILD_DIR="$work/cache/build/{workspace-path-hash}" \
397 DEV_CACHE="$DEV_CACHE" \
398 /bin/sh -c '. "$DEV_CACHE"; codewhale_dev_cache_write_owner'
399 if [ "$(sed -n '2p' "$resolved_dir/.cw-owner")" = "keep-stamp" ]; then
400 ok ".cw-owner is not rewritten when already matching"
401 else
402 bad ".cw-owner is not rewritten when already matching" "$(cat "$resolved_dir/.cw-owner" 2>/dev/null || true)"
403 fi
404
405 printf '/stale/old/worktree\nold-stamp\n' >"$resolved_dir/.cw-owner"
406 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
407 MOCK_BUILD_DIR="$resolved_dir" \
408 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
409 CARGO_BUILD_BUILD_DIR="$work/cache/build/{workspace-path-hash}" \
410 DEV_CACHE="$DEV_CACHE" \
411 /bin/sh -c '. "$DEV_CACHE"; codewhale_dev_cache_write_owner'
412 if [ "$(sed -n '1p' "$resolved_dir/.cw-owner")" = "$owner_canon" ] \
413 && [ "$(sed -n '2p' "$resolved_dir/.cw-owner")" != "old-stamp" ]; then
414 ok "stale .cw-owner is atomically updated"
415 else
416 bad "stale .cw-owner is atomically updated" "$(cat "$resolved_dir/.cw-owner" 2>/dev/null || true)"
417 fi
418
419 sym_dir="$work/cache/build/cd/0000000000000000"
420 sym_target="$work/do-not-touch"
421 mkdir -p "$sym_dir"
422 printf 'keep-me\n' >"$sym_target"
423 ln -s "$sym_target" "$sym_dir/.cw-owner"
424 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
425 MOCK_BUILD_DIR="$sym_dir" \
426 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
427 CARGO_BUILD_BUILD_DIR="$work/cache/build/{workspace-path-hash}" \
428 DEV_CACHE="$DEV_CACHE" \
429 /bin/sh -c '. "$DEV_CACHE"; codewhale_dev_cache_write_owner'
430 if [ -L "$sym_dir/.cw-owner" ] && [ "$(cat "$sym_target")" = "keep-me" ]; then
431 ok "symlink .cw-owner is never overwritten"
432 else
433 bad "symlink .cw-owner is never overwritten" "$(cat "$sym_target" 2>/dev/null || true)"
434 fi
435
436 unexpanded_dir="$work/cache/build/{workspace-path-hash}"
437 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
438 MOCK_BUILD_DIR="$unexpanded_dir" \
439 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
440 CARGO_BUILD_BUILD_DIR="$unexpanded_dir" \
441 DEV_CACHE="$DEV_CACHE" \
442 /bin/sh -c '. "$DEV_CACHE"; codewhale_dev_cache_write_owner'
443 if [ ! -e "$unexpanded_dir/.cw-owner" ]; then
444 ok "unexpanded {workspace-path-hash} does not write .cw-owner"
445 else
446 bad "unexpanded {workspace-path-hash} does not write .cw-owner"
447 fi
448
449 rm -rf "$resolved_dir" "$cargo_log"
450 exec_dir="$work/cache/build/ef/fedcba0987654321"
451 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
452 MOCK_BUILD_DIR="$exec_dir" \
453 CARGO_LOG="$cargo_log" \
454 CODEWHALE_DEV_CACHE_QUIET=1 \
455 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
456 CODEWHALE_CACHE_ROOT="$work/cache" \
457 CODEWHALE_BUILD_LOCK=0 \
458 repo_root="$repo_root" \
459 DEV_CACHE="$DEV_CACHE" \
460 /bin/sh -c '
461 set -eu
462 . "$DEV_CACHE"
463 codewhale_dev_cache_apply
464 codewhale_dev_cache_exec_cargo check -p codewhale-x
465 '
466 if [ -f "$exec_dir/.cw-owner" ] \
467 && [ "$(sed -n '1p' "$exec_dir/.cw-owner")" = "$owner_canon" ] \
468 && contains "$(cat "$cargo_log" 2>/dev/null || true)" "build.build-dir = \"$work/cache/build/{workspace-path-hash}\""; then
469 ok "exec_cargo in isolated-build-dir writes .cw-owner and passes build-dir config"
470 else
471 bad "exec_cargo in isolated-build-dir writes .cw-owner and passes build-dir config" "$(cat "$cargo_log" 2>/dev/null || true)"
472 fi
473
474 cat >"$FAKEBIN/cargo" <<'EOF'
475 #!/bin/sh
476 if [ "${1:-}" = --version ]; then
477 echo "cargo 1.95.0 (test)"
478 exit 0
479 fi
480 if [ "${1:-}" = metadata ] && [ "${2:-}" = --no-deps ]; then
481 exit 0
482 fi
483 printf 'cw=%s held=%s args=%s\n' "${CARGOWHALE:-unset}" "${CODEWHALE_BUILD_LOCK_HELD:-0}" "$*" >>"$CARGO_LOG"
484 EOF
485 chmod +x "$FAKEBIN/cargo"
486
487 rm -f "$cargo_log"
488 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
489 CARGO_LOG="$cargo_log" \
490 CODEWHALE_DEV_CACHE_QUIET=1 \
491 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
492 CODEWHALE_CACHE_ROOT="$work/cache" \
493 CODEWHALE_BUILD_LOCK=0 \
494 repo_root="$repo_root" \
495 DEV_CACHE="$DEV_CACHE" \
496 /bin/sh -c '
497 set -eu
498 . "$DEV_CACHE"
499 codewhale_dev_cache_apply
500 codewhale_dev_cache_exec_cargo nextest run -p codewhale-x --lib -E "test(a)"
501 '
502 unlocked_nx=$(cat "$cargo_log" 2>/dev/null || true)
503 expected_unlocked="cw=off held=0 args=nextest run --config build.build-dir = \"$work/cache/build/{workspace-path-hash}\" -p codewhale-x --lib -E test(a)"
504 if [ "$unlocked_nx" = "$expected_unlocked" ]; then
505 ok "unlocked fallback exports CARGOWHALE=off and puts --config after nextest run"
506 else
507 bad "unlocked fallback exports CARGOWHALE=off and puts --config after nextest run" "$unlocked_nx"
508 fi
509
510 rm -f "$cargo_log"
511 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
512 CARGO_LOG="$cargo_log" \
513 CODEWHALE_DEV_CACHE_QUIET=1 \
514 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
515 CODEWHALE_CACHE_ROOT="$work/cache" \
516 CODEWHALE_BUILD_LOCK_FILE="$work/lock/build.lock" \
517 repo_root="$repo_root" \
518 DEV_CACHE="$DEV_CACHE" \
519 /bin/sh -c '
520 set -eu
521 . "$DEV_CACHE"
522 codewhale_dev_cache_apply
523 codewhale_dev_cache_exec_cargo nextest run -p codewhale-x --lib --locked --retries 2 -E "test(a)" -- --nocapture
524 '
525 split_nx=$(sed -E 's|[^ ]+/cw-nextest-[^/]+/|<TMP>/|g' "$cargo_log" 2>/dev/null || true)
526 cfg_arg="build.build-dir = \"$work/cache/build/{workspace-path-hash}\""
527 expected_split=$(printf '%s\n' \
528 "cw=off held=1 args=metadata --format-version 1 --config $cfg_arg --locked" \
529 "cw=off held=1 args=nextest list --list-type binaries-only --message-format json --cargo-metadata <TMP>/cargo-metadata.json --config $cfg_arg -p codewhale-x --lib --locked" \
530 "cw=off held=0 args=nextest run --cargo-metadata <TMP>/cargo-metadata.json --binaries-metadata <TMP>/binaries-metadata.json --config $cfg_arg --retries 2 -E test(a) -- --nocapture")
531 if [ "$split_nx" = "$expected_split" ]; then
532 ok "isolated nextest split passes --config and strips build flags from stage 2"
533 else
534 bad "isolated nextest split passes --config and strips build flags from stage 2" "$split_nx"
535 fi
536
537 rm -f "$cargo_log"
538 env -i PATH="$BASE_PATH" HOME="$HOME_DIR" \
539 CARGO_LOG="$cargo_log" \
540 CODEWHALE_DEV_CACHE_QUIET=1 \
541 CODEWHALE_DEV_CACHE_REPO_ROOT="$owner_wt" \
542 CODEWHALE_CACHE_ROOT="$work/cache" \
543 CODEWHALE_BUILD_LOCK_FILE="$work/lock/build.lock" \
544 repo_root="$repo_root" \
545 DEV_CACHE="$DEV_CACHE" \
546 /bin/sh -c '
547 set -eu
548 . "$DEV_CACHE"
549 codewhale_dev_cache_apply
550 codewhale_dev_cache_exec_cargo nextest run -p codewhale-x --unknown-future-flag
551 '
552 fallback_nx=$(cat "$cargo_log" 2>/dev/null || true)
553 expected_fallback=$(printf '%s\n' \
554 "cw=off held=1 args=nextest run --config $cfg_arg -p codewhale-x --unknown-future-flag --no-run" \
555 "cw=off held=0 args=nextest run --config $cfg_arg -p codewhale-x --unknown-future-flag")
556 if [ "$fallback_nx" = "$expected_fallback" ]; then
557 ok "unrecognized nextest flag falls back cleanly with --config after nextest run"
558 else
559 bad "unrecognized nextest flag falls back cleanly with --config after nextest run" "$fallback_nx"
560 fi
561
562 for sig in INT TERM; do
563 grandchild_pid_file="$work/grandchild-${sig}.pid"
564 rm -f "$grandchild_pid_file"
565 python3 "$repo_root/scripts/build-lock.py" "$work/lock/sig-${sig}.lock" -- \
566 python3 -c '
567 import signal, subprocess, sys, time
568 signal.signal(signal.SIGINT, signal.default_int_handler)
569 p = subprocess.Popen([sys.executable, "-c", "import signal, time; signal.signal(signal.SIGINT, signal.default_int_handler); time.sleep(30)"])
570 with open(sys.argv[1], "w") as f:
571 f.write(str(p.pid))
572 p.wait()
573 ' "$grandchild_pid_file" 2>/dev/null &
574 locker_pid=$!
575 i=0
576 while [ ! -s "$grandchild_pid_file" ] && [ "$i" -lt 50 ]; do
577 sleep 0.1
578 i=$((i + 1))
579 done
580 grandchild_pid=$(cat "$grandchild_pid_file" 2>/dev/null || true)
581 kill -"$sig" "$locker_pid" 2>/dev/null || true
582 wait "$locker_pid" 2>/dev/null || true
583 sleep 0.1
584 if [ -n "$grandchild_pid" ] && ! kill -0 "$grandchild_pid" 2>/dev/null; then
585 ok "build-lock.py forwards SIG${sig} to child process group"
586 else
587 kill -KILL "$grandchild_pid" 2>/dev/null || true
588 bad "build-lock.py forwards SIG${sig} to child process group" "grandchild_pid=${grandchild_pid:-missing}"
589 fi
590 done
591
592 if [ "$fail" -eq 0 ]; then
593 printf 'dev-cache.test.sh: all %s checks passed\n' "$pass"
594 exit 0
595 fi
596 printf 'dev-cache.test.sh: %s/%s checks failed\n' "$fail" "$((pass + fail))" >&2
597 exit 1
598
598 lines BASH