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

Fix glob pattern support: no comma splitting for 'spec' and 'ignore' options #4315

Merged
merged 1 commit into from Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/cli/options.js
Expand Up @@ -54,16 +54,19 @@ const configuration = Object.assign({}, YARGS_PARSER_CONFIG, {

/**
* This is a really fancy way to:
* - ensure unique values for `array`-type options
* - use its array's last element for `boolean`/`number`/`string`- options given multiple times
* - `array`-type options: ensure unique values and evtl. split comma-delimited lists
* - `boolean`/`number`/`string`- options: use last element when given multiple times
* This is passed as the `coerce` option to `yargs-parser`
* @private
* @ignore
*/
const globOptions = ['spec', 'ignore'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking, is exclude option alias'd into ignore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but vice versa. ignore is the canonical and exclude the alias option name. Yargs-parser applies the coerce function automatically to both names.

const coerceOpts = Object.assign(
types.array.reduce(
(acc, arg) =>
Object.assign(acc, {[arg]: v => Array.from(new Set(list(v)))}),
Object.assign(acc, {
[arg]: v => Array.from(new Set(globOptions.includes(arg) ? v : list(v)))
}),
{}
),
types.boolean
Expand Down
39 changes: 36 additions & 3 deletions test/node-unit/cli/options.spec.js
Expand Up @@ -562,7 +562,9 @@ describe('options', function() {
readFileSync = sandbox.stub();
readFileSync.onFirstCall().throws();
findConfig = sandbox.stub().returns('/some/.mocharc.json');
loadConfig = sandbox.stub().returns({spec: '*.spec.js'});
loadConfig = sandbox
.stub()
.returns({spec: '{dirA,dirB}/**/*.spec.js'});
findupSync = sandbox.stub();
loadOptions = proxyLoadOptions({
readFileSync,
Expand All @@ -573,10 +575,41 @@ describe('options', function() {
result = loadOptions(['*.test.js']);
});

it('should place both into the positional arguments array', function() {
expect(result, 'to have property', '_', ['*.test.js', '*.spec.js']);
it('should place both - unsplitted - into the positional arguments array', function() {
expect(result, 'to have property', '_', [
'*.test.js',
'{dirA,dirB}/**/*.spec.js'
]);
});
});
});

describe('"ignore" handling', function() {
let result;

beforeEach(function() {
readFileSync = sandbox.stub();
readFileSync.onFirstCall().throws();
findConfig = sandbox.stub().returns('/some/.mocharc.json');
loadConfig = sandbox
.stub()
.returns({ignore: '{dirA,dirB}/**/*.spec.js'});
findupSync = sandbox.stub();
loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});
result = loadOptions(['--ignore', '*.test.js']);
});

it('should not split option values by comma', function() {
expect(result, 'to have property', 'ignore', [
'*.test.js',
'{dirA,dirB}/**/*.spec.js'
]);
});
});
});
});