From 5f6394d3253e9109ae6aeef4a4b9e36cc06e6bc0 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Fri, 4 May 2018 15:09:38 -0500 Subject: [PATCH 1/4] fix(bin/options.js): Throw error if unable to parse Mocha options file Code now throws an error for any problems (except nonexistent default "test/mocha.opts"). Fixes #3363 --- bin/options.js | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/bin/options.js b/bin/options.js index 60cbcd705e..7586b53e38 100644 --- a/bin/options.js +++ b/bin/options.js @@ -24,10 +24,12 @@ function getOptions() { return; } - const optsPath = - process.argv.indexOf('--opts') === -1 - ? 'test/mocha.opts' - : process.argv[process.argv.indexOf('--opts') + 1]; + const optsIndex = process.argv.indexOf('--opts'); + const optsPathSpecified = optsIndex !== -1; + const defaultOptsPath = 'test/mocha.opts'; + const optsPath = optsPathSpecified + ? process.argv[optsIndex + 1] + : defaultOptsPath; try { const opts = fs @@ -37,12 +39,19 @@ function getOptions() { .filter(Boolean) .map(value => value.replace(/%20/g, ' ')); - process.argv = process.argv - .slice(0, 2) - .concat(opts.concat(process.argv.slice(2))); - } catch (ignore) { - // NOTE: should console.error() and throw the error + if (opts.length > 0) { + process.argv = process.argv + .slice(0, 2) + .concat(opts.concat(process.argv.slice(2))); + } + } catch (err) { + // Default options file may not exist - rethrow anything else + if (optsPathSpecified || err.code !== 'ENOENT') { + console.error(`failed to load Mocha options file: ${optsPath}`); + throw err; + } + } finally { + // Despite its name, signifies loading was attempted and should not be again + process.env.LOADED_MOCHA_OPTS = '1'; } - - process.env.LOADED_MOCHA_OPTS = true; } From d6a5c105ba0485b3a6e5dc68c311b9a883cf2554 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 5 May 2018 09:50:08 -0500 Subject: [PATCH 2/4] docs(docs/index.md): Update "mocha.opts" documentation Added information on file content, noting support for comment/blank lines. --- docs/index.md | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/docs/index.md b/docs/index.md index a5acfc9945..b56610e043 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1257,15 +1257,32 @@ The "HTML" reporter is what you see when running Mocha in the browser. It looks ## `mocha.opts` -Back on the server, Mocha will attempt to load `./test/mocha.opts` as a configuration file of sorts. The lines in this file are combined with any command-line arguments. The command-line arguments take precedence. For example, suppose you have the following `mocha.opts` file: +Back on the server, Mocha will attempt to load `"./test/mocha.opts"` as a +Run-Control file of sorts. + +Beginning-of-line comment support is available; any line _starting_ with a +hash (#) symbol will be considered a comment. Blank lines may also be used. +Any other line will be treated as a command-line argument (along with any +associated option value) to be used as a default setting. Settings should be +specified one per line. + +The lines in this file are prepended to any actual command-line arguments. +As such, actual command-line arguments will take precedence over the defaults. + +For example, suppose you have the following `mocha.opts` file: ```sh +# mocha.opts + --require should --reporter dot --ui bdd ``` -This will default the reporter to `dot`, require the `should` library, and use `bdd` as the interface. With this, you may then invoke `mocha` with additional arguments, here enabling [Growl](http://growl.info) support, and changing the reporter to `list`: +The settings above will default the reporter to `dot`, require the `should` +library, and use `bdd` as the interface. With this, you may then invoke `mocha` +with additional arguments, here enabling [Growl](http://growl.info/) support, +and changing the reporter to `list`: ```sh $ mocha --reporter list --growl From 4d0aaa1af495436741e1bee193a0b8fa94ed7541 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 5 May 2018 10:22:26 -0500 Subject: [PATCH 3/4] Revert "docs(docs/index.md): Update "mocha.opts" documentation" This reverts commit d6a5c105ba0485b3a6e5dc68c311b9a883cf2554. --- docs/index.md | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/docs/index.md b/docs/index.md index b56610e043..a5acfc9945 100644 --- a/docs/index.md +++ b/docs/index.md @@ -1257,32 +1257,15 @@ The "HTML" reporter is what you see when running Mocha in the browser. It looks ## `mocha.opts` -Back on the server, Mocha will attempt to load `"./test/mocha.opts"` as a -Run-Control file of sorts. - -Beginning-of-line comment support is available; any line _starting_ with a -hash (#) symbol will be considered a comment. Blank lines may also be used. -Any other line will be treated as a command-line argument (along with any -associated option value) to be used as a default setting. Settings should be -specified one per line. - -The lines in this file are prepended to any actual command-line arguments. -As such, actual command-line arguments will take precedence over the defaults. - -For example, suppose you have the following `mocha.opts` file: +Back on the server, Mocha will attempt to load `./test/mocha.opts` as a configuration file of sorts. The lines in this file are combined with any command-line arguments. The command-line arguments take precedence. For example, suppose you have the following `mocha.opts` file: ```sh -# mocha.opts - --require should --reporter dot --ui bdd ``` -The settings above will default the reporter to `dot`, require the `should` -library, and use `bdd` as the interface. With this, you may then invoke `mocha` -with additional arguments, here enabling [Growl](http://growl.info/) support, -and changing the reporter to `list`: +This will default the reporter to `dot`, require the `should` library, and use `bdd` as the interface. With this, you may then invoke `mocha` with additional arguments, here enabling [Growl](http://growl.info) support, and changing the reporter to `list`: ```sh $ mocha --reporter list --growl From e0b1aaa33a79161db0b03d6d47cafb48eeaddd86 Mon Sep 17 00:00:00 2001 From: Paul Roebuck Date: Sat, 12 May 2018 10:27:55 -0500 Subject: [PATCH 4/4] test(options.spec.js): Added tests for `--opts` cmdline option Added tests for `--opts` as none previously existed. Added "do nothing" fixture test. Minor change to helper routine so that it used `path.join()` properly. --- .../fixtures/options/opts.fixture.js | 6 ++++ test/integration/helpers.js | 2 +- test/integration/options.spec.js | 36 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/integration/fixtures/options/opts.fixture.js diff --git a/test/integration/fixtures/options/opts.fixture.js b/test/integration/fixtures/options/opts.fixture.js new file mode 100644 index 0000000000..e32d4125e0 --- /dev/null +++ b/test/integration/fixtures/options/opts.fixture.js @@ -0,0 +1,6 @@ +'use strict'; + +describe('opts', function () { + it('should display this spec', function () {}); +}); + diff --git a/test/integration/helpers.js b/test/integration/helpers.js index c0f61f7487..a0db124ea1 100644 --- a/test/integration/helpers.js +++ b/test/integration/helpers.js @@ -165,7 +165,7 @@ function invokeMocha(args, fn, cwd) { } function resolveFixturePath(fixture) { - return path.join('./test/integration/fixtures', fixture); + return path.join('test', 'integration', 'fixtures', fixture); } function getSummary(res) { diff --git a/test/integration/options.spec.js b/test/integration/options.spec.js index 3bb8ce8823..2c6c81570c 100644 --- a/test/integration/options.spec.js +++ b/test/integration/options.spec.js @@ -484,6 +484,42 @@ describe('options', function() { }); }); + describe('--opts', function() { + var testFile = path.join('options', 'opts.fixture.js'); + + it('works despite nonexistent default options file', function(done) { + args = []; + run(testFile, args, function(err, res) { + if (err) { + return done(err); + } + assert.equal(res.stats.pending, 0); + assert.equal(res.stats.passes, 1); + assert.equal(res.stats.failures, 0); + assert.equal(res.code, 0); + return done(); + }); + }); + + it('should throw an error due to nonexistent options file', function(done) { + args = ['--opts', 'nosuchoptionsfile', testFile]; + directInvoke( + args, + function(err, res) { + if (err) { + return done(err); + } + expect(res.output).to.contain('failed to load Mocha options file'); + expect(res.output).to.contain('ENOENT:'); + expect(res.code).to.be(1); + return done(); + }, + path.join(__dirname, 'fixtures') + ); + }); + + }); + describe('--exclude', function() { /* * Runs mocha in {path} with the given args.