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: error handling for unexpected numeric arguments passed to cli #5263

Merged

Conversation

Dinika
Copy link
Contributor

@Dinika Dinika commented Nov 25, 2024

PR Checklist

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:

  • A numeric argument was passed to cli (such as mocha 123)
  • A numeric argument was passed as a value to a mocha option that does not accept numerical values (such as 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.

Copy link

linux-foundation-easycla bot commented Nov 25, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch from 63f9c9e to 28c801c Compare November 25, 2024 13:34
@Dinika Dinika changed the title Fix 5028 // Numerical arguments to cli throw uncaught error fix: error handling for unexpected numerical arguments passed to cli Nov 26, 2024
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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.

lib/cli/options.js Outdated Show resolved Hide resolved
lib/cli/options.js Outdated Show resolved Hide resolved
test/node-unit/cli/options.spec.js Show resolved Hide resolved
@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP - more information needed label Nov 27, 2024
@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch from b71034e to c370222 Compare November 28, 2024 18:41
@Dinika
Copy link
Contributor Author

Dinika commented Nov 29, 2024

@JoshuaKGoldberg Thanks so much for the review. I've revised the PR as per the suggestions.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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!

test/node-unit/cli/options.spec.js Outdated Show resolved Hide resolved
lib/cli/options.js Outdated Show resolved Hide resolved
@JoshuaKGoldberg
Copy link
Member

Btw, whenever you want a re-review, the re-request review button works.

@Dinika Dinika changed the title fix: error handling for unexpected numerical arguments passed to cli DRAFT // fix: error handling for unexpected numerical arguments passed to cli Dec 2, 2024
@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch 2 times, most recently from 86a9bd8 to c7905ff Compare December 3, 2024 09:38
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 <dinika@greyllama.cc>
Signed-off-by: Dinika Saxena <dinikasaxenas@gmail.com>
Signed-off-by: Dinika Saxena <dinikasaxenas@gmail.com>
@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch 5 times, most recently from 27e3a0c to 9ab6592 Compare December 3, 2024 09:51
@Dinika Dinika changed the title DRAFT // fix: error handling for unexpected numerical arguments passed to cli fix: error handling for unexpected numerical arguments passed to cli Dec 3, 2024
Signed-off-by: Dinika Saxena <dinikasaxenas@gmail.com>
@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch from 9ab6592 to 4e8ffce Compare December 3, 2024 10:34
@Dinika
Copy link
Contributor Author

Dinika commented Dec 3, 2024

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!

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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! 🚀

test/node-unit/utils.spec.js Show resolved Hide resolved
test/node-unit/cli/options.spec.js Outdated Show resolved Hide resolved
test/node-unit/cli/options.spec.js Outdated Show resolved Hide resolved
lib/cli/run-option-metadata.js Outdated Show resolved Hide resolved
test/node-unit/cli/mocha-flags.spec.js Outdated Show resolved Hide resolved
test/node-unit/cli/options.spec.js Show resolved Hide resolved
lib/cli/options.js Outdated Show resolved Hide resolved
lib/cli/options.js Show resolved Hide resolved
const nodeArgs = allArgs.reduce((acc, arg, index, allArgs) => {
const maybeFlag = allArgs[index - 1];

if (isNumeric(arg) && (!maybeFlag || !isMochaFlag(maybeFlag))) {
Copy link
Member

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?:

Suggested change
if (isNumeric(arg) && (!maybeFlag || !isMochaFlag(maybeFlag))) {
if (!maybeFlag || !isMochaFlag(maybeFlag)) {

Copy link
Contributor Author

@Dinika Dinika Dec 4, 2024

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?

Number(arg),
expectedTypeForFlag(maybeFlag)
);
}
Copy link
Member

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

Copy link
Contributor Author

@Dinika Dinika Dec 5, 2024

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.

Copy link
Member

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.

@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch from 12e7ac5 to 1deca6b Compare December 4, 2024 11:02
@Dinika Dinika changed the title fix: error handling for unexpected numerical arguments passed to cli fix: error handling for unexpected numeric arguments passed to cli Dec 4, 2024
@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch from 9cf5df0 to 5cc70f3 Compare December 5, 2024 11:55
…args

Signed-off-by: Dinika Saxena <dinikasaxenas@gmail.com>
@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch from 5cc70f3 to 912bdd1 Compare December 5, 2024 11:57
… readable values

Signed-off-by: Dinika Saxena <dinikasaxenas@gmail.com>
@Dinika Dinika force-pushed the issues-5028/fix-non-string-values-to-cli branch from 86e973e to 163dd91 Compare December 5, 2024 12:23
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a 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.

@JoshuaKGoldberg JoshuaKGoldberg removed the status: waiting for author waiting on response from OP - more information needed label Dec 6, 2024
@JoshuaKGoldberg
Copy link
Member

Kermit the Frog in a car turning to face the camera. Caption: "IT'S GO TIME"

@JoshuaKGoldberg JoshuaKGoldberg merged commit 210d658 into mochajs:main Dec 9, 2024
103 checks passed
@Dinika
Copy link
Contributor Author

Dinika commented Dec 11, 2024

Yayy!! Thanks for the thorough and quick review @JoshuaKGoldberg 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

TypeError: arg.split is not a function when passed a numerical argument
2 participants