1var fs = require('graceful-fs') 2var path = require('path') 3var existsSync = fs.existsSync || path.existsSync 4 5var mkdirp = require('mkdirp') 6var mr = require('npm-registry-mock') 7var rimraf = require('rimraf') 8var test = require('tap').test 9 10var common = require('../common-tap.js') 11var server 12 13var pkg = common.pkg 14 15var EXEC_OPTS = { cwd: pkg } 16 17var json = { 18 author: 'Dedupe tester', 19 name: 'dedupe', 20 version: '0.0.0', 21 dependencies: { 22 optimist: '0.6.0', 23 clean: '2.1.6' 24 } 25} 26 27var shrinkwrap = { 28 name: 'dedupe', 29 version: '0.0.0', 30 dependencies: { 31 clean: { 32 version: '2.1.6', 33 dependencies: { 34 checker: { 35 version: '0.5.2', 36 dependencies: { 37 async: { version: '0.2.10' } 38 } 39 }, 40 minimist: { version: '0.0.5' } 41 } 42 }, 43 optimist: { 44 version: '0.6.0', 45 dependencies: { 46 wordwrap: { version: '0.0.2' }, 47 minimist: { version: '0.0.5' } 48 } 49 } 50 } 51} 52 53test('setup', function (t) { 54 t.comment('test for https://github.com/npm/npm/issues/4675') 55 setup(function () { 56 t.end() 57 }) 58}) 59 60test('dedupe finds the common module and moves it up one level', function (t) { 61 common.npm([ 62 '--registry', common.registry, 63 '--no-save', 64 'install', '.' 65 ], 66 EXEC_OPTS, 67 function (err, code) { 68 t.ifError(err, 'successfully installed directory') 69 t.equal(code, 0, 'npm install exited with code') 70 common.npm( 71 [ 72 'dedupe' 73 ], 74 EXEC_OPTS, 75 function (err, code) { 76 t.ifError(err, 'successfully deduped against previous install') 77 t.notOk(code, 'npm dedupe exited with code') 78 79 t.ok(existsSync(path.join(pkg, 'node_modules', 'minimist')), 'minimist module exists') 80 t.notOk( 81 existsSync(path.join(pkg, 'node_modules', 'clean', 'node_modules', 'minimist')), 82 'no clean/minimist' 83 ) 84 t.notOk( 85 existsSync(path.join(pkg, 'node_modules', 'optimist', 'node_modules', 'minimist')), 86 'no optmist/minimist' 87 ) 88 t.end() 89 } 90 ) 91 }) 92}) 93 94test('cleanup', function (t) { 95 server.close() 96 cleanup() 97 98 t.end() 99}) 100 101function cleanup () { 102 rimraf.sync(pkg) 103} 104 105function setup (cb) { 106 cleanup() 107 mkdirp.sync(pkg) 108 fs.writeFileSync( 109 path.join(pkg, 'package.json'), 110 JSON.stringify(json, null, 2) 111 ) 112 fs.writeFileSync( 113 path.join(pkg, 'npm-shrinkwrap.json'), 114 JSON.stringify(shrinkwrap, null, 2) 115 ) 116 process.chdir(pkg) 117 118 mr({ port: common.port }, function (er, s) { 119 server = s 120 cb() 121 }) 122} 123