返回 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 #
8 # Usage:
9 # scripts/dev-test.sh <area|path> [filter...]
10 # scripts/dev-test.sh --list
11 # scripts/dev-test.sh --status
12 #
13 # Examples:
14 # scripts/dev-test.sh config
15 # scripts/dev-test.sh tui elapsed::
16 # scripts/dev-test.sh crates/tui/src/elapsed.rs
17 #
18 # Environment:
19 # CODEWHALE_DEV_NEXTEST auto|1|0 (default auto: use cargo-nextest when
20 # it is on PATH; 0 forces cargo test)
21 # Cache knobs are documented in scripts/dev-cache.sh.
22 set -eu
23
24 repo_root=$(CDPATH='' cd -- "$(dirname -- "$0")/.." && pwd)
25 cd "$repo_root"
26
27 # shellcheck source=scripts/dev-cache.sh
28 . "$repo_root/scripts/dev-cache.sh"
29
30 usage() {
31 printf '%s\n' "usage: scripts/dev-test.sh <area|path> [filter...]" >&2
32 printf '%s\n' " scripts/dev-test.sh --list|--status|--self-check" >&2
33 exit 2
34 }
35
36 list_areas() {
37 cat <<'EOF'
38 area command
39 ---- -------
40 agent cargo test -p codewhale-agent --lib --locked
41 app-server cargo test -p codewhale-app-server --lib --locked
42 build-support cargo test -p codewhale-build-support --lib --locked
43 cli cargo test -p codewhale-cli --lib --locked
44 command-contract cargo test -p codewhale-command-contract --lib --locked
45 config cargo test -p codewhale-config --lib --locked
46 core cargo test -p codewhale-core --lib --locked
47 execpolicy cargo test -p codewhale-execpolicy --lib --locked
48 hooks cargo test -p codewhale-hooks --lib --locked
49 lane cargo test -p codewhale-lane --lib --locked
50 mcp cargo test -p codewhale-mcp --lib --locked
51 paths cargo test -p codewhale-paths --lib --locked
52 protocol cargo test -p codewhale-protocol --lib --locked
53 release cargo test -p codewhale-release --lib --locked
54 secrets cargo test -p codewhale-secrets --lib --locked
55 state cargo test -p codewhale-state --lib --locked
56 telemetry cargo test -p codewhale-telemetry --lib --locked
57 tools cargo test -p codewhale-tools --lib --locked
58 tui cargo test -p codewhale-tui --lib --locked
59 tui-integration cargo test -p codewhale-tui --test integration --locked
60 tui-cucumber cargo test -p codewhale-tui --test cucumber --locked
61 workflow cargo test -p codewhale-workflow --lib --locked
62 workflow-js cargo test -p codewhale-workflow-js --lib --locked
63
64 path prefix area / extra filter
65 ----------- -------------------
66 crates/<crate>/ <crate>
67 crates/tui/src/tui/ tui tui::
68 crates/tui/src/tools/ tui tools::
69 crates/tui/src/core/ tui core::
70 crates/tui/src/commands/ tui commands::
71 crates/tui/src/<file>.rs tui <file>::
72 crates/tui/tests/integration/ tui-integration <stem>
73 crates/tui/tests/cucumber/ tui-cucumber <stem>
74 crates/tui/tests/ tui-integration
75
76 When cargo-nextest is on PATH, the run stage is `cargo nextest run`
77 instead of `cargo test` (same binaries; process per test). Set
78 CODEWHALE_DEV_NEXTEST=0 to force libtest. New worktrees get an isolated
79 Cargo build-dir via scripts/dev-cache.sh; sccache wraps rustc only when
80 incremental is already off. Test HOME and config paths are isolated by
81 scripts/with-hermetic-test-home.sh. Do not use cargo test --workspace for a
82 single-area edit. --lib and --tests are disjoint; a green --lib run does
83 not cover crates/tui/tests/.
84 EOF
85 }
86
87 [ $# -ge 1 ] || usage
88
89 if [ "$1" = "--list" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
90 list_areas
91 exit 0
92 fi
93
94 if [ "$1" = "--status" ] || [ "$1" = "--self-check" ]; then
95 exec "$repo_root/scripts/dev-cache.sh" "$1"
96 fi
97
98 area=$1
99 shift
100
101 # Path form: map a source path onto an area, and invent a filter only when
102 # the caller did not already pass one.
103 if [ -e "$area" ] || printf '%s' "$area" | grep -q /; then
104 rel=${area#./}
105 extra=
106 case $rel in
107 crates/tui/tests/integration/*)
108 area=tui-integration
109 extra=$(basename "$rel" .rs)
110 ;;
111 crates/tui/tests/cucumber/*)
112 area=tui-cucumber
113 extra=$(basename "$rel" .rs)
114 ;;
115 crates/tui/tests/*)
116 area=tui-integration
117 extra=$(basename "$rel" .rs)
118 ;;
119 crates/tui/src/tui/*)
120 area=tui
121 extra=tui::
122 ;;
123 crates/tui/src/tools/*)
124 area=tui
125 extra=tools::
126 ;;
127 crates/tui/src/core/*)
128 area=tui
129 extra=core::
130 ;;
131 crates/tui/src/commands/*)
132 area=tui
133 extra=commands::
134 ;;
135 crates/tui/src/*)
136 area=tui
137 extra=$(basename "$rel" .rs)::
138 ;;
139 crates/tui/*|crates/tui)
140 area=tui
141 ;;
142 crates/*)
143 crate=$(printf '%s' "$rel" | awk -F/ '{print $2}')
144 area=$crate
145 ;;
146 *)
147 printf '%s\n' "dev-test: no area mapping for path: $area" >&2
148 exit 2
149 ;;
150 esac
151 case $extra in
152 ''|main|main::|lib::|mod|mod::) extra= ;;
153 esac
154 if [ $# -eq 0 ] && [ -n "${extra:-}" ]; then
155 set -- "$extra"
156 fi
157 fi
158
159 pkg=
160 target=--lib
161 case $area in
162 agent) pkg=codewhale-agent ;;
163 app-server) pkg=codewhale-app-server ;;
164 build-support) pkg=codewhale-build-support ;;
165 cli) pkg=codewhale-cli ;;
166 command-contract) pkg=codewhale-command-contract ;;
167 config) pkg=codewhale-config ;;
168 core) pkg=codewhale-core ;;
169 execpolicy) pkg=codewhale-execpolicy ;;
170 hooks) pkg=codewhale-hooks ;;
171 lane) pkg=codewhale-lane ;;
172 mcp) pkg=codewhale-mcp ;;
173 paths) pkg=codewhale-paths ;;
174 protocol) pkg=codewhale-protocol ;;
175 release) pkg=codewhale-release ;;
176 secrets) pkg=codewhale-secrets ;;
177 state) pkg=codewhale-state ;;
178 telemetry) pkg=codewhale-telemetry ;;
179 tools) pkg=codewhale-tools ;;
180 tui) pkg=codewhale-tui ;;
181 workflow) pkg=codewhale-workflow ;;
182 workflow-js) pkg=codewhale-workflow-js ;;
183 tui-integration)
184 pkg=codewhale-tui
185 target=--test
186 harness=integration
187 ;;
188 tui-cucumber)
189 pkg=codewhale-tui
190 target=--test
191 harness=cucumber
192 ;;
193 *)
194 printf '%s\n' "dev-test: unknown area: $area (try --list)" >&2
195 exit 2
196 ;;
197 esac
198
199 use_nextest=0
200 _cw_nextest=${CODEWHALE_DEV_NEXTEST:-auto}
201 if codewhale_dev_cache_falsey "$_cw_nextest"; then
202 use_nextest=0
203 elif command -v cargo-nextest >/dev/null 2>&1; then
204 use_nextest=1
205 elif codewhale_dev_cache_truthy "$_cw_nextest"; then
206 printf '%s\n' "dev-test: CODEWHALE_DEV_NEXTEST=${_cw_nextest} but cargo-nextest is not on PATH" >&2
207 exit 2
208 fi
209
210 if [ "$target" = "--test" ]; then
211 if [ "$use_nextest" -eq 1 ]; then
212 set -- nextest run -p "$pkg" --test "$harness" --locked "$@"
213 else
214 set -- test -p "$pkg" --test "$harness" --locked "$@"
215 fi
216 else
217 if [ "$use_nextest" -eq 1 ]; then
218 set -- nextest run -p "$pkg" --lib --locked "$@"
219 else
220 set -- test -p "$pkg" --lib --locked "$@"
221 fi
222 fi
223
224 printf '+ cargo %s\n' "$*"
225 # Resolve the persistent cache before replacing HOME; dev-cargo applies the
226 # topology once, retaining Cargo's build-dir template and any caller overrides.
227 CODEWHALE_CACHE_ROOT=$(codewhale_dev_cache_root)
228 export CODEWHALE_CACHE_ROOT
229 exec "$repo_root/scripts/with-hermetic-test-home.sh" "$repo_root/scripts/dev-cargo.sh" "$@"
230
230 lines BASH