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