Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

commandLabel.test.ts

479 lines18.2 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { commandProgramName } from "./commandLabel.ts";
4​
5describe("commandProgramName", () => {
6 it.each([
7 ["/bin/zsh -lc 'vp test run apps/web/src/session-logic.test.ts'", "vp"],
8 ["/bin/zsh -lc 'git diff --check'", "git"],
9 ['/bin/zsh -lc "npx -y react-doctor@latest apps/web"', "npx"],
10 ["/bin/zsh -lc 'rg -n \"registerHooks|worker\" apps/web/src'", "rg"],
11 ["/bin/bash --noprofile --norc -l -c 'sed -n 1,270p file.ts'", "sed"],
12 ["/bin/bash -o pipefail -lc 'vp test run'", "vp"],
13 ["/bin/bash --rcfile /tmp/config -c 'git status'", "git"],
14 ["sh -ec 'node scripts/check.js'", "node"],
15 ["fish --command 'rg --files'", "rg"],
16 ["zsh -lc 'CI=1 env -u DEBUG sudo -u root vp test run'", "vp"],
17 ["env CI=1 /bin/zsh -lc '\"/Applications/My Tools/bin/check\" --verbose'", "check"],
18 ["bash -lc \"zsh -c 'git status'\"", "git"],
19 ['"C:\\Program Files\\Git\\bin\\bash.exe" -lc "git status"', "git"],
20 ["/bin/zsh -lc 'git status\nsed -n '\"'1,20p' apps/web/src/components/DiffPanel.tsx\"", "git"],
21 ])("unwraps shell scripts without executing them: %s", (command, program) => {
22 expect(commandProgramName(command)).toBe(program);
23 });
24​
25 it.each([
26 ["vp test run", "vp"],
27 ["sudo -u root pnpm test", "pnpm"],
28 ["env --split-string='CI=1 node scripts/check.js'", "node"],
29 ['"C:\\Program Files\\nodejs\\node.exe" script.js', "node.exe"],
30 ["/bin/zsh", "zsh"],
31 ["/bin/bash -l", "bash"],
32 ["zsh script.sh -c 'git status'", "zsh"],
33 ["bash -- -c 'git status'", "bash"],
34 ["bash --rcfile config.sh", "bash"],
35 ["my-shell -c 'git status'", "my-shell"],
36 ["$HOME/.bun/bin/bun test", "bun"],
37 ['"$ANDROID_HOME/emulator/emulator" -list-avds', "emulator"],
38 ["${ROOT}/bin/tool --version", "tool"],
39 ])("preserves ordinary programs and actual shell launches: %s", (command, program) => {
40 expect(commandProgramName(command)).toBe(program);
41 });
42​
43 it.each([
44 "",
45 "zsh -lc",
46 "zsh -lc ''",
47 "zsh -lc 'git status",
48 "zsh -lc 'env'",
49 'zsh -lc "git \\\"',
50 ])("falls back for missing or malformed scripts: %s", (command) => {
51 expect(commandProgramName(command)).toBeNull();
52 });
53​
54 it.each([
55 "if test -f package.json; then vp test; fi",
56 "[ -f package.json ]",
57 "[[ -f package.json ]]",
58 "test -f package.json",
59 'for file in *; do echo "$file"; done',
60 "while true; do sleep 1; done",
61 "until false; do sleep 1; done",
62 "case $name in test) vp test;; esac",
63 'select item in one two; do echo "$item"; done',
64 "function check() { vp test; }",
65 "check() { vp test; }",
66 "k(){ echo ok; }; k",
67 "{ vp test; }",
68 "(vp test)",
69 "(( count += 1 ))",
70 "! vp test",
71 ":",
72 ". ./script.sh",
73 "source ./script.sh",
74 "eval 'vp test'",
75 "cd packages/client-runtime",
76 "export NODE_ENV=test",
77 "local name=value",
78 "set -e",
79 "alias ll='ls -la'",
80 "repeat 3 echo ok",
81 "and vp test",
82 "return 1",
83 "break",
84 "continue",
85 "true",
86 "false",
87 ])("falls back for shell syntax and internal control commands: %s", (command) => {
88 expect(commandProgramName(command)).toBeNull();
89 });
90​
91 it.each([
92 ['rg -n "if|for|while" src', "rg"],
93 ["printf '%s\\n' 'a;b|c'", "printf"],
94 ["node -e \"if (true) console.log('ok')\"", "node"],
95 ["echo '$(git status)'", "echo"],
96 ["vp test && git status", "vp"],
97 ["vp test || git status", "vp"],
98 ["rg needle src | head", "rg"],
99 ["vp test; git status", "vp"],
100 ["vp test &", "vp"],
101 ["vp test\ngit status", "vp"],
102 ['echo "$(git status)"', "echo"],
103 ["echo `git status`", "echo"],
104 ["cat <(rg needle src)", "cat"],
105 ])("uses the first executable-looking program: %s", (command, program) => {
106 expect(commandProgramName(command)).toBe(program);
107 });
108​
109 it.each([
110 ["cd packages/client-runtime && vp test run", "vp"],
111 ['cd "a path with spaces"; git status', "git"],
112 ["cd apps/web\npnpm test", "pnpm"],
113 ["cd first && cd second && bun test", "bun"],
114 ["CI=1 cd apps/web && npm test", "npm"],
115 ["PATH+=:/tools npm test", "npm"],
116 ["PATH+=:/tools && npm test", "npm"],
117 ['TMP=$(mktemp -d); cd "$TMP"; npm pack ./package', "npm"],
118 ["cd $(find . -type d | head -1) && git status", "git"],
119 ["cd `find . -type d | head -1` && node script.js", "node"],
120 ["cd /tmp 2>&1 && npm test", "npm"],
121 ["cd /tmp 2<&0 && pnpm test", "pnpm"],
122 ["cd /tmp &>/dev/null && bun test", "bun"],
123 ["cd work |& npm test", "npm"],
124 ["cd /tmp && # use the selected workspace\nnpm test", "npm"],
125 ["export CI=1; # first note\n# second note\npnpm test", "pnpm"],
126 ["cd&&npm test", "npm"],
127 ["export CI=1;pnpm test", "pnpm"],
128 ["cd ${ROOT:-path;with;semicolons} && bun test", "bun"],
129 ["cd ${ROOT:-path&&fallback} && node app.js", "node"],
130 ["cd @(first|second) && npm test", "npm"],
131 ["cd /tmp \\\n&& npm test", "npm"],
132 ["/bin/zsh -lc 'cd apps/web && vp test run'", "vp"],
133 ])("skips leading cd commands and uses the next useful program: %s", (command, program) => {
134 expect(commandProgramName(command)).toBe(program);
135 });
136​
137 it.each([
138 ["source ~/.nvm/nvm.sh && nvm use", "nvm"],
139 [". ./.env && pnpm test", "pnpm"],
140 ["export CI=1 && vp test run", "vp"],
141 ["unset DEBUG; node app.js", "node"],
142 ["export CI=1 && cd apps/web && pnpm test", "pnpm"],
143 ["/bin/zsh -lc 'source ~/.nvm/nvm.sh && nvm use'", "nvm"],
144 ])("skips shell setup commands and uses the next useful program: %s", (command, program) => {
145 expect(commandProgramName(command)).toBe(program);
146 });
147​
148 it.each([
149 ["set -eu; npm test", "npm"],
150 [": && npm test", "npm"],
151 ["true && npm test", "npm"],
152 ["false || npm test", "npm"],
153 ["false; npm test", "npm"],
154 ["sudo -n true && npm test", "npm"],
155 ["sudo -n true; echo checked", "echo"],
156 ["test -d node_modules || vp i", "vp"],
157 ["[ -d node_modules ] || vp i", "vp"],
158 ])("skips non-descriptive shell commands before a useful program: %s", (command, program) => {
159 expect(commandProgramName(command)).toBe(program);
160 });
161​
162 it.each(
163 ["cd /tmp", "export CI=1", "unset DEBUG", "source env.sh", ". env.sh"].flatMap((setup) =>
164 ["&&", " || ", ";", "\n", "|", " |& ", " & "].map(
165 (operator) => [`${setup}${operator}npm test`, "npm"] as const,
166 ),
167 ),
168 )("handles shell setup followed by every command separator: %s", (command, program) => {
169 expect(commandProgramName(command)).toBe(program);
170 });
171​
172 it.each([
173 ["command git status", "git"],
174 ["command -p git status", "git"],
175 ["command -- git status", "git"],
176 ["builtin printf ok", "printf"],
177 ["builtin -- echo ok", "echo"],
178 ["command cd /tmp && npm test", "npm"],
179 ["builtin cd /tmp && pnpm test", "pnpm"],
180 ["exec node app.js", "node"],
181 ["exec -cl -a worker node app.js", "node"],
182 ["exec env CI=1 /opt/tools/check --verbose", "check"],
183 ['exec "C:\\Program Files\\nodejs\\node.exe" app.js', "node.exe"],
184 ["exec sh -c 'cd /tmp && npm test'", "npm"],
185 ["exec sh -c 'cd /tmp\nnpm test'", "npm"],
186 ["exec bash -c 'set -e\nnpm test'", "npm"],
187 ])("unwraps shell command wrappers: %s", (command, program) => {
188 expect(commandProgramName(command)).toBe(program);
189 });
190​
191 it.each([
192 ["timeout 10 pnpm test", "pnpm"],
193 ["timeout 10s python3 script.py", "python3"],
194 ["gtimeout 1.5 node app.js", "node"],
195 ["nohup npx expo start >/tmp/metro.log 2>&1 &", "npx"],
196 ["nohup -- env CI=1 bun test", "bun"],
197 ["arch -x86_64 ./build/app-under-test", "app-under-test"],
198 ["arch -arch arm64 /opt/tools/check", "check"],
199 ["bundle exec pod install", "pod"],
200 ["timeout 30 nohup env CI=1 node app.js", "node"],
201 ["timeout 60 script -q /dev/null env CI=1 node app.js", "node"],
202 ])("unwraps process-launch wrappers: %s", (command, program) => {
203 expect(commandProgramName(command)).toBe(program);
204 });
205​
206 it.each([
207 ["& 'C:\\Program Files\\nodejs\\node.exe' script.js", "node.exe"],
208 ['& "$env:WINDIR\\Microsoft.NET\\Framework64\\v4\\csc.exe" file.cs', "csc.exe"],
209 ])("resolves literal PowerShell call operators: %s", (command, program) => {
210 expect(commandProgramName(command)).toBe(program);
211 });
212​
213 it.each([
214 ["$env:CI='1'; npm test", "npm"],
215 ["$value = 'configured'; node app.js", "node"],
216 ["$process = Get-Process node; $process.Id", "Get-Process"],
217 ["$tmp = Join-Path $env:TEMP repo; git clone example", "Join-Path"],
218 ["$html = (Invoke-WebRequest https://example.com).Content", "Invoke-WebRequest"],
219 ["$process = Start-Process -FilePath .\\app.exe -PassThru; $process.Id", "app.exe"],
220 ])("labels commands inside simple PowerShell assignments: %s", (command, program) => {
221 expect(commandProgramName(command)).toBe(program);
222 });
223​
224 it.each([
225 ["cmd /c bcdedit /enum", "bcdedit"],
226 ["cmd /c cd C:\\work && npm test", "npm"],
227 ['cmd.exe /d /s /c "cd C:\\work && npm test"', "npm"],
228 [
229 'powershell.exe -NoProfile -ExecutionPolicy Bypass -File "C:\\work\\scripts\\doctor.ps1"',
230 "doctor.ps1",
231 ],
232 ['pwsh -NoProfile -Command "Set-Location C:\\work; pnpm test"', "pnpm"],
233 ["pwsh -Command Set-Location C:\\work; pnpm test", "pnpm"],
234 ])("unwraps Windows shell launchers: %s", (command, program) => {
235 expect(commandProgramName(command)).toBe(program);
236 });
237​
238 it.each([
239 ["Set-Location C:\\work; npm test", "npm"],
240 ["Push-Location C:\\work; node app.js", "node"],
241 ["Start-Process -FilePath node -ArgumentList server.js", "node"],
242 ["Start-Process -ArgumentList '-FilePath helper' node", "node"],
243 ["Start-Process -ErrorAction Stop -FilePath node", "node"],
244 ["Start-Process -NoNewWindow -WorkingDirectory C:\\work node", "node"],
245 ['Start-Process "C:\\Program Files\\Example\\app.exe"', "app.exe"],
246 ['Start-Process -FilePath ".\\dist\\Example App.exe" -PassThru', "Example App.exe"],
247 [".\\.venv\\Scripts\\python.exe script.py", "python.exe"],
248 ["$env:LOCALAPPDATA\\Programs\\tool.exe --version", "tool.exe"],
249 ['"=== CHECK FILE ==="; Get-Content file.txt', "Get-Content"],
250 ["$x = @'\ndata'; Get-Fake\n'@\nGet-Process", "Get-Process"],
251 ["@'\nprint('ok; still data')\n'@ | python -", "python"],
252 ])("handles common PowerShell setup and launch commands: %s", (command, program) => {
253 expect(commandProgramName(command)).toBe(program);
254 });
255​
256 it.each([
257 ["timeout --help", "timeout"],
258 ["nohup --version", "nohup"],
259 ["arch", "arch"],
260 ["bundle install", "bundle"],
261 ["script output.log", "script"],
262 ["/usr/bin/timeout 10 node app.js", "timeout"],
263 ])("keeps process-launch wrappers when no safe payload is present: %s", (command, program) => {
264 expect(commandProgramName(command)).toBe(program);
265 });
266​
267 it.each([
268 "command -v git",
269 "command -V git",
270 "command -a git",
271 "command -pv git",
272 "builtin -p",
273 "exec",
274 "exec > output.log",
275 "exec --",
276 "exec cd /tmp && npm test",
277 "exec env CI=1 cd /tmp && npm test",
278 "exec CI=1 npm test",
279 "env CI=1 cd /tmp && npm test",
280 "env CI=1; npm test",
281 "sudo cd /tmp && npm test",
282 "command CI=1 npm test",
283 "command false && npm test",
284 "cmd /c false && npm test",
285 "cd /tmp <<EOF\nunterminated heredoc",
286 "cd /tmp && >/tmp/log",
287 "(xcrun simctl io booted recordVideo /tmp/video.mp4 &) ; wait",
288 "export CI=1 && (bundle exec pod install || pod install)",
289 "$PY scripts/check.py",
290 "${TOOL} --version",
291 "%TOOL% --version",
292 "!TOOL! --version",
293 "& $tool --version",
294 "& { Get-Process }",
295 "$value = 'configured'",
296 "$headers = @{ 'Accept' = 'application/json'; 'Content-Type' = 'application/json' }; Invoke-WebRequest https://example.com",
297 '"sha256(value)=$hash"',
298 "broken{",
299 "@echo off",
300 ":: comment",
301 "time -- npm test",
302 "time -v npm test",
303 "coproc npm test",
304 "coproc worker { npm test; }",
305 "cd [first|second] && pnpm test",
306 "npm) --version",
307 "try { Invoke-WebRequest https://example.com } catch { Write-Error $_ }",
308 "for($i=0; $i -lt 2; $i++){ Start-Sleep 1 }",
309 "# comment only",
310 ])("does not treat shell lookup and commandless wrapper forms as executions: %s", (command) => {
311 expect(commandProgramName(command)).toBeNull();
312 });
313​
314 it.each([
315 ["parallel -j4", "parallel"],
316 ["hash --help", "hash"],
317 ["process --help", "process"],
318 ["rem comment", "rem"],
319 ["Exec node app.js", "Exec"],
320 ["CD /tmp && npm test", "CD"],
321 ])("does not hide legitimate or case-distinct program names: %s", (command, program) => {
322 expect(commandProgramName(command)).toBe(program);
323 });
324​
325 it.each([
326 ["env sh -c 'cd /tmp && npm test'", "npm"],
327 ["sudo zsh -lc 'export CI=1 && pnpm test'", "pnpm"],
328 ])("parses shell setup inside an explicitly launched shell: %s", (command, program) => {
329 expect(commandProgramName(command)).toBe(program);
330 });
331​
332 it.each([
333 ["nocorrect pnpm test", "pnpm"],
334 ["noglob bun test", "bun"],
335 ["time node app.js", "node"],
336 ["time -p deno test", "deno"],
337 ["time nocorrect npm test", "npm"],
338 ])("skips shell precommand modifiers: %s", (command, program) => {
339 expect(commandProgramName(command)).toBe(program);
340 });
341​
342 it("uses the command after leading shell comments", () => {
343 expect(commandProgramName("# first comment\n # second comment\ngit status")).toBe("git");
344 });
345​
346 it.each([
347 ["CI=1 # note\nnpm test", "npm"],
348 ["CI=1 # it's configured\nnpm test", "npm"],
349 ['CI=1 # "unterminated quote\nbun test', "bun"],
350 [">/tmp/log # note\npnpm test", "pnpm"],
351 [">/tmp/log # it's configured\ndeno test", "deno"],
352 ])("skips comments after commandless shell setup: %s", (command, program) => {
353 expect(commandProgramName(command)).toBe(program);
354 });
355​
356 it.each([
357 ["./cd /tmp && npm test", "cd"],
358 ["/opt/exec node app.js", "exec"],
359 ["/usr/bin/time npm test", "time"],
360 ["/usr/bin/test -f package.json", "test"],
361 ])("does not treat qualified paths as shell syntax: %s", (command, program) => {
362 expect(commandProgramName(command)).toBe(program);
363 });
364​
365 it("skips a shell array assignment before the command", () => {
366 expect(commandProgramName("items=(one two); npm test")).toBe("npm");
367 });
368​
369 it.each([
370 ['EMU="/opt/android/emulator"; "$EMU" -list-avds', "emulator"],
371 ["AAPT=/opt/android/aapt2\n$AAPT dump badging app.apk", "aapt2"],
372 [
373 'SSH=(ssh -i /tmp/key -o IdentitiesOnly=yes); HOST=user@example; "${SSH[@]}" "$HOST" uptime',
374 "ssh",
375 ],
376 ["SCP=(/usr/bin/scp -i /tmp/key); if true; then ${SCP[@]} file user@example:/tmp; fi", "scp"],
377 ['TOOL="/Applications/My Tool/bin/check"; "$TOOL" --verbose', "check"],
378 ])("resolves literal command aliases from earlier shell segments: %s", (command, program) => {
379 expect(commandProgramName(command)).toBe(program);
380 });
381​
382 it.each([
383 'TOOL=$(pick-command); "$TOOL" --version',
384 'TOOL="git status"; "$TOOL"',
385 "# TOOL=git\n$TOOL status",
386 'TOOL=git true; "$TOOL" status',
387 'TOOL=git; TOOL=$(pick-command); "$TOOL" status',
388 'TOOL=git; unset TOOL; "$TOOL" status',
389 ])("does not evaluate dynamic or non-persistent command aliases: %s", (command) => {
390 expect(commandProgramName(command)).toBeNull();
391 });
392​
393 it("does not retain aliases assigned inside control flow", () => {
394 expect(commandProgramName('TOOL=git; if false; then\nTOOL=npm\nfi\n"$TOOL" status')).toBe(
395 "git",
396 );
397 });
398​
399 it.each([
400 ["ROOT=${BASE:-path with spaces}; npm test", "npm"],
401 ["ROOT=`printf 'path with spaces'`; pnpm test", "pnpm"],
402 ])("keeps expansions inside assignment words: %s", (command, program) => {
403 expect(commandProgramName(command)).toBe(program);
404 });
405​
406 it.each([
407 [">/tmp/log && npm test", "npm"],
408 ["2>/tmp/error.log; pnpm test", "pnpm"],
409 ["cd /tmp && >/tmp/log npm test", "npm"],
410 ["cd /tmp && > /tmp/log pnpm test", "pnpm"],
411 ["cd /tmp && 2>&1 bun test", "bun"],
412 ["cd /tmp && 2>& 1 node app.js", "node"],
413 ["cd /tmp && &>/tmp/log git status", "git"],
414 ["cd /tmp && *>>/tmp/log vp test", "vp"],
415 ["cd /tmp && {output}>/tmp/log deno test", "deno"],
416 ["cd /tmp && <<<input ruby script.rb", "ruby"],
417 ])("skips redirections before the next command: %s", (command, program) => {
418 expect(commandProgramName(command)).toBe(program);
419 });
420​
421 it.each([
422 ["cd /tmp <<EOF\nnot-a-command\nEOF\nnpm test", "npm"],
423 ["cd /tmp <<'EOF'\nnot-a-command\nEOF\npnpm test", "pnpm"],
424 ["cd /tmp <<'EOF'\nnot-a-command\\\nEOF\npnpm test", "pnpm"],
425 ["cd /tmp <<-EOF\n\tnot-a-command\n\tEOF\nbun test", "bun"],
426 ["cd /tmp <<A <<B\none\nA\ntwo\nB\ngit status", "git"],
427 ["cd /tmp <<EOF; # setup\nnot-a-command\nEOF\nnpm test", "npm"],
428 ["cd /tmp <<EOF && pnpm test\nnot-a-command\nEOF\nbun test", "pnpm"],
429 ])("skips heredoc bodies before finding the next command: %s", (command, program) => {
430 expect(commandProgramName(command)).toBe(program);
431 });
432​
433 it.each(["cd apps/web && [ -f package.json ]", "cd apps/web || exit 1", "cd one && cd two"])(
434 "falls back when no useful program follows cd: %s",
435 (command) => {
436 expect(commandProgramName(command)).toBeNull();
437 },
438 );
439​
440 it.each([
441 "if test -f package.json\nthen\n npm test\nfi",
442 "[[ -d first && -d second ]] && npm test",
443 'for file in *\ndo\n echo "$file"\ndone',
444 "while true\ndo\n sleep 1\ndone",
445 "cd /tmp; build () { npm test; }",
446 ])("does not label commands inside multiline shell control flow: %s", (command) => {
447 expect(commandProgramName(command)).toBeNull();
448 });
449​
450 it("bounds nested shell unwrapping", () => {
451 let command = "git status";
452 for (let depth = 0; depth < 9; depth += 1) {
453 command = `sh -c '${command.replaceAll("'", "'\\''")}'`;
454 }
455 expect(commandProgramName(command)).toBeNull();
456 });
457​
458 it("does not spend the shell nesting budget on setup commands", () => {
459 const command = [
460 ...Array.from({ length: 10 }, (_, index) => `export VALUE_${index}=configured`),
461 "npm test",
462 ].join("\n");
463​
464 expect(commandProgramName(command)).toBe("npm");
465 });
466​
467 it("bounds the number of top-level setup segments", () => {
468 const command = [
469 ...Array.from({ length: 2_000 }, (_, index) => `export VALUE_${index}=configured`),
470 "npm test",
471 ].join(";");
472​
473 expect(commandProgramName(command)).toBeNull();
474 });
475​
476 it("bounds nested command wrappers", () => {
477 expect(commandProgramName(`${"command ".repeat(9)}git status`)).toBeNull();
478 });
479});