Skip to content

fix: Add error handling for nonexistent file case with --file option#5086

Merged
JoshuaKGoldberg merged 29 commits intomochajs:mainfrom
khoaHyh:issue/4047
Jun 25, 2024
Merged

fix: Add error handling for nonexistent file case with --file option#5086
JoshuaKGoldberg merged 29 commits intomochajs:mainfrom
khoaHyh:issue/4047

Conversation

@khoaHyh
Copy link
Copy Markdown
Contributor

@khoaHyh khoaHyh commented Jan 24, 2024

🔎 Overview

Fixes #4047

  • Added validation to the file(s) passed in to the --file option by resolving the argument to an absolute path and check for its existence. We now log a warning and exit if the file does not exist.
    • the errors for --require bubble up to the middleware in lib/cli/run.js. See here. This specific error occurs in lib/cli/run-helpers.js in the singleRun method when we load files asynchronously. Since we were lacking error handling there, the errors appeared the way they previously did before these changes.
  • Added a test to assert our changes

Screenshot from 2024-02-07 00-34-30

- added error handling when using the --file flag to do it the way
  --require does
- added a test to assert that we throw the same type of error
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Jan 24, 2024

CLA Signed

The committers listed above are authorized under a signed CLA.

@khoaHyh khoaHyh marked this pull request as ready for review January 24, 2024 06:57
@khoaHyh khoaHyh changed the title ISSUE #4047: Add error handling for nonexistent file case with --file option feat: Add error handling for nonexistent file case with --file option Jan 24, 2024
- require.resolve() by Node.js follows a Node.js module resolution algo
  which includes checking if the resolved path actually exists on the
  file system.
@coveralls
Copy link
Copy Markdown

coveralls commented Feb 7, 2024

Coverage Status

coverage: 94.257% (-0.1%) from 94.358%
when pulling 1a9f155 on khoaHyh:issue/4047
into 472a8be on mochajs:master.

Copy link
Copy Markdown
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.

Cool! 😎

Requesting changes on adding a bit more testing. But also let's talk about the direction a bit - I feel nervous adding in process.exit/throws deep within code. Thanks for sending!

@JoshuaKGoldberg JoshuaKGoldberg added the status: waiting for author waiting on response from OP or other posters - more information needed label Mar 4, 2024
@khoaHyh khoaHyh requested a review from JoshuaKGoldberg March 24, 2024 23:30
Copy link
Copy Markdown
Contributor Author

@khoaHyh khoaHyh left a comment

Choose a reason for hiding this comment

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

@JoshuaKGoldberg tagging for re-review just in case this disappeared into the backlog abyss 😅

@JoshuaKGoldberg JoshuaKGoldberg changed the title feat: Add error handling for nonexistent file case with --file option fix: Add error handling for nonexistent file case with --file option Jun 25, 2024
Copy link
Copy Markdown
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.

Looks great, thanks for the fix!

David Levinson in Independence Day performing a checkmate in chess and saying "Checkmate"

As before, the deploy preview failure is unrelated. It's 429 errors from us running more CI jobs than the service is used to.

@JoshuaKGoldberg
Copy link
Copy Markdown
Member

Note: I switched this to a fix: as it's more of a fix around improper inputs than a fully fledged feature. It doesn't specifically need a minor version bump. A patch would suffice.

@JoshuaKGoldberg JoshuaKGoldberg merged commit dbe229d into mochajs:main Jun 25, 2024
@voxpelli
Copy link
Copy Markdown
Member

Thanks for stepping in and merging @JoshuaKGoldberg 🙏 🙂

lennonnikolas pushed a commit to lennonnikolas/mocha that referenced this pull request Jan 24, 2026
…ochajs#5086)

* feat: handle nonexistent files passed to --file

- added error handling when using the --file flag to do it the way
  --require does
- added a test to assert that we throw the same type of error

* refactor: remove path.resolve()

- require.resolve() by Node.js follows a Node.js module resolution algo
  which includes checking if the resolved path actually exists on the
  file system.

* add comment to new code in collect-files.js

* fix: add back absolute path resolving

* refactor: log warning and remove call stack

* revert changes to bin/mocha.js

* improve test case

* throw error and have handler work with it

* change collectFiles to return object

* clean up

* exit mocha immediately on missing file

* new log message

* add tests

* code quality improvements

* add comments

* docs: update to new link name

* pass mocha instance to helper function

---------

Co-authored-by: Pelle Wessman <pelle@kodfabrik.se>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

semver-patch implementation requires increase of "patch" version number; "bug fixes"

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🚀 Feature: Better error for --file option with non-existent file

4 participants