Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Changes from 5 commits
74c13fd
26dd073
6c33877
a8ea4dd
4e8ffce
662a04e
09574d7
1deca6b
8f727b3
f894ed1
19bd2a4
912bdd1
163dd91
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
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?:
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 testingmy-test.spec.js
:Do you think I should instead throw an error in these cases?
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:
Doing roughly:
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.
number
valuemocha 123
- Mocha crashes with error "arg.split()"mocha --retries 123
- 123 is assigned toretries
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 charsmocha --file 123
: assigns value "123" to the flag. Appropriate error/warning is thrown if flag can't handle value (eg. file not found).mocha --grep 123
assigns "123" to flag. Appropriate action is taken.boolean
valuemocha true
: "true" is treated like a positional value for filename.mocha --retries true
: "NaN" is assigned to flag. This value is then ignored.mocha --delay true
: works same asmocha --delay
. true is assigned to flag.mocha --spec true
: stringified "true" is added to array of flag values. Appropriate action is taken.mocha --ui true
. Stringified "true" is assigned to flag.array
valuemocha v1,v2
: "v1,v2" is treated like a positional value for a filenamemocha --retries v1,v2
"NaN" is assigned to flag. This value is then ignored.mocha --delay v1,v2
: value true is assigned to flag. "v1,v2" is treated like a positional filename.mocha --spec v1,v2
Flag has value ["v1", "v2"]mocha --grep v1,v2
String "v1,v2" is assigned to flagstring
valuemocha abc
: "abc" is treated like a positional value for a filename.mocha --retries abc
: "NaN" is assigned to flag. This value is then ignored.mocha --delay abc
: valuetrue
is assigned to flag. "abc" is treated like positional value for a filename.mocha --spec abc
. "abc" is added to array of flag values.mocha --grep abc
: Works as expected.mocha --retries
. ErrorNot enough arguments following: retries
is thrownmocha --delay
: Flag has valuetrue
mocha --spec
ErrorNot enough arguments following: spec
is thrownmocha grep
: ErrorNot enough arguments following: grep
is thrownBased 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.