File: package-hoister.js

package info (click to toggle)
node-yarnpkg 1.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 92,980 kB
  • sloc: sh: 323; makefile: 19
file content (837 lines) | stat: -rw-r--r-- 31,398 bytes parent folder | download
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
/* @flow */

import PackageHoister, {HoistManifest} from '../src/package-hoister.js';
import PackageResolver from '../src/package-resolver.js';
import Lockfile from '../src/lockfile';
import type PackageReference from '../src/package-reference.js';
import {extractWorkspaces} from '../src/config.js';
import type Config from '../src/config.js';
import type {Manifest, WorkspacesManifestMap, WorkspacesConfig} from '../src/types.js';
import WorkspaceLayout from '../src/workspace-layout.js';
import {getPackagePath} from './commands/_helpers.js';
import mm from 'micromatch';

const path = require('path');

const CWD = 'tmp';

function createManifestForUid(uid, dependencies): Manifest {
  const name = uid.split('@')[0];
  return (({
    name,
    _reference: (({
      name,
      uid,
      dependencies,
    }: any): PackageReference),
  }: any): Manifest);
}

// Rig up a PackageHoister to use for testing.
// `testModules` is a hash where:
//   key is the module uid, in the form name@version ('lodash@1.2.3')
//   value is an array of strings of dependencies (['lodash@1.2.3', 'grunt@4.5.6'])
// These modules will be loaded into a mock PackageResolver that will use the hash's keys to resolve the package.
type UpdateResolver = (resolver: PackageResolver, config: Config) => void;

function createTestFixture(
  testModules: any = {},
  workspacesEnabled: boolean = true,
  updateResolver?: UpdateResolver,
): any {
  const config: Config = (({
    cwd: CWD,
    lockfileFolder: CWD,
    workspacesEnabled,
    getFolder(): string {
      return 'node_modules';
    },
    generateModuleCachePath(pkg: ?PackageReference): string {
      return pkg ? pkg.uid : '';
    },
    getWorkspaces(manifest: ?Manifest): ?WorkspacesConfig {
      if (this.workspacesEnabled) {
        return extractWorkspaces(manifest);
      }
      return undefined;
    },
  }: any): Config);

  // build Manifests with just enough information to get the PackageHoister to work.
  const lockfile = new Lockfile();
  const packageResolver = new PackageResolver(config, lockfile);
  Object.keys(testModules).map(uid => {
    const packageManifest = createManifestForUid(uid, testModules[uid]);

    // load the manifest into the PackageResolver
    packageResolver.addPattern(uid, packageManifest);
  });

  if (updateResolver) {
    updateResolver(packageResolver, config);
  }

  const packageHoister = new PackageHoister(config, packageResolver);

  const atPath = function(...installPaths): string {
    const rootPath = config.modulesFolder || path.join(config.cwd, 'node_modules');
    return path.join(rootPath, ...installPaths);
  };

  const ignorePackage = function(uid) {
    packageResolver.getStrictResolvedPattern(uid)._reference.ignore = true;
  };

  return {
    config,
    packageResolver,
    packageHoister,
    atPath,
    ignorePackage,
  };
}

const toContainPackage = function(received: any, ...expected: any): JestMatcherResult {
  const [uid, expectedInstallPath] = expected;

  let pass: boolean = false;
  received.forEach(pkg => {
    const [location: string, hoistManifest: HoistManifest] = pkg;
    if (location === expectedInstallPath && hoistManifest.pkg._reference.uid === uid) {
      pass = true;
    }
  });

  if (pass) {
    return {
      pass: true,
      message: () => `expected ${received} to not contain package UID ${uid} at path ${expectedInstallPath}`,
    };
  } else {
    return {
      pass: false,
      message: () => `expected ${received} to contain package UID ${uid} at path ${expectedInstallPath}`,
    };
  }
};

expect.extend({toContainPackage});

test('Produces valid destination paths for scoped modules', () => {
  const expected = path.join(CWD, 'node_modules', '@scoped', 'dep');
  const scopedPackageName = '@scoped/dep';

  const key = scopedPackageName;
  const parts = [scopedPackageName];

  const pkg = (({
    _reference: (({}: any): PackageReference),
  }: any): Manifest);

  const info = new HoistManifest(key, parts, pkg, '', true, true, false);

  const tree = new Map([['@scoped/dep', info]]);

  const fixture = createTestFixture();
  fixture.packageHoister.tree = tree;

  const result = fixture.packageHoister.init();
  const [actual] = result[0];

  expect(actual).toEqual(expected);
});

test('hoists dependencies of dependencies up to root level when no version conflicts exist', () => {
  const {atPath, packageHoister} = createTestFixture({
    'a@1.0.0': ['b@1.0.0'],
    'b@1.0.0': [],
  });

  packageHoister.seed(['a@1.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(2);
  expect(result).toContainPackage('a@1.0.0', atPath('a'));
  expect(result).toContainPackage('b@1.0.0', atPath('b'));
});

test('leaves dependencies of dependencies at leaf level when version conflict exists', () => {
  const {atPath, packageHoister} = createTestFixture({
    'a@1.0.0': ['b@2.0.0'],
    'b@1.0.0': [],
    'b@2.0.0': [],
  });

  packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(3);
  expect(result).toContainPackage('a@1.0.0', atPath('a'));
  expect(result).toContainPackage('b@2.0.0', atPath('a', 'node_modules', 'b'));
  expect(result).toContainPackage('b@1.0.0', atPath('b'));
});

test('eliminates duplicates when multiple packages depend on the same package', () => {
  // a@1 -> b@1 -> d@1
  //     -> c@1 -> d@1
  // should become
  // a@1
  // b@1
  // c@1
  // d@1
  const {atPath, packageHoister} = createTestFixture({
    'a@1.0.0': ['b@1.0.0', 'c@1.0.0'],
    'b@1.0.0': ['d@1.0.0'],
    'c@1.0.0': ['d@1.0.0'],
    'd@1.0.0': [],
  });

  packageHoister.seed(['a@1.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(4);
  expect(result).toContainPackage('a@1.0.0', atPath('a'));
  expect(result).toContainPackage('b@1.0.0', atPath('b'));
  expect(result).toContainPackage('c@1.0.0', atPath('c'));
  expect(result).toContainPackage('d@1.0.0', atPath('d'));
});

test('eliminates duplicates when version conflicts exist', () => {
  // a@1 -> b@1 -> c@1 -> d@2
  //            -> e@1 -> d@2
  //     -> d@1
  // should become
  // a@1
  // b@1
  // c@1 -> d@2
  // d@1
  // e@1 -> d@2
  const {atPath, packageHoister} = createTestFixture({
    'a@1.0.0': ['b@1.0.0', 'd@1.0.0'],
    'b@1.0.0': ['c@1.0.0', 'e@1.0.0'],
    'c@1.0.0': ['d@2.0.0'],
    'e@1.0.0': ['d@2.0.0'],
    'd@1.0.0': [],
    'd@2.0.0': [],
  });

  packageHoister.seed(['a@1.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(7);
  expect(result).toContainPackage('a@1.0.0', atPath('a'));
  expect(result).toContainPackage('b@1.0.0', atPath('b'));
  expect(result).toContainPackage('c@1.0.0', atPath('c'));
  expect(result).toContainPackage('d@1.0.0', atPath('d'));
  expect(result).toContainPackage('e@1.0.0', atPath('e'));
  expect(result).toContainPackage('d@2.0.0', atPath('c', 'node_modules', 'd'));
  expect(result).toContainPackage('d@2.0.0', atPath('e', 'node_modules', 'd'));
});

test('uses the modulesFolder configuration option as the first part of the path', () => {
  const {atPath, config, packageHoister} = createTestFixture({
    'a@1.0.0': ['b@2.0.0'],
    'b@1.0.0': [],
    'b@2.0.0': [],
  });
  config.modulesFolder = path.join('modules', 'folder');

  packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(3);
  expect(result).toContainPackage('a@1.0.0', atPath('a'));
  expect(result).toContainPackage('b@1.0.0', atPath('b'));
  expect(result).toContainPackage('b@2.0.0', atPath('a', 'node_modules', 'b'));
});

test('does not include ignored packages or their dependencies', () => {
  const {atPath, ignorePackage, packageHoister} = createTestFixture({
    'a@1.0.0': ['c@1.0.0'],
    'b@1.0.0': [],
    'c@1.0.0': [],
  });
  ignorePackage('a@1.0.0');

  packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(1);
  expect(result).toContainPackage('b@1.0.0', atPath('b'));
});

test('includes ignored packages dependencies if another non-ignored package depends on it', () => {
  const {atPath, ignorePackage, packageHoister} = createTestFixture({
    'a@1.0.0': ['c@1.0.0'],
    'b@1.0.0': ['c@1.0.0'],
    'c@1.0.0': [],
  });
  ignorePackage('a@1.0.0');

  packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(2);
  expect(result).toContainPackage('b@1.0.0', atPath('b'));
  expect(result).toContainPackage('c@1.0.0', atPath('c'));
});

test('considers ignored packages when determining hoisting', () => {
  // a@1 -> d@1 -> c@2
  // b@3(ignored) -> c@5
  // should become
  // a@1
  // d@1 -> c@2
  //
  // Normally c@2 would hoist to the root, but it cannot because c@5 would be there if it was not ignored.
  // This preserves deterministic install paths whether packages are ignored or not.
  const {atPath, ignorePackage, packageHoister} = createTestFixture({
    'a@1.0.0': ['d@1.0.0'],
    'b@3.0.0': ['c@5.0.0'],
    'd@1.0.0': ['c@2.0.0'],
    'c@2.0.0': [],
    'c@5.0.0': [],
  });
  ignorePackage('b@3.0.0');

  packageHoister.seed(['a@1.0.0', 'b@3.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(3);
  expect(result).toContainPackage('a@1.0.0', atPath('a'));
  expect(result).toContainPackage('d@1.0.0', atPath('d'));
  expect(result).toContainPackage('c@2.0.0', atPath('d', 'node_modules', 'c'));
});

test('will hoist packages under subdirectories when they cannot hoist to root', () => {
  // a@1 -> b@1 -> c@1 -> d@1
  // b@2(ignored) -> c@2 -> d@2
  // should become
  // a@1 -> b@1
  //     -> c@1
  //     -> d@1
  //
  // b,c,d@1 cannot hoist to the root because their @2 versions would be there if not ignored.
  // However they can still flatten under a@1.
  const {atPath, ignorePackage, packageHoister} = createTestFixture({
    'a@1.0.0': ['b@1.0.0'],
    'b@1.0.0': ['c@1.0.0'],
    'c@1.0.0': ['d@1.0.0'],
    'b@2.0.0': ['c@2.0.0'],
    'c@2.0.0': ['d@2.0.0'],
    'd@1.0.0': [],
    'd@2.0.0': [],
  });
  ignorePackage('b@2.0.0');

  packageHoister.seed(['a@1.0.0', 'b@2.0.0']);
  const result = packageHoister.init();

  expect(result.length).toEqual(4);
  expect(result).toContainPackage('a@1.0.0', atPath('a'));
  expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
  expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
  expect(result).toContainPackage('d@1.0.0', atPath('a', 'node_modules', 'd'));
});

describe('nohoist', () => {
  test('nohoist can be turned off by disable workspaces (workspaces-experimental)', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture(
      {
        'a@1.0.0': ['c@1.0.0'],
        'b@2.0.0': [],
        'c@1.0.0': [],
      },
      false,
    );
    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['c']};

    packageHoister.seed(['a@1.0.0', 'b@2.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(3);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@2.0.0', atPath('b'));
    expect(result).toContainPackage('c@1.0.0', atPath('c'));
  });
  describe('nohoist = hoist to the top of its branch instead of root', () => {
    function fixture(): any {
      return createTestFixture({
        'a@1.0.0': ['c@1.0.0'],
        'b@1.0.0': ['c@1.0.0'],
        'c@1.0.0': ['d@1.0.0'],
        'd@1.0.0': [],
      });
    }
    test('shallow nohoist', () => {
      const {atPath, packageHoister, packageResolver} = fixture();

      // nohoist a/c
      const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
      pkg.workspaces = {nohoist: ['c']};

      packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
      const result = packageHoister.init();

      expect(result.length).toEqual(5);
      expect(result).toContainPackage('a@1.0.0', atPath('a'));
      expect(result).toContainPackage('b@1.0.0', atPath('b'));
      expect(result).toContainPackage('c@1.0.0', atPath('c'));
      expect(result).toContainPackage('d@1.0.0', atPath('d'));
      expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    });
    test('deep nohoist', () => {
      const {atPath, packageHoister, packageResolver} = fixture();

      // nohoist a/c and everything under a/c
      const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
      pkg.workspaces = {nohoist: ['c', 'c/**']};

      packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
      const result = packageHoister.init();

      expect(result.length).toEqual(6);
      expect(result).toContainPackage('a@1.0.0', atPath('a'));
      expect(result).toContainPackage('b@1.0.0', atPath('b'));
      expect(result).toContainPackage('c@1.0.0', atPath('c'));
      expect(result).toContainPackage('d@1.0.0', atPath('d'));
      expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
      expect(result).toContainPackage('d@1.0.0', atPath('a', 'node_modules', 'd'));
    });
  });
  test('nohoist pkg will be duplicated independent of the hoisted version', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['c@1.0.0'],
      'b@1.0.0': ['c@1.0.0'],
      'c@1.0.0': ['d@1.0.0'],
      'd@1.0.0': [],
    });

    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['c']};

    packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(5);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@1.0.0', atPath('b'));
    expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    expect(result).toContainPackage('c@1.0.0', atPath('c'));
    expect(result).toContainPackage('d@1.0.0', atPath('d'));
  });
  test('nohoist pkg will NOT be duplicated within the same branch', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['b@1.0.0', 'c@1.0.0'],
      'b@1.0.0': ['d@1.0.0'],
      'c@1.0.0': ['d@1.0.0'],
      'd@1.0.0': [],
    });

    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['b', 'c', '**/d']};

    packageHoister.seed(['a@1.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(4);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
    expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    expect(result).toContainPackage('d@1.0.0', atPath('a', 'node_modules', 'd'));
  });
  test('can dedupe and avoid circular reference', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['c@1.0.0'],
      'b@2.0.0': [],
      'c@1.0.0': ['d@1.0.0'],
      'd@1.0.0': ['c@1.0.0'],
    });

    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['c']};

    packageHoister.seed(['a@1.0.0', 'b@2.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(5);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@2.0.0', atPath('b'));
    expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    expect(result).toContainPackage('d@1.0.0', atPath('d'));
    expect(result).toContainPackage('c@1.0.0', atPath('c'));
  });
  test('can handle conflict version', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['c@1.0.0'],
      'b@2.0.0': [],
      'b@1.0.0': [],
      'c@1.0.0': ['b@1.0.0'],
    });

    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['c']};

    packageHoister.seed(['a@1.0.0', 'b@2.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(4);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@2.0.0', atPath('b'));
    expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
  });

  test('nohoist matches package regardless version', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['b@1.0.0', 'c@1.0.0'],
      'b@1.0.0': [],
      'b@2.0.0': [],
      'c@1.0.0': ['b@2.0.0'],
    });

    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['b', 'c']};

    packageHoister.seed(['a@1.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(4);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
    expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    expect(result).toContainPackage('b@2.0.0', atPath('a', 'node_modules', 'c', 'node_modules', 'b'));
  });
  test('can detect circular reference', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['b@1.0.0', 'c@1.0.0'],
      'b@2.0.0': [],
      'b@1.0.0': ['c@1.0.0'],
      'c@1.0.0': ['b@2.0.0'],
    });

    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['b']};

    packageHoister.seed(['a@1.0.0', 'b@2.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(4);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@2.0.0', atPath('b'));
    expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
    expect(result).toContainPackage('c@1.0.0', atPath('c'));
  });
  test('can nohoist a linked package', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['b@1.0.0', 'c@1.0.0'],
      'b@1.0.0': ['d@1.0.0'],
      'c@1.0.0': [],
      'd@1.0.0': [],
    });

    // b is a linked package
    let pkg: any = packageResolver.getStrictResolvedPattern('b@1.0.0');
    pkg._remote = {type: 'link'};

    // a will not hoist b and everything under b
    pkg = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['b', 'b/**']};

    packageHoister.seed(['a@1.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(4);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('c@1.0.0', atPath('c'));
    expect(result).toContainPackage('d@1.0.0', atPath('a', 'node_modules', 'd'));
    expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
  });

  describe('nohoistList pattern matching', () => {
    const paths = ['a', 'a/b', 'a/b/c', 'a/b/c/d', 'a/d', 'b/c', 'd/a', 'e/d/a', 'd/a/e', 'd/a/e/b'];
    test('match explicit path', () => {
      expect(mm(paths, 'a')).toEqual(['a']);
      expect(mm(paths, 'b/c')).toEqual(['b/c']);
      expect(mm(paths, 'd')).toEqual([]);
      expect(mm(paths, 'a/b')).toEqual(['a/b']);
    });
    test('match glob pattern', () => {
      expect(mm(paths, '**')).toEqual(paths);
      expect(mm(paths, '*')).toEqual(['a']);

      expect(mm(paths, 'a/*')).toEqual(['a/b', 'a/d']);
      expect(mm(paths, 'a/**')).toEqual(['a/b', 'a/b/c', 'a/b/c/d', 'a/d']);
      expect(mm(paths, '*/a')).toEqual(['d/a']);
      expect(mm(paths, '**/a')).toEqual(['a', 'd/a', 'e/d/a']);
      expect(mm(paths, '*/a/*')).toEqual(['d/a/e']);
      expect(mm(paths, '**/a/**')).toEqual(['a/b', 'a/b/c', 'a/b/c/d', 'a/d', 'd/a/e', 'd/a/e/b']);
    });
  });
  test('can disable hoist for the whole branch', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['b@1.0.0', 'c@1.0.0'],
      'b@1.0.0': ['d@1.0.0'],
      'c@1.0.0': ['d@1.0.0'],
      'd@1.0.0': [],
    });

    // disable hoisting for everything under a
    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['**']};

    packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(6);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('b@1.0.0', atPath('b'));
    expect(result).toContainPackage('d@1.0.0', atPath('d'));

    expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
    expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    expect(result).toContainPackage('d@1.0.0', atPath('a', 'node_modules', 'd'));
  });
  test('can disable hoist for particular package tree', () => {
    const {atPath, packageHoister, packageResolver} = createTestFixture({
      'a@1.0.0': ['b@1.0.0', 'e@1.0.0'],
      'b@1.0.0': ['c@1.0.0'],
      'c@1.0.0': ['d@1.0.0'],
      'd@1.0.0': [],
      'e@1.0.0': [],
    });

    // disable hoisting for everything under a
    const pkg: any = packageResolver.getStrictResolvedPattern('a@1.0.0');
    pkg.workspaces = {nohoist: ['b', 'b/**']};

    packageHoister.seed(['a@1.0.0']);
    const result = packageHoister.init();

    expect(result.length).toEqual(5);
    expect(result).toContainPackage('a@1.0.0', atPath('a'));
    expect(result).toContainPackage('e@1.0.0', atPath('e'));

    expect(result).toContainPackage('b@1.0.0', atPath('a', 'node_modules', 'b'));
    expect(result).toContainPackage('c@1.0.0', atPath('a', 'node_modules', 'c'));
    expect(result).toContainPackage('d@1.0.0', atPath('a', 'node_modules', 'd'));
  });
  describe('nohoist with workspaces context', () => {
    function updateWorkspaces(rootPattern: string, nohoist?: Array<string>, workspaces: Array<string>): UpdateResolver {
      return (resolver: PackageResolver, config: Config): void => {
        const root: Manifest = resolver.getStrictResolvedPattern(rootPattern);
        if (nohoist) {
          root.workspaces = {nohoist};
        }

        const wsMap: WorkspacesManifestMap = {};
        wsMap[root.name] = {loc: root._loc || '', manifest: root};

        workspaces.forEach(w => {
          const pkg = resolver.getStrictResolvedPattern(w);
          pkg._remote = {
            type: 'workspace',
            registry: 'npm',
            hash: null,
            reference: '',
          };
          wsMap[pkg.name] = {loc: pkg._loc || '', manifest: pkg};
        });
        resolver.workspaceLayout = new WorkspaceLayout(wsMap, config);
        resolver.workspaceLayout.virtualManifestName = root.name;

        // hoister.nohoistResolver._wsRootPackageName = root.name;
      };
    }
    describe('disable hoist for a package', () => {
      function fixture(f: UpdateResolver): any {
        return createTestFixture(
          {
            'w1@1.0.0': ['a@1.0.0'],
            'w2@1.0.0': ['a@1.0.0', 'b@1.0.0'],
            'a@1.0.0': ['c@1.0.0'],
            'b@1.0.0': ['a@1.0.0'],
            'c@1.0.0': [],
            'root@1.0.0': ['w1@1.0.0', 'w2@1.0.0'],
          },
          true,
          f,
        );
      }

      test('from root for all workspaces', () => {
        // root disable 'a' hoisting, no matter where it is
        const {packageHoister} = fixture(updateWorkspaces('root@1.0.0', ['**/a'], ['w1@1.0.0', 'w2@1.0.0']));
        const config = packageHoister.config;

        packageHoister.seed(['root@1.0.0']);
        const result = packageHoister.init();

        expect(result.length).toEqual(8);
        expect(result).toContainPackage('root@1.0.0', getPackagePath(config, 'root'));
        expect(result).toContainPackage('w1@1.0.0', getPackagePath(config, 'w1'));
        expect(result).toContainPackage('w2@1.0.0', getPackagePath(config, 'w2'));
        expect(result).toContainPackage('b@1.0.0', getPackagePath(config, 'b'));
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'c'));

        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'w1/a'));
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'w2/a'));

        // note: a hoisted b will also inherit the root's nohoist constraint
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'b/a'));
      });
      test('from root for a specific workspaces', () => {
        // root disable 'a' hoisting, no matter where it is
        const {packageHoister} = fixture(updateWorkspaces('root@1.0.0', ['w1/a'], ['w1@1.0.0', 'w2@1.0.0']));
        const config = packageHoister.config;

        packageHoister.seed(['root@1.0.0']);
        const result = packageHoister.init();

        expect(result.length).toEqual(7);
        expect(result).toContainPackage('root@1.0.0', getPackagePath(config, 'root'));
        expect(result).toContainPackage('w1@1.0.0', getPackagePath(config, 'w1'));
        expect(result).toContainPackage('w2@1.0.0', getPackagePath(config, 'w2'));
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'a'));
        expect(result).toContainPackage('b@1.0.0', getPackagePath(config, 'b'));
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'c'));

        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'w1/a'));
      });
      test('from workspace', () => {
        // w2 disable 'a' hoisting
        const {packageHoister, packageResolver} = fixture(
          updateWorkspaces('root@1.0.0', undefined, ['w1@1.0.0', 'w2@1.0.0']),
        );
        const config = packageHoister.config;

        const pkg: any = packageResolver.getStrictResolvedPattern('w2@1.0.0');
        pkg.workspaces = {nohoist: ['a']};

        packageHoister.seed(['root@1.0.0']);
        const result = packageHoister.init();

        expect(result.length).toEqual(7);
        expect(result).toContainPackage('root@1.0.0', getPackagePath(config, 'root'));
        expect(result).toContainPackage('w1@1.0.0', getPackagePath(config, 'w1'));
        expect(result).toContainPackage('w2@1.0.0', getPackagePath(config, 'w2'));
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'a'));
        expect(result).toContainPackage('b@1.0.0', getPackagePath(config, 'b'));
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'c'));

        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'w2/a'));
      });
    });
    describe('workspace depends on another workspace', () => {
      function fixture(f: UpdateResolver): any {
        return createTestFixture(
          {
            'w1@1.0.0': ['a@1.0.0'],
            'w2@1.0.0': ['w1@1.0.0', 'b@1.0.0'],
            'a@1.0.0': ['c@1.0.0'],
            'b@1.0.0': ['c@1.0.0'],
            'c@1.0.0': [],
            'root@1.0.0': ['w1@1.0.0', 'w2@1.0.0'],
          },
          true,
          f,
        );
      }
      test('can nohoist dependent workspace just like any package', () => {
        const {packageHoister, packageResolver} = fixture(
          updateWorkspaces('root@1.0.0', undefined, ['w1@1.0.0', 'w2@1.0.0']),
        );
        const config = packageHoister.config;

        // w2 disable 'w1' and all of w1's direct dependency hoisting
        const pkg: any = packageResolver.getStrictResolvedPattern('w2@1.0.0');
        pkg.workspaces = {nohoist: ['w1', 'w1/**']};

        packageHoister.seed(['root@1.0.0']);
        const result = packageHoister.init();

        expect(result.length).toEqual(9);
        expect(result).toContainPackage('root@1.0.0', getPackagePath(config, 'root'));
        expect(result).toContainPackage('w1@1.0.0', getPackagePath(config, 'w1'));
        expect(result).toContainPackage('w2@1.0.0', getPackagePath(config, 'w2'));
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'a'));
        expect(result).toContainPackage('b@1.0.0', getPackagePath(config, 'b'));
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'c'));

        expect(result).toContainPackage('w1@1.0.0', getPackagePath(config, 'w2/w1'));
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'w2/a'));
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'w2/c'));
      });
      test('all workspaces nohoist will be honored', () => {
        const {packageHoister, packageResolver} = fixture(
          updateWorkspaces('root@1.0.0', undefined, ['w1@1.0.0', 'w2@1.0.0']),
        );
        const config = packageHoister.config;

        // w2 disable all of its top level packages hoisting
        let pkg: any = packageResolver.getStrictResolvedPattern('w2@1.0.0');
        pkg.workspaces = {nohoist: ['*']};

        // w1 disable 'a' and all of its dependency hoisting
        pkg = packageResolver.getStrictResolvedPattern('w1@1.0.0');
        pkg.workspaces = {nohoist: ['a', 'a/**']};

        packageHoister.seed(['root@1.0.0']);
        const result = packageHoister.init();

        expect(result.length).toEqual(10);
        // root
        expect(result).toContainPackage('root@1.0.0', getPackagePath(config, 'root'));
        expect(result).toContainPackage('w1@1.0.0', getPackagePath(config, 'w1'));
        expect(result).toContainPackage('w2@1.0.0', getPackagePath(config, 'w2'));
        // from w2/b/c
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'c'));

        // under w1
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'w1/a'));
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'w1/c'));

        // under w2
        expect(result).toContainPackage('w1@1.0.0', getPackagePath(config, 'w2/w1'));
        expect(result).toContainPackage('a@1.0.0', getPackagePath(config, 'w2/a'));
        expect(result).toContainPackage('b@1.0.0', getPackagePath(config, 'w2/b'));
        expect(result).toContainPackage('c@1.0.0', getPackagePath(config, 'w2/c'));
      });
    });
  });
  test('originalParentPath shows before-hoist dependency tree path', () => {
    const {packageHoister} = createTestFixture({
      'a@1.0.0': ['c@1.0.0'],
      'b@1.0.0': ['c@1.0.0'],
      'c@1.0.0': [],
    });

    // disable hoisting for everything under a
    packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
    const result = packageHoister.init();
    const paths = result.map(r => [r[1].key, r[1].originalParentPath]);

    expect(paths.length).toEqual(3);
    expect(paths).toContainEqual(['a', '']);
    expect(paths).toContainEqual(['b', '']);
    expect(paths).toContainEqual(['c', '/a']);
  });
  test('previousPaths should reflect hoist history', () => {
    const {packageHoister} = createTestFixture({
      'a@1.0.0': ['c@1.0.0'],
      'b@1.0.0': ['c@1.0.0'],
      'c@1.0.0': [],
    });

    // disable hoisting for everything under a
    packageHoister.seed(['a@1.0.0', 'b@1.0.0']);
    const result = packageHoister.init();
    const hoistHistory = result.map(r => [r[1].key, r[1].previousPaths]);

    expect(hoistHistory.length).toEqual(3);
    expect(hoistHistory).toContainEqual(['a', []]);
    expect(hoistHistory).toContainEqual(['b', []]);
    expect(hoistHistory).toContainEqual(['c', ['/a/c', '/b/c']]);
  });
});