Skip to content

Commit

Permalink
Find flag for faulty numeric args before yargs parser
Browse files Browse the repository at this point in the history
Signed-off-by: Dinika Saxena <[email protected]>
  • Loading branch information
Dinika committed Dec 3, 2024
1 parent a8ea4dd commit e8f0ec0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 25 deletions.
24 changes: 20 additions & 4 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,31 @@ const nargOpts = types.array
const parse = (args = [], defaultValues = {}, ...configObjects) => {
// save node-specific args for special handling.
// 1. when these args have a "=" they should be considered to have values
// 2. if they don't, they just boolean flags
// 2. if they don't, they are just boolean flags
// 3. to avoid explicitly defining the set of them, we tell yargs-parser they
// are ALL boolean flags.
// 4. we can then reapply the values after yargs-parser is done.
const allArgs = Array.isArray(args) ? args : args.split(' ');
const nodeArgs = allArgs.reduce((acc, arg) => {
if (typeof arg !== 'string') {
throw new Error(`Invalid option ${arg} passed to mocha cli`);
const nodeArgs = allArgs.reduce((acc, arg, index, allArgs) => {
const maybeFlag = allArgs[index - 1];

if (isNumeric(arg) && (!maybeFlag || !isMochaFlag(maybeFlag))) {
throw createUnsupportedError(
`Option ${arg} is unsupported by the mocha cli`
);
}
if (
isNumeric(arg) &&
isMochaFlag(maybeFlag) &&
expectedTypeForFlag(maybeFlag) !== 'number'
) {
throw createInvalidArgumentTypeError(
`Mocha flag '--${maybeFlag}' given invalid option: '${arg}'`,
Number(arg),
expectedTypeForFlag(maybeFlag)
);
}

const pair = arg.split('=');
let flag = pair[0];
if (isNodeFlag(flag, false)) {
Expand Down
13 changes: 2 additions & 11 deletions lib/cli/run-option-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,8 @@ const TYPES = (exports.types = {
'sort',
'watch'
],
number: ['retries', 'jobs'],
string: [
'config',
'fgrep',
'grep',
'package',
'reporter',
'ui',
'slow',
'timeout'
]
number: ['retries', 'jobs', 'slow', 'timeout'],
string: ['config', 'fgrep', 'grep', 'package', 'reporter', 'ui']
});

/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"test-coverage-clean": "rimraf .nyc_output coverage",
"test-coverage-generate": "nyc report --reporter=lcov --reporter=text",
"test-node-run-only": "nyc --no-clean --reporter=json node bin/mocha.js",
"test-node-run": "nyc --no-clean --reporter=json node bin/mocha.js --forbid-only",
"test-node-run": "nyc --no-clean --reporter=json node bin/mocha.js",
"test-node:integration": "run-s clean build && npm run -s test-node-run -- --parallel --timeout 10000 --slow 3750 \"test/integration/**/*.spec.js\"",
"test-node:interfaces:bdd": "npm run -s test-node-run -- --ui bdd test/interfaces/bdd.spec",
"test-node:interfaces:exports": "npm run -s test-node-run -- --ui exports test/interfaces/exports.spec",
Expand Down
27 changes: 27 additions & 0 deletions test/node-unit/cli/mocha-flags.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

const {
types,
expectedTypeForFlag
} = require('../../../lib/cli/run-option-metadata');

describe('mocha-flags', function () {
describe('expectedTypeForFlag()', function () {
it('returns expected type for all mocha flags', function () {
Object.entries(types).forEach(([dataType, flags]) => {
flags.forEach(flag => {
expect(expectedTypeForFlag(flag), 'to equal', dataType);
});
});
});

it('returns undefined for node flags', function () {
expect(expectedTypeForFlag('--throw-deprecation'), 'to equal', undefined);
expect(expectedTypeForFlag('throw-deprecation'), 'to equal', undefined);
});

it('returns undefined for unsupported flags', function () {
expect(expectedTypeForFlag('--foo'), 'to equal', undefined);
});
});
});
71 changes: 62 additions & 9 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const sinon = require('sinon');
const rewiremock = require('rewiremock/node');
const {ONE_AND_DONE_ARGS} = require('../../../lib/cli/one-and-dones');
const {constants} = require('../../../lib/errors');

const modulePath = require.resolve('../../../lib/cli/options');
const mocharcPath = require.resolve('../../../lib/mocharc.json');
Expand Down Expand Up @@ -524,7 +525,7 @@ describe('options', function () {
loadOptions('--timeout 500'),
'to have property',
'timeout',
'500'
500
);
});

Expand Down Expand Up @@ -678,6 +679,23 @@ describe('options', function () {
describe('"numeric arguments"', function () {
const numericArg = 123;

const unsupportedError = arg => {
return {
message: `Option ${arg} is unsupported by the mocha cli`,
code: constants.UNSUPPORTED
};
};

const invalidArgError = (flag, arg, expectedType = 'string') => {
return {
message: `Mocha flag '--${flag}' given invalid option: '${arg}'`,
code: constants.INVALID_ARG_TYPE,
argument: arg,
actual: 'number',
expected: expectedType
};
};

beforeEach(function () {
readFileSync = sinon.stub();
findConfig = sinon.stub();
Expand All @@ -691,16 +709,51 @@ describe('options', function () {
});
});

it('throws error when numeric option is passed to cli', function () {
expect(() => loadOptions(`${numericArg}`), 'to throw', {
message: `Invalid option ${numericArg} passed to mocha cli`
});
it('throws UNSUPPORTED error when numeric option is passed to cli', function () {
expect(
() => loadOptions(`${numericArg}`),
'to throw',
unsupportedError(numericArg)
);
});

it('throws error when numeric argument is passed to mocha flag that does not accept numeric value', function () {
expect(() => loadOptions(`--delay ${numericArg}`), 'to throw', {
message: `Invalid option ${numericArg} passed to mocha cli`
});
it('throws INVALID_ARG_TYPE error when numeric argument is passed to mocha flag that does not accept numeric value', function () {
const flag = '--delay';
expect(
() => loadOptions(`${flag} ${numericArg}`),
'to throw',
invalidArgError(flag, numericArg, 'boolean')
);
});

it('throws INVALID_ARG_TYPE error when incompatible flag does not have preceding "--"', function () {
const flag = 'delay';
expect(
() => loadOptions(`${flag} ${numericArg}`),
'to throw',
invalidArgError(flag, numericArg, 'boolean')
);
});

it('shows correct flag in error when multiple mocha flags have numeric values', function () {
const flag = '--delay';
expect(
() =>
loadOptions(
`--timeout ${numericArg} ${flag} ${numericArg} --retries ${numericArg}`
),
'to throw',
invalidArgError(flag, numericArg, 'boolean')
);
});

it('throws UNSUPPORTED error when numeric arg is passed to unsupported flag', function () {
const invalidFlag = 'foo';
expect(
() => loadOptions(`${invalidFlag} ${numericArg}`),
'to throw',
unsupportedError(numericArg)
);
});

it('does not throw error if numeric value is passed to a compatible mocha flag', function () {
Expand Down
11 changes: 11 additions & 0 deletions test/node-unit/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,16 @@ describe('utils', function () {
);
});
});
describe('isNumeric()', function () {
it('returns true for a number type', function () {
expect(utils.isNumeric(42), 'to equal', true);
});
it('returns true for a string that can be parsed as a number', function () {
expect(utils.isNumeric('42'), 'to equal', true);
});
it('returns false for a string that cannot be parsed as a number', function () {
expect(utils.isNumeric('foo'), 'to equal', false);
});
});
});
});

0 comments on commit e8f0ec0

Please sign in to comment.