Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { commandDisplayText, commandProgramName } from "./commandLabel.ts";
4
5describe("commandProgramName", () => {
6 it.each([
7 ["/bin/bash --rcfile /tmp/config -c 'git status'", "git"],
8 ['bash -lc -- "printf ok"', "printf"],
9 ["bash -c - '-e'", "-e"],
10 ['bash -euo pipefail -c "ls -la"', "ls"],
11 ["zsh -ofunctionargzero -c 'echo hi'", "echo"],
12 ['bash +x +o history -c "ls -la"', "ls"],
13 ['bash -lc "echo $(echo "hello world")"', "echo"],
14 // fish runs `--` itself as the script; "printf ok" is a positional argument
15 ['fish -c -- "printf ok"', "--"],
16 ["zsh -lc 'CI=1 env -u DEBUG sudo -u root vp test run'", "vp"],
17 ['"C:\\Program Files\\Git\\bin\\bash.exe" -lc "git status"', "git"],
18 // regression: #9371 a multiline script whose later line re-quotes must still label its first program
19 ["/bin/zsh -lc 'git status\nsed -n '\"'1,20p' apps/web/src/components/DiffPanel.tsx\"", "git"],
20 ["bash -c $'ls\\nnpm test'", "ls"],
21 ["bash -c $'\\x6e\\160m test'", "npm"],
22 ['powershell -NoP -exec bypass -c "git status"', "git"],
23 ["cmd /c env FOO=1 git status", "git"],
24 ["cmd /c bash -lc 'git status'", "git"],
25 ["pwsh -NoProfile -Command Start-Process -FilePath git", "git"],
26 ['powershell -Command "git;pwd" > out.txt', "git"],
27 ['cmd /c "git&&npm test" 2>&1', "git"],
28 ["pwsh -Command '& Get-Date; Get-Location'", "Get-Date"],
29 ["pwsh -Command '& git|Out-Null'", "git"],
30 ["pwsh -Command '& “./build;release.ps1”'", "build;release.ps1”"],
31 ])("unwraps shell scripts without executing them: %s", (command, program) => {
32 expect(commandProgramName(command)).toBe(program);
33 });
34
35 it.each([
36 'VAR="' + "$((".repeat(5000) + "echo hi" + ") )".repeat(5000) + '" && pnpm test',
37 'VAR="' + "$((a<<b ".repeat(5000) + ") )".repeat(5000) + '" && pnpm test',
38 ])("settles many non-arithmetic $(( openings without rescanning: %#", (command) => {
39 expect(commandProgramName(command)).toBe("pnpm");
40 });
41
42 it.each([
43 ["sudo -u root pnpm test", "pnpm"],
44 ["zsh script.sh -c 'git status'", "zsh"],
45 ["bash - -c 'git status'", "bash"],
46 ["zsh -onoclobber run.sh", "zsh"],
47 ["my-shell -c 'git status'", "my-shell"],
48 ["$HOME/.bun/bin/bun test", "bun"],
49 ])("preserves ordinary programs and actual shell launches: %s", (command, program) => {
50 expect(commandProgramName(command)).toBe(program);
51 });
52
53 it.each([
54 ["bash -c 'env X=one\u00a0two printf ok'", "printf"],
55 ["bash -c 'env X=one\u00a0#two printf ok'", "printf"],
56 ["bash -c './my\u00a0tool --flag'", "my\u00a0tool"],
57 ["fish -c 'env X=one \u00a0printf ok'", "printf"],
58 ["bash -c 'pwsh -Command env\u00a0X=one printf ok'", "printf"],
59 ['powershell -Command "git\u00a0status"', "git"],
60 ])("splits words only where the shell does: %j", (command, program) => {
61 expect(commandProgramName(command)).toBe(program);
62 });
63
64 it.each(["zsh -lc", "bash -c --", "zsh -lc 'git status", "zsh -lc 'env'"])(
65 "falls back for missing or malformed scripts: %s",
66 (command) => {
67 expect(commandProgramName(command)).toBeNull();
68 },
69 );
70
71 it.each([
72 "if test -f package.json; then vp test; fi",
73 "[[ -f package.json ]]",
74 "test -f package.json",
75 'for file in *; do echo "$file"; done',
76 "check() { vp test; }",
77 "{ vp test; }",
78 "(( count += 1 ))",
79 ". ./script.sh",
80 "export NODE_ENV=test",
81 "repeat 3 echo ok",
82 "return 1",
83 "true",
84 ])("falls back for shell syntax and internal control commands: %s", (command) => {
85 expect(commandProgramName(command)).toBeNull();
86 });
87
88 it.each([
89 ['rg -n "if|for|while" src', "rg"],
90 ["vp test && git status", "vp"],
91 ["echo `git status`", "echo"],
92 ["cat <(rg needle src)", "cat"],
93 ])("uses the first executable-looking program: %s", (command, program) => {
94 expect(commandProgramName(command)).toBe(program);
95 });
96
97 it.each([
98 ["cd packages/client-runtime && vp test run", "vp"],
99 ["PATH+=:/tools npm test", "npm"],
100 ["cd $(find . -type d | head -1) && git status", "git"],
101 ["cd /tmp && # use the selected workspace\nnpm test", "npm"],
102 ["cd ${ROOT:-path;with;semicolons} && bun test", "bun"],
103 ["cd /tmp || exit 1; vp test run", "vp"],
104 ["cd /tmp || exit 1\nnpm test", "npm"],
105 ["cd /tmp || return 1; make", "make"],
106 ["cd /tmp || exit 1 && make", "make"],
107 ["builtin cd /tmp || exit 1; make", "make"],
108 ["bash -c 'cd /tmp || exit 1; vp test run'", "vp"],
109 ])("skips leading cd commands and uses the next useful program: %s", (command, program) => {
110 expect(commandProgramName(command)).toBe(program);
111 });
112
113 // regression: #41718 a leading env-setup eval hid the real program behind "Running command"
114 it.each([
115 [
116 'cd /path/to/repo && eval "$(fnm env --shell zsh)" && fnm use 26.13.1 && git commit -m "msg"',
117 "fnm",
118 ],
119 ['eval "$(/opt/homebrew/bin/brew shellenv)" && pnpm test', "pnpm"],
120 ])("skips leading env-setup eval segments: %s", (command, program) => {
121 expect(commandProgramName(command)).toBe(program);
122 });
123
124 it.each([
125 ["source ~/.nvm/nvm.sh && nvm use", "nvm"],
126 ["unset DEBUG; node app.js", "node"],
127 ["/bin/zsh -lc 'source ~/.nvm/nvm.sh && nvm use'", "nvm"],
128 ["sudo [ -f /etc/hosts ] && git status", "git"],
129 ["sudo -n [ -d /opt ] && pytest", "pytest"],
130 ["sudo [[ -f /etc/hosts ]] && git status", "git"],
131 ["true && git --version", "git"],
132 ["[ -d /tmp ] || exit 1; make", "make"],
133 ["work=$(mktemp -d) || exit 1; make", "make"],
134 ['DIR="$(python3 -c "import sys; print(sys.version)")" && pnpm test', "pnpm"],
135 ['VAR="${FOO:-"a; b"}" && pnpm test', "pnpm"],
136 ['VAR="${FOO#*{}" && pnpm test', "pnpm"],
137 ['VAR="$(\n # don\'t break\n echo hi\n)" && pnpm test', "pnpm"],
138 ['VAR="$(echo $# "c")" && pnpm test', "pnpm"],
139 ['VAR="$(echo foo\\ # literal "c")" && pnpm test', "pnpm"],
140 ['VAR="$(cat <<EOF\n(\nEOF\n)" && pnpm test', "pnpm"],
141 ['VAR="${FOO:-don\'t}" && pnpm test', "pnpm"],
142 ['VAR="$( (echo hi)# don\'t break\n)" && pnpm test', "pnpm"],
143 ['VAR="$( (( x = 1 << 2 ))\necho hi\n)" && pnpm test', "pnpm"],
144 ['VAR="$(cat <(echo hi)# "c")" && pnpm test', "pnpm"],
145 ['VAR="$(cat <<OUT <(cat <<IN\ninner\nIN\n)\nouter\nOUT\n)" && pnpm test', "pnpm"],
146 ['VAR="${FOO#\'"\'}" && pnpm test', "pnpm"],
147 ['VAR="$((echo hi)# don\'t break\n)" && pnpm test', "pnpm"],
148 ['VAR="$(( (1 << 2) ))" && pnpm test', "pnpm"],
149 ])("skips shell setup commands and uses the next useful program: %s", (command, program) => {
150 expect(commandProgramName(command)).toBe(program);
151 });
152
153 it.each([
154 ["timeout 10s python3 script.py", "python3"],
155 ["arch -arch arm64 /opt/tools/check", "check"],
156 ["timeout 60 script -q /dev/null env CI=1 node app.js", "node"],
157 ["timeout -s KILL 30 npm test", "npm"],
158 ["timeout --signal=TERM -v 60 cargo test", "cargo"],
159 ["gtimeout -fk5 -- 30 node app.js", "node"],
160 ['env -S"FOO=1 git status"', "git"],
161 ['env -vS "FOO=1 git status"', "git"],
162 ['env -S"FOO=1" git status', "git"],
163 ["env - git status", "git"],
164 ["sudo --preserve-env=PATH vp test run", "vp"],
165 ["sudo --preserve-env vp test run", "vp"],
166 ["sudo --prompt='' --command-timeout=30 vp test run", "vp"],
167 ["sudo -Sp '' vp test run", "vp"],
168 ["sudo --non-interactive --stdin --login vp test run", "vp"],
169 ["exec -ca myname git status", "git"],
170 ["exec -cla myname git status", "git"],
171 ["exec -camyname git status", "git"],
172 ["exec -ca >out myname git status", "git"],
173 ["exec -ca <<<'first\nsecond' myname git status", "git"],
174 ["exec >out -ca myname git status", "git"],
175 ["exec -ca '>out' git status", "git"],
176 ])("unwraps process-launch wrappers: %s", (command, program) => {
177 expect(commandProgramName(command)).toBe(program);
178 });
179
180 it.each(["timeout -x 30 npm test", "timeout -s 30 npm test"])(
181 "keeps the timeout label for invalid timeout options: %s",
182 (command) => {
183 expect(commandProgramName(command)).toBe("timeout");
184 },
185 );
186
187 it.each([
188 "command -v git",
189 "exec > output.log",
190 "exec -ca",
191 "exec -ca myname",
192 "exec -ca >out myname",
193 "env CI=1 cd /tmp && npm test",
194 "$PY scripts/check.py",
195 "& { Get-Process }",
196 "coproc npm test",
197 "true || git --version",
198 "builtin true || git --version",
199 ": || git --version",
200 "builtin : || git --version",
201 "command : || git --version",
202 "cd /tmp || exit 1",
203 "cd /tmp && exit 1; make",
204 "cd /tmp || exit 1 || make",
205 "cd /tmp || exit 1 | make",
206 "false || exit 1; make",
207 "sudo --preserve-env=PATH",
208 ])("does not treat shell lookup and commandless wrapper forms as executions: %s", (command) => {
209 expect(commandProgramName(command)).toBeNull();
210 });
211
212 it.each([
213 ["hash --help", "hash"],
214 ["Exec node app.js", "Exec"],
215 ["CD /tmp && npm test", "CD"],
216 ])("does not hide legitimate or case-distinct program names: %s", (command, program) => {
217 expect(commandProgramName(command)).toBe(program);
218 });
219
220 it("uses the command after leading shell comments", () => {
221 expect(commandProgramName("# first comment\n # second comment\ngit status")).toBe("git");
222 });
223
224 it.each([
225 ["echo ${a:- #1}", "echo"],
226 ["x=${a:-$( # '\n printf hi\n) #1} # comment; npm test\ngit status", "git"],
227 ["x=${a:-<( # '\n printf hi\n) #1}; git status", "git"],
228 ["x=${a:-$(printf %s }) #1}; git status", "git"],
229 ])("keeps hashes inside unquoted parameter expansions: %j", (command, program) => {
230 expect(commandProgramName(command)).toBe(program);
231 });
232
233 it.each([
234 [">/tmp/log && npm test", "npm"],
235 ["cd /tmp && 2>& 1 node app.js", "node"],
236 ["cd /tmp && {output}>/tmp/log deno test", "deno"],
237 ["whoami>/dev/null", "whoami"],
238 ["cat</etc/hosts", "cat"],
239 ["whoami&>/dev/null", "whoami"],
240 ["2>|/dev/null git --version", "git"],
241 ["cd /tmp && >|/tmp/log deno test", "deno"],
242 ["'my>tool' arg", "my>tool"],
243 ["my\\>tool arg", "my>tool"],
244 ])("handles redirections around commands: %s", (command, program) => {
245 expect(commandProgramName(command)).toBe(program);
246 });
247
248 it.each([
249 ["cd /tmp <<-EOF\n\tnot-a-command\n\tEOF\nbun test", "bun"],
250 ["cd /tmp <<A <<B\none\nA\ntwo\nB\ngit status", "git"],
251 ["cd /tmp <<EOF && pnpm test\nnot-a-command\nEOF\nbun test", "pnpm"],
252 ["cd . <<''\npretend\n\nprintf ok", "printf"],
253 ['cd . <<""\npretend\n\nprintf ok', "printf"],
254 [": <<< hi\nprintf '%s' visible", "printf"],
255 [": <<<-hi && printf '%s' visible", "printf"],
256 [': <<"E\\X"\nhello\nE\\X\nprintf ok', "printf"],
257 [': <<"E\\"X"\nhello\nE"X\nprintf ok', "printf"],
258 ])("skips heredoc bodies before finding the next command: %s", (command, program) => {
259 expect(commandProgramName(command)).toBe(program);
260 });
261
262 it.each([
263 [": <<EOF\nbody\\\nEOF\necho wrong\nEOF\nprintf ok\n", "printf"],
264 [": <<-EOF\nbody\\\n\tEOF\necho wrong\n\tEOF\nprintf ok\n", "printf"],
265 [": <<'EOF'\nbody\\\nEOF\nprintf ok\n", "printf"],
266 [": <<EOF\nbody\\\\\nEOF\nprintf ok\n", "printf"],
267 [": <<EOF\nEO\\\nF\nprintf ok\n", null],
268 ["dash -c ': <<EOF\nEO\\\nF\nprintf ok\n'", null],
269 ])(
270 "joins continued lines before matching an unquoted heredoc delimiter: %s",
271 (command, program) => {
272 expect(commandProgramName(command)).toBe(program);
273 },
274 );
275
276 it("bounds nested shell unwrapping", () => {
277 let command = "git status";
278 for (let depth = 0; depth < 9; depth += 1) {
279 command = `sh -c '${command.replaceAll("'", "'\\''")}'`;
280 }
281 expect(commandProgramName(command)).toBeNull();
282 });
283
284 it("bounds the number of top-level setup segments", () => {
285 const command = [
286 ...Array.from({ length: 2_000 }, (_, index) => `export VALUE_${index}=configured`),
287 "npm test",
288 ].join(";");
289
290 expect(commandProgramName(command)).toBeNull();
291 });
292});
293
294describe("commandDisplayText with shell option terminators", () => {
295 it.each([
296 ["sh -co errexit 'git status'", "git", "git status"],
297 ["bash -cO extglob 'pytest'", "pytest", "pytest"],
298 ["bash -oOc pipefail extglob -- 'printf ok'", "printf", "printf ok"],
299 ["bash -co pipefail -c 'printf ok'", "printf", "printf ok"],
300 ["zsh -bc '-tool'", "-tool", "-tool"],
301 ["zsh +bc 'git status'", "git", "git status"],
302 ])("unwraps -c bundled with other shell options: %s", (input, program, script) => {
303 expect.soft(commandProgramName(input)).toBe(program);
304 expect.soft(commandDisplayText(input)).toBe(script);
305 });
306
307 it.each([
308 ['bash -lc -- "printf ok"', "printf ok"],
309 ['bash -eo pipefail -c "printf ok"', "printf ok"],
310 ['bash -lc "echo $(echo "hello world")"', 'echo $(echo "hello world")'],
311 ['fish -c -- "printf ok"', 'fish -c -- "printf ok"'],
312 ['bash -c -- "printf ok" arg0', 'bash -c -- "printf ok" arg0'],
313 ["ksh -onoclobber run.sh", "ksh -onoclobber run.sh"],
314 ])("shows %j as %j", (input, expected) => {
315 expect(commandDisplayText(input)).toBe(expected);
316 });
317});
318
319describe("commandDisplayText with trailing shell separators", () => {
320 it.each(["bash -c 'echo hi'&", "bash -c 'echo hi';"])("keeps the wrapper in %s", (input) => {
321 expect(commandDisplayText(input)).toBe(input);
322 });
323});
324
325describe("commandDisplayText with redacted Codex commands", () => {
326 // Codex replaces secrets in command text before sending it
327 // (codex-rs/secrets/src/sanitizer.rs). Usually the quoting survives.
328 it("unwraps a redacted command whose quoting is intact", () => {
329 expect(
330 commandDisplayText(
331 `/bin/zsh -lc 'curl -H "Authorization: Bearer [REDACTED_SECRET]" https://example.com'`,
332 ),
333 ).toBe('curl -H "Authorization: Bearer [REDACTED_SECRET]" https://example.com');
334 });
335
336 // Its secret-assignment pattern can also swallow the backslash of an escaped
337 // quote: `token=config[\"apiToken\"]` becomes `token=[REDACTED_SECRET]"apiToken\"]`.
338 // The bare quote ends the -lc argument early, so where the script ends is unknown.
339 it("keeps the wrapper when redaction broke the quoting", () => {
340 const input = `/bin/zsh -lc "python3 -c 'token=[REDACTED_SECRET]"apiToken\\"]; print(token)'"`;
341 expect(commandDisplayText(input)).toBe(input);
342 });
343});
344
345describe("commandDisplayText with Windows shell wrappers", () => {
346 it.each([
347 [
348 `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -Command "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'"`,
349 "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'",
350 ],
351 [
352 `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoLogo -NoProfile -Command 'rg -n -F "new Date()" .'`,
353 'rg -n -F "new Date()" .',
354 ],
355 ['cmd.exe /c "npm test"', "npm test"],
356 ['Bash.exe -lc "git status"', "git status"],
357 ['"C:\\Program Files\\Git\\bin\\Bash.exe" -lc "rg foo"', "rg foo"],
358 [
359 'powershell.exe -NoProf -NonI -ep Bypass -ExecutionPol Bypass -wd C:\\repo -Comm "git status"',
360 "git status",
361 ],
362 ['pwsh -v ignored -c "git status"', 'pwsh -v ignored -c "git status"'],
363 ['powershell -v 5.1 -c "git status"', "git status"],
364 ])("shows %j as %j", (input, expected) => {
365 expect(commandDisplayText(input)).toBe(expected);
366 });
367});
368
369describe("commandDisplayText with ANSI-C and locale quoting", () => {
370 it.each([
371 ["bash -c $'npm test'", "npm test"],
372 ["bash -c $'echo \\'hi\\'; ls'", "echo 'hi'; ls"],
373 ['bash -lc $"npm test"', "npm test"],
374 ])("shows %j as %j", (input, expected) => {
375 expect(commandDisplayText(input)).toBe(expected);
376 });
377});
378
379describe("commandDisplayText with line continuations", () => {
380 it.each([
381 ['bash \\\n-lc "git status"', "git status"],
382 ['bash -lc "git status \\\n--short"', "git status --short"],
383 ])("removes them as bash does: %j", (input, expected) => {
384 expect(commandDisplayText(input)).toBe(expected);
385 });
386
387 it.each([
388 ["bash -lc 'git status \\\n--short'", "git status \\\n--short"],
389 ["bash -c $'git status \\\n--short'", "git status \\\n--short"],
390 ['bash \\\n-lc "git status \\\r\n--short"', "git status \\\r\n--short"],
391 ])("keeps them where bash does: %j", (input, expected) => {
392 expect(commandDisplayText(input)).toBe(expected);
393 });
394
395 it("keeps Windows shell scripts on separate lines", () => {
396 expect(commandDisplayText('cmd.exe /c "dir C:\\\necho done"')).toMatch(
397 /^dir C:\\?\necho done$/u,
398 );
399 });
400});
401
402describe("commandDisplayText control characters", () => {
403 it.each([
404 ["printf '\u001b[31mred\u001b[0m'", "printf '␛[31mred␛[0m'"],
405 ["zsh -lc 'printf \"\u001b[1m\"'", 'printf "␛[1m"'],
406 ["printf 'a\\tb'\r\necho done", "printf 'a\\tb'\r\necho done"],
407 ])("shows %j as %j", (input, expected) => {
408 expect(commandDisplayText(input)).toBe(expected);
409 });
410});