2 var test = require('tap').test
3 var fs = require('graceful-fs')
4 var common = require('../common-tap.js')
5 var path = require('path')
6 var rimraf = require('rimraf')
7 var mkdirp = require('mkdirp')
8 var osenv = require('osenv')
9 var npmpath = path.resolve(__dirname, '../..')
10 var basepath = path.resolve(osenv.tmpdir(), path.basename(__filename, '.js'))
11 var globalpath = path.resolve(basepath, 'global')
12 var extend = Object.assign || require('util')._extend
13 var isWin32 = process.platform === 'win32'
15 test('setup', function (t) {
22 test('build-tarball', function (t) {
23 common.npm(['pack'], {cwd: npmpath, stdio: ['ignore', 'pipe', process.stderr]}, function (err, code, stdout) {
25 t.is(code, 0, 'pack went ok')
26 tarball = path.resolve(npmpath, stdout.trim().replace(/^(?:.|\n)*(?:^|\n)(.*?[.]tgz)$/, '$1'))
27 t.match(tarball, /[.]tgz$/, 'got a tarball')
34 fs.statSync(path.resolve.apply(null, arguments))
41 test('npm-self-install', function (t) {
42 if (!tarball) return t.done()
44 var env = extend({}, process.env)
45 var pathsep = isWin32 ? ';' : ':'
46 env.npm_config_prefix = globalpath
47 env.npm_config_global = 'true'
48 env.npm_config_npat = 'false'
50 env.npm_config_user_agent = null
51 env.npm_config_color = 'always'
52 env.npm_config_progress = 'always'
53 var PATH = env.PATH.split(pathsep)
54 var binpath = isWin32 ? globalpath : path.join(globalpath, 'bin')
55 var cmdname = isWin32 ? 'npm.cmd' : 'npm'
57 env.PATH = PATH.join(pathsep)
59 var opts = {cwd: basepath, env: env, stdio: ['ignore', 'ignore', process.stderr]}
61 common.npm(['install', '--ignore-scripts', tarball], opts, installCheckAndTest)
62 function installCheckAndTest (err, code) {
64 t.is(code, 0, 'install went ok')
65 t.is(exists(binpath, cmdname), true, 'binary was installed')
66 t.is(exists(globalpath, 'lib', 'node_modules', 'npm'), true, 'module path exists')
67 common.npm(['ls', '--json', '--depth=0'], {cwd: basepath, env: env}, lsCheckAndRemove)
69 function lsCheckAndRemove (err, code, stdout, stderr) {
70 t.ifError(err, 'npm test on array bin')
71 t.equal(code, 0, 'exited OK')
72 t.equal(stderr.trim(), '', 'no error output')
73 var installed = JSON.parse(stdout.trim())
74 t.is(Object.keys(installed.dependencies).length, 1, 'one thing installed')
75 t.is(path.resolve(globalpath, installed.dependencies.npm.from), tarball, 'and it was our npm tarball')
76 common.npm(['rm', 'npm'], {cwd: basepath, env: env, stdio: 'inherit'}, removeCheck)
78 function removeCheck (err, code) {
80 t.is(code, 0, 'remove went ok')
81 common.npm(['ls', '--json', '--depth=0'], {cwd: basepath, env: env}, andDone)
83 function andDone (err, code, stdout, stderr) {
85 t.is(code, 0, 'remove went ok')
86 t.equal(stderr.trim(), '', 'no error output')
87 var installed = JSON.parse(stdout.trim())
88 t.ok(!installed.dependencies || installed.dependencies.length === 0, 'nothing left')
89 t.is(exists(binpath, cmdname), false, 'binary was removed')
90 t.is(exists(globalpath, 'lib', 'node_modules', 'npm'), false, 'module was entirely removed')
95 test('cleanup', function (t) {
102 mkdirp.sync(globalpath)
105 function cleanup () {
106 rimraf.sync(basepath)