• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1var fs = require('fs')
2var path = require('path')
3
4var mkdirp = require('mkdirp')
5var mr = require('npm-registry-mock')
6var test = require('tap').test
7
8var common = require('../common-tap.js')
9var npm = require('../../')
10
11var pkg = common.pkg
12
13test('shrinkwrap does not fail on missing optional dependency', function (t) {
14  t.plan(1)
15
16  var mocks = {
17    get: {
18      '/random-package': [404, {}]
19    }
20  }
21
22  mr({port: common.port, mocks: mocks}, function (er, s) {
23    t.parent.teardown(() => s.close())
24    setup(function (err) {
25      if (err) {
26        throw err
27      }
28
29      // Install without the optional dependency
30      npm.install('.', function (err) {
31        if (err) {
32          throw err
33        }
34
35        // Pretend the optional dependency was specified, but somehow failed to load:
36        json.optionalDependencies = {
37          'random-package': '0.0.0'
38        }
39        writePackage()
40
41        npm.commands.shrinkwrap([], true, function (err, results) {
42          if (err) {
43            throw err
44          }
45
46          t.deepEqual(results.dependencies, desired.dependencies)
47          s.close()
48          t.end()
49        })
50      })
51    })
52  })
53})
54
55var desired = {
56  name: 'npm-test-shrinkwrap-optional-dependency',
57  version: '0.0.0',
58  dependencies: {
59    'test-package': {
60      version: '0.0.0',
61      resolved: common.registry + '/test-package/-/test-package-0.0.0.tgz',
62      integrity: 'sha1-sNMrbEXCWcV4uiADdisgUTG9+9E='
63    }
64  }
65}
66
67var json = {
68  author: 'Maximilian Antoni',
69  name: 'npm-test-shrinkwrap-optional-dependency',
70  version: '0.0.0',
71  dependencies: {
72    'test-package': '0.0.0'
73  }
74}
75
76function writePackage () {
77  fs.writeFileSync(path.join(pkg, 'package.json'), JSON.stringify(json, null, 2))
78}
79
80function setup (cb) {
81  mkdirp.sync(pkg)
82  writePackage()
83  process.chdir(pkg)
84
85  var opts = {
86    cache: common.cache,
87    registry: common.registry,
88    cwd: pkg
89  }
90  npm.load(opts, cb)
91}
92