• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const t = require('tap')
2const path = require('path')
3const fs = require('fs')
4
5const { load: loadMockNpm } = require('../../fixtures/mock-npm')
6const MockRegistry = require('@npmcli/mock-registry')
7
8const treeWithDupes = {
9  'package.json': JSON.stringify({
10    name: 'test-top',
11    version: '1.0.0',
12    dependencies: {
13      'test-dep-a': '*',
14      'test-dep-b': '*',
15    },
16  }),
17  node_modules: {
18    'test-dep-a': {
19      'package.json': JSON.stringify({
20        name: 'test-dep-a',
21        version: '1.0.1',
22        dependencies: { 'test-sub': '*' },
23      }),
24      node_modules: {
25        'test-sub': {
26          'package.json': JSON.stringify({
27            name: 'test-sub',
28            version: '1.0.0',
29          }),
30        },
31      },
32    },
33    'test-dep-b': {
34      'package.json': JSON.stringify({
35        name: 'test-dep-b',
36        version: '1.0.0',
37        dependencies: { 'test-sub': '*' },
38      }),
39      node_modules: {
40        'test-sub': {
41          'package.json': JSON.stringify({
42            name: 'test-sub',
43            version: '1.0.0',
44          }),
45        },
46      },
47    },
48  },
49}
50
51t.test('should run dedupe in dryRun mode', async (t) => {
52  const { npm, joinedOutput } = await loadMockNpm(t, {
53    prefixDir: treeWithDupes,
54    config: {
55      // explicitly set to false so we can be 100% sure it's always true when it
56      // hits arborist
57      'dry-run': false,
58    },
59  })
60  const registry = new MockRegistry({
61    tap: t,
62    registry: npm.config.get('registry'),
63  })
64
65  const manifestSub = registry.manifest({
66    name: 'test-sub',
67    manifests: [{ name: 'test-sub', version: '1.0.0' }],
68  })
69
70  await registry.package({ manifest: manifestSub })
71  await npm.exec('find-dupes', [])
72  t.match(joinedOutput(), /added 1 package, and removed 2 packages/)
73  t.notOk(
74    fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-sub')),
75    'test-sub was not hoisted'
76  )
77  t.ok(
78    fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-a', 'node_modules', 'test-sub')),
79    'test-dep-a/test-sub was not removed'
80  )
81  t.ok(
82    fs.existsSync(path.join(npm.prefix, 'node_modules', 'test-dep-b', 'node_modules', 'test-sub')),
83    'test-dep-b/test-sub was not removed')
84})
85