Skip to content
This repository was archived by the owner on Dec 4, 2023. It is now read-only.

Commit ff8a417

Browse files
ZarelScottFreeCode
authored andcommitted
Make sure mocha -h consistently shows help (mochajs#2746)
* Make sure `mocha -h` consistently shows help Previously, having options in `test/mocha.opts` would add those to `process.argv` to be passed to `program.parse`, which would make it stop showing help. Fixes mochajs#2745
1 parent 6711cc8 commit ff8a417

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

bin/options.js

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ module.exports = getOptions;
1717
*/
1818

1919
function getOptions () {
20+
if (process.argv.length === 3 && (process.argv[2] === '-h' || process.argv[2] === '--help')) {
21+
return;
22+
}
23+
2024
var optsPath = process.argv.indexOf('--opts') === -1
2125
? 'test/mocha.opts'
2226
: process.argv[process.argv.indexOf('--opts') + 1];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
foo

test/integration/helpers.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,34 @@ module.exports = {
110110
/**
111111
* regular expression used for splitting lines based on new line / dot symbol.
112112
*/
113-
splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+')
113+
splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+'),
114+
115+
/**
116+
* Invokes the mocha binary. Accepts an array of additional command line args
117+
* to pass. The callback is invoked with the exit code and output. Optional
118+
* current working directory as final parameter.
119+
*
120+
* In most cases runMocha should be used instead.
121+
*
122+
* Example response:
123+
* {
124+
* code: 1,
125+
* output: '...'
126+
* }
127+
*
128+
* @param {Array<string>} args - Extra args to mocha executable
129+
* @param {Function} done - Callback
130+
* @param {string} cwd - Current working directory for mocha run, optional
131+
*/
132+
invokeMocha: invokeMocha
114133
};
115134

116-
function invokeMocha (args, fn) {
135+
function invokeMocha (args, fn, cwd) {
117136
var output, mocha, listener;
118137

119138
output = '';
120-
args = [path.join('bin', 'mocha')].concat(args);
121-
mocha = spawn(process.execPath, args);
139+
args = [path.join(__dirname, '..', '..', 'bin', 'mocha')].concat(args);
140+
mocha = spawn(process.execPath, args, { cwd: cwd });
122141

123142
listener = function (data) {
124143
output += data;

test/integration/options.spec.js

+12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
'use strict';
22

3+
var path = require('path');
34
var assert = require('assert');
45
var run = require('./helpers').runMochaJSON;
6+
var directInvoke = require('./helpers').invokeMocha;
57
var args = [];
68

79
describe('options', function () {
@@ -386,4 +388,14 @@ describe('options', function () {
386388
it('should not force exit after root suite completion', runExit(false, 'disabled'));
387389
});
388390
});
391+
392+
describe('--help', function () {
393+
it('works despite the presence of mocha.opts', function (done) {
394+
directInvoke(['-h'], function (error, result) {
395+
if (error) { return done(error); }
396+
expect(result.output).to.contain('Usage:');
397+
done();
398+
}, path.join(__dirname, 'fixtures', 'options', 'help'));
399+
});
400+
});
389401
});

0 commit comments

Comments
 (0)