1var fs = require('fs') 2var path = require('path') 3 4var mkdirp = require('mkdirp') 5var mr = require('npm-registry-mock') 6var rimraf = require('rimraf') 7var test = require('tap').test 8 9var common = require('../common-tap.js') 10 11var pkg = common.pkg 12 13var EXEC_OPTS = { cwd: pkg, stdio: [0, 'ignore', 2] } 14 15var json = { 16 name: 'uninstall-save', 17 version: '0.0.1' 18} 19 20test('setup', function (t) { 21 mkdirp.sync(path.resolve(pkg, 'node_modules')) 22 fs.writeFileSync( 23 path.join(pkg, 'package.json'), 24 JSON.stringify(json, null, 2) 25 ) 26 mr({ port: common.port }, function (er, s) { 27 t.ifError(er, 'started mock registry') 28 t.parent.teardown(() => s.close()) 29 t.end() 30 }) 31}) 32 33test('uninstall --save removes rm-ed package from package.json', function (t) { 34 var config = [ 35 '--registry', common.registry, 36 '--save-prefix', '^', 37 '--save', 38 '--loglevel=error' 39 ] 40 return common.npm(config.concat(['install', 'underscore@latest']), EXEC_OPTS).spread((code) => { 41 t.notOk(code, 'npm install exited with code 0') 42 43 var p = path.join(pkg, 'node_modules', 'underscore', 'package.json') 44 t.ok(JSON.parse(fs.readFileSync(p))) 45 46 var pkgJson = JSON.parse(fs.readFileSync( 47 path.join(pkg, 'package.json'), 48 'utf8' 49 )) 50 t.deepEqual( 51 pkgJson.dependencies, 52 { 'underscore': '^1.5.1' }, 53 'got expected save prefix and version of 1.5.1' 54 ) 55 56 var installed = path.join(pkg, 'node_modules', 'underscore') 57 rimraf.sync(installed) 58 59 return common.npm(config.concat(['uninstall', 'underscore']), EXEC_OPTS) 60 }).spread((code) => { 61 var pkgJson = JSON.parse(fs.readFileSync( 62 path.join(pkg, 'package.json'), 63 'utf8' 64 )) 65 66 t.deepEqual( 67 pkgJson.dependencies, 68 { }, 69 'dependency removed as expected' 70 ) 71 }) 72}) 73