-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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: error handling for unexpected numeric arguments passed to cli #5263
fix: error handling for unexpected numeric arguments passed to cli #5263
Conversation
63f9c9e
to
28c801c
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting this started! 🙌 The unit tests in particular are lovely.
Requesting changes on a different strategy for detecting invalid args.
b71034e
to
c370222
Compare
@JoshuaKGoldberg Thanks so much for the review. I've revised the PR as per the suggestions. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks great, thank you! The approach seems very reasonable and straightforward. Just requesting changes on including a bit more information in the thrown error.
The [Style] nit is definitely not blocking if you want to assertively say that it's numerical, not numeric 😄
Thanks!
Btw, whenever you want a re-review, the re-request review button works. |
86a9bd8
to
c7905ff
Compare
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]>
Signed-off-by: Dinika Saxena <[email protected]>
Signed-off-by: Dinika Saxena <[email protected]>
27e3a0c
to
9ab6592
Compare
Signed-off-by: Dinika Saxena <[email protected]>
9ab6592
to
4e8ffce
Compare
I do not have permissions to check why the "netlify/mocha/deploy-preview" job is failing. Lemme know if there's something I need/can to do to fix it. Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow this really ballooned in scope! On the one hand, I feel bad that what looked like a really small-scoped good first issue is turning into a lot more work. On the other hand, the implementation is looking great and is touching on some edge cases Mocha doesn't yet handle well. So this is looking like a really great new feature addition! 🚀
lib/cli/options.js
Outdated
const nodeArgs = allArgs.reduce((acc, arg, index, allArgs) => { | ||
const maybeFlag = allArgs[index - 1]; | ||
|
||
if (isNumeric(arg) && (!maybeFlag || !isMochaFlag(maybeFlag))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Question] Why isNumeric(arg) &&
? Wouldn't we want to check this for other string flags too?
Unless I'm missing something, could this be trimmed down to?:
if (isNumeric(arg) && (!maybeFlag || !isMochaFlag(maybeFlag))) { | |
if (!maybeFlag || !isMochaFlag(maybeFlag)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I think one reason why I am only handling numeric args is because all other types of arguments don't really make mocha crash (summary in the comment below). They are converted to positional string arguments, mocha tries to find files matching those strings and if it doesn't find those files, it prints a warning.
Example: mocha --global var1,var2 var3 true false my-test.spec.js
prints this warning and carries on testing my-test.spec.js
:
Warning: Cannot find any files matching pattern "var3"
Warning: Cannot find any files matching pattern "true"
Warning: Cannot find any files matching pattern "false"
Do you think I should instead throw an error in these cases?
lib/cli/options.js
Outdated
Number(arg), | ||
expectedTypeForFlag(maybeFlag) | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Feature] This check seems legitimate and good. I like it. But, it feels odd to me that it's only implemented for expected numeric types. There are booleans and strings too. I think it'd be weird as a user to only get this report for an incorrect number, but not incorrect booleans or strings.
What do you think about generalizing it to all argument types?
I.e. instead of:
if numeric and expected_type !== 'number':
throw
Doing roughly:
if actual_type !== expected_type:
throw
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on this and the previous comment, I've summarized the current behaviour of mocha for different flags and values data-type.
Columns show flags and rows show values passed to flags.
Red dot 🔴 is placed on cells where mocha currently crashes without a good error message. So far, this PR has addressed only these cases.
Orange dot 🟠 is placed where the behaviour could be improved perhaps. Mocha does not crash in these situations. Lemme know if I should improve the behaviour in this PR.
Value / Flag | No flags | number ⚑ | boolean ⚑ | array ⚑ | string ⚑ |
---|---|---|---|---|---|
number value |
🔴 Eg: mocha 123 - Mocha crashes with error "arg.split()" |
Eg: mocha --retries 123 - 123 is assigned to retries |
🔴 Eg: mocha --delay 123 : delay is assigned value "true". 123 is treated as positional arg. Mocha crashes with error "arg.split()". Mocha does not accept filenames with only numeric chars |
Eg: mocha --file 123 : assigns value "123" to the flag. Appropriate error/warning is thrown if flag can't handle value (eg. file not found). |
Eg: mocha --grep 123 assigns "123" to flag. Appropriate action is taken. |
boolean value |
Eg: mocha true : "true" is treated like a positional value for filename. |
Eg: mocha --retries true : "NaN" is assigned to flag. This value is then ignored. |
Eg: mocha --delay true : works same as mocha --delay . true is assigned to flag. |
Eg mocha --spec true : stringified "true" is added to array of flag values. Appropriate action is taken. |
Eg mocha --ui true . Stringified "true" is assigned to flag. |
array value |
Eg: mocha v1,v2 : "v1,v2" is treated like a positional value for a filename |
🟠 Eg: mocha --retries v1,v2 "NaN" is assigned to flag. This value is then ignored. |
Eg: mocha --delay v1,v2 : value true is assigned to flag. "v1,v2" is treated like a positional filename. |
Eg: mocha --spec v1,v2 Flag has value ["v1", "v2"] |
Eg: mocha --grep v1,v2 String "v1,v2" is assigned to flag |
string value |
Eg: mocha abc : "abc" is treated like a positional value for a filename. |
🟠 Eg: mocha --retries abc : "NaN" is assigned to flag. This value is then ignored. |
Eg: mocha --delay abc : value true is assigned to flag. "abc" is treated like positional value for a filename. |
Eg: mocha --spec abc . "abc" is added to array of flag values. |
Eg: mocha --grep abc : Works as expected. |
No value | - | Eg: mocha --retries . Error Not enough arguments following: retries is thrown |
Eg: mocha --delay : Flag has value true |
Eg: mocha --spec Error Not enough arguments following: spec is thrown |
Eg: mocha grep : Error Not enough arguments following: grep is thrown |
Based on the table above, only numeric values (or flags) seem to be causing the problem right now. That being said, I don't mind creating a more generic error handling approach. Lemme know if you prefer that - I'll make the changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What a great table 😄. Very helpful. I think the 🟠 ones can be ignored for now, agreed.
Signed-off-by: Dinika Saxena <[email protected]>
12e7ac5
to
1deca6b
Compare
9cf5df0
to
5cc70f3
Compare
…args Signed-off-by: Dinika Saxena <[email protected]>
5cc70f3
to
912bdd1
Compare
… readable values Signed-off-by: Dinika Saxena <[email protected]>
86e973e
to
163dd91
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Wonderful! No more notes from me, this is all looking great. Thanks!
Since it's a nontrivial change, leaving it open for a bit in case others in @mochajs/maintenance-crew have time to review.
Yayy!! Thanks for the thorough and quick review @JoshuaKGoldberg 😊 |
PR Checklist
TypeError: arg.split is not a function
when passed a numerical argument #5028status: accepting prs
Overview
This PR adds error handling for
TypeError: arg.split is not a function
which is thrown when an unexpected numerical argument is passed to mocha cli. Hopefully the thrown error message is clearer to understand (I'm open to suggestions to further improve it).This custom error is thrown in following cases:
mocha 123
)mocha --delay 123
)The error is not thrown in if a numerical value is passed to mocha flag which can handle numerical values (such as
mocha --retries 2
) or if numerical value is passed to node options.PS - Apologies for posting this PR before receiving a reply to this comment I posted in the issue.