Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { embeddedScripts } from "./embeddedScripts";
4
5function scriptsOf(command: string) {
6 return embeddedScripts(command).map((script) => {
7 script.starts.forEach((start, index) => {
8 expect(start).toBeLessThan(script.ends[index]!);
9 if (index > 0) expect(start).toBeGreaterThanOrEqual(script.ends[index - 1]!);
10 });
11 return [
12 script.language,
13 script.text,
14 command.slice(script.starts[0], script.ends.at(-1)),
15 ] as const;
16 });
17}
18
19const pythonScript = `from pathlib import Path
20import json
21
22# print every package.json name and version
23for p in sorted(Path('.').glob('**/package.json')):
24 data = json.loads(p.read_text())
25 print(f"{data['name']:16} {data.get('version', '?')}")
26`;
27
28describe("embeddedScripts", () => {
29 it("finds the script inside a Codex login-shell wrapper and the script inside that", () => {
30 const inner = `console.log('interrupt fixture tool started'); setTimeout(() => {}, 30000)`;
31 const command = `/bin/bash -lc "node -e \\"${inner}\\""`;
32 expect(scriptsOf(command)).toEqual([
33 ["shellscript", `node -e "${inner}"`, `node -e \\"${inner}\\"`],
34 ["javascript", inner, inner],
35 ]);
36 });
37
38 it("highlights a heredoc fed to an interpreter in that interpreter's language", () => {
39 const command = `python3 - <<'PY'\n${pythonScript}PY`;
40 expect(scriptsOf(command)).toEqual([["python", pythonScript, pythonScript]]);
41 });
42
43 it("maps escaped characters back to both source characters", () => {
44 const command = `/bin/zsh -lc "python3 - <<'PY'\nprint(\\"\\$HOME\\")\nPY"`;
45 const [shell, python] = scriptsOf(command);
46 expect(shell?.[1]).toBe(`python3 - <<'PY'\nprint("$HOME")\nPY`);
47 expect(python).toEqual(["python", `print("$HOME")\n`, `print(\\"\\$HOME\\")\n`]);
48 });
49
50 it.each([
51 [`python3 -c 'print(1)'`, "python", "print(1)"],
52 [`python3 -X dev -c 'print(1)'`, "python", "print(1)"],
53 [`py -3 -c 'print(1)'`, "python", "print(1)"],
54 [`C:\\Python312\\python.exe -c 'print(1)'`, "python", "print(1)"],
55 [`"C:\\Program Files\\Python312\\python.exe" -c 'print(1)'`, "python", "print(1)"],
56 [`node --eval "process.exit(0)"`, "javascript", "process.exit(0)"],
57 [`bun -e 'console.log(Bun.version)'`, "typescript", "console.log(Bun.version)"],
58 [`deno eval 'console.log(1)'`, "typescript", "console.log(1)"],
59 [`deno eval --config deno.json 'console.log(1)'`, "typescript", "console.log(1)"],
60 [`deno eval --env-file 'console.log(1)'`, "typescript", "console.log(1)"],
61 [`psql -f schema.sql -c 'select 1'`, "sql", "select 1"],
62 [`node -C development -e 'console.log(1)'`, "javascript", "console.log(1)"],
63 [`ruby -e 'puts 1'`, "ruby", "puts 1"],
64 [`ruby -I lib -e 'puts 1'`, "ruby", "puts 1"],
65 [`perl -E 'say 1'`, "perl", "say 1"],
66 [`sqlite3 app.db "select * from users"`, "sql", "select * from users"],
67 [`psql -d app -c 'select 1'`, "sql", "select 1"],
68 [`pwsh -NoProfile -Command "Get-ChildItem"`, "powershell", "Get-ChildItem"],
69 [`env FOO=1 sudo -u bob python3 -c 'print(1)'`, "python", "print(1)"],
70 [`env -- FOO=1 python3 -c 'print(1)'`, "python", "print(1)"],
71 [`node --eval="process.exit(0)"`, "javascript", "process.exit(0)"],
72 [`psql --command='select 1'`, "sql", "select 1"],
73 [`mysql -c -e 'select 1'`, "sql", "select 1"],
74 [`mysql -f app <<'EOF'\nselect 1;\nEOF`, "sql", "select 1;\n"],
75 [`psql app <<'EOF'\nselect 1;\nEOF`, "sql", "select 1;\n"],
76 [`echo "$(node -e 'console.log(1)')"`, "javascript", "console.log(1)"],
77 [`python3 <<< 'print(1)'`, "python", "print(1)"],
78 ])("finds the inline script in %s", (command, language, script) => {
79 expect(scriptsOf(command)).toEqual([[language, script, expect.any(String)]]);
80 });
81
82 it.each([[`psql -c 'select 1' -c 'select 2'`], [`sqlite3 app.db "select 1" "select 2"`]])(
83 "finds every statement a SQL shell runs: %s",
84 (command) => {
85 expect(scriptsOf(command).map(([language, text]) => [language, text])).toEqual([
86 ["sql", "select 1"],
87 ["sql", "select 2"],
88 ]);
89 },
90 );
91
92 it.each([
93 ["the file `cat` writes", `cat > src/app.ts <<'EOF'\nconst a = 1;\nEOF`, "typescript"],
94 ["the file `tee` writes", `tee -a config.json <<EOF\n{}\nEOF`, "json"],
95 ["its delimiter", `kubectl apply -f - <<'YAML'\nkind: Pod\nYAML`, "yaml"],
96 ["the shell reading it", `bash <<'EOF'\necho hi\nEOF`, "shellscript"],
97 ])("takes a heredoc's language from %s", (_source, command, language) => {
98 expect(scriptsOf(command).map(([lang]) => lang)).toEqual([language]);
99 });
100
101 it.each([
102 ["a script file", `python3 build.py <<EOF\ndata\nEOF`],
103 ["a module", `python3 -m json.tool <<EOF\n{}\nEOF`],
104 ["a SQL file", `psql -f migrate.sql <<EOF\ny\nEOF`],
105 ["a SQL file given with =", `psql --file=migrate.sql <<EOF\ny\nEOF`],
106 ["plain commands", `rg -n "foo" src && git status`],
107 ["an unnamed heredoc into a plain command", `git commit -F - <<'EOF'\nfix: x\nEOF`],
108 ["an empty script", `bash -lc ' '`],
109 ["a heredoc `cat` sends to stdout while stderr goes to a file", `cat 2>log.ts <<'EOF'\nx\nEOF`],
110 ])("finds nothing for %s", (_case, command) => {
111 expect(scriptsOf(command)).toEqual([]);
112 });
113
114 it("trusts nothing in a command whose quoting does not parse", () => {
115 // Codex's secret redaction dropped the backslash of an escaped quote, so
116 // the -lc argument ends early and the rest no longer balances.
117 const command = `/bin/zsh -lc "python3 -c 'token=[REDACTED_SECRET]"apiToken\\"]; print(token)'"`;
118 expect(scriptsOf(command)).toEqual([]);
119 });
120});