UNPKG

5.4 kBJavaScriptView Raw
1'use strict'
2var fs = require('graceful-fs')
3var readCmdShim = require('read-cmd-shim')
4var isWindows = require('../lib/utils/is-windows.js')
5var extend = Object.assign || require('util')._extend
6var Bluebird = require('bluebird')
7
8// cheesy hackaround for test deps (read: nock) that rely on setImmediate
9if (!global.setImmediate || !require('timers').setImmediate) {
10 require('timers').setImmediate = global.setImmediate = function () {
11 var args = [arguments[0], 0].concat([].slice.call(arguments, 1))
12 setTimeout.apply(this, args)
13 }
14}
15
16var spawn = require('child_process').spawn
17var path = require('path')
18
19var port = exports.port = 1337
20exports.registry = 'http://localhost:' + port
21const ourenv = {}
22ourenv.npm_config_loglevel = 'error'
23ourenv.npm_config_progress = 'false'
24
25var npm_config_cache = path.resolve(__dirname, 'npm_cache')
26ourenv.npm_config_cache = exports.npm_config_cache = npm_config_cache
27ourenv.npm_config_userconfig = exports.npm_config_userconfig = path.join(__dirname, 'fixtures', 'config', 'userconfig')
28ourenv.npm_config_globalconfig = exports.npm_config_globalconfig = path.join(__dirname, 'fixtures', 'config', 'globalconfig')
29ourenv.npm_config_global_style = 'false'
30ourenv.npm_config_legacy_bundling = 'false'
31ourenv.npm_config_fetch_retries = '0'
32ourenv.random_env_var = 'foo'
33// suppress warnings about using a prerelease version of node
34ourenv.npm_config_node_version = process.version.replace(/-.*$/, '')
35for (let key of Object.keys(ourenv)) process.env[key] = ourenv[key]
36
37var bin = exports.bin = require.resolve('../bin/npm-cli.js')
38
39var chain = require('slide').chain
40var once = require('once')
41
42var nodeBin = exports.nodeBin = process.env.npm_node_execpath || process.env.NODE || process.execPath
43
44exports.npm = function (cmd, opts, cb) {
45 if (!cb) {
46 var prom = new Bluebird((resolve, reject) => {
47 cb = function (err, code, stdout, stderr) {
48 if (err) return reject(err)
49 return resolve([code, stdout, stderr])
50 }
51 })
52 }
53 cb = once(cb)
54 cmd = [bin].concat(cmd)
55 opts = extend({}, opts || {})
56
57 opts.env = opts.env || process.env
58 if (opts.env._storage) opts.env = Object.assign({}, opts.env._storage)
59 if (!opts.env.npm_config_cache) {
60 opts.env.npm_config_cache = npm_config_cache
61 }
62 if (!opts.env.npm_config_send_metrics) {
63 opts.env.npm_config_send_metrics = 'false'
64 }
65
66 nodeBin = opts.nodeExecPath || nodeBin
67
68 var stdout = ''
69 var stderr = ''
70 var child = spawn(nodeBin, cmd, opts)
71
72 if (child.stderr) {
73 child.stderr.on('data', function (chunk) {
74 stderr += chunk
75 })
76 }
77
78 if (child.stdout) {
79 child.stdout.on('data', function (chunk) {
80 stdout += chunk
81 })
82 }
83
84 child.on('error', cb)
85
86 child.on('close', function (code) {
87 cb(null, code, stdout, stderr)
88 })
89 return prom || child
90}
91
92exports.makeGitRepo = function (params, cb) {
93 // git must be called after npm.load because it uses config
94 var git = require('../lib/utils/git.js')
95
96 var root = params.path || process.cwd()
97 var user = params.user || 'PhantomFaker'
98 var email = params.email || 'nope@not.real'
99 var added = params.added || ['package.json']
100 var message = params.message || 'stub repo'
101
102 var opts = { cwd: root, env: { PATH: process.env.PATH } }
103 var commands = [
104 git.chainableExec(['init'], opts),
105 git.chainableExec(['config', 'user.name', user], opts),
106 git.chainableExec(['config', 'user.email', email], opts),
107 git.chainableExec(['add'].concat(added), opts),
108 git.chainableExec(['commit', '-m', message], opts)
109 ]
110
111 if (Array.isArray(params.commands)) {
112 commands = commands.concat(params.commands)
113 }
114
115 chain(commands, cb)
116}
117
118exports.readBinLink = function (path) {
119 if (isWindows) {
120 return readCmdShim.sync(path)
121 } else {
122 return fs.readlinkSync(path)
123 }
124}
125
126exports.skipIfWindows = function (why) {
127 if (!isWindows) return
128 console.log('1..1')
129 if (!why) why = 'this test not available on windows'
130 console.log('ok 1 # skip ' + why)
131 process.exit(0)
132}
133
134exports.pendIfWindows = function (why) {
135 if (!isWindows) return
136 console.log('1..1')
137 if (!why) why = 'this test is pending further changes on windows'
138 console.log('not ok 1 # todo ' + why)
139 process.exit(0)
140}
141
142exports.newEnv = function () {
143 return new Environment(process.env)
144}
145
146exports.emptyEnv = function () {
147 const filtered = {}
148 for (let key of Object.keys(process.env)) {
149 if (!/^npm_/.test(key)) filtered[key] = process.env[key]
150 }
151 for (let key of Object.keys(ourenv)) {
152 filtered[key] = ourenv[key]
153 }
154 return new Environment(filtered)
155}
156
157function Environment (env) {
158 if (env instanceof Environment) return env.clone()
159
160 Object.defineProperty(this, '_storage', {
161 value: extend({}, env)
162 })
163}
164Environment.prototype = {}
165
166Environment.prototype.delete = function (key) {
167 var args = Array.isArray(key) ? key : arguments
168 var ii
169 for (ii = 0; ii < args.length; ++ii) {
170 delete this._storage[args[ii]]
171 }
172 return this
173}
174
175Environment.prototype.clone = function () {
176 return new Environment(this._storage)
177}
178
179Environment.prototype.extend = function (env) {
180 var self = this.clone()
181 var args = Array.isArray(env) ? env : arguments
182 var ii
183 for (ii = 0; ii < args.length; ++ii) {
184 var arg = args[ii]
185 if (!arg) continue
186 Object.keys(arg).forEach(function (name) {
187 self._storage[name] = arg[name]
188 })
189 }
190 return self
191}