Skip to content

Commit

Permalink
test: add npm self installation test
Browse files Browse the repository at this point in the history
Credit: @iarna
PR-URL: npm/npm#11292
Reviewed-By: @zkat
  • Loading branch information
iarna committed Feb 18, 2016
1 parent 502d7d0 commit 420f267
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ npm-debug.log
/test/packages/npm-test-depends-on-spark/which-spark.log
/test/packages/test-package/random-data.txt
/test/root
/test/npm_cache
node_modules/marked
node_modules/ronn
node_modules/tap
Expand Down
106 changes: 106 additions & 0 deletions test/tap/legacy-npm-self-install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
'use strict'
var test = require('tap').test
var fs = require('graceful-fs')
var common = require('../common-tap.js')
var path = require('path')
var rimraf = require('rimraf')
var mkdirp = require('mkdirp')
var npmpath = path.resolve(__dirname, '../..')
var basepath = path.resolve(__dirname, path.basename(__filename, '.js'))
var globalpath = path.resolve(basepath, 'global')
var extend = Object.assign || require('util')._extend
var isWin32 = process.platform === 'win32'

test('setup', function (t) {
setup()
t.done()
})

var tarball

test('build-tarball', function (t) {
common.npm(['pack'], {cwd: npmpath, stdio: ['ignore', 'pipe', process.stderr]}, function (err, code, stdout) {
if (err) throw err
t.is(code, 0, 'pack went ok')
tarball = path.resolve(npmpath, stdout.trim().replace(/^(?:.|\n)*(?:^|\n)(.*?[.]tgz)$/, '$1'))
t.match(tarball, /[.]tgz$/, 'got a tarball')
t.done()
})
})

function exists () {
try {
fs.statSync(path.resolve.apply(null, arguments))
return true
} catch (ex) {
return false
}
}

test('npm-self-install', function (t) {
if (!tarball) return t.done()

var env = extend({}, process.env)
var pathsep = isWin32 ? ';' : ':'
env.npm_config_prefix = globalpath
env.npm_config_global = 'true'
env.npm_config_npat = 'false'
env.NODE_PATH = null
env.npm_config_user_agent = null
env.npm_config_color = 'always'
env.npm_config_progress = 'always'
var PATH = env.PATH.split(pathsep)
var binpath = isWin32 ? globalpath : path.join(globalpath, 'bin')
var cmdname = isWin32 ? 'npm.cmd' : 'npm'
PATH.unshift(binpath)
env.PATH = PATH.join(pathsep)

var opts = {cwd: basepath, env: env, stdio: ['ignore', 'ignore', process.stderr]}

common.npm(['install', '--ignore-scripts', tarball], opts, installCheckAndTest)
function installCheckAndTest (err, code) {
if (err) throw err
t.is(code, 0, 'install went ok')
t.is(exists(binpath, cmdname), true, 'binary was installed')
t.is(exists(globalpath, 'lib', 'node_modules', 'npm'), true, 'module path exists')
common.npm(['ls', '--json', '--depth=0'], {cwd: basepath, env: env}, lsCheckAndRemove)
}
function lsCheckAndRemove (err, code, stdout, stderr) {
t.ifError(err, 'npm test on array bin')
t.equal(code, 0, 'exited OK')
t.equal(stderr.trim(), '', 'no error output')
var installed = JSON.parse(stdout.trim())
t.is(Object.keys(installed.dependencies).length, 1, 'one thing installed')
t.is(path.resolve(globalpath, installed.dependencies.npm.from), tarball, 'and it was our npm tarball')
common.npm(['rm', 'npm'], {cwd: basepath, env: env, stdio: 'inherit'}, removeCheck)
}
function removeCheck (err, code) {
if (err) throw err
t.is(code, 0, 'remove went ok')
common.npm(['ls', '--json', '--depth=0'], {cwd: basepath, env: env}, andDone)
}
function andDone (err, code, stdout, stderr) {
if (err) throw err
t.is(code, 0, 'remove went ok')
t.equal(stderr.trim(), '', 'no error output')
var installed = JSON.parse(stdout.trim())
t.ok(!installed.dependencies || installed.dependencies.length === 0, 'nothing left')
t.is(exists(binpath, cmdname), false, 'binary was removed')
t.is(exists(globalpath, 'lib', 'node_modules', 'npm'), false, 'module was entirely removed')
t.done()
}
})

test('cleanup', function (t) {
cleanup()
t.done()
})

function setup () {
cleanup()
mkdirp.sync(globalpath)
}

function cleanup () {
rimraf.sync(basepath)
}

0 comments on commit 420f267

Please sign in to comment.