Skip to content

Commit 9ea45e7

Browse files
boneskulljuergba
authored andcommitted
do not fork if no node flags present (#3827)
* do not fork if no node flags present * peer review changes, also ensure `lib/cli/cli.js` is executable
1 parent d02a096 commit 9ea45e7

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

bin/mocha

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
'use strict';
44

55
/**
6-
* This wrapper executable checks for known node flags and appends them when found, before invoking the "real" _mocha(1) executable.
6+
* This wrapper executable checks for known node flags and appends them when found,
7+
* before invoking the "real" executable (`lib/cli/cli.js`)
78
*
89
* @module bin/mocha
910
* @private
1011
*/
1112

1213
const {deprecate, warn} = require('../lib/utils');
13-
const {spawn} = require('child_process');
1414
const {loadOptions} = require('../lib/cli/options');
1515
const {
1616
unparseNodeFlags,
@@ -22,7 +22,6 @@ const debug = require('debug')('mocha:cli:mocha');
2222
const {aliases} = require('../lib/cli/run-option-metadata');
2323
const nodeEnv = require('node-environment-flags');
2424

25-
const mochaPath = require.resolve('./_mocha');
2625
const mochaArgs = {};
2726
const nodeArgs = {};
2827

@@ -118,32 +117,39 @@ if (nodeArgs.gc) {
118117
delete nodeArgs.gc;
119118
}
120119

121-
debug('final node args', nodeArgs);
120+
if (Object.keys(nodeArgs).length) {
121+
const {spawn} = require('child_process');
122+
const mochaPath = require.resolve('../lib/cli/cli.js');
122123

123-
const args = [].concat(
124-
unparseNodeFlags(nodeArgs),
125-
mochaPath,
126-
unparse(mochaArgs, {alias: aliases})
127-
);
124+
debug('final node args', nodeArgs);
128125

129-
debug(`exec ${process.execPath} w/ args:`, args);
126+
const args = [].concat(
127+
unparseNodeFlags(nodeArgs),
128+
mochaPath,
129+
unparse(mochaArgs, {alias: aliases})
130+
);
130131

131-
const proc = spawn(process.execPath, args, {
132-
stdio: 'inherit'
133-
});
132+
debug(`exec ${process.execPath} w/ args:`, args);
134133

135-
proc.on('exit', (code, signal) => {
136-
process.on('exit', () => {
137-
if (signal) {
138-
process.kill(process.pid, signal);
139-
} else {
140-
process.exit(code);
141-
}
134+
const proc = spawn(process.execPath, args, {
135+
stdio: 'inherit'
142136
});
143-
});
144137

145-
// terminate children.
146-
process.on('SIGINT', () => {
147-
proc.kill('SIGINT'); // calls runner.abort()
148-
proc.kill('SIGTERM'); // if that didn't work, we're probably in an infinite loop, so make it die.
149-
});
138+
proc.on('exit', (code, signal) => {
139+
process.on('exit', () => {
140+
if (signal) {
141+
process.kill(process.pid, signal);
142+
} else {
143+
process.exit(code);
144+
}
145+
});
146+
});
147+
148+
// terminate children.
149+
process.on('SIGINT', () => {
150+
proc.kill('SIGINT'); // calls runner.abort()
151+
proc.kill('SIGTERM'); // if that didn't work, we're probably in an infinite loop, so make it die.
152+
});
153+
} else {
154+
require('../lib/cli/cli').main(unparse(mochaArgs, {alias: aliases}));
155+
}

lib/cli/cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#!/usr/bin/env node
2+
13
'use strict';
24

35
/**

0 commit comments

Comments
 (0)