• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1const t = require('tap')
2const installedDeep = require('../../../../lib/utils/completion/installed-deep.js')
3const mockNpm = require('../../../fixtures/mock-npm')
4
5const fixture = {
6  'package.json': JSON.stringify({
7    name: 'test-installed-deep',
8    version: '1.0.0',
9    dependencies: {
10      a: '^1.0.0',
11      b: '^1.0.0',
12      c: '^1.0.0',
13    },
14    devDependencies: {
15      d: '^1.0.0',
16    },
17    peerDependencies: {
18      e: '^1.0.0',
19    },
20  }),
21  node_modules: {
22    a: {
23      'package.json': JSON.stringify({
24        name: 'a',
25        version: '1.0.0',
26        dependencies: {
27          f: '^1.0.0',
28        },
29      }),
30    },
31    b: {
32      'package.json': JSON.stringify({
33        name: 'b',
34        version: '1.0.0',
35      }),
36    },
37    c: {
38      'package.json': JSON.stringify({
39        name: 'c',
40        version: '1.0.0',
41        dependencies: {
42          ch: '1.0.0',
43        },
44      }),
45    },
46    ch: {
47      'package.json': JSON.stringify({
48        name: 'ch',
49        version: '1.0.0',
50      }),
51    },
52    d: {
53      'package.json': JSON.stringify({
54        name: 'd',
55        version: '1.0.0',
56      }),
57    },
58    e: {
59      'package.json': JSON.stringify({
60        name: 'e',
61        version: '1.0.0',
62      }),
63    },
64    f: {
65      'package.json': JSON.stringify({
66        name: 'f',
67        version: '1.0.0',
68        dependencies: {
69          g: '^1.0.0',
70          e: '^2.0.0',
71        },
72      }),
73      node_modules: {
74        e: {
75          'package.json': JSON.stringify({
76            name: 'e',
77            version: '2.0.0',
78            dependencies: {
79              bb: '^1.0.0',
80            },
81          }),
82          node_modules: {
83            bb: {
84              'package.json': JSON.stringify({
85                name: 'bb',
86                version: '1.0.0',
87              }),
88            },
89          },
90        },
91      },
92    },
93    g: {
94      'package.json': JSON.stringify({
95        name: 'g',
96        version: '1.0.0',
97      }),
98    },
99  },
100}
101
102const globalFixture = {
103  node_modules: {
104    foo: {
105      'package.json': JSON.stringify({
106        name: 'foo',
107        version: '1.0.0',
108      }),
109    },
110    bar: {
111      'package.json': JSON.stringify({
112        name: 'bar',
113        version: '1.0.0',
114        dependencies: {
115          'a-bar': '^1.0.0',
116        },
117      }),
118      node_modules: {
119        'a-bar': {
120          'package.json': JSON.stringify({
121            name: 'a-bar',
122            version: '1.0.0',
123          }),
124        },
125      },
126    },
127  },
128}
129
130const mockDeep = async (t, config) => {
131  const mock = await mockNpm(t, {
132    prefixDir: fixture,
133    globalPrefixDir: globalFixture,
134    config: {
135      depth: Infinity,
136      ...config,
137    },
138  })
139
140  const res = await installedDeep(mock.npm)
141
142  return res
143}
144
145t.test('get list of package names', async t => {
146  const res = await mockDeep(t)
147  t.same(
148    res,
149    [
150      ['bar', '-g'],
151      ['foo', '-g'],
152      ['a-bar', '-g'],
153      'a', 'b', 'c',
154      'ch', 'd', 'e',
155      'f', 'g', 'bb',
156    ],
157    'should return list of package names and global flag'
158  )
159  t.end()
160})
161
162t.test('get list of package names as global', async t => {
163  const res = await mockDeep(t, { global: true })
164  t.same(
165    res,
166    [
167      'bar',
168      'foo',
169      'a-bar',
170    ],
171    'should return list of global packages with no extra flags'
172  )
173})
174
175t.test('limit depth', async t => {
176  const res = await mockDeep(t, { depth: 0 })
177  t.same(
178    res,
179    [
180      ['bar', '-g'],
181      ['foo', '-g'],
182      // XXX https://github.com/npm/statusboard/issues/380
183      ['a-bar', '-g'],
184      'a', 'b',
185      'c', 'ch',
186      'd', 'e',
187      'f', 'g',
188    ],
189    'should print only packages up to the specified depth'
190  )
191})
192
193t.test('limit depth as global', async t => {
194  const res = await mockDeep(t, { depth: 0, global: true })
195  t.same(
196    res,
197    [
198      'bar',
199      'foo',
200      // https://github.com/npm/statusboard/issues/380
201      'a-bar',
202    ],
203    'should reorder so that packages above that level depth goes last'
204  )
205})
206