forked from cloudflare/workers-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtemplates.test.ts
320 lines (277 loc) · 7.86 KB
/
templates.test.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
import { existsSync, statSync } from "fs";
import { spinner } from "@cloudflare/cli/interactive";
import degit from "degit";
import { mockSpinner } from "helpers/__tests__/mocks";
import {
appendFile,
directoryExists,
readFile,
writeFile,
} from "helpers/files";
import { beforeEach, describe, expect, test, vi } from "vitest";
import {
addWranglerToGitIgnore,
deriveCorrelatedArgs,
downloadRemoteTemplate,
} from "../templates";
import type { PathLike } from "fs";
import type { C3Args, C3Context } from "types";
vi.mock("degit");
vi.mock("fs");
vi.mock("helpers/files");
vi.mock("@cloudflare/cli/interactive");
beforeEach(() => {
mockSpinner();
});
describe("addWranglerToGitIgnore", () => {
const writeFileResults: {
file: string | undefined;
content: string | undefined;
} = { file: undefined, content: undefined };
const appendFileResults: {
file: string | undefined;
content: string | undefined;
} = { file: undefined, content: undefined };
beforeEach(() => {
vi.mocked(writeFile).mockImplementation((file: string, content: string) => {
writeFileResults.file = file;
writeFileResults.content = content;
});
vi.mocked(appendFile).mockImplementation(
(file: string, content: string) => {
appendFileResults.file = file;
appendFileResults.content = content;
},
);
});
beforeEach(() => {
vi.mocked(statSync).mockImplementation(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
(path: string) => ({
isDirectory() {
return path.endsWith(".git");
},
}),
);
vi.mocked(existsSync).mockReset();
vi.mocked(readFile).mockReset();
vi.mocked(directoryExists).mockReset();
appendFileResults.file = undefined;
appendFileResults.content = undefined;
writeFileResults.file = undefined;
writeFileResults.content = undefined;
});
test("should append the wrangler section to a standard gitignore file", () => {
mockGitIgnore(
"my-project/.gitignore",
`
node_modules
.vscode`,
);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
expect(appendFileResults.file).toMatchInlineSnapshot(
`"my-project/.gitignore"`,
);
expect(appendFileResults.content).toMatchInlineSnapshot(`
"
# wrangler files
.wrangler
.dev.vars
"
`);
});
test("should not touch the gitignore file if it already contains all wrangler files", () => {
mockGitIgnore(
"my-project/.gitignore",
`
node_modules
.dev.vars
.vscode
.wrangler
`,
);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
expect(appendFileResults.file).toBeUndefined();
expect(appendFileResults.content).toBeUndefined();
});
test("should not touch the gitignore file if contains all wrangler files (and can cope with comments)", () => {
mockGitIgnore(
"my-project/.gitignore",
`
node_modules
.wrangler # This is for wrangler
.dev.vars # this is for wrangler and getPlatformProxy
.vscode
`,
);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
expect(appendFileResults.file).toBeUndefined();
expect(appendFileResults.content).toBeUndefined();
});
test("should append to the gitignore file the missing wrangler files when some is already present (without including the section heading)", () => {
mockGitIgnore(
"my-project/.gitignore",
`
node_modules
.dev.vars
.vscode`,
);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
expect(appendFileResults.file).toMatchInlineSnapshot(
`"my-project/.gitignore"`,
);
expect(appendFileResults.content).toMatchInlineSnapshot(`
"
.wrangler
"
`);
});
test("when it appends to the gitignore file it doesn't include an empty line only if there was one already", () => {
mockGitIgnore(
"my-project/.gitignore",
`
node_modules
.dev.vars
.vscode
`,
);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
expect(appendFileResults.file).toMatchInlineSnapshot(
`"my-project/.gitignore"`,
);
expect(appendFileResults.content).toMatchInlineSnapshot(`
"
.wrangler
"
`);
});
test("should create the gitignore file if it didn't exist already", () => {
// let's mock a gitignore file to be read by readFile
mockGitIgnore("my-project/.gitignore", "");
// but let's pretend that it doesn't exist
vi.mocked(existsSync).mockImplementation(() => false);
// let's also pretend that the .git directory exists
vi.mocked(directoryExists).mockImplementation(() => true);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
// writeFile wrote the (empty) gitignore file
expect(writeFileResults.file).toMatchInlineSnapshot(
`"my-project/.gitignore"`,
);
expect(writeFileResults.content).toMatchInlineSnapshot(`""`);
// and the correct lines were then added to it
expect(appendFileResults.file).toMatchInlineSnapshot(
`"my-project/.gitignore"`,
);
expect(appendFileResults.content).toMatchInlineSnapshot(`
"
# wrangler files
.wrangler
.dev.vars
"
`);
});
test("should not create the gitignore file the project doesn't use git", () => {
// no .gitignore file exists
vi.mocked(existsSync).mockImplementation(() => false);
// neither a .git directory does
vi.mocked(directoryExists).mockImplementation(() => false);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
expect(writeFileResults.file).toBeUndefined();
expect(writeFileResults.content).toBeUndefined();
});
test("should not add the .wrangler entry if a .wrangler/ is already included)", () => {
mockGitIgnore(
"my-project/.gitignore",
`
node_modules
.wrangler/ # This is for wrangler
.vscode
`,
);
addWranglerToGitIgnore({
project: { path: "my-project" },
} as unknown as C3Context);
expect(appendFileResults.file).toMatchInlineSnapshot(
`"my-project/.gitignore"`,
);
expect(appendFileResults.content).toMatchInlineSnapshot(`
"
.dev.vars
"
`);
});
function mockGitIgnore(path: string, content: string) {
vi.mocked(existsSync).mockImplementation(
(filePath: PathLike) => filePath === path,
);
vi.mocked(readFile).mockImplementation((filePath: string) =>
filePath === path ? content.replace(/\n\s*/g, "\n") : "",
);
}
});
describe("downloadRemoteTemplate", () => {
function mockDegit() {
// @ts-expect-error only clone will be used
return vi.mocked(degit).mockReturnValue({
clone: () => Promise.resolve(),
});
}
test("should download template using degit", async () => {
const mock = mockDegit();
await downloadRemoteTemplate("cloudflare/workers-sdk");
expect(mock).toBeCalled();
});
test("should not use a spinner", async () => {
// Degit runs `git clone` internally which might prompt for credentials
// A spinner will suppress the prompt and keep the CLI waiting in the cloning stage
mockDegit();
await downloadRemoteTemplate("cloudflare/workers-sdk");
expect(spinner).not.toBeCalled();
});
});
describe("deriveCorrelatedArgs", () => {
test("should derive the lang as TypeScript if `--ts` is specified", () => {
const args: Partial<C3Args> = {
ts: true,
};
deriveCorrelatedArgs(args);
expect(args.lang).toBe("ts");
});
test("should derive the lang as JavaScript if `--ts=false` is specified", () => {
const args: Partial<C3Args> = {
ts: false,
};
deriveCorrelatedArgs(args);
expect(args.lang).toBe("js");
});
test("should crash if both the lang and ts arguments are specified", () => {
expect(() =>
deriveCorrelatedArgs({
lang: "ts",
}),
).not.toThrow();
expect(() =>
deriveCorrelatedArgs({
ts: true,
lang: "ts",
}),
).toThrow(
"The `--ts` argument cannot be specified in conjunction with the `--lang` argument",
);
});
});