Skip to content

Commit b17677d

Browse files
committedSep 21, 2021
gracefully handle the lack of a global.process object
1 parent de1f7f6 commit b17677d

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed
 

‎lib/repl.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
const nodeProcess = typeof process === 'object' && process ? process : {
2+
stdin: null,
3+
stdout: null,
4+
env: {},
5+
}
16
const {Watch} = require('./watch.js')
27
const repl = require('repl')
38
const rimraf = require('rimraf').sync
@@ -6,12 +11,18 @@ const path = require('path')
611
const fs = require('fs')
712
/* istanbul ignore next */
813
const noop = () => {}
14+
const {isStream} = require('minipass')
915

1016
// XXX useGlobal = true, because it doesn't matter, so save the cycles
1117
class Repl {
1218
constructor (options, input, output) {
13-
this.output = output || /* istanbul ignore next */ process.stdout
14-
this.input = input || /* istanbul ignore next */ process.stdin
19+
this.output = output || nodeProcess.stdout
20+
if (!isStream(this.output))
21+
throw new Error('output stream not provided, stdout unavailable')
22+
23+
this.input = input || nodeProcess.stdin
24+
if (!isStream(this.input))
25+
throw new Error('input stream not provided, stdin unavailable')
1526

1627
this.repl = null
1728
this._cb = null
@@ -50,7 +61,7 @@ class Repl {
5061
}
5162

5263
loadHistory () {
53-
const dir = process.env.HOME || 'node_modules/.cache/tap'
64+
const dir = nodeProcess.env.HOME || 'node_modules/.cache/tap'
5465

5566
try {
5667
return fs.readFileSync(dir + '/.tap_repl_history', 'utf8')
@@ -61,7 +72,7 @@ class Repl {
6172
}
6273

6374
saveHistory () {
64-
const dir = process.env.HOME || 'node_modules/.cache/tap'
75+
const dir = nodeProcess.env.HOME || 'node_modules/.cache/tap'
6576

6677
require('../settings.js').mkdirRecursiveSync(dir)
6778
try {

‎lib/watch.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const proc = typeof process === 'object' && process ? process : null
2+
13
const chokidar = require('chokidar')
24
const EE = require('events')
35
const Minipass = require('minipass')
@@ -12,25 +14,27 @@ class Watch extends Minipass {
1214
constructor (options) {
1315
if (!options.coverage)
1416
throw new Error('--watch requires coverage to be enabled')
17+
if (!proc)
18+
throw new Error('--watch requires working node.js process object')
1519
super()
1620
this.args = [bin, ...options._.parsed, '--no-watch']
1721
this.positionals = [...options._]
1822
this.log('initial test run', this.args)
19-
this.proc = spawn(process.execPath, this.args, {
23+
this.proc = spawn(proc.execPath, this.args, {
2024
stdio: 'inherit'
2125
})
2226
this.proc.on('close', () => this.main())
2327
const saveFolder = 'node_modules/.cache/tap'
2428
require('../settings.js').mkdirRecursiveSync(saveFolder)
25-
this.saveFile = saveFolder + '/watch-' + process.pid
29+
this.saveFile = saveFolder + '/watch-' + proc.pid
2630
/* istanbul ignore next */
2731
onExit(() => require('../settings.js').rmdirRecursiveSync(this.saveFile))
2832
this.index = null
2933
this.indexFile = '.nyc_output/processinfo/index.json'
3034
this.fileList = []
3135
this.queue = []
3236
this.watcher = null
33-
this.env = { ...process.env }
37+
this.env = { ...proc.env }
3438
}
3539

3640
readIndex () {
@@ -51,7 +55,7 @@ class Watch extends Minipass {
5155
// Since a covered test was definitely included in its own
5256
// test run, don't add it a second time, so we don't get
5357
// two chokidar events for the same file change.
54-
const cwd = process.cwd()
58+
const cwd = proc.cwd()
5559
const fileSet = new Set(Object.keys(this.index.files))
5660
Object.keys(this.index.externalIds)
5761
.filter(f => !fileSet.has(resolve(f)))
@@ -109,7 +113,7 @@ class Watch extends Minipass {
109113
writeFileSync(this.saveFile, set.join('\n') + '\n')
110114
this.queue.length = 0
111115

112-
this.proc = spawn(process.execPath, [
116+
this.proc = spawn(proc.execPath, [
113117
...this.args, '--save=' + this.saveFile, '--nyc-arg=--no-clean'
114118
], {
115119
stdio: 'inherit',

‎test/repl.js

+14
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,17 @@ t.test('exit', t => {
240240
t.matchSnapshot(output.read(), 'output')
241241
t.end()
242242
})
243+
244+
t.test('throw if input/output not a stream', t => {
245+
const proc = process
246+
global.process = null
247+
t.teardown(() => global.process = proc)
248+
const {Repl} = t.mock('../lib/repl.js')
249+
t.throws(() => new Repl({}, input, null), {
250+
message: 'output stream not provided, stdout unavailable',
251+
})
252+
t.throws(() => new Repl({}, null, output), {
253+
message: 'input stream not provided, stdin unavailable',
254+
})
255+
t.end()
256+
})

‎test/watch.js

+11
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,17 @@ t.throws(() => new Watch({}), {
157157
message: '--watch requires coverage to be enabled'
158158
})
159159

160+
t.test('not available when no node process', t => {
161+
const proc = process
162+
global.process = null
163+
t.teardown(() => global.process = proc)
164+
const {Watch} = t.mock('../lib/watch.js')
165+
t.throws(() => new Watch({ coverage: true }), {
166+
message: '--watch requires working node.js process object',
167+
})
168+
t.end()
169+
})
170+
160171
t.test('run tests on changes', t => {
161172
t.test('initial test', t => {
162173
spawnTrack.once('spawn', proc => {

0 commit comments

Comments
 (0)
Please sign in to comment.