-
Notifications
You must be signed in to change notification settings - Fork 734
/
helpers.ts
491 lines (432 loc) · 12.9 KB
/
helpers.ts
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import assert from "assert";
import {
createWriteStream,
mkdirSync,
mkdtempSync,
realpathSync,
rmSync,
} from "fs";
import crypto from "node:crypto";
import { tmpdir } from "os";
import path from "path";
import { setTimeout } from "timers/promises";
import { stripAnsi } from "@cloudflare/cli";
import { spawn } from "cross-spawn";
import { retry } from "helpers/retry";
import treeKill from "tree-kill";
import { fetch } from "undici";
import { expect, test as originalTest } from "vitest";
import { version } from "../package.json";
import type {
ChildProcess,
ChildProcessWithoutNullStreams,
SpawnOptionsWithoutStdio,
} from "child_process";
import type { Writable } from "stream";
import type { RunnerTestCase, Suite, Test } from "vitest";
export const C3_E2E_PREFIX = "tmp-e2e-c3";
export const keys = {
enter: "\x0d",
backspace: "\x7f",
escape: "\x1b",
up: "\x1b\x5b\x41",
down: "\x1b\x5b\x42",
right: "\x1b\x5b\x43",
left: "\x1b\x5b\x44",
};
const testEnv = {
...process.env,
// The following env vars are set to ensure that package managers
// do not use the same global cache and accidentally hit race conditions.
YARN_CACHE_FOLDER: "./.yarn/cache",
YARN_ENABLE_GLOBAL_CACHE: "false",
PNPM_HOME: "./.pnpm",
npm_config_cache: "./.npm/cache",
// unset the VITEST env variable as this causes e2e issues with some frameworks
VITEST: undefined,
};
export type PromptHandler = {
matcher: RegExp;
input:
| string[]
| {
type: "text";
chunks: string[];
assertErrorMessage?: string;
}
| {
type: "select";
target: RegExp | string;
assertDefaultSelection?: string;
assertDescriptionText?: string;
};
};
export type RunnerConfig = {
promptHandlers?: PromptHandler[];
argv?: string[];
quarantine?: boolean;
timeout?: number;
verifyDeploy: null | {
route: string;
expectedText: string;
};
verifyPreview: null | {
route: string;
expectedText: string;
};
};
export const runC3 = async (
argv: string[] = [],
promptHandlers: PromptHandler[] = [],
logStream: Writable,
) => {
const cmd = ["node", "./dist/cli.js", ...argv];
const proc = spawnWithLogging(cmd, { env: testEnv }, logStream);
const onData = (data: string) => {
handlePrompt(data);
};
// Clone the prompt handlers so we can consume them destructively
promptHandlers = [...promptHandlers];
// When changing selection, stdout updates but onData may not include the question itself
// so we store the current PromptHandler if we have already matched the question
let currentSelectDialog: PromptHandler | undefined;
const handlePrompt = (data: string) => {
const text = stripAnsi(data.toString());
const lines = text.split("\n");
const currentDialog = currentSelectDialog ?? promptHandlers[0];
if (!currentDialog) {
return;
}
const matchesPrompt = lines.some((line) =>
currentDialog.matcher.test(line),
);
// if we don't match the current question and we haven't already matched it previously
if (!matchesPrompt && !currentSelectDialog) {
return;
}
if (Array.isArray(currentDialog.input)) {
// keyboard input prompt handler
currentDialog.input.forEach((keystroke) => {
proc.stdin.write(keystroke);
});
} else if (currentDialog.input.type === "text") {
// text prompt handler
const { assertErrorMessage, chunks } = currentDialog.input;
if (
assertErrorMessage !== undefined &&
!text.includes(assertErrorMessage)
) {
throw new Error(
`The error message does not match; Expected "${assertErrorMessage}" but found "${text}".`,
);
}
chunks.forEach((keystroke) => {
proc.stdin.write(keystroke);
});
} else if (currentDialog.input.type === "select") {
// select prompt handler
// FirstFrame: The first onData call for the current select dialog
const isFirstFrameOfCurrentSelectDialog =
currentSelectDialog === undefined;
// Our select prompt options start with ○ / ◁ for unselected options and ● / ◀ for the current selection
const selectedOptionRegex = /^(●|◀)\s/;
const currentSelection = lines
.find((line) => line.match(selectedOptionRegex))
?.replace(selectedOptionRegex, "");
if (!currentSelection) {
// sometimes `lines` contain only the 'clear screen' ANSI codes and not the prompt options
return;
}
const { target, assertDefaultSelection, assertDescriptionText } =
currentDialog.input;
if (
isFirstFrameOfCurrentSelectDialog &&
assertDefaultSelection !== undefined &&
assertDefaultSelection !== currentSelection
) {
throw new Error(
`The default selection does not match; Expected "${assertDefaultSelection}" but found "${currentSelection}".`,
);
}
const matchesSelectionTarget =
typeof target === "string"
? currentSelection.includes(target)
: target.test(currentSelection);
const description = text.replaceAll("\n", " ");
if (
matchesSelectionTarget &&
assertDescriptionText !== undefined &&
!description.includes(assertDescriptionText)
) {
throw new Error(
`The description does not match; Expected "${assertDescriptionText}" but found "${description}".`,
);
}
if (matchesSelectionTarget) {
// matches selection, so hit enter
proc.stdin.write(keys.enter);
currentSelectDialog = undefined;
} else {
// target not selected, hit down and wait for stdout to update (onData will be called again)
proc.stdin.write(keys.down);
currentSelectDialog = currentDialog;
return;
}
}
// Consume the handler once we've used it
promptHandlers.shift();
// If we've consumed the last prompt handler, close the input stream
// Otherwise, the process wont exit properly
if (promptHandlers[0] === undefined) {
proc.stdin.end();
}
};
return waitForExit(proc, onData);
};
/**
* Spawn a child process and attach a handler that will log any output from
* `stdout` or errors from `stderr` to a dedicated log file.
*
* @param args The command and arguments as an array
* @param opts Additional options to be passed to the `spawn` call
* @param logStream A write stream to the log file for the test
* @returns the child process that was created
*/
export const spawnWithLogging = (
args: string[],
opts: SpawnOptionsWithoutStdio,
logStream: Writable,
) => {
const [cmd, ...argv] = args;
logStream.write(`\nRunning command: ${[cmd, ...argv].join(" ")}\n\n`);
const proc = spawn(cmd, argv, {
...opts,
env: {
...testEnv,
...opts.env,
},
});
proc.stdout.on("data", (data) => {
const lines: string[] = data.toString().split("\n");
lines.forEach(async (line) => {
const stripped = stripAnsi(line).trim();
if (stripped.length > 0) {
logStream.write(`${stripped}\n`);
}
});
});
proc.stderr.on("data", (data) => {
logStream.write(data);
});
return proc;
};
/**
* An async function that waits on a spawned process to run to completion, collecting
* any output or errors from `stdout` and `stderr`, respectively.
*
* @param proc The child process to wait for
* @param onData An optional handler to be called on `stdout.on('data')`
*/
export const waitForExit = async (
proc: ChildProcessWithoutNullStreams,
onData?: (chunk: string) => void,
) => {
const stdout: string[] = [];
const stderr: string[] = [];
await new Promise((resolve, rejects) => {
proc.stdout.on("data", (data) => {
stdout.push(data);
try {
if (onData) {
onData(data);
}
} catch (error) {
// Close the input stream so the process can exit properly
proc.stdin.end();
throw error;
}
});
proc.stderr.on("data", (data) => {
stderr.push(data);
});
proc.on("close", (code) => {
if (code === 0) {
resolve(null);
} else {
rejects({
code,
output: stdout.join("\n").trim(),
errors: stderr.join("\n").trim(),
});
}
});
proc.on("error", (exitCode) => {
rejects({
exitCode,
output: stdout.join("\n").trim(),
errors: stderr.join("\n").trim(),
});
});
});
return {
output: stdout.join("\n").trim(),
errors: stderr.join("\n").trim(),
};
};
export const createTestLogStream = (
opts: { experimental: boolean },
task: RunnerTestCase,
) => {
// The .ansi extension allows for editor extensions that format ansi terminal codes
const fileName = `${normalizeTestName(task)}.ansi`;
assert(task.suite, "Expected task.suite to be defined");
return createWriteStream(path.join(getLogPath(opts, task.suite), fileName), {
flags: "a",
});
};
export const recreateLogFolder = (
opts: { experimental: boolean },
suite: Suite,
) => {
// Clean the old folder if exists (useful for dev)
rmSync(getLogPath(opts, suite), {
recursive: true,
force: true,
});
mkdirSync(getLogPath(opts, suite), { recursive: true });
};
const getLogPath = (opts: { experimental: boolean }, suite: Suite) => {
const { file } = suite;
const suiteFilename = file
? path.basename(file.name).replace(".test.ts", "")
: "unknown";
return path.join(
"./.e2e-logs" + (opts.experimental ? "-experimental" : ""),
process.env.TEST_PM as string,
suiteFilename,
);
};
const normalizeTestName = (task: Test) => {
const baseName = task.name
.toLowerCase()
.replace(/\s+/g, "_") // replace any whitespace with `_`
.replace(/\W/g, ""); // strip special characters
// Ensure that each retry gets its own log file
const retryCount = task.result?.retryCount ?? 0;
const suffix = retryCount > 0 ? `_${retryCount}` : "";
return baseName + suffix;
};
export const testProjectDir = (suite: string, test: string) => {
const tmpDirPath =
process.env.E2E_PROJECT_PATH ??
realpathSync(mkdtempSync(path.join(tmpdir(), `c3-tests-${suite}`)));
const randomSuffix = crypto.randomBytes(4).toString("hex");
const baseProjectName = `${C3_E2E_PREFIX}${randomSuffix}`;
const getName = () =>
// Worker project names cannot be longer than 58 characters
`${baseProjectName}-${test.substring(0, 57 - baseProjectName.length)}`;
const getPath = () => path.join(tmpDirPath, getName());
const clean = () => {
try {
if (process.env.E2E_PROJECT_PATH) {
return;
}
realpathSync(mkdtempSync(path.join(tmpdir(), `c3-tests-${suite}`)));
const filepath = getPath();
rmSync(filepath, {
recursive: true,
force: true,
maxRetries: 10,
retryDelay: 100,
});
} catch (e) {
if (typeof e === "object" && e !== null && "code" in e) {
const code = e.code;
if (code === "EBUSY" || code === "ENOENT" || code === "ENOTEMPTY") {
return;
}
}
throw e;
}
};
return { getName, getPath, clean };
};
export const testDeploymentCommitMessage = async (
projectName: string,
framework: string,
) => {
const projectLatestCommitMessage = await retry({ times: 5 }, async () => {
// Wait for 2 seconds between each attempt
await setTimeout(2000);
// Note: we cannot simply run git and check the result since the commit can be part of the
// deployment even without git, so instead we fetch the deployment info from the pages api
const response = await fetch(
`https://api.cloudflare.com/client/v4/accounts/${process.env.CLOUDFLARE_ACCOUNT_ID}/pages/projects`,
{
headers: {
Authorization: `Bearer ${process.env.CLOUDFLARE_API_TOKEN}`,
},
},
);
const result = (
(await response.json()) as {
result: {
name: string;
latest_deployment?: {
deployment_trigger: {
metadata?: {
commit_message: string;
};
};
};
}[];
}
).result;
const commitMessage = result.find((project) => project.name === projectName)
?.latest_deployment?.deployment_trigger?.metadata?.commit_message;
if (!commitMessage) {
throw new Error("Could not find deployment with name " + projectName);
}
return commitMessage;
});
expect(projectLatestCommitMessage).toMatch(
/Initialize web application via create-cloudflare CLI/,
);
expect(projectLatestCommitMessage).toContain(
`C3 = create-cloudflare@${version}`,
);
expect(projectLatestCommitMessage).toContain(`project name = ${projectName}`);
expect(projectLatestCommitMessage).toContain(`framework = ${framework}`);
};
export const isQuarantineMode = () => {
return process.env.E2E_QUARANTINE === "true";
};
/**
* A custom Vitest `test` that is extended to provide a project path and name, and a logStream.
*/
export const test = (opts: { experimental: boolean }) =>
originalTest.extend<{
project: { path: string; name: string };
logStream: Writable;
}>({
async project({ task }, use) {
assert(task.suite, "Expected task.suite to be defined");
const suite = task.suite.name
.toLowerCase()
.replaceAll(/[^a-z0-9-]/g, "-");
const suffix = task.name.toLowerCase().replaceAll(/[^a-z0-9-]/g, "-");
const { getPath, getName, clean } = testProjectDir(suite, suffix);
await use({ path: getPath(), name: getName() });
clean();
},
async logStream({ task }, use) {
const logStream = createTestLogStream(opts, task);
await use(logStream);
logStream.close();
},
});
export function kill(proc: ChildProcess) {
return new Promise<void>(
(resolve) => proc.pid && treeKill(proc.pid, "SIGINT", () => resolve()),
);
}