Skip to content

Commit

Permalink
Make sure mocha -h consistently shows help (#2746)
Browse files Browse the repository at this point in the history
* 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 #2745
  • Loading branch information
Zarel authored and ScottFreeCode committed Oct 18, 2017
1 parent 2d7e252 commit da901da
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions bin/options.js
Expand Up @@ -17,6 +17,10 @@ module.exports = getOptions;
*/

function getOptions () {
if (process.argv.length === 3 && (process.argv[2] === '-h' || process.argv[2] === '--help')) {
return;
}

var optsPath = process.argv.indexOf('--opts') === -1
? 'test/mocha.opts'
: process.argv[process.argv.indexOf('--opts') + 1];
Expand Down
1 change: 1 addition & 0 deletions test/integration/fixtures/options/help/test/mocha.opts
@@ -0,0 +1 @@
foo
27 changes: 23 additions & 4 deletions test/integration/helpers.js
Expand Up @@ -110,15 +110,34 @@ module.exports = {
/**
* regular expression used for splitting lines based on new line / dot symbol.
*/
splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+')
splitRegExp: new RegExp('[\\n' + baseReporter.symbols.dot + ']+'),

/**
* Invokes the mocha binary. Accepts an array of additional command line args
* to pass. The callback is invoked with the exit code and output. Optional
* current working directory as final parameter.
*
* In most cases runMocha should be used instead.
*
* Example response:
* {
* code: 1,
* output: '...'
* }
*
* @param {Array<string>} args - Extra args to mocha executable
* @param {Function} done - Callback
* @param {string} cwd - Current working directory for mocha run, optional
*/
invokeMocha: invokeMocha
};

function invokeMocha (args, fn) {
function invokeMocha (args, fn, cwd) {
var output, mocha, listener;

output = '';
args = [path.join('bin', 'mocha')].concat(args);
mocha = spawn(process.execPath, args);
args = [path.join(__dirname, '..', '..', 'bin', 'mocha')].concat(args);
mocha = spawn(process.execPath, args, { cwd: cwd });

listener = function (data) {
output += data;
Expand Down
12 changes: 12 additions & 0 deletions test/integration/options.spec.js
@@ -1,7 +1,9 @@
'use strict';

var path = require('path');
var assert = require('assert');
var run = require('./helpers').runMochaJSON;
var directInvoke = require('./helpers').invokeMocha;
var args = [];

describe('options', function () {
Expand Down Expand Up @@ -386,4 +388,14 @@ describe('options', function () {
it('should not force exit after root suite completion', runExit(false, 'disabled'));
});
});

describe('--help', function () {
it('works despite the presence of mocha.opts', function (done) {
directInvoke(['-h'], function (error, result) {
if (error) { return done(error); }
expect(result.output).to.contain('Usage:');
done();
}, path.join(__dirname, 'fixtures', 'options', 'help'));
});
});
});

0 comments on commit da901da

Please sign in to comment.