返回 CodeWhale
dev-test.sh
根目录 / scripts / dev-test.sh
1 #!/bin/sh
2 # Map a workspace area or source path to the fastest cargo/nextest
3 # invocation for that area, and apply the portable cache topology so a
4 # new worktree actually gets isolated build-dir (+ sccache only when
5 # incremental is already off). Tests run under the shared temporary HOME
6 # boundary; compiler caches and toolchain homes remain persistent.
7 # A libtest run with an explicit filter refuses green when the filter
8 # matches zero tests (nextest already fails loud on empty selections).
9 #
10 # Usage:
11 # scripts/dev-test.sh <area|path> [filter...]
12 # scripts/dev-test.sh --list
13 # scripts/dev-test.sh --status
14 #
15 # Examples:
16 # scripts/dev-test.sh config
17 # scripts/dev-test.sh command-contract elapsed::
18 # scripts/dev-test.sh crates/command-contract/src/elapsed.rs
19 #
20 # Environment:
21 # CODEWHALE_DEV_NEXTEST auto|1|0 (default auto: use cargo-nextest when
22 # it is on PATH; 0 forces cargo test)
23 # Cache knobs are documented in scripts/dev-cache.sh.
24 set -eu
25
26 repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
27 cd "$repo_root"
28
29 # shellcheck source=scripts/dev-cache.sh
30 . "$repo_root/scripts/dev-cache.sh"
31
32 usage() {
33 printf '%s\n' "usage: scripts/dev-test.sh <area|path> [filter...]" >&2
34 printf '%s\n' " scripts/dev-test.sh --list|--status|--self-check" >&2
35 exit 2
36 }
37
38 list_areas() {
39 cat <<'EOF'
40 area command
41 ---- -------
42 account-cli cargo test -p codewhale-account-cli --lib --locked
43 agent-contract cargo test -p codewhale-agent-contract --lib --locked
44 app-server cargo test -p codewhale-app-server --lib --locked
45 build-support cargo test -p codewhale-build-support --lib --locked
46 cli cargo test -p codewhale-cli --lib --locked
47 command-contract cargo test -p codewhale-command-contract --lib --locked
48 commands cargo test -p codewhale-commands --lib --locked
49 config cargo test -p codewhale-config --lib --locked
50 core cargo test -p codewhale-core --lib --locked
51 engine cargo test -p codewhale-engine --lib --locked
52 error-taxonomy cargo test -p codewhale-error-taxonomy --lib --locked
53 execpolicy cargo test -p codewhale-execpolicy --lib --locked
54 hooks cargo test -p codewhale-hooks --lib --locked
55 lane cargo test -p codewhale-lane --lib --locked
56 mcp cargo test -p codewhale-mcp --lib --locked
57 models cargo test -p codewhale-models --lib --locked
58 net cargo test -p codewhale-net --lib --locked
59 notify cargo test -p codewhale-notify --lib --locked
60 paths cargo test -p codewhale-paths --lib --locked
61 portable-config cargo test -p codewhale-portable-config --lib --locked
62 portable-core-navigation cargo test -p codewhale-portable-core-navigation --lib --locked
63 protocol cargo test -p codewhale-protocol --lib --locked
64 release cargo test -p codewhale-release --lib --locked
65 runtime cargo test -p codewhale-runtime --lib --locked
66 sanitize cargo test -p codewhale-sanitize --lib --locked
67 secrets cargo test -p codewhale-secrets --lib --locked
68 state cargo test -p codewhale-state --lib --locked
69 telemetry cargo test -p codewhale-telemetry --lib --locked
70 tools cargo test -p codewhale-tools --lib --locked
71 tui cargo test -p codewhale-tui --lib --locked
72 tui-kit cargo test -p codewhale-tui-kit --lib --locked
73 skill-install cargo test -p codewhale-skill-install --lib --locked
74 skills cargo test -p codewhale-skills --lib --locked
75 plugins cargo test -p codewhale-plugins --lib --locked
76 util cargo test -p codewhale-util --lib --locked
77 dependencies cargo test -p codewhale-dependencies --lib --locked
78 snapshot cargo test -p codewhale-snapshot --lib --locked
79 lsp cargo test -p codewhale-lsp --lib --locked
80 repl cargo test -p codewhale-repl --lib --locked
81 tui-integration cargo test -p codewhale-tui --test integration --locked
82 tui-cucumber cargo test -p codewhale-tui --test cucumber --locked
83 workflow cargo test -p codewhale-workflow --lib --locked
84 workflow-js cargo test -p codewhale-workflow-js --lib --locked
85
86 path prefix area / extra filter
87 ----------- -------------------
88 crates/<crate>/ <crate>
89 crates/tui/src/tui/ tui tui::
90 crates/engine/src/tools/ engine tools::
91 crates/engine/src/core/ engine core::
92 crates/tui/src/commands/ tui commands::
93 crates/tui/src/<file>.rs tui <file>::
94 crates/runtime/src/<file>.rs runtime <file>::
95 crates/tui/tests/integration/ tui-integration <stem>
96 crates/tui/tests/cucumber/ tui-cucumber <stem>
97 crates/tui/tests/ tui-integration
98
99 When cargo-nextest is on PATH, the run stage is `cargo nextest run`
100 instead of `cargo test` (same binaries; process per test). Set
101 CODEWHALE_DEV_NEXTEST=0 to force libtest. New worktrees get an isolated
102 Cargo build-dir via scripts/dev-cache.sh; sccache wraps rustc only when
103 incremental is already off. Test HOME and config paths are isolated by
104 scripts/with-hermetic-test-home.sh. Do not use cargo test --workspace for a
105 single-area edit. --lib and --tests are disjoint; a green --lib run does
106 not cover crates/tui/tests/.
107 EOF
108 }
109
110 [ $# -ge 1 ] || usage
111
112 if [ "$1" = "--list" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
113 list_areas
114 exit 0
115 fi
116
117 if [ "$1" = "--status" ] || [ "$1" = "--self-check" ]; then
118 exec "$repo_root/scripts/dev-cache.sh" "$1"
119 fi
120
121 area=$1
122 shift
123
124 # Path form: map a source path onto an area, and invent a filter only when
125 # the caller did not already pass one.
126 if [ "$area" != plugins ] && [ -e "$area" ] || printf '%s' "$area" | grep -q /; then
127 rel=${area#"$repo_root"/}
128 rel=${rel#./}
129 extra=
130 case $rel in
131 tests/portable-config/*|tests/portable-config)
132 area=portable-config
133 ;;
134 tests/portable-core-navigation/*|tests/portable-core-navigation)
135 area=portable-core-navigation
136 ;;
137 crates/command-contract/src/navigation.rs)
138 area=command-contract
139 ;;
140 crates/tui/tests/cucumber|crates/tui/tests/cucumber/)
141 area=tui-cucumber
142 ;;
143 crates/tui/tests/cucumber/*)
144 area=tui-cucumber
145 extra=$(basename "$rel" .rs)
146 ;;
147 crates/tui/tests|crates/tui/tests/|crates/tui/tests/integration|crates/tui/tests/integration/)
148 area=tui-integration
149 ;;
150 crates/tui/tests/*)
151 area=tui-integration
152 extra=$(basename "$rel" .rs)
153 ;;
154 crates/tui/src/tui/*)
155 area=tui
156 extra=tui::
157 ;;
158 crates/engine/src/tools/*)
159 area=engine
160 extra=tools::
161 ;;
162 crates/engine/src/core/*)
163 area=engine
164 extra=core::
165 ;;
166 crates/tui/src/commands/*)
167 area=tui
168 extra=commands::
169 ;;
170 crates/tui/src/*)
171 area=tui
172 extra=$(basename "$rel" .rs)::
173 ;;
174 crates/tui/*|crates/tui)
175 area=tui
176 ;;
177 crates/engine/src/*|crates/runtime/src/*|crates/commands/src/*|crates/command-contract/src/*)
178 area=$(printf '%s' "$rel" | awk -F/ '{print $2}')
179 extra=$(printf '%s' "${rel#crates/*/src/}" | awk -F/ '{print $1}')
180 extra=${extra%.rs}::
181 ;;
182 crates/*)
183 crate=$(printf '%s' "$rel" | awk -F/ '{print $2}')
184 area=$crate
185 ;;
186 *)
187 printf '%s\n' "dev-test: no area mapping for path: $area" >&2
188 exit 2
189 ;;
190 esac
191 case $extra in
192 ''|main|main::|lib::|mod|mod::) extra= ;;
193 esac
194 if [ $# -eq 0 ] && [ -n "${extra:-}" ]; then
195 set -- "$extra"
196 fi
197 fi
198
199 pkg=
200 target=--lib
201 case $area in
202 account-cli) pkg=codewhale-account-cli ;;
203 agent-contract) pkg=codewhale-agent-contract ;;
204 app-server) pkg=codewhale-app-server ;;
205 build-support) pkg=codewhale-build-support ;;
206 cli) pkg=codewhale-cli ;;
207 command-contract) pkg=codewhale-command-contract ;;
208 commands) pkg=codewhale-commands ;;
209 config) pkg=codewhale-config ;;
210 core) pkg=codewhale-core ;;
211 engine) pkg=codewhale-engine ;;
212 error-taxonomy) pkg=codewhale-error-taxonomy ;;
213 execpolicy) pkg=codewhale-execpolicy ;;
214 hooks) pkg=codewhale-hooks ;;
215 lane) pkg=codewhale-lane ;;
216 mcp) pkg=codewhale-mcp ;;
217 models) pkg=codewhale-models ;;
218 net) pkg=codewhale-net ;;
219 notify) pkg=codewhale-notify ;;
220 paths) pkg=codewhale-paths ;;
221 portable-config) pkg=codewhale-portable-config ;;
222 portable-core-navigation) pkg=codewhale-portable-core-navigation ;;
223 protocol) pkg=codewhale-protocol ;;
224 release) pkg=codewhale-release ;;
225 runtime) pkg=codewhale-runtime ;;
226 sanitize) pkg=codewhale-sanitize ;;
227 secrets) pkg=codewhale-secrets ;;
228 state) pkg=codewhale-state ;;
229 telemetry) pkg=codewhale-telemetry ;;
230 tools) pkg=codewhale-tools ;;
231 tui) pkg=codewhale-tui ;;
232 tui-kit) pkg=codewhale-tui-kit ;;
233 skill-install) pkg=codewhale-skill-install ;;
234 skills) pkg=codewhale-skills ;;
235 plugins) pkg=codewhale-plugins ;;
236 util) pkg=codewhale-util ;;
237 dependencies) pkg=codewhale-dependencies ;;
238 snapshot) pkg=codewhale-snapshot ;;
239 lsp) pkg=codewhale-lsp ;;
240 repl) pkg=codewhale-repl ;;
241 workflow) pkg=codewhale-workflow ;;
242 workflow-js) pkg=codewhale-workflow-js ;;
243 tui-integration)
244 pkg=codewhale-tui
245 target=--test
246 harness=integration
247 ;;
248 tui-cucumber)
249 pkg=codewhale-tui
250 target=--test
251 harness=cucumber
252 ;;
253 *)
254 printf '%s\n' "dev-test: unknown area: $area (try --list)" >&2
255 exit 2
256 ;;
257 esac
258
259 use_nextest=0
260 _cw_nextest=${CODEWHALE_DEV_NEXTEST:-auto}
261 if codewhale_dev_cache_falsey "$_cw_nextest"; then
262 use_nextest=0
263 elif command -v cargo-nextest >/dev/null 2>&1; then
264 use_nextest=1
265 elif codewhale_dev_cache_truthy "$_cw_nextest"; then
266 printf '%s\n' "dev-test: CODEWHALE_DEV_NEXTEST=${_cw_nextest} but cargo-nextest is not on PATH" >&2
267 exit 2
268 fi
269
270 if [ "$target" = "--test" ]; then
271 if [ "$use_nextest" -eq 1 ]; then
272 set -- nextest run -p "$pkg" --test "$harness" --locked "$@"
273 else
274 set -- test -p "$pkg" --test "$harness" --locked "$@"
275 fi
276 else
277 if [ "$use_nextest" -eq 1 ]; then
278 set -- nextest run -p "$pkg" --lib --locked "$@"
279 else
280 set -- test -p "$pkg" --lib --locked "$@"
281 fi
282 fi
283
284 printf '+ cargo %s\n' "$*"
285 # Resolve the persistent cache before replacing HOME; dev-cargo applies the
286 # topology once, retaining Cargo's build-dir template and any caller overrides.
287 CODEWHALE_CACHE_ROOT=$(codewhale_dev_cache_root)
288 export CODEWHALE_CACHE_ROOT
289 # Engine acceptance launches the canonical CLI, which is not a binary target
290 # of the Engine library package. Build it with the same cache/lock topology.
291 if [ "$target" = "--test" ]; then
292 "$repo_root/scripts/with-hermetic-test-home.sh" "$repo_root/scripts/dev-cargo.sh" \
293 build -p codewhale-cli --bin codewhale --locked
294 fi
295 # libtest exits 0 when a filter matches nothing, which has been mistaken for
296 # a pass. With an explicit filter, refuse that green. (nextest already fails
297 # loud on an empty selection, so the guard only wraps libtest.)
298 #
299 # pipefail is not POSIX and probing it outside a subshell is fatal where it
300 # is unsupported: `set` is a special builtin, so an illegal option exits the
301 # shell outright with status 2 instead of returning a status a `&&` list can
302 # absorb. That killed this script on every filtered libtest run under dash
303 # (Ubuntu's /bin/sh, which is what CI and Debian users get) while passing on
304 # macOS. Probe in a subshell, and on shells without pipefail capture the run
305 # and replay it so the refusal below still applies; those shells lose live
306 # streaming for the duration of the filtered run, not the guard.
307 base_args=5
308 if [ "$target" = "--test" ]; then
309 base_args=6
310 fi
311 if [ "$use_nextest" -eq 0 ] && [ "$#" -gt "$base_args" ]; then
312 tmp_log=$(mktemp -t dev-test-log.XXXXXX)
313 trap 'rm -f "$tmp_log"' EXIT INT TERM
314 set +e
315 if (set -o pipefail) 2>/dev/null; then
316 set -o pipefail
317 "$repo_root/scripts/with-hermetic-test-home.sh" "$repo_root/scripts/dev-cargo.sh" "$@" 2>&1 | tee "$tmp_log"
318 test_status=$?
319 else
320 "$repo_root/scripts/with-hermetic-test-home.sh" "$repo_root/scripts/dev-cargo.sh" "$@" > "$tmp_log" 2>&1
321 test_status=$?
322 cat "$tmp_log"
323 fi
324 set -e
325 if [ "$test_status" -eq 0 ]; then
326 if grep -q 'test result:' "$tmp_log"; then
327 if grep 'test result:' "$tmp_log" | grep -Eqv '(^|[^0-9])0 passed;'; then
328 : # at least one binary ran tests
329 else
330 printf '%s\n' "dev-test: filter matched zero tests (every binary reports 0 passed); refusing green." >&2
331 test_status=1
332 fi
333 else
334 printf '%s\n' "dev-test: no 'test result:' lines in output; refusing green." >&2
335 test_status=1
336 fi
337 fi
338 rm -f "$tmp_log"
339 trap - EXIT INT TERM
340 exit "$test_status"
341 fi
342 exec "$repo_root/scripts/with-hermetic-test-home.sh" "$repo_root/scripts/dev-cargo.sh" "$@"
343
343 lines BASH