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

Commit 6d3795d

Browse files
plroebuckboneskull
authored andcommitted
fix(cli): Throw error if unable to parse Mocha options file
Code now throws an error for any problems (except nonexistent default "test/mocha.opts"). Includes tests. Fixes mochajs#3363 Fixes mochajs#2576
1 parent 00d54aa commit 6d3795d

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

bin/options.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
const fs = require('fs');
8+
const path = require('path');
89

910
/**
1011
* Export `getOptions`.
@@ -13,13 +14,22 @@ const fs = require('fs');
1314
module.exports = getOptions;
1415

1516
/**
16-
* Default pathname for run-control file.
17+
* Default test directory.
1718
*
1819
* @constant
1920
* @type {string}
2021
* @default
2122
*/
22-
const defaultPathname = 'test/mocha.opts';
23+
const DEFAULT_TEST_DIRECTORY = 'test';
24+
25+
/**
26+
* Default filename of run-control file.
27+
*
28+
* @constant
29+
* @type {string}
30+
* @default
31+
*/
32+
const DEFAULT_OPTS_FILENAME = 'mocha.opts';
2333

2434
/**
2535
* Reads contents of the run-control file.
@@ -69,20 +79,32 @@ function getOptions() {
6979
return;
7080
}
7181

72-
const optsPath =
73-
process.argv.indexOf('--opts') === -1
74-
? defaultPathname
75-
: process.argv[process.argv.indexOf('--opts') + 1];
82+
const optsIndex = process.argv.indexOf('--opts');
83+
const optsPathSpecified = optsIndex !== -1;
84+
const defaultOptsPath = path.join(
85+
DEFAULT_TEST_DIRECTORY,
86+
DEFAULT_OPTS_FILENAME
87+
);
88+
const optsPath = optsPathSpecified
89+
? process.argv[optsIndex + 1]
90+
: defaultOptsPath;
7691

7792
try {
7893
const opts = parseOptions(readOptionsFile(optsPath));
7994

95+
if (opts.length > 0) {
8096
process.argv = process.argv
8197
.slice(0, 2)
8298
.concat(opts.concat(process.argv.slice(2)));
83-
} catch (ignore) {
84-
// NOTE: should console.error() and throw the error
99+
}
100+
} catch (err) {
101+
// Default options file may not exist - rethrow anything else
102+
if (optsPathSpecified || err.code !== 'ENOENT') {
103+
console.error(`failed to load Mocha options file: ${optsPath}`);
104+
throw err;
105+
}
106+
} finally {
107+
// Despite its name, signifies loading was attempted and should not be again
108+
process.env.LOADED_MOCHA_OPTS = '1';
85109
}
86-
87-
process.env.LOADED_MOCHA_OPTS = true;
88110
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
describe('opts', function () {
4+
it('should display this spec', function () {});
5+
});

test/integration/helpers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ function _spawnMochaWithListeners(args, fn, cwd) {
150150
}
151151

152152
function resolveFixturePath(fixture) {
153-
return path.join('./test/integration/fixtures', fixture);
153+
return path.join('test', 'integration', 'fixtures', fixture);
154154
}
155155

156156
function getSummary(res) {

test/integration/options.spec.js

+36
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,42 @@ describe('options', function() {
558558
});
559559
});
560560

561+
describe('--opts', function() {
562+
var testFile = path.join('options', 'opts.fixture.js');
563+
564+
it('works despite nonexistent default options file', function(done) {
565+
args = [];
566+
run(testFile, args, function(err, res) {
567+
if (err) {
568+
return done(err);
569+
}
570+
expect(res, 'to have passed').and('to have passed test count', 1);
571+
return done();
572+
});
573+
});
574+
575+
it('should throw an error due to nonexistent options file', function(done) {
576+
args = ['--opts', 'nosuchoptionsfile', testFile];
577+
directInvoke(
578+
args,
579+
function(err, res) {
580+
if (err) {
581+
return done(err);
582+
}
583+
expect(
584+
res.output,
585+
'to contain',
586+
'failed to load Mocha options file',
587+
'ENOENT:'
588+
);
589+
expect(res.code, 'to be', 1); // failed
590+
return done();
591+
},
592+
path.join(__dirname, 'fixtures')
593+
);
594+
});
595+
});
596+
561597
describe('--exclude', function() {
562598
/*
563599
* Runs mocha in {path} with the given args.

0 commit comments

Comments
 (0)