/
mocha
executable file
·144 lines (128 loc) · 3.93 KB
/
mocha
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env node
'use strict';
/**
* This wrapper executable checks for known node flags and appends them when found, before invoking the "real" _mocha(1) executable.
*
* @module bin/mocha
* @private
*/
const {deprecate, warn} = require('../lib/utils');
const {spawn} = require('child_process');
const {loadOptions} = require('../lib/cli/options');
const {isNodeFlag, impliesNoTimeouts} = require('../lib/cli/node-flags');
const unparse = require('yargs-unparser');
const debug = require('debug')('mocha:cli');
const {aliases} = require('../lib/cli/run-option-metadata');
const mochaPath = require.resolve('./_mocha');
const mochaArgs = {};
const nodeArgs = {};
const opts = loadOptions(process.argv.slice(2));
debug('loaded opts', opts);
/**
* Given option/command `value`, disable timeouts if applicable
* @param {string} [value] - Value to check
* @ignore
*/
const disableTimeouts = value => {
if (impliesNoTimeouts(value)) {
debug(`option "${value}" disabled timeouts`);
mochaArgs.timeout = false;
delete mochaArgs.timeouts;
delete mochaArgs.t;
}
};
/**
* If `value` begins with `v8-` and is not explicitly `v8-options`, remove prefix
* @param {string} [value] - Value to check
* @returns {string} `value` with prefix (maybe) removed
* @ignore
*/
const trimV8Option = value =>
value !== 'v8-options' && /^v8-/.test(value) ? value.slice(2) : value;
// sort options into "node" and "mocha" buckets
Object.keys(opts).forEach(opt => {
opt = trimV8Option(opt);
if (isNodeFlag(opt)) {
nodeArgs[opt] = opts[opt];
disableTimeouts(opt);
} else {
mochaArgs[opt] = opts[opt];
}
});
// Native debugger handling
// see https://nodejs.org/api/debugger.html#debugger_debugger
// look for 'debug' or 'inspect' that would launch this debugger,
// remove it from Mocha's opts and prepend it to Node's opts.
// also coerce depending on Node.js version.
// A deprecation warning will be printed by node, if applicable.
// (mochaArgs._ are "positional" arguments, not prefixed with - or --)
if (/^(debug|inspect)$/.test(mochaArgs._[0])) {
const command = mochaArgs._.shift();
disableTimeouts(command);
// don't conflict with inspector
['debug', 'inspect', 'debug-brk', 'inspect-brk']
.filter(opt => opt in nodeArgs || opt in mochaArgs)
.forEach(opt => {
warn(`command "${command}" provided; --${opt} ignored`);
delete nodeArgs[opt];
delete mochaArgs[opt];
});
nodeArgs._ = [
parseInt(
process.version
.slice(1)
.split('.')
.shift(),
10
) >= 8
? 'inspect'
: 'debug'
];
}
// allow --debug to invoke --inspect on Node.js v8 or newer.
// these show up in childOpts because they are not recognized as valid node flags in this version of node.
['debug', 'debug-brk']
.filter(opt => opt in mochaArgs)
.forEach(opt => {
const newOpt = opt === 'debug' ? 'inspect' : 'inspect-brk';
warn(
`"--${opt}" is not available in Node.js ${
process.version
}; use "--${newOpt}" instead.`
);
nodeArgs[newOpt] = mochaArgs[opt];
mochaArgs.timeout = false;
debug(`--${opt} -> ${newOpt}`);
delete mochaArgs[opt];
});
// historical
if (nodeArgs.gc) {
deprecate(
'"-gc" is deprecated and will be removed from a future version of Mocha. Use "--gc-global" instead.'
);
nodeArgs['gc-global'] = nodeArgs.gc;
delete nodeArgs.gc;
}
const args = [].concat(
unparse(nodeArgs),
mochaPath,
unparse(mochaArgs, {alias: aliases})
);
debug(`exec ${process.execPath} w/ args:`, args);
const proc = spawn(process.execPath, args, {
stdio: 'inherit'
});
proc.on('exit', (code, signal) => {
process.on('exit', () => {
if (signal) {
process.kill(process.pid, signal);
} else {
process.exit(code);
}
});
});
// terminate children.
process.on('SIGINT', () => {
proc.kill('SIGINT'); // calls runner.abort()
proc.kill('SIGTERM'); // if that didn't work, we're probably in an infinite loop, so make it die.
});