forked from 18F/charlie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lts.test.js
274 lines (228 loc) · 8.25 KB
/
lts.test.js
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
const fs = require("fs/promises");
const lts = require("./lts");
jest.mock("fs/promises");
const {
main,
getCurrentLTSVersion,
getPackageNodeVersion,
getDockerNodeVersion,
getWorkflowLinesWithInvalidNodeVersion,
} = lts;
describe("Charlie's Node.js LTS checker", () => {
beforeEach(() => {
jest.resetAllMocks();
});
describe("gets the current LTS version", () => {
const REAL_FETCH = global.fetch;
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(Date.parse("2022-10-31"));
global.fetch = jest.fn("fetch");
});
afterAll(() => {
global.fetch = REAL_FETCH;
jest.useRealTimers();
});
it("from the Node.js roadmap", async () => {
global.fetch.mockImplementation(async () => ({
json: async () => ({
// Throw in a higher version with no LTS, to test that it gets ignored
v22: {},
// Put the versions out of order to test that we get the highest one
// regardless of order or actual LTS date. (There's no good reason a
// higher version should ever have an earlier date, so the code just
// assumes it'll never be that way. We can accept that assumption.)
v20: {
lts: "2020-02-02",
},
v19: {
lts: "2021-02-02",
},
// And one version that has an LTS in the future (based on our faked
// timers above) to test that it also gets filtered out.
v23: {
lts: "2024-04-04",
},
}),
}));
const version = await getCurrentLTSVersion();
expect(version).toEqual(20);
expect(global.fetch.mock.calls[0][0]).toEqual(
"https://raw.githubusercontent.com/nodejs/Release/main/schedule.json",
);
});
});
describe("gets the package Node version", () => {
it("from package.json", async () => {
fs.readFile.mockResolvedValue(
JSON.stringify({
engines: { node: "^1527.54.2" },
}),
);
const version = await getPackageNodeVersion();
expect(version).toEqual(1527);
expect(fs.readFile).toHaveBeenCalledWith("./package.json");
});
it("with an invalid version if the package doesn't specify a version", async () => {
fs.readFile.mockResolvedValue("{}");
const version = await getPackageNodeVersion();
expect(version).toEqual("not set");
});
});
describe("gets the Node version used for Docker", () => {
it("from Dockerfile", async () => {
fs.readFile.mockResolvedValue(`# This is a comment
# make sure it gets handled
FROM node:16
more stuff to ignore`);
const version = await getDockerNodeVersion();
expect(version).toEqual(16);
expect(fs.readFile).toHaveBeenCalledWith(
"Dockerfile",
expect.any(Object),
);
});
it("with an invalid version if the Docker image isn't based on Node", async () => {
fs.readFile.mockResolvedValue(`FROM ruby:3`);
const version = await getDockerNodeVersion();
expect(version).toEqual("not set");
});
});
describe("scans GitHub workflow files for Node.js versions", () => {
it("only checks yaml files in the workflows directory", async () => {
fs.readdir.mockResolvedValue(["a.txt", "b.yml", "c.yml", "d.js", "."]);
fs.readFile.mockResolvedValue("");
await getWorkflowLinesWithInvalidNodeVersion();
const enc = { encoding: "utf-8" };
expect(fs.readFile).toHaveBeenCalledWith(".github/workflows/b.yml", enc);
expect(fs.readFile).toHaveBeenCalledWith(".github/workflows/c.yml", enc);
expect(fs.readFile.mock.calls.length).toEqual(2);
});
describe("it checks the yaml files it finds", () => {
beforeEach(() => {
fs.readdir.mockResolvedValue(["a.yml"]);
});
it("ignores references to the word 'container' that are not a yaml key", async () => {
fs.readFile.mockResolvedValue(`
line 1
line 2
bob: container: node:18
line 4`);
const output = await getWorkflowLinesWithInvalidNodeVersion(16);
// Identifies line 3 as the problem, referencing version 18
expect(output).toEqual([]);
});
it("finds containers built from non-matching Node versions", async () => {
fs.readFile.mockResolvedValue(`line 1
line 2
container: node:18
line 4
container: node:16`);
const output = await getWorkflowLinesWithInvalidNodeVersion(16);
// Identifies line 3 as the problem, referencing version 18; but does
// not identify line 5, which uses the expected version 16
expect(output).toEqual([[".github/workflows/a.yml", [[3, 18]]]]);
});
it("finds Node versions configured for the setup-node action", async () => {
fs.readFile.mockResolvedValue(
[
" line 1",
" line 2",
// Test a block where the "uses" and "with" properties are at the
// same depth (e.g., maybe the first property of the block was the
// step name instead of the uses key)
" uses: actions/setup-node@bob",
" with:",
" node-version: 14",
// Test a block where the "uses" property is the first one in the
// block.
" - uses: actions/setup-node",
" with:",
" node-version: 18",
// Test a block with the expected Node version, to make sure it
// doesn't show up in the list
" - uses: actions/setup-node",
" node-version: 16",
// Test a uses block that does not have a node-version but is
// followed by a node-version that is not part of the block.
" - uses: actions/setup-node",
" - node-version: 22",
// Similar to above
" - uses: actions/setup-node",
" - something-else",
" node-version: 88",
].join("\n"),
);
const output = await getWorkflowLinesWithInvalidNodeVersion(16);
expect(output).toEqual([
[
".github/workflows/a.yml",
[
[5, 14],
[8, 18],
[11, "not set"],
[13, "not set"],
],
],
]);
});
});
});
describe("stringing everything together", () => {
beforeEach(() => {
lts.getCurrentLTSVersion = jest.fn();
lts.getDockerNodeVersion = jest.fn();
lts.getPackageNodeVersion = jest.fn();
lts.getWorkflowLinesWithInvalidNodeVersion = jest.fn();
});
afterAll(() => {
lts.getCurrentLTSVersion = getCurrentLTSVersion;
lts.getDockerNodeVersion = getDockerNodeVersion;
lts.getPackageNodeVersion = getPackageNodeVersion;
lts.getWorkflowLinesWithInvalidNodeVersion =
getWorkflowLinesWithInvalidNodeVersion;
});
it("returns an empty list if there aren't any errors", async () => {
lts.getCurrentLTSVersion.mockResolvedValue(16);
lts.getDockerNodeVersion.mockResolvedValue(16);
lts.getPackageNodeVersion.mockResolvedValue(16);
lts.getWorkflowLinesWithInvalidNodeVersion.mockResolvedValue([]);
const errors = await main();
expect(errors).toEqual([]);
});
it("tells us about the errors it finds", async () => {
lts.getCurrentLTSVersion.mockResolvedValue(16);
lts.getDockerNodeVersion.mockResolvedValue(14);
lts.getPackageNodeVersion.mockResolvedValue(12);
lts.getWorkflowLinesWithInvalidNodeVersion.mockResolvedValue([
[
"file 1",
[
[1, 12],
[17, 18],
],
],
[
"file 2",
[
[9, 22],
[14, 14],
],
],
]);
const errors = await main();
expect(errors).toEqual([
"package.json is out of date",
" found Node 12; wanted 16",
"Dockerfile is out of date",
" found Node 14; wanted 16",
"Workflow file file 1 is out of date",
" line 1 uses Node 12; wanted 16",
" line 17 uses Node 18; wanted 16",
"Workflow file file 2 is out of date",
" line 9 uses Node 22; wanted 16",
" line 14 uses Node 14; wanted 16",
]);
});
});
});