Skip to content

Commit

Permalink
Fix 5028 // Numerical arguments to cli throw uncaught error
Browse files Browse the repository at this point in the history
This PR throws a custom error which is (hopefully) easier to understand
when:

i. a numerical argument is passed to mocha cli
ii. numerical value is used as a value for one of the mocha flags that is not compatible with
numerical values.

Signed-off-by: Dinika Saxena <[email protected]>
  • Loading branch information
Dinika committed Nov 25, 2024
1 parent 43c3157 commit 28c801c
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 14 deletions.
23 changes: 13 additions & 10 deletions lib/cli/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,19 @@ const parse = (args = [], defaultValues = {}, ...configObjects) => {
// 4. we can then reapply the values after yargs-parser is done.
const nodeArgs = (Array.isArray(args) ? args : args.split(' ')).reduce(
(acc, arg) => {
const pair = arg.split('=');
let flag = pair[0];
if (isNodeFlag(flag, false)) {
flag = flag.replace(/^--?/, '');
return arg.includes('=')
? acc.concat([[flag, pair[1]]])
: acc.concat([[flag, true]]);
try {
const pair = arg.split('=');
let flag = pair[0];
if (isNodeFlag(flag, false)) {
flag = flag.replace(/^--?/, '');
return arg.includes('=')
? acc.concat([[flag, pair[1]]])
: acc.concat([[flag, true]]);
}
return acc;
} catch (err) {
throw new Error(`Invalid option ${arg} passed to mocha cli`);
}
return acc;
},
[]
);
Expand Down Expand Up @@ -192,8 +196,7 @@ const loadPkgRc = (args = {}) => {
filepath
);
} else {
debug('failed to read default package.json at %s; ignoring',
filepath);
debug('failed to read default package.json at %s; ignoring', filepath);
return result;
}
}
Expand Down
49 changes: 45 additions & 4 deletions test/node-unit/cli/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,7 @@ describe('options', function () {
const filepath = '/some/package.json';
readFileSync = sinon.stub();
// package.json
readFileSync
.onFirstCall()
.returns('{definitely-invalid');
readFileSync.onFirstCall().returns('{definitely-invalid');
findConfig = sinon.stub().returns('/some/.mocharc.json');
loadConfig = sinon.stub().returns({});
findupSync = sinon.stub().returns(filepath);
Expand All @@ -224,7 +222,7 @@ describe('options', function () {
loadOptions();
},
'to throw',
/SyntaxError/,
/SyntaxError/
);
});
});
Expand Down Expand Up @@ -676,5 +674,48 @@ describe('options', function () {
]);
});
});

describe('"numerical arguments"', function () {
const numericalArg = 123;

beforeEach(function () {
readFileSync = sinon.stub();
findConfig = sinon.stub();
loadConfig = sinon.stub();
findupSync = sinon.stub();
loadOptions = proxyLoadOptions({
readFileSync,
findConfig,
loadConfig,
findupSync
});
});

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

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

it('does not throw error if numerical value is passed to a compatible mocha flag', function () {
expect(() => loadOptions(`--retries ${numericalArg}`), 'not to throw');
});

it('does not throw error if numerical value is passed to a node options', function () {
expect(
() =>
loadOptions(
`--secure-heap-min=${numericalArg} --conditions=${numericalArg}`
),
'not to throw'
);
});
});
});
});

0 comments on commit 28c801c

Please sign in to comment.