-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathhelpers.ts
More file actions
64 lines (56 loc) · 1.53 KB
/
helpers.ts
File metadata and controls
64 lines (56 loc) · 1.53 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
import { stripAnsi } from "@cloudflare/cli-shared-helpers";
import { stderr, stdout } from "@cloudflare/cli-shared-helpers/streams";
import { C3_DEFAULTS } from "helpers/cli";
import { afterEach, beforeEach } from "vitest";
import type { TemplateConfig } from "../templates";
import type { C3Args, C3Context } from "types";
export const createTestArgs = (args?: Partial<C3Args>) => {
return {
...C3_DEFAULTS,
...args,
};
};
export const createTestContext = (name = "test", args?: C3Args): C3Context => {
const path = `./${name}`;
return {
project: { name, path },
args: args ?? createTestArgs(),
originalCWD: path,
gitRepoAlreadyExisted: false,
template: createTestTemplate(),
deployment: {},
};
};
export const createTestTemplate = (
config?: Partial<TemplateConfig>
): TemplateConfig => {
return {
...config,
id: "test",
platform: "workers",
displayName: "Test Template",
configVersion: 1,
generate: Promise.resolve,
};
};
export function collectCLIOutput() {
const std = { out: "", err: "" };
const onStdOutData = (chunk: Buffer) => (std.out += chunk.toString());
const onStdErrData = (chunk: Buffer) => (std.err += chunk.toString());
beforeEach(() => {
stdout.on("data", onStdOutData);
stderr.on("data", onStdErrData);
});
afterEach(() => {
stdout.off("data", onStdOutData);
stderr.off("data", onStdErrData);
std.out = "";
std.err = "";
});
return std;
}
export function normalizeOutput(output: string) {
return stripAnsi(output)
.replace(/\\/g, "/")
.replaceAll(/\u200a|\u200b/g, " ");
}