-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
integration.js
991 lines (835 loc) · 38.4 KB
/
integration.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
/* @flow */
import type Config from '../../../src/config';
import PackageResolver from '../../../src/package-resolver.js';
import {run as cache} from '../../../src/cli/commands/cache.js';
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/wrapper.js';
import * as fs from '../../../src/util/fs.js';
import {getPackageVersion, explodeLockfile, runInstall, createLockfile} from '../_helpers.js';
import {promisify} from '../../../src/util/promise';
jasmine.DEFAULT_TIMEOUT_INTERVAL = 150000;
let request = require('request');
const assert = require('assert');
const semver = require('semver');
const fsNode = require('fs');
const path = require('path');
const stream = require('stream');
const os = require('os');
async function mockConstants(base: Config, mocks: Object, cb: (config: Config) => Promise<void>): Promise<void> {
// We cannot put this function inside _helpers, because we need to change the "request" variable
// after resetting the modules. Updating this variable is required because some tests check what
// happened during the Yarn execution, and they need to use the same instance of "request" than
// the Yarn environment.
const opts = {};
opts.binLinks = base.binLinks;
opts.cwd = base.cwd;
opts.globalFolder = base.globalFolder;
opts.linkFolder = base.linkFolder;
opts.production = base.production;
opts.cacheFolder = base._cacheRootFolder;
const automock = jest.genMockFromModule('../../../src/constants');
jest.setMock('../../../src/constants', Object.assign(automock, mocks));
jest.resetModules();
request = require('request');
jest.mock('../../../src/constants');
await cb(await require('../../../src/config.js').default.create(opts, base.reporter));
jest.unmock('../../../src/constants');
}
beforeEach(request.__resetAuthedRequests);
afterEach(request.__resetAuthedRequests);
test.concurrent('properly find and save build artifacts', async () => {
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
const cacheFolder = path.join(config.cacheFolder, 'npm-dummy-0.0.0');
assert.deepEqual(
(await fs.readJson(path.join(cacheFolder, constants.METADATA_FILENAME))).artifacts,
['dummy', path.join('dummy', 'dummy.txt'), 'dummy.txt'],
);
// retains artifact
const moduleFolder = path.join(config.cwd, 'node_modules', 'dummy');
assert.equal(await fs.readFile(path.join(moduleFolder, 'dummy.txt')), 'foobar');
assert.equal(await fs.readFile(path.join(moduleFolder, 'dummy', 'dummy.txt')), 'foobar');
});
});
test('changes the cache path when bumping the cache version', async () => {
await runInstall({}, 'install-github', async (config): Promise<void> => {
const inOut = new stream.PassThrough();
const reporter = new reporters.JSONReporter({stdout: inOut});
await cache(config, reporter, {}, ['dir']);
assert.ok(!!(JSON.parse(String(inOut.read())) : any).data.match(/[\\\/]v1[\\\/]?$/));
await mockConstants(config, {CACHE_VERSION: 42}, async (config): Promise<void> => {
await cache(config, reporter, {}, ['dir']);
assert.ok(!!(JSON.parse(String(inOut.read())) : any).data.match(/[\\\/]v42[\\\/]?$/));
});
});
});
test('changes the cache directory when bumping the cache version', async () => {
await runInstall({}, 'install-production', async (config, reporter): Promise<void> => {
const lockfile = await Lockfile.fromDirectory(config.cwd);
const resolver = new PackageResolver(config, lockfile);
await resolver.init([{pattern: 'is-array', registry: 'npm'}]);
const ref = resolver.getPackageReferences()[0];
const cachePath = config.generateHardModulePath(ref, true);
await fs.writeFile(path.join(cachePath, 'yarn.test'), 'YARN TEST');
await fs.unlink(path.join(config.cwd, 'node_modules'));
const firstReinstall = new Install({skipIntegrityCheck: true}, config, reporter, lockfile);
await firstReinstall.init();
assert.ok(await fs.exists(path.join(config.cwd, 'node_modules', 'is-array', 'yarn.test')));
await mockConstants(config, {CACHE_VERSION: 42}, async (config): Promise<void> => {
const secondReinstall = new Install({skipIntegrityCheck: true}, config, reporter, lockfile);
await secondReinstall.init();
assert.ok(!await fs.exists(path.join(config.cwd, 'node_modules', 'is-array', 'yarn.test')));
});
});
});
test.concurrent("removes extraneous files that aren't in module or artifacts", async () => {
async function check(cwd: string): Promise<void> {
// retains artifact
const moduleFolder = path.join(cwd, 'node_modules', 'dummy');
assert.equal(await fs.readFile(path.join(moduleFolder, 'dummy.txt')), 'foobar');
assert.equal(await fs.readFile(path.join(moduleFolder, 'dummy', 'dummy.txt')), 'foobar');
// removes extraneous
assert.ok(!(await fs.exists(path.join(moduleFolder, 'dummy2.txt'))));
}
async function create(cwd: string): Promise<void> {
// create an extraneous file
const moduleFolder = path.join(cwd, 'node_modules', 'dummy');
await fs.mkdirp(moduleFolder);
await fs.writeFile(path.join(moduleFolder, 'dummy2.txt'), 'foobar');
}
await runInstall({}, 'artifacts-finds-and-saves', async (config): Promise<void> => {
await check(config.cwd);
await create(config.cwd);
// run install again
const install = new Install({force: true}, config, config.reporter, new Lockfile());
await install.init();
await check(config.cwd);
}, create);
});
test.concurrent("production mode with deduped dev dep shouldn't be removed", async () => {
await runInstall({production: true}, 'install-prod-deduped-dev-dep', async (config) => {
assert.equal(
(await fs.readJson(path.join(config.cwd, 'node_modules', 'a', 'package.json'))).version,
'1.0.0',
);
assert.equal(
(await fs.readJson(path.join(config.cwd, 'node_modules', 'c', 'package.json'))).version,
'1.0.0',
);
assert.ok(
!await fs.exists(path.join(config.cwd, 'node_modules', 'b')),
);
});
});
test.concurrent('hoisting should factor ignored dependencies', async () => {
// you should only modify this test if you know what you're doing
// when we calculate hoisting we need to factor in ignored dependencies in it
// so we get deterministic hoisting across environments, for example in production mode
// we should still be taking dev dependencies into consideration
async function checkNormal(config): Promise<void> {
assert.equal(
(await fs.readJson(path.join(config.cwd, 'node_modules', 'a', 'package.json'))).version,
'1.0.0',
);
assert.equal(
(await fs.readJson(path.join(config.cwd, 'node_modules', 'd', 'package.json'))).version,
'1.0.0',
);
assert.equal(
(await fs.readJson(path.join(config.cwd, 'node_modules', 'd', 'node_modules', 'c', 'package.json'))).version,
'2.0.0',
);
}
await runInstall({}, 'install-ignored-retains-hoisting-structure', async (config) => {
await checkNormal(config);
assert.equal(
(await fs.readJson(path.join(config.cwd, 'node_modules', 'b', 'package.json'))).version,
'3.0.0',
);
assert.equal(
(await fs.readJson(path.join(config.cwd, 'node_modules', 'c', 'package.json'))).version,
'5.0.0',
);
});
await runInstall({production: true}, 'install-ignored-retains-hoisting-structure', async (config) => {
await checkNormal(config);
assert.ok(
!await fs.exists(path.join(config.cwd, 'node_modules', 'b')),
);
assert.ok(
!await fs.exists(path.join(config.cwd, 'node_modules', 'c')),
);
});
});
test.concurrent('--production flag ignores dev dependencies', () => {
return runInstall({production: true}, 'install-production', async (config) => {
assert.ok(
!await fs.exists(path.join(config.cwd, 'node_modules', 'left-pad')),
);
assert.ok(
await fs.exists(path.join(config.cwd, 'node_modules', 'is-array')),
);
});
});
test.concurrent('--production flag does not link dev dependency bin scripts', () => {
return runInstall({production: true, binLinks: true}, 'install-production-bin', async (config) => {
assert.ok(
!await fs.exists(path.join(config.cwd, 'node_modules', '.bin', 'touch')),
);
assert.ok(
await fs.exists(path.join(config.cwd, 'node_modules', '.bin', 'rimraf')),
);
});
});
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'));
assert(lockfile.indexOf('foobar') >= 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'));
assert(lockfile.indexOf('foobar') === -1);
});
});
test.concurrent('writes a lockfile even when there are no dependencies', (): Promise<void> => {
// https://github.com/yarnpkg/yarn/issues/679
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'));
assert(lockfileExists);
// 1 for integrity file (located in node_modules)
assert.equal(installedDepFiles.length, 1);
});
});
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'));
}
assert(thrown);
});
test.concurrent('install transitive optional dependency from lockfile', (): Promise<void> => {
return runInstall({}, 'install-optional-dep-from-lockfile', (config, reporter, install) => {
assert(install && install.resolver && install.resolver.patterns['fsevents@^1.0.0']);
});
});
test.concurrent('root install from shrinkwrap', (): Promise<void> => {
return runInstall({}, 'root-install-with-lockfile');
});
test.concurrent('root install with optional deps', (): Promise<void> => {
return runInstall({}, 'root-install-with-optional-dependency');
});
test.concurrent('install file: protocol with relative paths', (): Promise<void> => {
return runInstall({noLockfile: true}, 'install-file-relative', async (config) => {
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'root-a', 'index.js')),
'foobar\n',
);
});
});
test.concurrent('install file: protocol without cache', async (): Promise<void> => {
const fixturesLoc = path.join(__dirname, '..', '..', 'fixtures', 'install');
const compLoc = path.join(fixturesLoc, 'install-file-without-cache', 'comp', 'index.js');
await fs.writeFile(compLoc, 'foo\n');
await runInstall({}, 'install-file-without-cache', async (config, reporter) => {
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'comp', 'index.js')),
'foo\n',
);
await fs.writeFile(compLoc, 'bar\n');
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
// TODO: This should actually be equal. See https://github.com/yarnpkg/yarn/pull/2443.
assert.notEqual(
await fs.readFile(path.join(config.cwd, 'node_modules', 'comp', 'index.js')),
'bar\n',
);
});
});
test.concurrent('install file: local packages with local dependencies', async (): Promise<void> => {
await runInstall({}, 'install-file-local-dependency', async (config, reporter) => {
const reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'a', 'index.js')),
'foo\n',
);
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'b', 'index.js')),
'bar\n',
);
});
});
test.concurrent('install file: protocol', (): Promise<void> => {
return runInstall({noLockfile: true}, 'install-file', async (config) => {
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'foo', 'index.js')),
'foobar\n',
);
});
});
test.concurrent('install with file: protocol as default', (): Promise<void> => {
return runInstall({noLockfile: true}, 'install-file-as-default', async (config) => {
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'foo', 'index.js')),
'foobar\n',
);
});
});
test.concurrent('install everything when flat is enabled', (): Promise<void> => {
return runInstall({noLockfile: true, flat: true}, 'install-file', async (config) => {
assert.equal(
await fs.readFile(path.join(config.cwd, 'node_modules', 'foo', 'index.js')),
'foobar\n',
);
});
});
test.concurrent('install renamed packages', (): Promise<void> => {
return runInstall({}, 'install-renamed-packages', async (config): Promise<void> => {
const dir = path.join(config.cwd, 'node_modules');
const json = await fs.readJson(path.join(dir, 'left-pad', 'package.json'));
assert.equal(json.version, '1.0.0');
const json2 = await fs.readJson(path.join(dir, 'left-pad2', 'package.json'));
assert.equal(json2.version, '1.1.0');
});
});
test.concurrent('install from offline mirror', (): Promise<void> => {
return runInstall({}, 'install-from-offline-mirror', async (config): Promise<void> => {
const allFiles = await fs.walk(config.cwd);
assert(allFiles.findIndex((file): boolean => {
return file.relative === path.join('node_modules', 'fake-dependency', 'package.json');
}) !== -1);
assert(allFiles.findIndex((file): boolean => {
return file.relative === path.join('node_modules', '@fakescope', 'fake-dependency', 'package.json');
}) !== -1);
});
});
test.concurrent('install from git cache', (): Promise<void> => {
return runInstall({}, 'install-from-git-cache', async (config): Promise<void> => {
assert.equal(await getPackageVersion(config, 'dep-a'), '0.0.1');
});
});
test.concurrent('install from github', (): Promise<void> => {
return runInstall({}, 'install-github');
});
test.concurrent('check and install should verify integrity in the same way when flat', (): Promise<void> => {
return runInstall({flat: true}, 'install-should-dedupe-avoiding-conflicts-1', async (config, reporter) => {
// Will raise if check doesn't flatten the patterns
await check(config, reporter, {flat: true, integrity: true}, []);
});
});
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
// (deduped)
return runInstall(
{},
'install-should-cleanup-when-package-json-changed',
async (config, reporter): Promise<void> => {
assert.equal(await getPackageVersion(config, 'dep-a'), '1.0.0');
assert.equal(await getPackageVersion(config, 'dep-b'), '2.0.0');
assert.equal(await getPackageVersion(config, 'dep-a/dep-b'), '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();
assert.equal(await getPackageVersion(config, 'dep-a'), '1.2.0');
assert.equal(await getPackageVersion(config, 'dep-b'), '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
return runInstall(
{},
'install-should-cleanup-when-package-json-changed-2',
async (config, reporter): Promise<void> => {
assert.equal(await getPackageVersion(config, 'dep-a'), '1.0.0');
assert.equal(await getPackageVersion(config, 'dep-b'), '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();
assert.equal(await getPackageVersion(config, 'dep-a'), '1.2.0');
assert(!await fs.exists(path.join(config.cwd, 'node_modules/dep-b')));
},
);
},
);
test.concurrent('check should verify that top level dependencies are installed correctly', (): Promise<void> => {
return runInstall({}, 'check-top-correct', async (config, reporter) => {
const pkgDep = JSON.parse(await fs.readFile(path.join(
config.cwd,
'node_modules/fake-yarn-dependency/package.json',
)));
pkgDep.version = '2.0.0';
await fs.writeFile(
path.join(config.cwd, 'node_modules/fake-yarn-dependency/package.json'),
JSON.stringify(pkgDep, null, 4),
);
let allCorrect = false;
try {
await check(config, reporter, {}, []);
} catch (err) {
allCorrect = true;
}
expect(allCorrect).toBe(true);
});
});
test.concurrent('install should run install scripts in the order of dependencies', (): Promise<void> => {
return runInstall({}, 'scripts-order', async (config, reporter) => {
expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-a/dep-a-built'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-b/dep-b-built'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-c/dep-c-built'))).toBe(true);
});
});
test.concurrent(
'run install scripts in the order when one dependency does not have install script',
(): Promise<void> => {
return runInstall({}, 'scripts-order-with-one-package-missing-install-script', async (config, reporter) => {
expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-a/dep-a-built'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-b/dep-b-built'))).toBe(true);
expect(await fs.exists(path.join(config.cwd, 'node_modules/dep-d/dep-d-built'))).toBe(true);
});
},
);
test.concurrent('install should circumvent circular dependencies', (): Promise<void> => {
return runInstall({}, 'install-should-circumvent-circular-dependencies', async (config, reporter) => {
assert.equal(
await getPackageVersion(config, 'dep-a'),
'1.0.0',
);
assert.equal(
await getPackageVersion(config, 'dep-b'),
'1.0.0',
);
assert.equal(
await getPackageVersion(config, 'dep-c'),
'1.0.0',
);
});
});
// don't run this test in `concurrent`, it will affect other tests
test('install should respect NODE_ENV=production', (): Promise<void> => {
const env = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
return runInstall({}, 'install-should-respect-node_env', async (config) => {
expect(await fs.exists(path.join(config.cwd, 'node_modules/is-negative-zero/package.json'))).toBe(false);
// restore env
process.env.NODE_ENV = env;
});
});
// don't run this test in `concurrent`, it will affect other tests
test('install should respect NPM_CONFIG_PRODUCTION=false over NODE_ENV=production', (): Promise<void> => {
const env = process.env.NODE_ENV;
const prod = process.env.NPM_CONFIG_PRODUCTION;
process.env.NODE_ENV = 'production';
process.env.NPM_CONFIG_PRODUCTION = 'false';
return runInstall({}, 'install-should-respect-npm_config_production', async (config) => {
expect(await fs.exists(path.join(config.cwd, 'node_modules/is-negative-zero/package.json'))).toBe(true);
// restore env
process.env.NODE_ENV = env;
process.env.NPM_CONFIG_PRODUCTION = prod;
});
});
// don't run this test in `concurrent`, it will affect other tests
test('install should respect production flag false over NODE_ENV=production', (): Promise<void> => {
const env = process.env.NODE_ENV;
process.env.NODE_ENV = 'production';
return runInstall({production: 'false'}, 'install-should-respect-production_flag_over_node-env', async (config) => {
expect(await fs.exists(path.join(config.cwd, 'node_modules/is-negative-zero/package.json'))).toBe(true);
// restore env
process.env.NODE_ENV = env;
});
});
test.concurrent('install should resolve circular dependencies 2', (): Promise<void> => {
return runInstall({}, 'install-should-circumvent-circular-dependencies-2', async (config, reporter) => {
assert.equal(
await getPackageVersion(config, 'es5-ext'),
'0.10.12',
);
});
});
test.concurrent('install should be idempotent', (): Promise<void> => {
// Install a package twice
runInstall({}, 'install-should-be-idempotent', async (config, reporter) => {
assert.equal(
await getPackageVersion(config, 'dep-a'),
'1.0.0',
);
}, null, false);
return runInstall({}, 'install-should-be-idempotent', async (config, reporter) => {
assert.equal(
await getPackageVersion(config, 'dep-a'),
'1.0.0',
);
});
});
test.concurrent(
'install should add missing deps to yarn and mirror (PR import scenario)',
(): Promise<void> => {
return runInstall({}, 'install-import-pr', async (config) => {
assert.equal(await getPackageVersion(config, 'mime-types'), '2.0.0');
assert(semver.satisfies(await getPackageVersion(config, 'mime-db'), '~1.0.1'));
assert.equal(await getPackageVersion(config, 'fake-yarn-dependency'), '1.0.1');
const mirror = await fs.walk(path.join(config.cwd, 'mirror-for-offline'));
assert.equal(mirror.length, 3);
assert.equal(mirror[0].relative, 'fake-yarn-dependency-1.0.1.tgz');
assert.equal(mirror[1].relative.indexOf('mime-db-1.0.'), 0);
assert.equal(mirror[2].relative, 'mime-types-2.0.0.tgz');
const lockFileContent = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const lockFileLines = explodeLockfile(lockFileContent);
assert.equal(lockFileLines.length, 11);
assert.equal(lockFileLines[3].indexOf('mime-db@'), 0);
assert.equal(lockFileLines[6].indexOf('[email protected]'), 0);
});
},
);
// disabled to resolve https://github.com/yarnpkg/yarn/pull/1210
test.skip('install should hoist nested bin scripts', (): Promise<void> => {
return runInstall({binLinks: true}, 'install-nested-bin', async (config) => {
const binScripts = await fs.walk(path.join(config.cwd, 'node_modules', '.bin'));
// need to double the amount as windows makes 2 entries for each dependency
// so for below, there would be an entry for eslint and eslint.cmd on win32
const amount = process.platform === 'win32' ? 20 : 10;
assert.equal(binScripts.length, amount);
assert(binScripts.findIndex((f) => f.basename === 'eslint') > -1);
});
});
test.concurrent('install should respect --no-bin-links flag', (): Promise<void> => {
return runInstall({binLinks: false}, 'install-nested-bin', async (config) => {
const binExists = await fs.exists(path.join(config.cwd, 'node_modules', '.bin'));
assert(!binExists);
});
});
test.concurrent('install should update a dependency to yarn and mirror (PR import scenario 2)', (): Promise<void> => {
// [email protected] is gets updated to [email protected] via
// a change in package.json,
// files in mirror, yarn.lock, package.json and node_modules should reflect that
return runInstall({}, 'install-import-pr-2', async (config, reporter): Promise<void> => {
assert(semver.satisfies(
await getPackageVersion(config, 'mime-db'),
'~1.0.1'),
);
assert.equal(
await getPackageVersion(config, 'mime-types'),
'2.0.0',
);
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();
assert(semver.satisfies(
await getPackageVersion(config, 'mime-db'),
'~1.23.0',
));
assert.equal(
await getPackageVersion(config, 'mime-types'),
'2.1.11',
);
const lockFileWritten = await fs.readFile(path.join(config.cwd, 'yarn.lock'));
const lockFileLines = explodeLockfile(lockFileWritten);
assert.equal(lockFileLines[0], 'mime-db@~1.23.0:');
assert.notEqual(lockFileLines[2].indexOf('resolved mime-db-'), -1);
assert.equal(lockFileLines[3], '[email protected]:');
assert.notEqual(lockFileLines[5].indexOf('resolved mime-types-2.1.11.tgz'), -1);
const mirror = await fs.walk(path.join(config.cwd, 'mirror-for-offline'));
assert.equal(mirror.length, 4);
const newFilesInMirror = mirror.filter((elem): boolean => {
return elem.relative !== 'mime-db-1.0.3.tgz' && elem.relative !== 'mime-types-2.0.0.tgz';
});
assert.equal(newFilesInMirror.length, 2);
});
});
if (process.platform !== 'win32') {
// TODO: This seems like a real issue, not just a config issue
test.concurrent('install cache symlinks properly', (): Promise<void> => {
return runInstall({}, 'cache-symlinks', async (config, reporter) => {
const symlink = path.resolve(config.cwd, 'node_modules', 'dep-a', 'link-index.js');
expect(await fs.exists(symlink)).toBe(true);
await fs.unlink(path.join(config.cwd, 'node_modules'));
const lockfile = await createLockfile(config.cwd);
const install = new Install({}, config, reporter, lockfile);
await install.init();
expect(await fs.exists(symlink)).toBe(true);
});
});
}
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', 'package.json'));
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 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')));
});
});
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');
assert(/\r\n/.test(lockfile));
assert(!/[^\r]\n/.test(lockfile));
},
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');
assert(/[^\r]\n/.test(lockfile));
assert(!/\r\n/.test(lockfile));
},
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');
assert(lockfile.indexOf(os.EOL) >= 0);
});
});
// sync test because we need to get all the requests to confirm their validity
test('install a scoped module from authed private registry', (): Promise<void> => {
return runInstall({noLockfile: true}, 'install-from-authed-private-registry', async (config) => {
const authedRequests = request.__getAuthedRequests();
assert.equal(authedRequests[0].url, 'https://registry.yarnpkg.com/@types%2flodash');
assert.equal(authedRequests[0].headers.authorization, 'Bearer abc123');
assert.equal(authedRequests[1].url, 'https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.37.tgz');
assert.equal(authedRequests[1].headers.authorization, 'Bearer abc123');
assert.equal(
(await fs.readFile(path.join(config.cwd, 'node_modules', '@types', 'lodash', 'index.d.ts'))).split('\n')[0],
'// Type definitions for Lo-Dash 4.14',
);
});
});
test('install a scoped module from authed private registry with a missing trailing slash', (): Promise<void> => {
return runInstall({noLockfile: true}, 'install-from-authed-private-registry-no-slash', async (config) => {
const authedRequests = request.__getAuthedRequests();
assert.equal(authedRequests[0].url, 'https://registry.yarnpkg.com/@types%2flodash');
assert.equal(authedRequests[0].headers.authorization, 'Bearer abc123');
assert.equal(authedRequests[1].url, 'https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.37.tgz');
assert.equal(authedRequests[1].headers.authorization, 'Bearer abc123');
assert.equal(
(await fs.readFile(path.join(config.cwd, 'node_modules', '@types', 'lodash', 'index.d.ts'))).split('\n')[0],
'// Type definitions for Lo-Dash 4.14',
);
});
});
test.concurrent('install will not overwrite files in symlinked scoped directories', async (): Promise<void> => {
await runInstall({}, 'install-dont-overwrite-linked-scoped', async (config): Promise<void> => {
const dependencyPath = path.join(config.cwd, 'node_modules', '@fakescope', 'fake-dependency');
assert.equal(
'Symlinked scoped package test',
(await fs.readJson(path.join(dependencyPath, 'package.json'))).description,
);
assert.ok(!(await fs.exists(path.join(dependencyPath, 'index.js'))));
}, async (cwd) => {
const dirToLink = path.join(cwd, 'dir-to-link');
await fs.mkdirp(path.join(cwd, '.yarn-link', '@fakescope'));
await fs.symlink(dirToLink, path.join(cwd, '.yarn-link', '@fakescope', 'fake-dependency'));
await fs.mkdirp(path.join(cwd, 'node_modules', '@fakescope'));
await fs.symlink(dirToLink, path.join(cwd, 'node_modules', '@fakescope', 'fake-dependency'));
});
});
test.concurrent('install of scoped package with subdependency conflict should pass check', (): Promise<void> => {
return runInstall({}, 'install-scoped-package-with-subdependency-conflict', async (config, reporter) => {
let allCorrect = true;
try {
await check(config, reporter, {integrity: false}, []);
} catch (err) {
allCorrect = false;
}
expect(allCorrect).toBe(true);
});
});
test.concurrent('install a module with incompatible optional dependency should skip dependency',
(): Promise<void> => {
return runInstall({}, 'install-should-skip-incompatible-optional-dep', async (config) => {
assert.ok(!(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-incompatible'))));
});
});
test.concurrent('install a module with incompatible optional dependency should skip transient dependencies',
(): Promise<void> => {
return runInstall({}, 'install-should-skip-incompatible-optional-dep', async (config) => {
assert.ok(!(await fs.exists(path.join(config.cwd, 'node_modules', 'dep-a'))));
});
});
// this tests for a problem occuring due to optional dependency incompatible with os, in this case fsevents
// this would fail on os's incompatible with fsevents, which is everything except osx.
if (process.platform !== 'darwin') {
test.concurrent('install incompatible optional dependency should still install shared child dependencies',
(): Promise<void> => {
return runInstall({}, 'install-should-not-skip-required-shared-deps', async (config) => {
assert.ok(await fs.exists(path.join(config.cwd, 'node_modules', 'deep-extend')));
assert.ok(await fs.exists(path.join(config.cwd, 'node_modules', 'ini')));
assert.ok(await fs.exists(path.join(config.cwd, 'node_modules', 'strip-json-comments')));
});
});
}
test.concurrent('optional dependency that fails to build should not be installed',
(): Promise<void> => {
return runInstall({}, 'should-not-install-failing-optional-deps', async (config) => {
assert.equal(await fs.exists(path.join(config.cwd, 'node_modules', 'optional-failing')), false);
});
});
// Covers current behavior, issue opened whether this should be changed https://github.com/yarnpkg/yarn/issues/2274
test.concurrent('a subdependency of an optional dependency that fails should be installed',
(): Promise<void> => {
return runInstall({}, 'should-install-failing-optional-sub-deps', async (config) => {
assert.equal(await fs.exists(path.join(config.cwd, 'node_modules', 'optional-failing')), false);
assert.equal(await fs.exists(path.join(config.cwd, 'node_modules', 'sub-dep')), true);
});
});
test.concurrent('should not loose dependencies when installing with --production',
(): Promise<void> => {
// revealed https://github.com/yarnpkg/yarn/issues/2263
return runInstall({production: true}, 'prod-should-keep-subdeps', async (config) => {
// would be hoisted from gulp/vinyl-fs/glob-stream/minimatch/brace-expansion/balanced-match
assert.equal(await getPackageVersion(config, 'balanced-match'), '0.4.2');
});
});
// https://github.com/yarnpkg/yarn/issues/2470
test.concurrent('a allows dependency with [] in os cpu requirements',
(): Promise<void> => {
return runInstall({}, 'empty-os', async (config) => {
assert(await fs.exists(path.join(config.cwd, 'node_modules', 'feed')));
});
});
test.concurrent('should skip integrity check and do install when --skip-integrity-check flag is passed',
(): Promise<void> => {
return runInstall({}, 'skip-integrity-check', async (config, reporter) => {
assert.equal(await fs.exists(path.join(config.cwd, 'node_modules', 'sub-dep')), true);
await fs.unlink(path.join(config.cwd, 'node_modules', 'sub-dep'));
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 reinstall = new Install({}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
// reinstall will be successful but it won't reinstall anything
assert.equal(await fs.exists(path.join(config.cwd, 'node_modules', 'sub-dep')), false);
reinstall = new Install({skipIntegrityCheck: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
// reinstall will reinstall deps
assert.equal(await fs.exists(path.join(config.cwd, 'node_modules', 'sub-dep')), true);
let newLockContent = await fs.readFile(
path.join(config.cwd, 'yarn.lock'),
);
assert.equal(lockContent, newLockContent);
reinstall = new Install({force: true}, config, reporter, await Lockfile.fromDirectory(config.cwd));
await reinstall.init();
// force rewrites lockfile
newLockContent = await fs.readFile(
path.join(config.cwd, 'yarn.lock'),
);
assert.notEqual(lockContent, newLockContent);
});
});
test.concurrent(
'should install if symlink source does not exist',
async (): Promise<void> => {
await runInstall({}, 'relative-symlinks-work', () => {});
});