-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
workspaces-install.js
433 lines (391 loc) · 18.7 KB
/
workspaces-install.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
/* @flow */
import {run as check} from '../../../src/cli/commands/check.js';
import {Install, run as install} from '../../../src/cli/commands/install.js';
import * as reporters from '../../../src/reporters/index.js';
import * as fs from '../../../src/util/fs.js';
import type ConfigType from '../../../src/config.js';
import {runInstall, run as buildRun, makeConfigFromDirectory, isPackagePresent} from '../_helpers.js';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;
const path = require('path');
test.concurrent("workspaces don't work with disabled configuration in .yarnrc", async (): Promise<void> => {
let error = '';
const reporter = new reporters.ConsoleReporter({});
try {
await runInstall({}, 'workspaces-install-enabled');
} catch (e) {
error = e.message;
}
expect(error).toContain(reporter.lang('workspacesDisabled'));
});
test.concurrent("workspaces don't work on non private projects", async (): Promise<void> => {
let error = '';
const reporter = new reporters.ConsoleReporter({});
try {
await runInstall({}, 'workspaces-install-private');
} catch (e) {
error = e.message;
}
expect(error).toContain(reporter.lang('workspacesRequirePrivateProjects'));
});
test.concurrent("workspaces don't work with duplicate names", async (): Promise<void> => {
let error = '';
const reporter = new reporters.ConsoleReporter({});
try {
await runInstall({}, 'workspaces-install-duplicate');
} catch (e) {
error = e.message;
}
expect(error).toContain(reporter.lang('workspaceNameDuplicate', 'workspace-1'));
});
test.concurrent("workspaces warn and get ignored if they don't have a name and a version", (): Promise<void> => {
return buildRun(
reporters.BufferReporter,
path.join(__dirname, '..', '..', 'fixtures', 'install'),
async (args, flags, config, reporter, lockfile): Promise<void> => {
const install = new Install(flags, config, reporter, lockfile);
await install.init();
const warnings = reporter.getBuffer();
expect(
warnings.some(warning => {
return warning.data.toString().toLowerCase().indexOf('missing version in workspace') > -1;
}),
).toEqual(true);
expect(
warnings.some(warning => {
return warning.data.toString().toLowerCase().indexOf('missing name in workspace') > -1;
}),
).toEqual(true);
},
[],
{},
'workspaces-install-mandatory-fields',
);
});
test.concurrent('installs workspaces dependencies into root folder', (): Promise<void> => {
return runInstall({}, 'workspaces-install-basic', async (config): Promise<void> => {
const lockfile = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
expect(lockfile.indexOf('isarray')).toBeGreaterThanOrEqual(0);
expect(lockfile.indexOf('repeat-string')).toBeGreaterThanOrEqual(0);
expect(lockfile.indexOf('left-pad')).toBeGreaterThanOrEqual(0);
expect(lockfile.indexOf('right-pad')).toBeGreaterThanOrEqual(0);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'isarray'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'repeat-string'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'left-pad'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'right-pad'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'workspace-child', 'node_modules'))).toBe(false);
expect(await fs.exists(path.join(config.cwd, 'workspace-child', 'yarn.lock'))).toBe(false);
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-child-2', 'node_modules'))).toBe(false);
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-child-2', 'yarn.lock'))).toBe(false);
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-child-3', 'node_modules'))).toBe(false);
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-child-3', 'yarn.lock'))).toBe(false);
});
});
test.concurrent('install should install unhoistable dependencies in workspace node_modules', (): Promise<void> => {
return runInstall({}, 'workspaces-install-conflict', async (config): Promise<void> => {
// node_modules/left-pad@1.1.3
let packageFile = await fs.readFile(path.join(config.cwd, 'node_modules', 'left-pad', 'package.json'));
expect(JSON.parse(packageFile).version).toEqual('1.1.3');
// node_modules/workspace-child/left-pad@1.1.1
packageFile = await fs.readFile(
path.join(config.cwd, 'workspace-child', 'node_modules', 'left-pad', 'package.json'),
);
expect(JSON.parse(packageFile).version).toEqual('1.1.1');
});
});
test.concurrent(
'install should install unhoistable dependencies in workspace node_modules even when no symlink exists',
(): Promise<void> => {
return runInstall({}, 'workspaces-install-conflict-without-symlink', async (config): Promise<void> => {
// node_modules/isarray@1.0.0
let packageFile = await fs.readFile(path.join(config.cwd, 'node_modules', 'isarray', 'package.json'));
expect(JSON.parse(packageFile).version).toEqual('1.0.0');
// node_modules/arrify@1.0.0
packageFile = await fs.readFile(path.join(config.cwd, 'node_modules', 'arrify', 'package.json'));
expect(JSON.parse(packageFile).version).toEqual('1.0.0');
// node_modules/arrify/isarray@2.0.0
packageFile = await fs.readFile(path.join(config.cwd, 'arrify', 'node_modules', 'isarray', 'package.json'));
expect(JSON.parse(packageFile).version).toEqual('2.0.0');
});
},
);
test.concurrent('install should link workspaces that refer each other', (): Promise<void> => {
return runInstall({}, 'workspaces-install-link', async (config): Promise<void> => {
// packages/workspace-1/node_modules/left-pad - missing because it is hoisted to the root
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-1', 'node_modules'))).toBe(false);
// node_modules/left-pad - link
const packageFile = await fs.readFile(path.join(config.cwd, 'node_modules', 'left-pad', 'package.json'));
expect(JSON.parse(packageFile).version).toEqual('1.1.2');
const readme = await fs.readFile(path.join(config.cwd, 'node_modules', 'left-pad', 'README.md'));
expect(readme.split('\n')[0]).toEqual('WORKSPACES ROCK!');
});
});
test.concurrent(
'install should not link workspaces that refer not compatible version of another workspace',
(): Promise<void> => {
return runInstall({}, 'workspaces-install-link', async (config): Promise<void> => {
// packages/workspace-2/node_modules/left-pad - from npm
const packageFile = await fs.readFile(
path.join(config.cwd, 'packages', 'workspace-2', 'node_modules', 'left-pad', 'package.json'),
);
const readme = await fs.readFile(
path.join(config.cwd, 'packages', 'workspace-2', 'node_modules', 'left-pad', 'README.md'),
);
expect(JSON.parse(packageFile).version).not.toBe('1.1.2');
expect(readme.split('\n')[0]).not.toEqual('WORKSPACES ROCK!');
});
},
);
test.concurrent('install should not link a workspace if the version is not compatible', (): Promise<void> => {
return runInstall({binLinks: true}, 'workspaces-install-link-invalid', async (config): Promise<void> => {
// node_modules/left-pad - from npm
const packageFile = await fs.readFile(path.join(config.cwd, 'node_modules', 'left-pad', 'package.json'));
const readme = await fs.readFile(path.join(config.cwd, 'node_modules', 'left-pad', 'README.md'));
expect(JSON.parse(packageFile).version).not.toBe('1.1.2');
expect(readme.split('\n')[0]).not.toEqual('WORKSPACES ROCK!');
});
});
test.concurrent('install should prioritize non workspace dependency at root over the workspace symlink', (): Promise<
void,
> => {
return runInstall({}, 'workspaces-install-link-root', async (config): Promise<void> => {
// node_modules/left-pad - from npm
let packageFile = await fs.readFile(path.join(config.cwd, 'node_modules', 'left-pad', 'package.json'));
expect(JSON.parse(packageFile).version).toEqual('1.1.3');
let readme = await fs.readFile(path.join(config.cwd, 'node_modules', 'left-pad', 'README.md'));
expect(readme.split('\n')[0]).not.toEqual('WORKSPACES ROCK!');
// node_modules/workspace-1/left-pad - link
packageFile = await fs.readFile(
path.join(config.cwd, 'packages', 'workspace-1', 'node_modules', 'left-pad', 'package.json'),
);
expect(JSON.parse(packageFile).version).toEqual('1.1.2');
readme = await fs.readFile(
path.join(config.cwd, 'packages', 'workspace-1', 'node_modules', 'left-pad', 'README.md'),
);
expect(readme.split('\n')[0]).toEqual('WORKSPACES ROCK!');
// packages/workspace-2/node_modules/left-pad - missing because it is hoisted to the root
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-2', 'node_modules'))).toBe(false);
});
});
test.concurrent('install should install subdependencies of workspaces', (): Promise<void> => {
// the tricky part is that isarray is a subdependency of left-pad that is not referenced in the root
// but another workspace
return runInstall({}, 'workspaces-install-subdeps', async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'isarray'))).toBe(true);
});
});
test.concurrent(
'install should install subdependencies of workspaces that are not referenced in other workspaces',
(): Promise<void> => {
// the tricky part is that left-pad is not a dependency of root
return runInstall({}, 'workspaces-install-subdeps-2', async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'isarray'))).toBe(true);
});
},
);
test.concurrent('install should install dev dependencies of workspaces', (): Promise<void> => {
// the tricky part is that left-pad is not a dependency of root
return runInstall({}, 'workspaces-install-subdeps-dev', async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'left-pad'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-1', 'node_modules', 'left-pad'))).toBe(true);
});
});
test.concurrent('install should not install dev dependencies of workspaces in production mode', (): Promise<void> => {
// the tricky part is that left-pad is not a dependency of root
return runInstall({production: true}, 'workspaces-install-subdeps-dev', async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'left-pad'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'packages', 'workspace-1', 'node_modules', 'left-pad'))).toBe(false);
});
});
// https://github.com/yarnpkg/yarn/issues/3598
test.concurrent('install should work correctly for workspaces that have similar names', (): Promise<void> => {
return runInstall({production: true}, 'workspaces-install-names-issue', async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'packages', 'jest', 'package.json'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'packages', 'jest-cli', 'package.json'))).toBe(true);
});
});
test.concurrent('check command should work', (): Promise<void> => {
return runInstall({checkFiles: true}, 'workspaces-install-basic', async (config, reporter): Promise<void> => {
// check command + integrity check
let thrown = false;
try {
await check(config, reporter, {integrity: true, checkFiles: true}, []);
await check(config, reporter, {}, []);
} catch (e) {
thrown = true;
}
expect(thrown).toBe(false);
});
});
test.concurrent('install should link binaries at root and in workspace dependents', (): Promise<void> => {
return runInstall({binLinks: true}, 'workspaces-install-link-bin', async (config): Promise<void> => {
// node_modules/.bin/workspace-1 - link
expect(await fs.exists(path.join(config.cwd, 'node_modules', '.bin', 'workspace-1'))).toBe(true);
// packages/workspace-2/node_modules/.bin/workspace-1 - link
expect(
await fs.exists(path.join(config.cwd, 'packages', 'workspace-2', 'node_modules', '.bin', 'workspace-1')),
).toBe(true);
});
});
test.concurrent('install should ignore node_modules in workspaces when used with **/*', (): Promise<void> => {
return runInstall({}, 'workspaces-install-already-exists', async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'a'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'b'))).toBe(true);
});
});
describe('install should ignore deep node_modules in workspaces', () => {
test('without nohoist', (): Promise<void> => {
return runInstall(
{workspacesNohoistEnabled: false},
'workspaces-install-already-exists-deep',
async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'a'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'b'))).toBe(true);
},
);
});
test('with nohoist', (): Promise<void> => {
return runInstall(
{workspacesNohoistEnabled: true},
'workspaces-install-already-exists-deep',
async (config): Promise<void> => {
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'a'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules', 'b'))).toBe(true);
},
);
});
});
test.concurrent('install should link binaries properly when run from child workspace', async () => {
await runInstall({binLinks: true}, 'workspaces-install-bin', async (config, reporter): Promise<void> => {
// initial install
expect(await fs.exists(`${config.cwd}/node_modules/.bin/rimraf`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/node_modules/.bin/touch`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/node_modules/.bin/workspace-1`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/packages/workspace-2/node_modules/.bin/rimraf`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/packages/workspace-2/node_modules/.bin/workspace-1`)).toEqual(true);
// reset package folders to simulate running 'install' from
// child workspace _before_ running it in the root (this is not
// possible to do without an initial install using the current
// testing infrastructure)
await fs.unlink(`${config.cwd}/node_modules`);
await fs.unlink(`${config.cwd}/packages/workspace-1/node_modules`);
await fs.unlink(`${config.cwd}/packages/workspace-2/node_modules`);
// run "install" in child package
const childConfig = await makeConfigFromDirectory(`${config.cwd}/packages/workspace-1`, reporter, {binLinks: true});
await install(childConfig, reporter, {}, []);
expect(await fs.exists(`${config.cwd}/node_modules/.bin/rimraf`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/node_modules/.bin/touch`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/node_modules/.bin/workspace-1`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/packages/workspace-2/node_modules/.bin/rimraf`)).toEqual(true);
expect(await fs.exists(`${config.cwd}/packages/workspace-2/node_modules/.bin/workspace-1`)).toEqual(true);
});
});
// TODO need more thorough tests for all kinds of checks: integrity, verify-tree
describe('nohoist', () => {
async function checkPackage(config: ConfigType, path: string, shouldPresent: boolean): Promise<void> {
const isPresent = await isPackagePresent(config, path);
try {
expect(isPresent).toEqual(shouldPresent);
} catch (e) {
throw new Error(`error: ${path} should ${shouldPresent ? '' : 'NOT'} exist`);
}
}
test.concurrent('exclude packages by workspace', (): Promise<void> => {
return runInstall({}, 'workspaces-install-nohoist-by-ws', async (config): Promise<void> => {
const existingPackages = [
'workspace-disable-a',
'workspace-disable-all',
'workspace-hoist-all',
'workspace-disable-all/c',
'workspace-disable-all/b',
'workspace-disable-all/d',
'workspace-disable-a/a',
'workspace-disable-a/b',
'workspace-disable-all/workspace-hoist-all',
'b',
'c',
'd',
];
const notExistingPackages = ['a', 'workspace-hoist-all/b', 'workspace-hoist-all/d'];
for (const p of existingPackages) {
await checkPackage(config, p, true);
}
for (const p of notExistingPackages) {
await checkPackage(config, p, false);
}
});
});
test.concurrent('disable all hoist for every workspace', (): Promise<void> => {
return runInstall({}, 'workspaces-install-nohoist-all-from-root', async config => {
const existingPackages = [
'workspace-disable-a',
'workspace-disable-all',
'workspace-hoist-all',
'workspace-disable-all/c',
'workspace-disable-all/b',
'workspace-disable-all/d',
'workspace-disable-a/a',
'workspace-disable-a/b',
'workspace-disable-a/d',
'workspace-hoist-all/b',
'workspace-hoist-all/d',
];
const notExistingPackages = ['a', 'b', 'c', 'd'];
for (const p of existingPackages) {
await checkPackage(config, p, true);
}
for (const p of notExistingPackages) {
await checkPackage(config, p, false);
}
});
});
test.concurrent('disable some hoist for every workspace', (): Promise<void> => {
return runInstall({}, 'workspaces-install-nohoist-some-from-root', async config => {
const existingPackages = [
'workspace-disable-a',
'workspace-disable-all',
'workspace-hoist-all',
'workspace-disable-all/c',
'workspace-disable-all/b',
'workspace-disable-all/d',
'workspace-disable-a/a',
'workspace-disable-a/b',
'workspace-disable-a/d',
'workspace-hoist-all/d',
'c',
'b',
];
const notExistingPackages = ['a', 'd'];
for (const p of existingPackages) {
await checkPackage(config, p, true);
}
for (const p of notExistingPackages) {
await checkPackage(config, p, false);
}
});
});
test.concurrent('disable hoisting package across versions', (): Promise<void> => {
return runInstall({}, 'workspaces-install-nohoist-across-versions', async config => {
const existingPackages = [
'workspace-1',
'workspace-2',
'workspace-3',
'workspace-1/c',
'workspace-1/b',
'workspace-1/a',
'workspace-2/b',
'workspace-2/c',
'workspace-2/b',
'workspace-2/c/b',
'workspace-3/b',
'd',
];
const notExistingPackages = ['a', 'b', 'c', 'workspace-3/d'];
for (const p of existingPackages) {
await checkPackage(config, p, true);
}
for (const p of notExistingPackages) {
await checkPackage(config, p, false);
}
});
});
});