/
check.js
455 lines (411 loc) · 15.5 KB
/
check.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
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
/* @flow */
import {run as buildRun, runInstall} from './_helpers.js';
import * as checkCmd from '../../src/cli/commands/check.js';
import {Install} from '../../src/cli/commands/install.js';
import Lockfile from '../../src/lockfile';
import * as reporters from '../../src/reporters/index.js';
import type {CLIFunctionReturn} from '../../src/types.js';
import * as fs from '../../src/util/fs.js';
const path = require('path');
jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;
const fixturesLoc = path.join(__dirname, '..', 'fixtures', 'check');
const runCheck = buildRun.bind(
null,
reporters.ConsoleReporter,
fixturesLoc,
(args, flags, config, reporter): CLIFunctionReturn => {
return checkCmd.run(config, reporter, flags, args);
},
);
test.concurrent('--verify-tree should report wrong version', async (): Promise<void> => {
let thrown = false;
try {
await runCheck([], {verifyTree: true}, 'verify-tree-version-mismatch');
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
});
test.concurrent('--verify-tree should work from a workspace cwd', async (): Promise<void> => {
let thrown = false;
try {
await runCheck([], {verifyTree: true}, {source: 'verify-tree-workspace-cwd', cwd: '/packages/workspace-1'});
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(false);
});
test.concurrent('--verify-tree should report missing dependency', async (): Promise<void> => {
let thrown = false;
try {
await runCheck([], {verifyTree: true}, 'verify-tree-not-found');
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
});
test.concurrent('--verify-tree should pass on hoisted dependency ', async (): Promise<void> => {
await runCheck([], {verifyTree: true}, 'verify-tree-hoisted');
});
test.concurrent('--verify-tree should check dev dependencies ', async (): Promise<void> => {
let thrown = false;
try {
await runCheck([], {verifyTree: true, production: false}, 'verify-tree-dev');
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
});
test.concurrent('--verify-tree should check skip dev dependencies if --production flag passed', async (): Promise<
void,
> => {
await runCheck([], {verifyTree: true, production: true}, 'verify-tree-dev-prod');
});
test.concurrent('--verify-tree should check skip deeper dev dependencies', async (): Promise<void> => {
await runCheck([], {verifyTree: true, production: true}, 'verify-tree-dev-deep');
});
test.concurrent('--integrity should ignore comments and whitespaces in yarn.lock', async (): Promise<void> => {
await runInstall({}, path.join('..', 'check', 'integrity-lock-check'), async (config, reporter): Promise<void> => {
let lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
lockfile += "\n# ADDING THIS COMMENT WON'T AFFECT INTEGRITY CHECK \n";
await fs.writeFile(path.join(config.cwd, 'yarn.lock'), lockfile);
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(false);
});
});
test.concurrent('--integrity should fail if integrity file is missing', async (): Promise<void> => {
await runInstall({}, path.join('..', 'check', 'integrity-lock-check'), async (config, reporter): Promise<void> => {
await fs.unlink(path.join(config.cwd, 'node_modules', '.yarn-integrity'));
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
});
});
test.concurrent('--integrity should fail if integrity file is not a json', async (): Promise<void> => {
await runInstall(
{},
path.join('..', 'check', 'integrity-lock-check'),
async (config, reporter, install, getStdout): Promise<void> => {
await fs.writeFile(path.join(config.cwd, 'node_modules', '.yarn-integrity'), 'not a json');
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
expect(getStdout()).toContain('Integrity check: integrity file is not a json');
},
);
});
test.concurrent('--integrity should fail if yarn.lock has patterns changed', async (): Promise<void> => {
await runInstall({}, path.join('..', 'check', 'integrity-lock-check'), async (config, reporter): Promise<void> => {
let lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
lockfile = lockfile.replace('left-pad@1.1.1', 'left-pad@1.1.0');
await fs.writeFile(path.join(config.cwd, 'yarn.lock'), lockfile);
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
});
});
test.concurrent('--integrity should fail if yarn.lock has new pattern', async (): Promise<void> => {
await runInstall(
{},
path.join('..', 'check', 'integrity-lock-check'),
async (config, reporter, install, getStdout): Promise<void> => {
let lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
lockfile += `\nxtend@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/xtend/-/xtend-4.0.1.tgz#a5c6d532be656e23db820efb943a1f04998d63af"`;
await fs.writeFile(path.join(config.cwd, 'yarn.lock'), lockfile);
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
expect(getStdout()).toContain("Integrity check: Lock files don't match");
},
);
});
test.concurrent('--integrity should fail if yarn.lock has resolved changed', async (): Promise<void> => {
await runInstall(
{},
path.join('..', 'check', 'integrity-lock-check'),
async (config, reporter, install, getStdout): Promise<void> => {
let lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
lockfile = lockfile.replace(
'https://registry.npmjs.org/left-pad/-/left-pad-1.1.1.tgz',
'https://registry.yarnpkg.com/left-pad/-/left-pad-1.1.1.tgz',
);
await fs.writeFile(path.join(config.cwd, 'yarn.lock'), lockfile);
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
expect(getStdout()).toContain("Integrity check: Lock files don't match");
},
);
});
test.concurrent('--integrity should fail if files are missing and --check-files is passed', async (): Promise<void> => {
await runInstall(
{checkFiles: true},
path.join('..', 'check', 'integrity-lock-check'),
async (config, reporter, install, getStdout): Promise<void> => {
await fs.unlink(path.join(config.cwd, 'node_modules', 'left-pad', 'index.js'));
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true, checkFiles: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
expect(getStdout()).toContain('Integrity check: Files are missing');
},
);
});
test.concurrent('--integrity should fail if --ignore-scripts is changed', async (): Promise<void> => {
await runInstall(
{ignoreScripts: true},
path.join('..', 'check', 'integrity-lock-check'),
async (config, reporter, install, getStdout): Promise<void> => {
let thrown = false;
try {
config.ignoreScripts = false;
await checkCmd.run(config, reporter, {integrity: true, ignoreScripts: false}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
expect(getStdout()).toContain("Integrity check: Flags don't match");
},
);
});
test.concurrent('when switching to --check-files install should rebuild integrity file', async (): Promise<void> => {
await runInstall({}, path.join('..', 'check', 'integrity-lock-check'), async (config, reporter): Promise<void> => {
await fs.unlink(path.join(config.cwd, 'node_modules', 'left-pad', 'index.js'));
// reinstall should skip because current installation does not track files
let reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'left-pad', 'index.js'))).toEqual(false);
// integrity check won't notice missing file
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(false);
// reinstall with --check-files tag should reinstall missing files and generate proper integrity
reinstall = new Install({checkFiles: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
// all correct
thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true, checkFiles: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(false);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'left-pad', 'index.js'))).toEqual(true);
// removed file will be noticed
thrown = false;
await fs.unlink(path.join(config.cwd, 'node_modules', 'left-pad', 'index.js'));
try {
await checkCmd.run(config, reporter, {integrity: true, checkFiles: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
});
});
test.concurrent('--integrity should fail if integrity file have different linkedModules', async (): Promise<void> => {
await runInstall(
{},
path.join('..', 'check', 'integrity-lock-check'),
async (config, reporter, install, getStdout): Promise<void> => {
const integrityFilePath = path.join(config.cwd, 'node_modules', '.yarn-integrity');
const integrityFile = JSON.parse(await fs.readFile(integrityFilePath));
integrityFile.linkedModules.push('aLinkedModule');
await fs.writeFile(integrityFilePath, JSON.stringify(integrityFile, null, 2));
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
expect(getStdout()).toContain("Integrity check: Linked modules don't match");
},
);
});
test.concurrent('--integrity should fail if integrity file has different systemParams', async (): Promise<void> => {
await runInstall(
{},
path.join('..', 'check', 'integrity-lock-check'),
async (config, reporter, install, getStdout): Promise<void> => {
const integrityFilePath = path.join(config.cwd, 'node_modules', '.yarn-integrity');
const integrityFile = JSON.parse(await fs.readFile(integrityFilePath));
integrityFile.systemParams = '[unexpected systemParams value]';
await fs.writeFile(integrityFilePath, JSON.stringify(integrityFile, null, 2));
let thrown = false;
try {
await checkCmd.run(config, reporter, {integrity: true}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(true);
expect(getStdout()).toContain(reporter.lang('integritySystemParamsDontMatch'));
},
);
});
test.concurrent('--integrity should create the integrity file under the meta folder if enabled', async (): Promise<
void,
> => {
await runInstall(
{},
path.join('..', 'check', 'integrity-meta-folder'),
async (config, reporter, install, getStdout): Promise<void> => {
await checkCmd.run(config, reporter, {integrity: true}, []);
expect(await fs.exists(path.join(config.cwd, '.yarn-meta', '.yarn-integrity'))).toEqual(true);
},
);
});
test.concurrent('--check-files should register the right entries even when using the meta folder', async (): Promise<
void,
> => {
await runInstall(
{checkFiles: true},
path.join('..', 'check', 'integrity-meta-folder'),
async (config, reporter, install, getStdout): Promise<void> => {
const integrityFilePath = path.join(config.cwd, '.yarn-meta', '.yarn-integrity');
const integrityFile = JSON.parse(await fs.readFile(integrityFilePath));
expect(integrityFile.files.length).toBeGreaterThan(0);
},
);
});
// https://github.com/yarnpkg/yarn/issues/3276
test.concurrent('--integrity --check-files should not die on broken symlinks', async (): Promise<void> => {
await runInstall(
{checkFiles: true, binLinks: true},
path.join('..', 'check', 'integrity-symlinks'),
async (config, reporter, install): Promise<void> => {
await fs.unlink(path.join(config.cwd, 'node_modules', 'acorn'));
let thrown = false;
try {
const reinstall = new Install(
{checkFiles: true, binLinks: true},
config,
reporter,
await Lockfile.fromDirectory(config.cwd),
);
await reinstall.init();
} catch (e) {
thrown = true;
}
expect(thrown).toEqual(false);
},
);
});
test.concurrent('--integrity should not die on missing fields in integrity file', async (): Promise<void> => {
let integrityError = false;
try {
await runCheck([], {integrity: true}, 'missing-fields');
} catch (err) {
integrityError = true;
}
expect(integrityError).toEqual(false);
});
test.concurrent('should ignore bundled dependencies', async (): Promise<void> => {
await runInstall(
{},
path.join('..', 'check', 'bundled-dep-check'),
async (config, reporter, install, getStdout): Promise<void> => {
await checkCmd.run(config, reporter, {}, []);
expect(getStdout().indexOf('warning')).toEqual(-1);
},
);
});
test.concurrent('should warn about mismatched dependencies if they match resolutions (simple)', async (): Promise<
void,
> => {
let mismatchError = false;
let stdout = '';
try {
await runCheck([], {}, 'resolutions', (config, reporter, check, getStdout) => {
stdout = getStdout();
});
} catch (err) {
mismatchError = true;
}
expect(mismatchError).toEqual(false);
expect(
stdout.search(
`warning.*"repeat-string@1.4.0" is incompatible with requested version "pad-left#repeat-string@\\^1.5.4"`,
),
).toBeGreaterThan(-1);
});
test.concurrent('should warn about mismatched dependencies if they match resolutions (tree)', async (): Promise<
void,
> => {
let mismatchError = false;
let stdout = '';
try {
await runCheck([], {}, 'resolutions-tree', (config, reporter, check, getStdout) => {
stdout = getStdout();
});
} catch (err) {
mismatchError = true;
}
expect(mismatchError).toEqual(false);
expect(
stdout.search(
`warning.*"repeat-string@1.4.0" is incompatible with requested version "pad-left#repeat-string@\\^1.5.4"`,
),
).toBeGreaterThan(-1);
});
test.concurrent('should warn about mismatched dependencies if they match resolutions (glob)', async (): Promise<
void,
> => {
let mismatchError = false;
let stdout = '';
try {
await runCheck([], {}, 'resolutions-glob', (config, reporter, check, getStdout) => {
stdout = getStdout();
});
} catch (err) {
mismatchError = true;
}
expect(mismatchError).toEqual(false);
expect(
stdout.search(
`warning.*"repeat-string@1.4.0" is incompatible with requested version "pad-left#repeat-string@\\^1.5.4"`,
),
).toBeGreaterThan(-1);
});
test.concurrent('--integrity should throw an error if top level patterns do not match', async (): Promise<void> => {
let integrityError = false;
try {
await runCheck([], {integrity: true}, 'integrity-top-level-patters');
} catch (err) {
integrityError = true;
}
expect(integrityError).toEqual(true);
});