1var fs = require('fs') 2 3if (process.argv[2] === 'write-builtin') { 4 var pid = process.argv[3] 5 fs.writeFileSync('npmrc', 'foo=bar\npid=' + pid + '\n') 6 process.exit(0) 7} 8 9var common = require('../common-tap.js') 10var path = require('path') 11var rimraf = require('rimraf') 12var mkdirp = require('mkdirp') 13var folder = common.pkg 14var test = require('tap').test 15var npm = path.resolve(__dirname, '../..') 16var spawn = require('child_process').spawn 17var node = common.nodeBin 18 19test('setup', function (t) { 20 t.plan(1) 21 rimraf.sync(folder) 22 mkdirp.sync(folder + '/first') 23 mkdirp.sync(folder + '/second') 24 mkdirp.sync(folder + '/cache') 25 mkdirp.sync(folder + '/tmp') 26 27 t.pass('finished setup') 28 t.end() 29}) 30 31test('install npm into first folder', function (t) { 32 t.plan(1) 33 var args = ['install', npm, '-g', 34 '--prefix=' + folder + '/first', 35 '--ignore-scripts', 36 '--cache=' + folder + '/cache', 37 '--tmp=' + folder + '/tmp', 38 '--loglevel=warn', 39 '--progress'] 40 common.npm(args, {}, function (er, code) { 41 if (er) throw er 42 t.equal(code, 0) 43 t.end() 44 }) 45}) 46 47test('write npmrc file', function (t) { 48 t.plan(1) 49 common.npm(['explore', 'npm', '-g', 50 '--prefix=' + folder + '/first', 51 '--cache=' + folder + '/cache', 52 '--tmp=' + folder + '/tmp', 53 '--', node, __filename, 'write-builtin', process.pid 54 ], 55 {'stdio': 'inherit'}, 56 function (er, code) { 57 if (er) throw er 58 t.equal(code, 0) 59 t.end() 60 }) 61}) 62 63test('use first npm to install second npm', function (t) { 64 t.plan(3) 65 // get the root location 66 common.npm( 67 [ 68 'root', '-g', 69 '--prefix=' + folder + '/first', 70 '--cache=' + folder + '/cache', 71 '--tmp=' + folder + '/tmp' 72 ], 73 {}, 74 function (er, code, so) { 75 if (er) throw er 76 t.equal(code, 0, 'got npm root') 77 var root = so.trim() 78 t.ok(fs.statSync(root).isDirectory(), 'npm root is dir') 79 80 var bin = path.resolve(root, 'npm/bin/npm-cli.js') 81 spawn( 82 node, 83 [ 84 bin, 85 'install', npm, 86 '-g', 87 '--ignore-scripts', 88 '--prefix=' + folder + '/second', 89 '--cache=' + folder + '/cache', 90 '--tmp=' + folder + '/tmp' 91 ], 92 {} 93 ) 94 .on('error', function (er) { throw er }) 95 .on('close', function (code) { 96 t.equal(code, 0, 'second npm install') 97 t.end() 98 }) 99 } 100 ) 101}) 102 103test('verify that the builtin config matches', function (t) { 104 t.plan(3) 105 common.npm([ 'root', '-g', 106 '--prefix=' + folder + '/first', 107 '--cache=' + folder + '/cache', 108 '--tmp=' + folder + '/tmp' 109 ], {}, 110 function (er, code, so) { 111 if (er) throw er 112 t.equal(code, 0) 113 var firstRoot = so.trim() 114 common.npm([ 'root', '-g', 115 '--prefix=' + folder + '/second', 116 '--cache=' + folder + '/cache', 117 '--tmp=' + folder + '/tmp' 118 ], {}, 119 function (er, code, so) { 120 if (er) throw er 121 t.equal(code, 0) 122 var secondRoot = so.trim() 123 var firstRc = path.resolve(firstRoot, 'npm', 'npmrc') 124 var secondRc = path.resolve(secondRoot, 'npm', 'npmrc') 125 var firstData = fs.readFileSync(firstRc, 'utf8').split(/\r?\n/) 126 var secondData = fs.readFileSync(secondRc, 'utf8').split(/\r?\n/) 127 t.isDeeply(firstData, secondData) 128 t.end() 129 }) 130 }) 131}) 132 133test('clean', function (t) { 134 rimraf.sync(folder) 135 t.end() 136}) 137