-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathcliExec.test.ts
More file actions
237 lines (212 loc) · 7.19 KB
/
cliExec.test.ts
File metadata and controls
237 lines (212 loc) · 7.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import fs from "fs/promises";
import os from "os";
import path from "path";
import { beforeAll, describe, expect, it, vi } from "vitest";
import { MockConfigurationProvider } from "../../mocks/testHelpers";
import { isWindows, quoteCommand, writeExecutable } from "../../utils/platform";
import type { CliEnv } from "@/core/cliExec";
// Shim execFile so .js test scripts are run through node cross-platform.
vi.mock("node:child_process", async (importOriginal) => {
const { shimExecFile } = await import("../../utils/platform");
return shimExecFile(await importOriginal());
});
// Import after mock so the module picks up the shimmed execFile.
const cliExec = await import("@/core/cliExec");
describe("cliExec", () => {
const tmp = path.join(os.tmpdir(), "vscode-coder-tests-cliExec");
let echoArgsBin: string;
beforeAll(async () => {
await fs.rm(tmp, { recursive: true, force: true });
await fs.mkdir(tmp, { recursive: true });
const code = `process.argv.slice(2).forEach(a => console.log(a));`;
echoArgsBin = await writeExecutable(tmp, "echo-args", code);
});
function setup(auth: CliEnv["auth"], binary = echoArgsBin) {
const configs = new MockConfigurationProvider();
const env: CliEnv = { binary, auth, configs };
return { configs, env };
}
/** JS code for a fake CLI that writes a fixed string to stdout. */
function echoBin(output: string): string {
return `process.stdout.write(${JSON.stringify(output)});`;
}
/**
* JS code for a fake old CLI that rejects --output with stderr,
* and otherwise writes to stdout.
*/
function oldCliBin(stderr: string, stdout: string): string {
return [
`if (process.argv.includes("--output")) {`,
` process.stderr.write(${JSON.stringify(stderr)});`,
` process.exitCode = 1;`,
`} else {`,
` process.stdout.write(${JSON.stringify(stdout)});`,
`}`,
].join("\n");
}
describe("version", () => {
it("throws when binary does not exist", async () => {
const missing = path.join(tmp, "nonexistent");
await expect(cliExec.version(missing)).rejects.toThrow("ENOENT");
});
it.skipIf(isWindows())("throws when binary is not executable", async () => {
const noExec = path.join(tmp, "version-noperm");
await fs.writeFile(noExec, "hello");
await expect(cliExec.version(noExec)).rejects.toThrow("EACCES");
});
it("throws on invalid JSON output", async () => {
const bin = await writeExecutable(tmp, "ver-bad-json", echoBin("hello"));
await expect(cliExec.version(bin)).rejects.toThrow("Unexpected token");
});
it("throws when JSON has no version field", async () => {
const bin = await writeExecutable(tmp, "ver-no-field", echoBin("{}"));
await expect(cliExec.version(bin)).rejects.toThrow(
"No version found in output",
);
});
it("parses version from JSON output", async () => {
const bin = await writeExecutable(
tmp,
"ver-ok",
echoBin(JSON.stringify({ version: "v0.0.0" })),
);
expect(await cliExec.version(bin)).toBe("v0.0.0");
});
it("re-throws non-output errors from old CLI", async () => {
const bin = await writeExecutable(
tmp,
"ver-old-err",
oldCliBin("foobar", "Coder v1.1.1"),
);
await expect(cliExec.version(bin)).rejects.toThrow("foobar");
});
it("falls back to plain version for old CLI", async () => {
const bin = await writeExecutable(
tmp,
"ver-old-ok",
oldCliBin("unknown flag: --output", "Coder v1.1.1"),
);
expect(await cliExec.version(bin)).toBe("v1.1.1");
});
it("trims trailing newlines from old CLI", async () => {
const bin = await writeExecutable(
tmp,
"ver-old-trim",
oldCliBin("unknown flag: --output\n", "Coder v1.1.1\n"),
);
expect(await cliExec.version(bin)).toBe("v1.1.1");
});
it("re-throws when old CLI output is not Coder", async () => {
const bin = await writeExecutable(
tmp,
"ver-old-unrelated",
oldCliBin("unknown flag: --output", "Unrelated"),
);
await expect(cliExec.version(bin)).rejects.toThrow("unknown flag");
});
it("throws when old CLI has no version after Coder prefix", async () => {
const bin = await writeExecutable(
tmp,
"ver-old-noversion",
oldCliBin("unknown flag: --output", "Coder"),
);
await expect(cliExec.version(bin)).rejects.toThrow("No version found");
});
});
describe("speedtest", () => {
it("passes global, header, and command-specific flags", async () => {
const { configs, env } = setup({
mode: "url",
url: "http://localhost:3000",
});
configs.set("coder.headerCommand", "my-header-cmd");
const args = (await cliExec.speedtest(env, "owner/workspace", "10s"))
.trim()
.split("\n");
expect(args).toEqual([
"--url",
"http://localhost:3000",
"--header-command",
quoteCommand("my-header-cmd"),
"speedtest",
"owner/workspace",
"--output",
"json",
"-t",
"10s",
]);
});
it("surfaces stderr instead of full command line on failure", async () => {
const code = [
`process.stderr.write("invalid argument for -t flag\\n");`,
`process.exitCode = 1;`,
].join("\n");
const bin = await writeExecutable(tmp, "speedtest-err", code);
const { env } = setup({ mode: "global-config", configDir: "/tmp" }, bin);
await expect(
cliExec.speedtest(env, "owner/workspace", "bad"),
).rejects.toThrow("invalid argument for -t flag");
});
});
describe("supportBundle", () => {
it("passes global, header, and command-specific flags", async () => {
// Use a binary that writes args to the --output-file path
// so we can verify them after the void-returning function completes.
const code = [
`const args = process.argv.slice(2);`,
`const idx = args.indexOf("--output-file");`,
`if (idx !== -1) { require("fs").writeFileSync(args[idx+1], args.join("\\n")); }`,
].join("\n");
const bin = await writeExecutable(tmp, "sb-echo-args", code);
const outputPath = path.join(tmp, "sb-args-output.zip");
const { configs, env } = setup(
{ mode: "url", url: "http://localhost:3000" },
bin,
);
configs.set("coder.headerCommand", "my-header-cmd");
await cliExec.supportBundle(env, "owner/workspace", outputPath);
const args = (await fs.readFile(outputPath, "utf-8")).trim().split("\n");
expect(args).toEqual([
"--url",
"http://localhost:3000",
"--header-command",
quoteCommand("my-header-cmd"),
"support",
"bundle",
"owner/workspace",
"--output-file",
outputPath,
"--yes",
]);
});
it("surfaces stderr on failure", async () => {
const code = [
`process.stderr.write("workspace not found\\n");`,
`process.exitCode = 1;`,
].join("\n");
const bin = await writeExecutable(tmp, "sb-err", code);
const { env } = setup({ mode: "global-config", configDir: "/tmp" }, bin);
await expect(
cliExec.supportBundle(env, "owner/workspace", "/tmp/bundle.zip"),
).rejects.toThrow("workspace not found");
});
});
describe("isGoDuration", () => {
it.each([
"5s",
"10m",
"1h",
"1h30m",
"500ms",
"1.5s",
"2h45m10s",
"100ns",
"50us",
"50µs",
])("accepts %s", (v) => expect(cliExec.isGoDuration(v)).toBe(true));
it.each(["", "bjbmn", "5", "s", "5x", "1h 30m", "-5s", "5S"])(
"rejects %s",
(v) => expect(cliExec.isGoDuration(v)).toBe(false),
);
});
});