• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2const path = require('path')
3const test = require('tap').test
4const mr = require('npm-registry-mock')
5const Tacks = require('tacks')
6const File = Tacks.File
7const Dir = Tacks.Dir
8const common = require('../common-tap.js')
9
10const basedir = common.pkg
11const testdir = path.join(basedir, 'testdir')
12const cachedir = common.cache
13const globaldir = path.join(basedir, 'global')
14const tmpdir = path.join(basedir, 'tmp')
15
16const conf = {
17  cwd: testdir,
18  env: Object.assign({}, process.env, {
19    npm_config_cache: cachedir,
20    npm_config_tmp: tmpdir,
21    npm_config_prefix: globaldir,
22    npm_config_registry: common.registry,
23    npm_config_loglevel: 'warn'
24  })
25}
26
27let server
28const fixture = new Tacks(Dir({
29  cache: Dir(),
30  global: Dir(),
31  tmp: Dir(),
32  testdir: Dir({
33    node_modules: Dir({
34      minimist: Dir({
35        'package.json': File({
36          _integrity: 'sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=',
37          _resolved: 'https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz',
38          name: 'minimist',
39          version: '0.0.8'
40        })
41      }),
42      mkdirp: Dir({
43        'package.json': File({
44          _integrity: 'sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=',
45          _resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz',
46          dependencies: {
47            minimist: '0.0.8'
48          },
49          name: 'mkdirp',
50          version: '0.5.1'
51        })
52      }),
53      null: Dir({
54        'package.json': File({
55          _integrity: 'sha1-WoIdUnAxMlyG06AasQFzKgkfoew=',
56          _resolved: 'https://registry.npmjs.org/null/-/null-1.0.1.tgz',
57          _spec: 'null',
58          name: 'null',
59          version: '1.0.1'
60        })
61      })
62    }),
63    'package-lock.json': File({
64      name: 'with-lock',
65      version: '1.0.0',
66      lockfileVersion: 1,
67      requires: true,
68      dependencies: {
69        minimist: {
70          version: '0.0.8',
71          resolved: 'https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz',
72          integrity: 'sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0='
73        },
74        mkdirp: {
75          version: '0.5.1',
76          resolved: 'https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz',
77          integrity: 'sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=',
78          requires: {
79            minimist: '0.0.8'
80          }
81        }
82      }
83    }),
84    'package.json': File({
85      name: 'with-lock',
86      version: '1.0.0',
87      dependencies: {
88        mkdirp: '^0.5.1'
89      }
90    })
91  })
92}))
93
94function setup () {
95  cleanup()
96  fixture.create(basedir)
97}
98
99function cleanup () {
100  fixture.remove(basedir)
101}
102
103test('setup', function (t) {
104  setup()
105  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
106    if (err) throw err
107    server = s
108    t.done()
109  })
110})
111
112test('auto-prune w/ package-lock', function (t) {
113  common.npm(['install', '--dry-run', '--json'], conf, function (err, code, stdout, stderr) {
114    if (err) throw err
115    t.is(code, 0, 'command ran ok')
116    t.comment(stderr.trim())
117    const result = JSON.parse(stdout)
118    t.is(result.added.length, 0, 'nothing added')
119    t.is(result.updated.length, 0, 'nothing updated')
120    t.is(result.moved.length, 0, 'nothing moved')
121    t.is(result.failed.length, 0, 'nothing failed')
122    t.is(result.removed.length, 1, 'pruned 1')
123    t.like(result, {'removed': [{'name': 'null'}]}, 'pruned the right one')
124    t.done()
125  })
126})
127
128test('auto-prune w/ --no-package-lock', function (t) {
129  common.npm(['install', '--dry-run', '--json', '--no-package-lock'], conf, function (err, code, stdout, stderr) {
130    if (err) throw err
131    t.is(code, 0, 'command ran ok')
132    t.comment(stderr.trim())
133    const result = JSON.parse(stdout)
134    t.is(result.added.length, 0, 'nothing added')
135    t.is(result.updated.length, 0, 'nothing updated')
136    t.is(result.moved.length, 0, 'nothing moved')
137    t.is(result.failed.length, 0, 'nothing failed')
138    t.is(result.removed.length, 0, 'nothing pruned')
139    t.done()
140  })
141})
142
143test('cleanup', function (t) {
144  server.close()
145  cleanup()
146  t.done()
147})
148