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

commandLabel.test.ts

367 lines14.7 KB

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