Skip to content
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

3363: Throw error if unable to parse Mocha options file #3381

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions bin/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

const fs = require('fs');
const path = require('path');

/**
* Export `getOptions`.
Expand All @@ -13,13 +14,22 @@ const fs = require('fs');
module.exports = getOptions;

/**
* Default pathname for run-control file.
* Default test directory.
*
* @constant
* @type {string}
* @default
*/
const defaultPathname = 'test/mocha.opts';
const DEFAULT_TEST_DIRECTORY = 'test';

/**
* Default filename of run-control file.
*
* @constant
* @type {string}
* @default
*/
const DEFAULT_OPTS_FILENAME = 'mocha.opts';

/**
* Reads contents of the run-control file.
Expand Down Expand Up @@ -69,20 +79,32 @@ function getOptions() {
return;
}

const optsPath =
process.argv.indexOf('--opts') === -1
? defaultPathname
: process.argv[process.argv.indexOf('--opts') + 1];
const optsIndex = process.argv.indexOf('--opts');
const optsPathSpecified = optsIndex !== -1;
const defaultOptsPath = path.join(
DEFAULT_TEST_DIRECTORY,
DEFAULT_OPTS_FILENAME
);
const optsPath = optsPathSpecified
? process.argv[optsIndex + 1]
: defaultOptsPath;

try {
const opts = parseOptions(readOptionsFile(optsPath));

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;
}
5 changes: 5 additions & 0 deletions test/integration/fixtures/options/opts.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

describe('opts', function () {
it('should display this spec', function () {});
});
2 changes: 1 addition & 1 deletion test/integration/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ function _spawnMochaWithListeners(args, fn, cwd) {
}

function resolveFixturePath(fixture) {
return path.join('./test/integration/fixtures', fixture);
return path.join('test', 'integration', 'fixtures', fixture);
}

function getSummary(res) {
Expand Down
36 changes: 36 additions & 0 deletions test/integration/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,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);
}
expect(res, 'to have passed').and('to have passed test count', 1);
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',
'ENOENT:'
);
expect(res.code, 'to be', 1); // failed
return done();
},
path.join(__dirname, 'fixtures')
);
});
});

describe('--exclude', function() {
/*
* Runs mocha in {path} with the given args.
Expand Down