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