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

Do not silently fail on bad opts argument #2716

Closed
wants to merge 1 commit into from
Closed

Do not silently fail on bad opts argument #2716

wants to merge 1 commit into from

Conversation

samhh
Copy link

@samhh samhh commented Feb 16, 2017

For #2576.

@jsf-clabot
Copy link

jsf-clabot commented Feb 16, 2017

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.06%) to 88.125% when pulling a257b6b on SamHH:master into a2fc76c on mochajs:master.

@samhh
Copy link
Author

samhh commented Feb 16, 2017

Ah, missed the tests. If we want to cover that if (shouldThrowError) block, is this the right file for that? https://github.com/mochajs/mocha/blob/master/test/integration/options.spec.js

@ScottFreeCode
Copy link
Contributor

(DISCLAIMER: the following is a thought on an impression after taking one look at the change and not thoroughly investigating the code. It would need at least as much review as the PR itself.)

Could this be equivalently fixed by removing the try-catch altogether and replacing it with only executing the contents of the try block if either the opts file was specified or the default opts file exists? I suppose that would make it also throw an error if there's an opts file but it can't be parsed or anything like that, but frankly, I think that would be a similar improvement to throwing an error when the specified file is missing. I wasn't around when this code was created, so I don't know for sure what was intended, but at a glance it looks as though try-catch is being used as a lazy hack to avoid error on the default file not existing instead of just checking for the file's existence, and I would point to it swallowing real errors as a perfect example of why try-catch should not be used that way in the first place... Adding more complex logic to determine when to rethrow the error after all just seems like the tail wagging the dog, unless I'm missing some legit rationale for suppressing errors other than "default file does not exist" (which I'm pretty sure could be properly tested instead).

@ScottFreeCode
Copy link
Contributor

Re. tests -- that file does look like it's the place where commandline arguments (of which --opts is one) are tested, yes. In fact, even if we don't merge anything for this fix, we could use --opts tests added there, since there don't appear to be any...

@samhh
Copy link
Author

samhh commented Feb 17, 2017

I see what you mean, I do tend to steer clear of try/catch blocks in my own work. Figured though that for my first PR for a big project like mocha I should keep it adhering to what's already there haha. I can certainly try my hand at dropping the try/catch if consensus agrees that's the way to go. In which case, how far down in Node versions does Mocha support? Asking so I know what modern JS I can('t) use.

@ScottFreeCode
Copy link
Contributor

@mochajs/core Any opinions on try-catch here?

Re. Node versions, I believe we're testing all the supported versions on Travis (so... looks like Node 0.10?), if I recall correct.

@boneskull
Copy link
Member

I agree with @ScottFreeCode; let's just let it throw.

var optsPath = optsIndex === -1
? defaultPath
: process.argv[optsIndex + 1];
var shouldThrowError = optsIndex !== -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename this to wasOptsProvided. It makes the code / reasoning more clearer imo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind the feedback in case the variable is no longer necessary.

@dasilvacontin
Copy link
Contributor

@ScottFreeCode @boneskull Yeah, but how do you check if the file exists otherwise? (to get the default one, if it exists). fileExists and others have been deprecated. I recall not being able to check for file existence without using a method that throws. But maybe I'm wrong.

@ScottFreeCode
Copy link
Contributor

Hmm... according to the docs: "Note that fs.exists() is deprecated, but fs.existsSync() is not." https://nodejs.org/dist/latest-v8.x/docs/api/fs.html#fs_fs_existssync_path

@boneskull
Copy link
Member

you can use fs.stat() in place of fs.exists()

@ScottFreeCode
Copy link
Contributor

So, fs.stat if asynchronous and fs.existsSync if synchronous?

@boneskull
Copy link
Member

@ScottFreeCode ...apparently? Either way, checking file existence is usually an anti-pattern

@ScottFreeCode
Copy link
Contributor

Usually; although using try-catch for program logic rather than just error handling is also an antipattern unless there's no good alternative... I guess it comes down to whether we're more concerned by the race condition that the file could be deleted in the split instant between the existence check and the attempt to use it, resulting in a clear error that doesn't recur on subsequent runs, or by the risk that we screw up the try-catch handling, resulting in things like this issue about errors being silently (!) suppressed that should not have been suppressed at all.

@stale
Copy link

stale bot commented Oct 6, 2017

I am a bot that watches issues for inactivity.
This issue hasn't had any recent activity, and I'm labeling it stale. In 14 days, if there are no further comments or activity, I will close this issue.
Thanks for contributing to Mocha!

@stale stale bot added the stale this has been inactive for a while... label Oct 6, 2017
@stale stale bot closed this Oct 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale this has been inactive for a while...
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants