Welcome to mirror list, hosted at ThFree Co, Russian Federation.

install.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2cbee02e67b28784dc35bf2722d2a9c09b122a2f (plain)
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
const t = require('tap')

const Install = require('../../lib/install.js')
const { fake: mockNpm } = require('../fixtures/mock-npm')

t.test('should install using Arborist', (t) => {
  const SCRIPTS = []
  let ARB_ARGS = null
  let REIFY_CALLED = false
  let ARB_OBJ = null

  const Install = t.mock('../../lib/install.js', {
    '@npmcli/run-script': ({ event }) => {
      SCRIPTS.push(event)
    },
    npmlog: {
      warn: () => {},
    },
    '@npmcli/arborist': function (args) {
      ARB_ARGS = args
      ARB_OBJ = this
      this.reify = () => {
        REIFY_CALLED = true
      }
    },
    '../../lib/utils/reify-finish.js': (npm, arb) => {
      if (arb !== ARB_OBJ)
        throw new Error('got wrong object passed to reify-finish')
    },
  })

  const npm = mockNpm({
    config: { dev: true },
    flatOptions: { global: false, auditLevel: 'low' },
    globalDir: 'path/to/node_modules/',
    prefix: 'foo',
  })
  const install = new Install(npm)

  t.test('with args', t => {
    install.exec(['fizzbuzz'], er => {
      if (er)
        throw er
      t.match(ARB_ARGS,
        { global: false, path: 'foo', auditLevel: null },
        'Arborist gets correct args and ignores auditLevel')
      t.equal(REIFY_CALLED, true, 'called reify')
      t.strictSame(SCRIPTS, [], 'no scripts when adding dep')
      t.end()
    })
  })

  t.test('just a local npm install', t => {
    install.exec([], er => {
      if (er)
        throw er
      t.match(ARB_ARGS, { global: false, path: 'foo' })
      t.equal(REIFY_CALLED, true, 'called reify')
      t.strictSame(SCRIPTS, [
        'preinstall',
        'install',
        'postinstall',
        'prepublish',
        'preprepare',
        'prepare',
        'postprepare',
      ], 'exec scripts when doing local build')
      t.end()
    })
  })

  t.end()
})

t.test('should ignore scripts with --ignore-scripts', (t) => {
  const SCRIPTS = []
  let REIFY_CALLED = false
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    '@npmcli/run-script': ({ event }) => {
      SCRIPTS.push(event)
    },
    '@npmcli/arborist': function () {
      this.reify = () => {
        REIFY_CALLED = true
      }
    },
  })
  const npm = mockNpm({
    globalDir: 'path/to/node_modules/',
    prefix: 'foo',
    flatOptions: { global: false },
    config: {
      global: false,
      'ignore-scripts': true,
    },
  })
  const install = new Install(npm)
  install.exec([], er => {
    if (er)
      throw er
    t.equal(REIFY_CALLED, true, 'called reify')
    t.strictSame(SCRIPTS, [], 'no scripts when adding dep')
    t.end()
  })
})

t.test('should install globally using Arborist', (t) => {
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    '@npmcli/arborist': function () {
      this.reify = () => {}
    },
  })
  const npm = mockNpm({
    globalDir: 'path/to/node_modules/',
    prefix: 'foo',
    config: { global: true },
    flatOptions: { global: true },
  })
  const install = new Install(npm)
  install.exec([], er => {
    if (er)
      throw er
    t.end()
  })
})

t.test('npm i -g npm engines check success', (t) => {
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    '@npmcli/arborist': function () {
      this.reify = () => {}
    },
    pacote: {
      manifest: () => {
        return {
          version: '100.100.100',
          engines: {
            node: '>1',
          },
        }
      },
    },
  })
  const npm = mockNpm({
    globalDir: 'path/to/node_modules/',
    config: {
      global: true,
    },
  })
  const install = new Install(npm)
  install.exec(['npm'], er => {
    if (er)
      throw er
    t.end()
  })
})

t.test('npm i -g npm engines check failure', (t) => {
  const Install = t.mock('../../lib/install.js', {
    pacote: {
      manifest: () => {
        return {
          _id: 'npm@1.2.3',
          version: '100.100.100',
          engines: {
            node: '>1000',
          },
        }
      },
    },
  })
  const npm = mockNpm({
    globalDir: 'path/to/node_modules/',
    config: {
      global: true,
    },
  })
  const install = new Install(npm)
  install.exec(['npm'], er => {
    t.match(er, {
      message: 'Unsupported engine',
      pkgid: 'npm@1.2.3',
      current: {
        node: process.version,
        npm: '100.100.100',
      },
      required: {
        node: '>1000',
      },
      code: 'EBADENGINE',
    })
    t.end()
  })
})

t.test('npm i -g npm engines check failure forced override', (t) => {
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    '@npmcli/arborist': function () {
      this.reify = () => {}
    },
    pacote: {
      manifest: () => {
        return {
          _id: 'npm@1.2.3',
          version: '100.100.100',
          engines: {
            node: '>1000',
          },
        }
      },
    },
  })
  const npm = mockNpm({
    globalDir: 'path/to/node_modules/',
    config: {
      force: true,
      global: true,
    },
  })
  const install = new Install(npm)
  install.exec(['npm'], er => {
    if (er)
      throw er
    t.end()
  })
})

t.test('npm i -g npm@version engines check failure', (t) => {
  const Install = t.mock('../../lib/install.js', {
    pacote: {
      manifest: () => {
        return {
          _id: 'npm@1.2.3',
          version: '100.100.100',
          engines: {
            node: '>1000',
          },
        }
      },
    },
  })
  const npm = mockNpm({
    globalDir: 'path/to/node_modules/',
    config: {
      global: true,
    },
  })
  const install = new Install(npm)
  install.exec(['npm@100'], er => {
    t.match(er, {
      message: 'Unsupported engine',
      pkgid: 'npm@1.2.3',
      current: {
        node: process.version,
        npm: '100.100.100',
      },
      required: {
        node: '>1000',
      },
      code: 'EBADENGINE',
    })
    t.end()
  })
})

t.test('completion to folder', async t => {
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    util: {
      promisify: (fn) => fn,
    },
    fs: {
      readdir: (path) => {
        if (path === '/')
          return ['arborist']
        else
          return ['package.json']
      },
    },
  })
  const install = new Install({})
  const res = await install.completion({ partialWord: '/ar' })
  const expect = process.platform === 'win32' ? '\\arborist' : '/arborist'
  t.strictSame(res, [expect], 'package dir match')
  t.end()
})

t.test('completion to folder - invalid dir', async t => {
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    util: {
      promisify: (fn) => fn,
    },
    fs: {
      readdir: () => {
        throw new Error('EONT')
      },
    },
  })
  const install = new Install({})
  const res = await install.completion({ partialWord: 'path/to/folder' })
  t.strictSame(res, [], 'invalid dir: no matching')
  t.end()
})

t.test('completion to folder - no matches', async t => {
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    util: {
      promisify: (fn) => fn,
    },
    fs: {
      readdir: (path) => {
        return ['foobar']
      },
    },
  })
  const install = new Install({})
  const res = await install.completion({ partialWord: '/pa' })
  t.strictSame(res, [], 'no name match')
  t.end()
})

t.test('completion to folder - match is not a package', async t => {
  const Install = t.mock('../../lib/install.js', {
    '../../lib/utils/reify-finish.js': async () => {},
    util: {
      promisify: (fn) => fn,
    },
    fs: {
      readdir: (path) => {
        if (path === '/')
          return ['arborist']
        else
          throw new Error('EONT')
      },
    },
  })
  const install = new Install({})
  const res = await install.completion({ partialWord: '/ar' })
  t.strictSame(res, [], 'no name match')
  t.end()
})

t.test('completion to url', async t => {
  const install = new Install({})
  const res = await install.completion({ partialWord: 'http://path/to/url' })
  t.strictSame(res, [])
  t.end()
})

t.test('completion', async t => {
  const install = new Install({})
  const res = await install.completion({ partialWord: 'toto' })
  t.notOk(res)
  t.end()
})