• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var fs = require('fs')
3var path = require('path')
4var test = require('tap').test
5var mr = require('npm-registry-mock')
6var Tacks = require('tacks')
7var File = Tacks.File
8var Dir = Tacks.Dir
9var common = require('../common-tap.js')
10
11var basedir = common.pkg
12var testdir = path.join(basedir, 'testdir')
13var cachedir = common.cache
14var globaldir = path.join(basedir, 'global')
15var tmpdir = path.join(basedir, 'tmp')
16
17var pkgLockPath = path.join(testdir, 'package-lock.json')
18var nodeModulesPath = path.join(testdir, 'node_modules')
19
20var conf = {
21  cwd: testdir,
22  env: Object.assign({}, process.env, {
23    npm_config_cache: cachedir,
24    npm_config_tmp: tmpdir,
25    npm_config_prefix: globaldir,
26    npm_config_registry: common.registry,
27    npm_config_loglevel: 'warn'
28  })
29}
30
31const confPkgLockFalse = {
32  cwd: testdir,
33  env: Object.assign({}, process.env, {
34    npm_config_cache: cachedir,
35    npm_config_tmp: tmpdir,
36    npm_config_prefix: globaldir,
37    npm_config_registry: common.registry,
38    npm_config_loglevel: 'warn',
39    npm_config_package_lock: false
40  })
41}
42
43var server
44var fixture = new Tacks(Dir({
45  cache: Dir(),
46  global: Dir(),
47  tmp: Dir(),
48  testdir: Dir({
49    'package.json': File({
50      name: 'install-package-lock-only',
51      version: '1.0.0',
52      dependencies: {
53        mkdirp: '^0.3.4'
54      }
55    })
56  })
57}))
58
59function setup () {
60  cleanup()
61  fixture.create(basedir)
62}
63
64function cleanup () {
65  fixture.remove(basedir)
66}
67
68test('setup', function (t) {
69  mr({port: common.port, throwOnUnmatched: true}, function (err, s) {
70    if (err) throw err
71    server = s
72    t.done()
73  })
74})
75
76test('package-lock-only', function (t) {
77  setup()
78  return common.npm(['install', '--package-lock-only'], conf).spread((code, stdout, stderr) => {
79    t.is(code, 0, 'command ran ok')
80    t.comment(stdout.trim())
81    t.comment(stderr.trim())
82    // Verify that node_modules does not exist
83    t.notOk(fs.existsSync(nodeModulesPath), 'ensure that node_modules does not exist')
84
85    // Verify that package-lock.json exists and has `mkdirp@0.3.5` in it.
86    t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
87    var pkgLock = JSON.parse(fs.readFileSync(pkgLockPath, 'utf-8'))
88    t.equal(pkgLock.dependencies.mkdirp.version, '0.3.5')
89    t.done()
90  })
91})
92
93test('--package-lock-only with --package-lock negates `package_lock: false`', function (t) {
94  setup()
95  return common.npm(['install', '--package-lock', '--package-lock-only'], confPkgLockFalse).spread((code, stdout, stderr) => {
96    t.is(code, 0, 'ok')
97    t.comment(stdout.trim())
98    t.comment(stderr.trim())
99
100    // Verify that package-lock.json exists.
101    t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
102    t.end()
103  })
104})
105
106test('package-lock-only creates package_lock.json when config has `package_lock: false`', function (t) {
107  setup()
108  return common.npm(['install', '--package-lock-only'], confPkgLockFalse).spread((code, stdout, stderr) => {
109    t.is(code, 0, 'ok')
110    t.comment(stdout.trim())
111    t.comment(stderr.trim())
112
113    // Verify that package-lock.json exists.
114    t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created')
115    t.end()
116  })
117})
118
119test('cleanup', function (t) {
120  server.close()
121  cleanup()
122  t.done()
123})
124