-
-
Notifications
You must be signed in to change notification settings - Fork 3k
fix --inspect and its ilk; closes #3681 #3699
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
Changes from all commits
0f6b91d
2abc1a8
9b4c6f0
79b5f09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ global: | |
- okGlobalA,okGlobalB | ||
- okGlobalC | ||
- callback* | ||
timeout: 200 | ||
timeout: 300 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,10 +12,15 @@ | |
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 { | ||
unparseNodeFlags, | ||
isNodeFlag, | ||
impliesNoTimeouts | ||
} = require('../lib/cli/node-flags'); | ||
const unparse = require('yargs-unparser'); | ||
const debug = require('debug')('mocha:cli'); | ||
const debug = require('debug')('mocha:cli:mocha'); | ||
const {aliases} = require('../lib/cli/run-option-metadata'); | ||
const nodeEnv = require('node-environment-flags'); | ||
|
||
const mochaPath = require.resolve('./_mocha'); | ||
const mochaArgs = {}; | ||
|
@@ -32,7 +37,7 @@ debug('loaded opts', opts); | |
const disableTimeouts = value => { | ||
if (impliesNoTimeouts(value)) { | ||
debug(`option "${value}" disabled timeouts`); | ||
mochaArgs.timeout = false; | ||
mochaArgs.timeout = 0; | ||
delete mochaArgs.timeouts; | ||
delete mochaArgs.t; | ||
} | ||
|
@@ -45,13 +50,12 @@ const disableTimeouts = value => { | |
* @ignore | ||
*/ | ||
const trimV8Option = value => | ||
value !== 'v8-options' && /^v8-/.test(value) ? value.slice(2) : value; | ||
value !== 'v8-options' && /^v8-/.test(value) ? value.slice(3) : value; | ||
|
||
// sort options into "node" and "mocha" buckets | ||
Object.keys(opts).forEach(opt => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just noted, there is a Mocha flag There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mocha's |
||
opt = trimV8Option(opt); | ||
if (isNodeFlag(opt)) { | ||
nodeArgs[opt] = opts[opt]; | ||
nodeArgs[trimV8Option(opt)] = opts[opt]; | ||
disableTimeouts(opt); | ||
} else { | ||
mochaArgs[opt] = opts[opt]; | ||
|
@@ -90,20 +94,19 @@ if (/^(debug|inspect)$/.test(mochaArgs._[0])) { | |
} | ||
|
||
// 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) | ||
.filter(opt => opt in nodeArgs && !nodeEnv.has(opt)) | ||
.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]; | ||
nodeArgs[newOpt] = nodeArgs[opt]; | ||
mochaArgs.timeout = false; | ||
debug(`--${opt} -> ${newOpt}`); | ||
delete mochaArgs[opt]; | ||
delete nodeArgs[opt]; | ||
}); | ||
|
||
// historical | ||
|
@@ -115,8 +118,10 @@ if (nodeArgs.gc) { | |
delete nodeArgs.gc; | ||
} | ||
|
||
debug('final node args', nodeArgs); | ||
|
||
const args = [].concat( | ||
unparse(nodeArgs), | ||
unparseNodeFlags(nodeArgs), | ||
mochaPath, | ||
unparse(mochaArgs, {alias: aliases}) | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. down L131: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the short answer is "yes". the longer answer is we ignore |
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ const findup = require('findup-sync'); | |
const {deprecate} = require('../utils'); | ||
const debug = require('debug')('mocha:cli:options'); | ||
const {createMissingArgumentError} = require('../errors'); | ||
const {isNodeFlag} = require('./node-flags'); | ||
|
||
/** | ||
* The `yargs-parser` namespace | ||
|
@@ -75,22 +76,46 @@ const nargOpts = types.array | |
* @ignore | ||
*/ | ||
const parse = (args = [], ...configObjects) => { | ||
const result = yargsParser.detailed( | ||
args, | ||
Object.assign( | ||
{ | ||
configuration, | ||
configObjects, | ||
coerce: coerceOpts, | ||
narg: nargOpts, | ||
alias: aliases | ||
}, | ||
types | ||
) | ||
// save node-specific args for special handling. | ||
// 1. when these args have a "=" they should be considered to have values | ||
// 2. if they don't, they just boolean flags | ||
// 3. to avoid explicitly defining the set of them, we tell yargs-parser they | ||
// are ALL boolean flags. | ||
// 4. we can then reapply the values after yargs-parser is done. | ||
const nodeArgs = (Array.isArray(args) ? args : args.split(' ')).reduce( | ||
(acc, arg) => { | ||
const pair = arg.split('='); | ||
const flag = pair[0].replace(/^--?/, ''); | ||
if (isNodeFlag(flag)) { | ||
return arg.includes('=') | ||
? acc.concat([[flag, pair[1]]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (sic) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we save ALL found node args here so we can use them in the boolean array below. |
||
: acc.concat([[flag, true]]); | ||
} | ||
return acc; | ||
}, | ||
[] | ||
); | ||
|
||
const result = yargsParser.detailed(args, { | ||
configuration, | ||
configObjects, | ||
coerce: coerceOpts, | ||
narg: nargOpts, | ||
alias: aliases, | ||
string: types.string, | ||
array: types.array, | ||
number: types.number, | ||
boolean: types.boolean.concat(nodeArgs.map(pair => pair[0])) | ||
}); | ||
if (result.error) { | ||
throw createMissingArgumentError(result.error.message); | ||
} | ||
|
||
// reapply "=" arg values from above | ||
nodeArgs.forEach(([key, value]) => { | ||
result.argv[key] = value; | ||
}); | ||
|
||
return result.argv; | ||
}; | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,19 +14,117 @@ describe('--debug', function() { | |
|
||
it('should invoke --inspect', function(done) { | ||
invokeMocha( | ||
['--debug', '--file', DEFAULT_FIXTURE], | ||
['--debug', DEFAULT_FIXTURE], | ||
function(err, res) { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res, 'to have passed').and( | ||
expect(res, 'to contain output', /Debugger listening/i); | ||
done(); | ||
}, | ||
'pipe' | ||
); | ||
}); | ||
|
||
it('should invoke --inspect-brk', function(done) { | ||
var proc = invokeMocha( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one is failing with "Uncaught UnexpectedError" on my Windows. I need to investigate further. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, I don't know how to fix this. that's where I was hoping you could help (eventually) killing the subprocess using our method (because |
||
['--debug-brk', DEFAULT_FIXTURE], | ||
function(err, res) { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res, 'to contain output', /Debugger listening/i); | ||
done(); | ||
}, | ||
'pipe' | ||
); | ||
|
||
// debugger must be manually killed | ||
setTimeout(function() { | ||
process.kill(proc.pid, 'SIGINT'); | ||
}, 2000); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change to:
This way the On my windows platform the four 'Node.js v8+' tests do pass. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand what you're asking for |
||
|
||
it('should respect custom host/port', function(done) { | ||
invokeMocha( | ||
['--debug=127.0.0.1:9229', DEFAULT_FIXTURE], | ||
function(err, res) { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect( | ||
res, | ||
'to contain output', | ||
/Debugger listening on .*127.0.0.1:9229/i | ||
); | ||
done(); | ||
}, | ||
'pipe' | ||
); | ||
}); | ||
|
||
it('should warn about incorrect usage for version', function(done) { | ||
invokeMocha( | ||
['--debug=127.0.0.1:9229', DEFAULT_FIXTURE], | ||
function(err, res) { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res, 'to contain output', /"--debug" is not available/i); | ||
done(); | ||
}, | ||
'pipe' | ||
); | ||
}); | ||
}); | ||
boneskull marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
describe('Node.js v6', function() { | ||
// note that v6.3.0 and newer supports --inspect but still supports --debug. | ||
before(function() { | ||
if (process.version.substring(0, 2) !== 'v6') { | ||
this.skip(); | ||
} | ||
}); | ||
|
||
it('should start debugger', function(done) { | ||
var proc = invokeMocha( | ||
['--debug', DEFAULT_FIXTURE], | ||
function(err, res) { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect(res, 'to contain output', /Debugger listening/i); | ||
done(); | ||
}, | ||
'pipe' | ||
); | ||
|
||
// debugger must be manually killed | ||
setTimeout(function() { | ||
process.kill(proc.pid, 'SIGINT'); | ||
}, 2000); | ||
}); | ||
|
||
it('should respect custom host/port', function(done) { | ||
var proc = invokeMocha( | ||
['--debug=127.0.0.1:9229', DEFAULT_FIXTURE], | ||
function(err, res) { | ||
if (err) { | ||
return done(err); | ||
} | ||
expect( | ||
res, | ||
'to contain output', | ||
/Debugger listening/i | ||
/Debugger listening on .*127.0.0.1:9229/i | ||
); | ||
done(); | ||
}, | ||
{stdio: 'pipe'} | ||
'pipe' | ||
); | ||
|
||
setTimeout(function() { | ||
process.kill(proc.pid, 'SIGINT'); | ||
}, 2000); | ||
}); | ||
}); | ||
}); |
Uh oh!
There was an error while loading. Please reload this page.