• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var test = require('tap').test
3var fs = require('graceful-fs')
4var common = require('../common-tap.js')
5var path = require('path')
6var rimraf = require('rimraf')
7var mkdirp = require('mkdirp')
8var osenv = require('osenv')
9var npmpath = path.resolve(__dirname, '../..')
10var basepath = path.resolve(osenv.tmpdir(), path.basename(__filename, '.js'))
11var globalpath = path.resolve(basepath, 'global')
12var isWin32 = process.platform === 'win32'
13
14test('setup', function (t) {
15  setup()
16  t.done()
17})
18
19var tarball
20
21test('build-tarball', function (t) {
22  common.npm(['pack'], {cwd: npmpath, stdio: ['ignore', 'pipe', process.stderr]}, function (err, code, stdout) {
23    if (err) throw err
24    t.is(code, 0, 'pack went ok')
25    tarball = path.resolve(npmpath, stdout.trim().replace(/^(?:.|\n)*(?:^|\n)(.*?[.]tgz)$/, '$1'))
26    t.match(tarball, /[.]tgz$/, 'got a tarball')
27    t.done()
28  })
29})
30
31function exists () {
32  try {
33    fs.statSync(path.resolve.apply(null, arguments))
34    return true
35  } catch (ex) {
36    return false
37  }
38}
39
40test('npm-self-install', function (t) {
41  if (!tarball) return t.done()
42
43  var env = Object.assign({}, process.env)
44  var pathsep = isWin32 ? ';' : ':'
45  env.npm_config_prefix = globalpath
46  env.npm_config_global = 'true'
47  env.NODE_PATH = null
48  env.npm_config_user_agent = null
49  env.npm_config_color = 'always'
50  env.npm_config_progress = 'always'
51  env.npm_config_shrinkwrap = 'false'
52  var PATH = env.PATH ? env.PATH.split(pathsep) : []
53  var binpath = isWin32 ? globalpath : path.join(globalpath, 'bin')
54  var cmdname = isWin32 ? 'npm.cmd' : 'npm'
55  PATH.unshift(binpath)
56  env.PATH = PATH.join(pathsep)
57
58  var opts = {cwd: basepath, env: env, stdio: ['ignore', 'ignore', process.stderr]}
59
60  common.npm(['install', '--ignore-scripts', tarball], opts, installCheckAndTest)
61  function installCheckAndTest (err, code) {
62    if (err) throw err
63    t.is(code, 0, 'install went ok')
64    t.is(exists(binpath, cmdname), true, 'binary was installed')
65    t.is(exists(globalpath, isWin32 ? '' : 'lib', 'node_modules', 'npm'), true, 'module path exists')
66    common.npm(['ls', '--json', '--depth=0'], {cwd: basepath, env: env}, lsCheckAndRemove)
67  }
68  function lsCheckAndRemove (err, code, stdout, stderr) {
69    t.ifError(err, 'npm test on array bin')
70    t.equal(code, 0, 'exited OK')
71    t.equal(stderr.trim(), '', 'no error output')
72    var installed = JSON.parse(stdout.trim())
73    t.is(Object.keys(installed.dependencies).length, 1, 'one thing installed')
74    t.is(path.resolve(globalpath, installed.dependencies.npm.from), tarball, 'and it was our npm tarball')
75    common.npm(['rm', 'npm'], {cwd: basepath, env: env}, removeCheck)
76  }
77  function removeCheck (err, code) {
78    if (err) throw err
79    t.is(code, 0, 'remove went ok')
80    common.npm(['ls', '--json', '--depth=0'], {cwd: basepath, env: env}, andDone)
81  }
82  function andDone (err, code, stdout, stderr) {
83    if (err) throw err
84    t.is(code, 0, 'remove went ok')
85    t.equal(stderr.trim(), '', 'no error output')
86    var installed = JSON.parse(stdout.trim())
87    t.ok(!installed.dependencies || installed.dependencies.length === 0, 'nothing left')
88    t.is(exists(binpath, cmdname), false, 'binary was removed')
89    t.is(exists(globalpath, 'lib', 'node_modules', 'npm'), false, 'module was entirely removed')
90    t.done()
91  }
92})
93
94test('cleanup', function (t) {
95  cleanup()
96  t.done()
97})
98
99function setup () {
100  cleanup()
101  mkdirp.sync(globalpath)
102}
103
104function cleanup () {
105  rimraf.sync(basepath)
106}
107