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