From b29be195f85d923c931fcb4c1a5a396f10c407e2 Mon Sep 17 00:00:00 2001 From: Danielle Adams Date: Wed, 1 Apr 2020 10:12:29 -0400 Subject: [PATCH] add tests to raise message for regenerate of lockfile or shrinkwrap when a dependency has an empty object value --- lib/install/inflate-shrinkwrap.js | 14 +++--- test/tap/lockfile-empty-dep-value.js | 67 ++++++++++++++++++++++++++ test/tap/shrinkwrap-empty-dep-value.js | 65 +++++++++++++++++++++++++ 3 files changed, 139 insertions(+), 7 deletions(-) create mode 100644 test/tap/lockfile-empty-dep-value.js create mode 100644 test/tap/shrinkwrap-empty-dep-value.js diff --git a/lib/install/inflate-shrinkwrap.js b/lib/install/inflate-shrinkwrap.js index 1665cd09cf0d7..1cc85bd8b23a5 100644 --- a/lib/install/inflate-shrinkwrap.js +++ b/lib/install/inflate-shrinkwrap.js @@ -53,13 +53,13 @@ function inflateShrinkwrap (topPath, tree, swdeps, opts) { const sw = swdeps[name] const dependencies = sw.dependencies || {} const requested = realizeShrinkwrapSpecifier(name, sw, topPath) - - // if (Object.keys(sw).length === 0) { - // let message = `Object for dependency "${name}" is empty.\n` - // message += 'Something went wrong. Regenerate the package-lock.json with "npm install".\n' - // message += 'If using a shrinkwrap, regenerate with "npm shrinkwrap".' - // return errorHandler(message) - // } + + if (Object.keys(sw).length === 0) { + let message = `Object for dependency "${name}" is empty.\n` + message += 'Something went wrong. Regenerate the package-lock.json with "npm install".\n' + message += 'If using a shrinkwrap, regenerate with "npm shrinkwrap".' + return errorHandler(message) + } return inflatableChild( onDisk[name], name, topPath, tree, sw, requested, opts diff --git a/test/tap/lockfile-empty-dep-value.js b/test/tap/lockfile-empty-dep-value.js new file mode 100644 index 0000000000000..642170639083c --- /dev/null +++ b/test/tap/lockfile-empty-dep-value.js @@ -0,0 +1,67 @@ +'use strict' + +const common = require('../common-tap.js') +const path = require('path') +const test = require('tap').test + +const Tacks = require('tacks') +const File = Tacks.File +const Dir = Tacks.Dir + +const basedir = common.pkg +const testdir = path.join(basedir, 'testdir') + +const fixture = new Tacks(Dir({ + cache: Dir(), + global: Dir(), + tmp: Dir(), + testdir: Dir({ + 'package-lock.json': File({ + name: 'http-locks', + version: '1.0.0', + lockfileVersion: 1, + requires: true, + dependencies: { + minimist: {} + } + }), + 'package.json': File({ + name: 'http-locks', + version: '1.0.0', + dependencies: { + minimist: common.registry + '/minimist/-/minimist-0.0.5.tgz' + } + }) + }) +})) + +function setup () { + cleanup() + fixture.create(basedir) +} + +function cleanup () { + fixture.remove(basedir) +} + +test('setup', function (t) { + setup() + t.done() +}) + +test('raises error to regenerate the lock file', function (t) { + common.npm(['install'], {cwd: testdir}, function (err, code, stdout, stderr) { + t.match( + stderr, + 'npm ERR! Something went wrong. Regenerate the package-lock.json with "npm install".', + 'returns message to regenerate package-lock' + ) + + t.done() + }) +}) + +test('cleanup', function (t) { + cleanup() + t.done() +}) diff --git a/test/tap/shrinkwrap-empty-dep-value.js b/test/tap/shrinkwrap-empty-dep-value.js new file mode 100644 index 0000000000000..9670936fea810 --- /dev/null +++ b/test/tap/shrinkwrap-empty-dep-value.js @@ -0,0 +1,65 @@ +'use strict' + +const common = require('../common-tap.js') +const path = require('path') +const test = require('tap').test + +const Tacks = require('tacks') +const File = Tacks.File +const Dir = Tacks.Dir + +const basedir = common.pkg +const testdir = path.join(basedir, 'testdir') + +const fixture = new Tacks(Dir({ + cache: Dir(), + global: Dir(), + tmp: Dir(), + testdir: Dir({ + 'npm-shrinkwrap.json': File({ + name: 'http-locks', + version: '0.0.0', + dependencies: { + minimist: {} + } + }), + 'package.json': File({ + name: 'http-locks', + version: '1.0.0', + dependencies: { + minimist: common.registry + '/minimist/-/minimist-0.0.5.tgz' + } + }) + }) +})) + +function setup () { + cleanup() + fixture.create(basedir) +} + +function cleanup () { + fixture.remove(basedir) +} + +test('setup', function (t) { + setup() + t.done() +}) + +test('raises error to regenerate the shrinkwrap', function (t) { + common.npm(['install'], {cwd: testdir}, function (err, code, stdout, stderr) { + t.match( + stderr, + 'npm ERR! If using a shrinkwrap, regenerate with "npm shrinkwrap".', + 'returns message to regenerate shrinkwrap' + ) + + t.done() + }) +}) + +test('cleanup', function (t) { + cleanup() + t.done() +})