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

commandLabel.test.ts

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