From ff8a4172c0a0487fead6397dcfd580efea822831 Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Tue, 17 Oct 2017 22:48:38 -0500 Subject: [PATCH] Make sure `mocha -h` consistently shows help (#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 #2745 --- bin/options.js | 4 +++ .../fixtures/options/help/test/mocha.opts | 1 + test/integration/helpers.js | 27 ++++++++++++++++--- test/integration/options.spec.js | 12 +++++++++ 4 files changed, 40 insertions(+), 4 deletions(-) create mode 100644 test/integration/fixtures/options/help/test/mocha.opts diff --git a/bin/options.js b/bin/options.js index be5a60c9c3..7ee0eca8dc 100644 --- a/bin/options.js +++ b/bin/options.js @@ -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]; diff --git a/test/integration/fixtures/options/help/test/mocha.opts b/test/integration/fixtures/options/help/test/mocha.opts new file mode 100644 index 0000000000..257cc5642c --- /dev/null +++ b/test/integration/fixtures/options/help/test/mocha.opts @@ -0,0 +1 @@ +foo diff --git a/test/integration/helpers.js b/test/integration/helpers.js index 4f10921a3a..ec50116920 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -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} 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; diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js index 15a46ebf2d..f77da38bb8 100644 --- a/test/integration/options.spec.js +++ b/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 () { @@ -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')); + }); + }); });