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

fix: do not break if global.process missing #41

Merged
merged 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/base.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const process = require('./process.js')
const assert = require('assert')
const util = require('util')
const {AsyncResource} = require('async_hooks')
Expand Down Expand Up @@ -104,7 +105,7 @@ class Base extends MiniPass {
}

setTimeout (n) {
if (!this.hrtime)
if (typeof process.hrtime === 'function' && !this.hrtime)
this.hrtime = process.hrtime()

if (!this.start)
Expand Down Expand Up @@ -196,7 +197,7 @@ class Base extends MiniPass {
}

oncomplete (results) {
if (this.hrtime) {
if (typeof process.hrtime === 'function' && this.hrtime) {
this.hrtime = process.hrtime(this.hrtime)
this.time = results.time ||
Math.round(this.hrtime[0] * 1e6 + this.hrtime[1] / 1e3) / 1e3
Expand Down
1 change: 1 addition & 0 deletions lib/clean-yaml-object.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const process = require('./process.js')
const path = require('path')
const fs = require('fs')
const diff = require('diff')
Expand Down
12 changes: 12 additions & 0 deletions lib/fake-process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
env: {},
pid: 0,
hrtime: null,
argv: [],
cwd: () => '.',
platform: 'unknown',
exit: () => {},
versions: {
node: '',
},
}
2 changes: 2 additions & 0 deletions lib/find-main-script.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const process = require('./process.js')

// Node.js should provide an API for this
function mainScript(defaultName) {
if (typeof repl !== 'undefined' || '_eval' in process) {
Expand Down
1 change: 1 addition & 0 deletions lib/mock.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Module = require('module')
const { isAbsolute } = require('path')
const process = require('./process.js')

const isPlainObject = obj => obj
&& typeof obj === 'object'
Expand Down
2 changes: 2 additions & 0 deletions lib/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module.exports = typeof process === 'object' && process ? process
: require('./fake-process.js')
4 changes: 3 additions & 1 deletion lib/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

const fs = require('fs')
const path = require('path')
const cwd = process.cwd()

// initialize once so it doesn't get borked if process.argv is changed later
const process = require('./process.js')
const cwd = process.cwd()
const main = require('./find-main-script.js')('TAP')
const settings = require('../settings.js')

Expand Down
1 change: 1 addition & 0 deletions lib/spawn.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict'
const process = require('../lib/process.js')
const path = require('path')
const cp = require('child_process')
const ownOr = require('own-or')
Expand Down
8 changes: 8 additions & 0 deletions lib/tap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
'use strict'
const process = require('./process.js')
const fakeProcess = require('./fake-process.js')
const Domain = require('async-hook-domain')
const onExit = require('signal-exit')
const Test = require('./test.js')
Expand Down Expand Up @@ -40,6 +42,12 @@ const monkeypatchEpipe = () => {
}

const monkeypatchExit = () => {
/* istanbul ignore next - impossible to cover, but we do have a test
* that verifies we don't try to call process.on if process is gone */
if (global.process !== process) {
return
}

const exit = process.exit
const reallyExit = process.reallyExit

Expand Down
6 changes: 6 additions & 0 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
// encounter an unfinished test. When we encounter ANY kind of test, we
// block until its output is completed, dumping it all into the parser.

// grab a reference right away, in case the user clubs global.process
const process = require('./process.js')

const path = require('path')
const assert = require('assert')
const util = require('util')
Expand Down Expand Up @@ -256,6 +259,9 @@ class Test extends Base {

stdinOnly (extra) {
const stream = extra && extra.tapStream || process.stdin
if (!stream)
throw new Error('cannot read stdin without stdin stream')

if (this.queue.length !== 1 ||
this.queue[0] !== 'TAP version 13\n' ||
this.processing ||
Expand Down
Loading