/
lockfiles.js
327 lines (269 loc) · 12.8 KB
/
lockfiles.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
/* @flow */
import {run as check} from '../../../src/cli/commands/check.js';
import * as constants from '../../../src/constants.js';
import * as reporters from '../../../src/reporters/index.js';
import {Install} from '../../../src/cli/commands/install.js';
import Lockfile from '../../../src/lockfile';
import * as fs from '../../../src/util/fs.js';
import {getPackageVersion, isPackagePresent, runInstall} from '../_helpers.js';
import {promisify} from '../../../src/util/promise';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;
const fsNode = require('fs');
const path = require('path');
const os = require('os');
test('does fetch files from the local filesystem', (): Promise<void> => {
return runInstall(
{},
'install-should-fetch-local-tarballs',
(config): Promise<void> => {
return Promise.resolve();
},
async cwd => {
let packageJson = await fs.readFile(`${cwd}/package.json`);
packageJson = packageJson.replace(/%%CWD%%/g, cwd.replace(/\\/g, `/`));
await fs.writeFile(`${cwd}/package.json`, packageJson);
},
);
});
test.concurrent("doesn't write new lockfile if existing one satisfied", (): Promise<void> => {
return runInstall({}, 'install-dont-write-lockfile-if-satisfied', async (config): Promise<void> => {
const lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(lockfile.indexOf('foobar')).toBeGreaterThanOrEqual(0);
});
});
test.concurrent("writes new lockfile if existing one isn't satisfied", async (): Promise<void> => {
await runInstall({}, 'install-write-lockfile-if-not-satisfied', async (config): Promise<void> => {
const lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(lockfile.indexOf('foobar')).toEqual(-1);
});
});
test.concurrent("doesn't write a lockfile when there are no dependencies", (): Promise<void> => {
return runInstall({}, 'install-without-dependencies', async config => {
const lockfileExists = await fs.exists(path.join(config.cwd, 'yarn.lock'));
const installedDepFiles = await fs.walk(path.join(config.cwd, 'node_modules'));
expect(lockfileExists).toEqual(false);
expect(installedDepFiles).toHaveLength(0);
});
});
test.concurrent("throws an error if existing lockfile isn't satisfied with --frozen-lockfile", async (): Promise<
void,
> => {
const reporter = new reporters.ConsoleReporter({});
let thrown = false;
try {
await runInstall({frozenLockfile: true}, 'install-throws-error-if-not-satisfied-and-frozen-lockfile', () => {});
} catch (err) {
thrown = true;
expect(err.message).toContain(reporter.lang('frozenLockfileError'));
}
expect(thrown).toEqual(true);
});
test.concurrent(
"doesn't write new lockfile if existing one satisfied but not fully optimized with --frozen-lockfile",
(): Promise<void> => {
return runInstall(
{frozenLockfile: true},
'install-should-not-write-lockfile-if-not-optimized-and-frozen',
async (config): Promise<void> => {
const lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(lockfile.indexOf('left-pad@1.1.3:')).toBeGreaterThanOrEqual(0);
},
);
},
);
test.concurrent('install transitive optional dependency from lockfile', (): Promise<void> => {
return runInstall({}, 'install-optional-dep-from-lockfile', (config, reporter, install) => {
expect(install && install.resolver && install.resolver.patterns['fsevents@^1.0.0']).toBeTruthy();
});
});
test.concurrent('root install from shrinkwrap', (): Promise<void> => {
return runInstall({}, 'root-install-with-lockfile');
});
test.concurrent('install have a clean node_modules after lockfile update (branch switch scenario)', (): Promise<
void,
> => {
// A@1 -> B@1
// B@2
// after package.json/lock file update
// A@1.2 -> B@1.2
// (deduped)
// A@1.2
// B@1.2
return runInstall({}, 'install-should-cleanup-when-package-json-changed', async (config, reporter): Promise<void> => {
expect(await getPackageVersion(config, 'dep-a')).toEqual('1.0.0');
expect(await getPackageVersion(config, 'dep-b')).toEqual('2.0.0');
expect(await getPackageVersion(config, 'dep-a/dep-b')).toEqual('1.0.0');
await fs.unlink(path.join(config.cwd, 'yarn.lock'));
await fs.unlink(path.join(config.cwd, 'package.json'));
await fs.copy(path.join(config.cwd, 'yarn.lock.after'), path.join(config.cwd, 'yarn.lock'), reporter);
await fs.copy(path.join(config.cwd, 'package.json.after'), path.join(config.cwd, 'package.json'), reporter);
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await getPackageVersion(config, 'dep-a')).toEqual('1.2.0');
expect(await getPackageVersion(config, 'dep-b')).toEqual('1.2.0');
});
});
test.concurrent('install have a clean node_modules after lockfile update (branch switch scenario 2)', (): Promise<
void,
> => {
// A@1 -> B@1
// after package.json/lock file update
// A@1.2
return runInstall({}, 'install-should-cleanup-when-package-json-changed-2', async (config, reporter): Promise<
void,
> => {
expect(await getPackageVersion(config, 'dep-a')).toEqual('1.0.0');
expect(await getPackageVersion(config, 'dep-b')).toEqual('1.0.0');
await fs.unlink(path.join(config.cwd, 'yarn.lock'));
await fs.unlink(path.join(config.cwd, 'package.json'));
await fs.copy(path.join(config.cwd, 'yarn.lock.after'), path.join(config.cwd, 'yarn.lock'), reporter);
await fs.copy(path.join(config.cwd, 'package.json.after'), path.join(config.cwd, 'package.json'), reporter);
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await getPackageVersion(config, 'dep-a')).toEqual('1.2.0');
expect(await isPackagePresent(config, 'dep-b')).toEqual(false);
});
});
test.concurrent('install should write and read integrity file based on lockfile entries', (): Promise<void> => {
return runInstall({}, 'lockfile-stability', async (config, reporter) => {
let lockContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
lockContent += `
# changed the file, integrity should be fine
`;
await fs.writeFile(path.join(config.cwd, 'yarn.lock'), lockContent);
let allCorrect = true;
try {
await check(config, reporter, {integrity: true}, []);
} catch (err) {
allCorrect = false;
}
expect(allCorrect).toBe(true);
// install should bail out with integrity check
await fs.unlink(path.join(config.cwd, 'node_modules', 'mime-types'));
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
// integrity check should keep passing
allCorrect = true;
try {
await check(config, reporter, {integrity: true}, []);
} catch (err) {
allCorrect = false;
}
expect(allCorrect).toBe(true);
// full check should fail because of deleted file
allCorrect = false;
try {
await check(config, reporter, {integrity: false}, []);
} catch (err) {
allCorrect = true;
}
expect(allCorrect).toBe(true);
});
});
test.concurrent('install should retain artifacts when missing integrity file', (): Promise<void> => {
return runInstall({}, 'install-should-retain-artifacts-when-missing-integrity', async (config, reporter) => {
const expectedArtifacts = ['foo.txt'];
const integrityLoc = path.join(config.cwd, 'node_modules', constants.INTEGRITY_FILENAME);
const beforeIntegrity = await fs.readJson(integrityLoc);
expect(beforeIntegrity.artifacts['a@0.0.0']).toEqual(expectedArtifacts);
await fs.unlink(integrityLoc);
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
const afterIntegrity = await fs.readJson(integrityLoc);
expect(afterIntegrity.artifacts['a@0.0.0']).toEqual(expectedArtifacts);
});
});
test.concurrent('install should not continue if integrity check passes', (): Promise<void> => {
return runInstall({}, 'lockfile-stability', async (config, reporter) => {
await fs.writeFile(path.join(config.cwd, 'node_modules', 'yarn.test'), 'YARN TEST');
// install should bail out with integrity check and not remove extraneous file
let reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'yarn.test'))).toBeTruthy();
await fs.unlink(path.join(config.cwd, 'node_modules', 'yarn.test'));
reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
expect(!await fs.exists(path.join(config.cwd, 'node_modules', 'yarn.test'))).toBeTruthy();
});
});
test.concurrent('install should not rewrite lockfile with no substantial changes', (): Promise<void> => {
const fixture = 'lockfile-no-rewrites';
return runInstall({}, fixture, async (config, reporter) => {
const originalLockContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const lockContent =
originalLockContent +
`
# changed the file, and it should remain changed after force install
`;
await fs.writeFile(path.join(config.cwd, 'yarn.lock'), lockContent);
await fs.unlink(path.join(config.cwd, 'node_modules', constants.INTEGRITY_FILENAME));
let reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
let newLockContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(newLockContent).toEqual(lockContent);
// force should rewrite lockfile
reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
newLockContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(newLockContent).not.toEqual(lockContent);
});
});
test.concurrent('lockfile should be created when missing even if integrity matches', (): Promise<void> => {
return runInstall({}, 'lockfile-missing', async (config, reporter) => {
expect(await fs.exists(path.join(config.cwd, 'yarn.lock'))).toBeTruthy();
});
});
test.concurrent('install infers line endings from existing win32 lockfile', async (): Promise<void> => {
await runInstall(
{},
'install-infers-line-endings-from-existing-lockfile',
async (config): Promise<void> => {
const lockfile = await promisify(fsNode.readFile)(path.join(config.cwd, 'yarn.lock'), 'utf8');
expect(lockfile).toMatch(/\r\n/);
expect(lockfile).not.toMatch(/[^\r]\n/);
},
async (cwd): Promise<void> => {
const existingLockfile = '# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\r\n';
await promisify(fsNode.writeFile)(path.join(cwd, 'yarn.lock'), existingLockfile, 'utf8');
},
);
});
test.concurrent('install infers line endings from existing unix lockfile', async (): Promise<void> => {
await runInstall(
{},
'install-infers-line-endings-from-existing-lockfile',
async (config): Promise<void> => {
const lockfile = await promisify(fsNode.readFile)(path.join(config.cwd, 'yarn.lock'), 'utf8');
expect(lockfile).toMatch(/[^\r]\n/);
expect(lockfile).not.toMatch(/\r\n/);
},
async (cwd): Promise<void> => {
const existingLockfile = '# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.\n';
await promisify(fsNode.writeFile)(path.join(cwd, 'yarn.lock'), existingLockfile, 'utf8');
},
);
});
test.concurrent("install uses OS line endings when lockfile doesn't exist", async (): Promise<void> => {
await runInstall({}, 'install-infers-line-endings-from-existing-lockfile', async (config): Promise<void> => {
const lockfile = await promisify(fsNode.readFile)(path.join(config.cwd, 'yarn.lock'), 'utf8');
expect(lockfile.indexOf(os.EOL)).toBeGreaterThan(0);
});
});
test.concurrent('install should rewrite lockfile if patterns can be merged', (): Promise<void> => {
const fixture = 'lockfile-trimmed';
return runInstall({}, fixture, async (config, reporter) => {
const lockContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(lockContent).toContain('mime-db@^1.0.0');
expect(lockContent).not.toContain('https://fakepath.wont.download.com/mime-db/-/mime-db-1.0.0.tgz');
});
});
test.concurrent("install should fix if lockfile patterns don't match resolved version", (): Promise<void> => {
const fixture = 'lockfile-fixed';
return runInstall({}, fixture, async (config, reporter) => {
const lockContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(lockContent).not.toContain('mime-db-1.24.0.tgz');
expect(lockContent).toContain('mime-db-1.23.0.tgz');
expect(lockContent).not.toContain('left-pad-1.1.3.tgz');
expect(lockContent).toContain('left-pad-1.1.2.tgz');
});
});