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