summaryrefslogtreecommitdiff
path: root/deps/node/deps/npm/test/need-npm5-update/legacy-npm-self-install.js
blob: 6bcd8073621d0b898b732b26aea9a85efb2be11e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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 osenv = require('osenv')
var npmpath = path.resolve(__dirname, '../..')
var basepath = path.resolve(osenv.tmpdir(), path.basename(__filename, '.js'))
var globalpath = path.resolve(basepath, 'global')
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 = Object.assign({}, process.env)
  var pathsep = isWin32 ? ';' : ':'
  env.npm_config_prefix = globalpath
  env.npm_config_global = 'true'
  env.NODE_PATH = null
  env.npm_config_user_agent = null
  env.npm_config_color = 'always'
  env.npm_config_progress = 'always'
  env.npm_config_shrinkwrap = 'false'
  var PATH = env.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, isWin32 ? '' : '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}, 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)
}