Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gracefully handle the lack of a global.process object #775

Merged
merged 3 commits into from
Sep 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 15 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
const nodeProcess = typeof process === 'object' && process ? process : {
stdin: null,
stdout: null,
env: {},
}
const {Watch} = require('./watch.js')
const repl = require('repl')
const rimraf = require('rimraf').sync
Expand All @@ -6,12 +11,18 @@ const path = require('path')
const fs = require('fs')
/* istanbul ignore next */
const noop = () => {}
const {isStream} = require('minipass')

// XXX useGlobal = true, because it doesn't matter, so save the cycles
class Repl {
constructor (options, input, output) {
this.output = output || /* istanbul ignore next */ process.stdout
this.input = input || /* istanbul ignore next */ process.stdin
this.output = output || nodeProcess.stdout
if (!isStream(this.output))
throw new Error('output stream not provided, stdout unavailable')

this.input = input || nodeProcess.stdin
if (!isStream(this.input))
throw new Error('input stream not provided, stdin unavailable')

this.repl = null
this._cb = null
Expand Down Expand Up @@ -50,7 +61,7 @@ class Repl {
}

loadHistory () {
const dir = process.env.HOME || 'node_modules/.cache/tap'
const dir = nodeProcess.env.HOME || 'node_modules/.cache/tap'

try {
return fs.readFileSync(dir + '/.tap_repl_history', 'utf8')
Expand All @@ -61,7 +72,7 @@ class Repl {
}

saveHistory () {
const dir = process.env.HOME || 'node_modules/.cache/tap'
const dir = nodeProcess.env.HOME || 'node_modules/.cache/tap'

require('../settings.js').mkdirRecursiveSync(dir)
try {
Expand Down
14 changes: 9 additions & 5 deletions lib/watch.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const proc = typeof process === 'object' && process ? process : null

const chokidar = require('chokidar')
const EE = require('events')
const Minipass = require('minipass')
Expand All @@ -12,25 +14,27 @@ class Watch extends Minipass {
constructor (options) {
if (!options.coverage)
throw new Error('--watch requires coverage to be enabled')
if (!proc)
throw new Error('--watch requires working node.js process object')
super()
this.args = [bin, ...options._.parsed, '--no-watch']
this.positionals = [...options._]
this.log('initial test run', this.args)
this.proc = spawn(process.execPath, this.args, {
this.proc = spawn(proc.execPath, this.args, {
stdio: 'inherit'
})
this.proc.on('close', () => this.main())
const saveFolder = 'node_modules/.cache/tap'
require('../settings.js').mkdirRecursiveSync(saveFolder)
this.saveFile = saveFolder + '/watch-' + process.pid
this.saveFile = saveFolder + '/watch-' + proc.pid
/* istanbul ignore next */
onExit(() => require('../settings.js').rmdirRecursiveSync(this.saveFile))
this.index = null
this.indexFile = '.nyc_output/processinfo/index.json'
this.fileList = []
this.queue = []
this.watcher = null
this.env = { ...process.env }
this.env = { ...proc.env }
}

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

this.proc = spawn(process.execPath, [
this.proc = spawn(proc.execPath, [
...this.args, '--save=' + this.saveFile, '--nyc-arg=--no-clean'
], {
stdio: 'inherit',
Expand Down