]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/test/tap/legacy-npm-self-install.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / test / tap / legacy-npm-self-install.js
1 'use strict'
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'
14
15 test('setup', function (t) {
16   setup()
17   t.done()
18 })
19
20 var tarball
21
22 test('build-tarball', function (t) {
23   common.npm(['pack'], {cwd: npmpath, stdio: ['ignore', 'pipe', process.stderr]}, function (err, code, stdout) {
24     if (err) throw err
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')
28     t.done()
29   })
30 })
31
32 function exists () {
33   try {
34     fs.statSync(path.resolve.apply(null, arguments))
35     return true
36   } catch (ex) {
37     return false
38   }
39 }
40
41 test('npm-self-install', function (t) {
42   if (!tarball) return t.done()
43
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'
49   env.NODE_PATH = null
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'
56   PATH.unshift(binpath)
57   env.PATH = PATH.join(pathsep)
58
59   var opts = {cwd: basepath, env: env, stdio: ['ignore', 'ignore', process.stderr]}
60
61   common.npm(['install', '--ignore-scripts', tarball], opts, installCheckAndTest)
62   function installCheckAndTest (err, code) {
63     if (err) throw err
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)
68   }
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)
77   }
78   function removeCheck (err, code) {
79     if (err) throw err
80     t.is(code, 0, 'remove went ok')
81     common.npm(['ls', '--json', '--depth=0'], {cwd: basepath, env: env}, andDone)
82   }
83   function andDone (err, code, stdout, stderr) {
84     if (err) throw err
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')
91     t.done()
92   }
93 })
94
95 test('cleanup', function (t) {
96   cleanup()
97   t.done()
98 })
99
100 function setup () {
101   cleanup()
102   mkdirp.sync(globalpath)
103 }
104
105 function cleanup () {
106   rimraf.sync(basepath)
107 }