Skip to content

Commit

Permalink
fix broken positional arguments in config; ensure positional args are…
Browse files Browse the repository at this point in the history
… unique; closes #3763

Signed-off-by: Christopher Hiller <boneskull@boneskull.com>
  • Loading branch information
boneskull committed Feb 25, 2019
1 parent 6535965 commit acd82c3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
6 changes: 6 additions & 0 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,15 @@ const loadOptions = (argv = []) => {

if (rcConfig) {
args.config = false;
args._ = args._.concat(rcConfig._ || []);
}
if (pkgConfig) {
args.package = false;
args._ = args._.concat(pkgConfig._ || []);
}
if (optsConfig) {
args.opts = false;
args._ = args._.concat(optsConfig._ || []);
}

args = parse(
Expand All @@ -324,6 +327,9 @@ const loadOptions = (argv = []) => {
delete args.spec;
}

// make unique
args._ = Array.from(new Set(args._));

return args;
};

Expand Down
58 changes: 41 additions & 17 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,31 +130,52 @@ describe('options', function() {

describe('when path to mocha.opts is unspecified', function() {
let result;

beforeEach(function() {
const filepath = '/path/to/mocha.opts';
readFileSync = sandbox.stub();
readFileSync.onFirstCall().returns('{}');
readFileSync.onSecondCall().returns('{}');
readFileSync.onThirdCall().throws();
// package.json
readFileSync.onFirstCall().throws();
// mocha.opts
readFileSync.onSecondCall().returns('--retries 3 foobar.spec.js');
findConfig = sandbox.stub().returns('/some/.mocharc.json');
loadConfig = sandbox.stub().returns({});
findupSync = sandbox.stub().returns('/some/package.json');

loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});
result = loadOptions(['--opts', filepath]);
});

result = loadOptions();
it('should return merged options incl. mocha.opts', function() {
expect(
result,
'to equal',
Object.assign(
{
_: ['foobar.spec.js']
},
defaults,
{
config: false,
opts: false,
package: false,
retries: 3
}
)
);
});

it('should attempt to load default mocha.opts', function() {
expect(readFileSync, 'to have a call satisfying', [
defaults.opts,
'utf8'
]);
it('should have attempted to load two files', function() {
expect(readFileSync, 'was called times', 2).and(
'to have calls satisfying',
[
{args: ['/some/package.json', 'utf8']},
{args: ['/path/to/mocha.opts', 'utf8']}
]
);
});

it('should set opts = false', function() {
Expand All @@ -165,7 +186,6 @@ describe('options', function() {

describe('when path to mocha.opts (`--opts <path>`) is valid', function() {
let result;

beforeEach(function() {
const filepath = '/path/to/mocha.opts';
readFileSync = sandbox.stub();
Expand Down Expand Up @@ -353,7 +373,9 @@ describe('options', function() {
const filepath = '/some/package.json';
readFileSync = sandbox.stub();
// package.json
readFileSync.onFirstCall().returns('{"mocha": {"retries": 3}}');
readFileSync
.onFirstCall()
.returns('{"mocha": {"retries": 3, "_": ["foobar.spec.js"]}}');
// mocha.opts
readFileSync.onSecondCall().throws();
findConfig = sandbox.stub().returns('/some/.mocharc.json');
Expand All @@ -374,7 +396,7 @@ describe('options', function() {
'to equal',
Object.assign(
{
_: []
_: ['foobar.spec.js']
},
defaults,
{
Expand Down Expand Up @@ -443,8 +465,10 @@ describe('options', function() {
readFileSync = sandbox.stub();
readFileSync
.onFirstCall()
.returns('{"mocha": {"check-leaks": true}}');
readFileSync.onSecondCall().returns('--retries 3');
.returns(
'{"mocha": {"check-leaks": true, "_": ["foobar.spec.js"]}}'
);
readFileSync.onSecondCall().returns('--retries 3 foobar.spec.js');
findConfig = sandbox.stub();
loadConfig = sandbox.stub();
findupSync = sandbox.stub().returns('/some/package.json');
Expand All @@ -463,7 +487,7 @@ describe('options', function() {
expect(
result,
'to equal',
Object.assign({_: []}, defaults, {
Object.assign({_: ['foobar.spec.js']}, defaults, {
'check-leaks': true,
config: false,
opts: false,
Expand Down

0 comments on commit acd82c3

Please sign in to comment.