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 ["env -a app git status", "git"],
165 ["env --argv0 app git status", "git"],
166 ["sudo --preserve-env=PATH vp test run", "vp"],
167 ["sudo --preserve-env vp test run", "vp"],
168 ["sudo --prompt='' --command-timeout=30 vp test run", "vp"],
169 ["sudo -Sp '' vp test run", "vp"],
170 ["sudo --non-interactive --stdin --login vp test run", "vp"],
171 ["exec -ca myname git status", "git"],
172 ["exec -cla myname git status", "git"],
173 ["exec -camyname git status", "git"],
174 ["exec -ca >out myname git status", "git"],
175 ["exec -ca <<<'first\nsecond' myname git status", "git"],
176 ["exec >out -ca myname git status", "git"],
177 ["exec -ca '>out' git status", "git"],
178 ])("unwraps process-launch wrappers: %s", (command, program) => {
179 expect(commandProgramName(command)).toBe(program);
180 });
181
182 it.each(["timeout -x 30 npm test", "timeout -s 30 npm test"])(
183 "keeps the timeout label for invalid timeout options: %s",
184 (command) => {
185 expect(commandProgramName(command)).toBe("timeout");
186 },
187 );
188
189 it.each([
190 "command -v git",
191 "exec > output.log",
192 "exec -ca",
193 "exec -ca myname",
194 "exec -ca >out myname",
195 "env CI=1 cd /tmp && npm test",
196 "$PY scripts/check.py",
197 "& { Get-Process }",
198 "coproc npm test",
199 "true || git --version",
200 "builtin true || git --version",
201 ": || git --version",
202 "builtin : || git --version",
203 "command : || git --version",
204 "cd /tmp || exit 1",
205 "cd /tmp && exit 1; make",
206 "cd /tmp || exit 1 || make",
207 "cd /tmp || exit 1 | make",
208 "false || exit 1; make",
209 "sudo --preserve-env=PATH",
210 ])("does not treat shell lookup and commandless wrapper forms as executions: %s", (command) => {
211 expect(commandProgramName(command)).toBeNull();
212 });
213
214 it.each([
215 ["hash --help", "hash"],
216 ["Exec node app.js", "Exec"],
217 ["CD /tmp && npm test", "CD"],
218 ])("does not hide legitimate or case-distinct program names: %s", (command, program) => {
219 expect(commandProgramName(command)).toBe(program);
220 });
221
222 it("uses the command after leading shell comments", () => {
223 expect(commandProgramName("# first comment\n # second comment\ngit status")).toBe("git");
224 });
225
226 it.each([
227 ["echo ${a:- #1}", "echo"],
228 ["x=${a:-$( # '\n printf hi\n) #1} # comment; npm test\ngit status", "git"],
229 ["x=${a:-<( # '\n printf hi\n) #1}; git status", "git"],
230 ["x=${a:-$(printf %s }) #1}; git status", "git"],
231 ])("keeps hashes inside unquoted parameter expansions: %j", (command, program) => {
232 expect(commandProgramName(command)).toBe(program);
233 });
234
235 it.each([
236 [">/tmp/log && npm test", "npm"],
237 ["cd /tmp && 2>& 1 node app.js", "node"],
238 ["cd /tmp && {output}>/tmp/log deno test", "deno"],
239 ["whoami>/dev/null", "whoami"],
240 ["cat</etc/hosts", "cat"],
241 ["whoami&>/dev/null", "whoami"],
242 ["2>|/dev/null git --version", "git"],
243 ["cd /tmp && >|/tmp/log deno test", "deno"],
244 ["'my>tool' arg", "my>tool"],
245 ["my\\>tool arg", "my>tool"],
246 ])("handles redirections around commands: %s", (command, program) => {
247 expect(commandProgramName(command)).toBe(program);
248 });
249
250 it.each([
251 ["cd /tmp <<-EOF\n\tnot-a-command\n\tEOF\nbun test", "bun"],
252 ["cd /tmp <<A <<B\none\nA\ntwo\nB\ngit status", "git"],
253 ["cd /tmp <<EOF && pnpm test\nnot-a-command\nEOF\nbun test", "pnpm"],
254 ["cd . <<''\npretend\n\nprintf ok", "printf"],
255 ['cd . <<""\npretend\n\nprintf ok', "printf"],
256 [": <<< hi\nprintf '%s' visible", "printf"],
257 [": <<<-hi && printf '%s' visible", "printf"],
258 [': <<"E\\X"\nhello\nE\\X\nprintf ok', "printf"],
259 [': <<"E\\"X"\nhello\nE"X\nprintf ok', "printf"],
260 ])("skips heredoc bodies before finding the next command: %s", (command, program) => {
261 expect(commandProgramName(command)).toBe(program);
262 });
263
264 it.each([
265 [": <<EOF\nbody\\\nEOF\necho wrong\nEOF\nprintf ok\n", "printf"],
266 [": <<-EOF\nbody\\\n\tEOF\necho wrong\n\tEOF\nprintf ok\n", "printf"],
267 [": <<'EOF'\nbody\\\nEOF\nprintf ok\n", "printf"],
268 [": <<EOF\nbody\\\\\nEOF\nprintf ok\n", "printf"],
269 [": <<EOF\nEO\\\nF\nprintf ok\n", null],
270 ["dash -c ': <<EOF\nEO\\\nF\nprintf ok\n'", null],
271 ])(
272 "joins continued lines before matching an unquoted heredoc delimiter: %s",
273 (command, program) => {
274 expect(commandProgramName(command)).toBe(program);
275 },
276 );
277
278 it("bounds nested shell unwrapping", () => {
279 let command = "git status";
280 for (let depth = 0; depth < 9; depth += 1) {
281 command = `sh -c '${command.replaceAll("'", "'\\''")}'`;
282 }
283 expect(commandProgramName(command)).toBeNull();
284 });
285
286 it("bounds the number of top-level setup segments", () => {
287 const command = [
288 ...Array.from({ length: 2_000 }, (_, index) => `export VALUE_${index}=configured`),
289 "npm test",
290 ].join(";");
291
292 expect(commandProgramName(command)).toBeNull();
293 });
294});
295
296describe("commandDisplayText with shell option terminators", () => {
297 it.each([
298 ["sh -co errexit 'git status'", "git", "git status"],
299 ["bash -cO extglob 'pytest'", "pytest", "pytest"],
300 ["bash -oOc pipefail extglob -- 'printf ok'", "printf", "printf ok"],
301 ["bash -co pipefail -c 'printf ok'", "printf", "printf ok"],
302 ["zsh -bc '-tool'", "-tool", "-tool"],
303 ["zsh +bc 'git status'", "git", "git status"],
304 ])("unwraps -c bundled with other shell options: %s", (input, program, script) => {
305 expect.soft(commandProgramName(input)).toBe(program);
306 expect.soft(commandDisplayText(input)).toBe(script);
307 });
308
309 it.each([
310 ['bash -lc -- "printf ok"', "printf ok"],
311 ['bash -eo pipefail -c "printf ok"', "printf ok"],
312 ['bash -lc "echo $(echo "hello world")"', 'echo $(echo "hello world")'],
313 ['fish -c -- "printf ok"', 'fish -c -- "printf ok"'],
314 ['bash -c -- "printf ok" arg0', 'bash -c -- "printf ok" arg0'],
315 ["ksh -onoclobber run.sh", "ksh -onoclobber run.sh"],
316 ])("shows %j as %j", (input, expected) => {
317 expect(commandDisplayText(input)).toBe(expected);
318 });
319});
320
321describe("commandDisplayText with trailing shell separators", () => {
322 it.each(["bash -c 'echo hi'&", "bash -c 'echo hi';"])("keeps the wrapper in %s", (input) => {
323 expect(commandDisplayText(input)).toBe(input);
324 });
325});
326
327describe("commandDisplayText with redacted Codex commands", () => {
328 // Codex replaces secrets in command text before sending it
329 // (codex-rs/secrets/src/sanitizer.rs). Usually the quoting survives.
330 it("unwraps a redacted command whose quoting is intact", () => {
331 expect(
332 commandDisplayText(
333 `/bin/zsh -lc 'curl -H "Authorization: Bearer [REDACTED_SECRET]" https://example.com'`,
334 ),
335 ).toBe('curl -H "Authorization: Bearer [REDACTED_SECRET]" https://example.com');
336 });
337
338 // Its secret-assignment pattern can also swallow the backslash of an escaped
339 // quote: `token=config[\"apiToken\"]` becomes `token=[REDACTED_SECRET]"apiToken\"]`.
340 // The bare quote ends the -lc argument early, so where the script ends is unknown.
341 it("keeps the wrapper when redaction broke the quoting", () => {
342 const input = `/bin/zsh -lc "python3 -c 'token=[REDACTED_SECRET]"apiToken\\"]; print(token)'"`;
343 expect(commandDisplayText(input)).toBe(input);
344 });
345});
346
347describe("commandDisplayText with Windows shell wrappers", () => {
348 it.each([
349 [
350 `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -Command "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'"`,
351 "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'",
352 ],
353 [
354 `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -NoLogo -NoProfile -Command 'rg -n -F "new Date()" .'`,
355 'rg -n -F "new Date()" .',
356 ],
357 ['cmd.exe /c "npm test"', "npm test"],
358 ['Bash.exe -lc "git status"', "git status"],
359 ['"C:\\Program Files\\Git\\bin\\Bash.exe" -lc "rg foo"', "rg foo"],
360 [
361 'powershell.exe -NoProf -NonI -ep Bypass -ExecutionPol Bypass -wd C:\\repo -Comm "git status"',
362 "git status",
363 ],
364 ['pwsh -v ignored -c "git status"', 'pwsh -v ignored -c "git status"'],
365 ['powershell -v 5.1 -c "git status"', "git status"],
366 ])("shows %j as %j", (input, expected) => {
367 expect(commandDisplayText(input)).toBe(expected);
368 });
369});
370
371describe("commandDisplayText with ANSI-C and locale quoting", () => {
372 it.each([
373 ["bash -c $'npm test'", "npm test"],
374 ["bash -c $'echo \\'hi\\'; ls'", "echo 'hi'; ls"],
375 ['bash -lc $"npm test"', "npm test"],
376 ])("shows %j as %j", (input, expected) => {
377 expect(commandDisplayText(input)).toBe(expected);
378 });
379});
380
381describe("commandDisplayText with line continuations", () => {
382 it.each([
383 ['bash \\\n-lc "git status"', "git status"],
384 ['bash -lc "git status \\\n--short"', "git status --short"],
385 ])("removes them as bash does: %j", (input, expected) => {
386 expect(commandDisplayText(input)).toBe(expected);
387 });
388
389 it.each([
390 ["bash -lc 'git status \\\n--short'", "git status \\\n--short"],
391 ["bash -c $'git status \\\n--short'", "git status \\\n--short"],
392 ['bash \\\n-lc "git status \\\r\n--short"', "git status \\\r\n--short"],
393 ])("keeps them where bash does: %j", (input, expected) => {
394 expect(commandDisplayText(input)).toBe(expected);
395 });
396
397 it("keeps Windows shell scripts on separate lines", () => {
398 expect(commandDisplayText('cmd.exe /c "dir C:\\\necho done"')).toMatch(
399 /^dir C:\\?\necho done$/u,
400 );
401 });
402});
403
404describe("commandDisplayText control characters", () => {
405 it.each([
406 ["printf '\u001b[31mred\u001b[0m'", "printf '␛[31mred␛[0m'"],
407 ["zsh -lc 'printf \"\u001b[1m\"'", 'printf "␛[1m"'],
408 ["printf 'a\\tb'\r\necho done", "printf 'a\\tb'\r\necho done"],
409 ])("shows %j as %j", (input, expected) => {
410 expect(commandDisplayText(input)).toBe(expected);
411 });
412});