From 2f7482386ea965287ba593dcc664c5e982871fd4 Mon Sep 17 00:00:00 2001 From: Audrey Eschright Date: Mon, 18 Feb 2019 14:23:05 -0800 Subject: [PATCH] install: always save package_lock.json when using `--package-lock-only` (#146) PR-URL: https://github.com/npm/cli/pull/146 Credit: @aeschright Reviewed-By: @zkat --- lib/install/save.js | 2 +- test/tap/install-package-lock-only.js | 40 ++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/lib/install/save.js b/lib/install/save.js index 7227e78852ac7..92b44a108099b 100644 --- a/lib/install/save.js +++ b/lib/install/save.js @@ -44,7 +44,7 @@ exports.saveShrinkwrap = saveShrinkwrap function saveShrinkwrap (tree, next) { validate('OF', arguments) - if (!npm.config.get('shrinkwrap') || !npm.config.get('package-lock')) { + if (!npm.config.get('package-lock-only') && (!npm.config.get('shrinkwrap') || !npm.config.get('package-lock'))) { return next() } require('../shrinkwrap.js').createShrinkwrap(tree, {silent: false}, next) diff --git a/test/tap/install-package-lock-only.js b/test/tap/install-package-lock-only.js index 9d8aa8dbec313..1610988851c12 100644 --- a/test/tap/install-package-lock-only.js +++ b/test/tap/install-package-lock-only.js @@ -28,6 +28,18 @@ var conf = { }) } +const confPkgLockFalse = { + cwd: testdir, + env: Object.assign({}, process.env, { + npm_config_cache: cachedir, + npm_config_tmp: tmpdir, + npm_config_prefix: globaldir, + npm_config_registry: common.registry, + npm_config_loglevel: 'warn', + npm_config_package_lock: false + }) +} + var server var fixture = new Tacks(Dir({ cache: Dir(), @@ -54,7 +66,6 @@ function cleanup () { } test('setup', function (t) { - setup() mr({port: common.port, throwOnUnmatched: true}, function (err, s) { if (err) throw err server = s @@ -63,6 +74,7 @@ test('setup', function (t) { }) test('package-lock-only', function (t) { + setup() return common.npm(['install', '--package-lock-only'], conf).spread((code, stdout, stderr) => { t.is(code, 0, 'command ran ok') t.comment(stdout.trim()) @@ -78,6 +90,32 @@ test('package-lock-only', function (t) { }) }) +test('--package-lock-only with --package-lock negates `package_lock: false`', function (t) { + setup() + return common.npm(['install', '--package-lock', '--package-lock-only'], confPkgLockFalse).spread((code, stdout, stderr) => { + t.is(code, 0, 'ok') + t.comment(stdout.trim()) + t.comment(stderr.trim()) + + // Verify that package-lock.json exists. + t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created') + t.end() + }) +}) + +test('package-lock-only creates package_lock.json when config has `package_lock: false`', function (t) { + setup() + return common.npm(['install', '--package-lock-only'], confPkgLockFalse).spread((code, stdout, stderr) => { + t.is(code, 0, 'ok') + t.comment(stdout.trim()) + t.comment(stderr.trim()) + + // Verify that package-lock.json exists. + t.ok(fs.existsSync(pkgLockPath), 'ensure that package-lock.json was created') + t.end() + }) +}) + test('cleanup', function (t) { server.close() cleanup()