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

Verify passed test path is a file before resolving #5317

Merged
merged 4 commits into from
Jan 15, 2018
Merged

Verify passed test path is a file before resolving #5317

merged 4 commits into from
Jan 15, 2018

Conversation

LINKIWI
Copy link
Contributor

@LINKIWI LINKIWI commented Jan 15, 2018

Summary

This PR fixes #5272 by fixing the regression introduced in #3882.

#3882 allows directly specifying file names as arguments to the jest CLI but this unintentionally breaks existing behavior when a test directory is passed as the sole argument to jest.

Fix details

Symptom: When *.test.js files are stored in a directory e.g. test, invoking jest test on v22.0.5+ results in the following:

 FAIL  ./test
  ● Test suite failed to run

    EISDIR: illegal operation on a directory, read
      
      at Object.readSync (node_modules/graceful-fs/polyfills.js:138:28)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        0.03s
Ran all test suites matching /test/i.

Root cause: The change allowing directly running tests by file name does not ensure that the passed argument is not a directory (or, more generally, that the passed argument is a file).

Remediation: Filter out all valid test paths that aren't files before continuing existing logic.

Test plan

The test added by #3882 was updated to also pass a directory as an argument to the CLI. We observe that no errors are thrown; without this change, the test would fatally throw with EISDIR as shown in the example symptom above.

Discussion

  1. Unclear if the performance impact by invoking fs.lstatSync on each valid test path is significant enough to explore alternatives.

  2. After poking around with this for a bit, I actually believe this regression creates a desired interface change. If the client wants to recursively search a directory for tests to run, he/she should explicitly pass option --testPathPattern as stated here as a workaround for the bug. Based on the changeset in Allow unmatched test paths from CLI #3882 it seems like the fact that invoking the CLI as jest dir-with-tests works seems to be a coincidental side effect.

But anyway, that is a breaking interface change, so this PR just tries to fix the regression as-is.

@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks!

const validTestPaths = paths && paths.filter(fs.existsSync);
const validTestPaths =
paths &&
paths.filter(fs.existsSync).filter(name => fs.lstatSync(name).isFile());
Copy link
Member

Choose a reason for hiding this comment

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

What about

    const validTestPaths =
        paths &&
        paths.filter(name => {
            try {
                return fs.lstatSync(name).isFile();
            } catch (e) {
                return false;
            }
        });

to at least save a single FS operation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call; fixed

@codecov-io
Copy link

codecov-io commented Jan 15, 2018

Codecov Report

Merging #5317 into master will decrease coverage by 0.02%.
The diff coverage is 0%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5317      +/-   ##
==========================================
- Coverage   61.27%   61.25%   -0.03%     
==========================================
  Files         205      205              
  Lines        6893     6896       +3     
  Branches        4        4              
==========================================
  Hits         4224     4224              
- Misses       2668     2671       +3     
  Partials        1        1
Impacted Files Coverage Δ
packages/jest-cli/src/search_source.js 40.5% <0%> (-1.6%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8b46fa8...281a589. Read the comment docs.

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

CHANGELOG.md Outdated
@@ -1,5 +1,12 @@
## master

## jest 22.1.1
Copy link
Member

Choose a reason for hiding this comment

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

Just remove this line, we'll add version number on publish

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

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

Thanks!

@cpojer cpojer merged commit d73328f into jestjs:master Jan 15, 2018
@LINKIWI LINKIWI deleted the test-dir-regression branch January 15, 2018 17:22
@alexilyaev
Copy link
Contributor

Ahm, is this already in v22.1.1? I'm still getting the error.

Looking at the file itself, it doesn't seem to have the changes:
https://github.com/facebook/jest/blob/v22.1.1/packages/jest-cli/src/search_source.js

But master does.
Is there going t be another release that includes this?

@LINKIWI
Copy link
Contributor Author

LINKIWI commented Jan 15, 2018

This changeset isn't included in the v22.1.1 release. It will probably be included in another patch release soon.

@alexilyaev
Copy link
Contributor

Oh I found my confusion, on the Release page it says "x commits to master since this tag" and I clicked it without reading, thinking those are the commits of this tag.

Looking forward for the next patch.

@SimenB
Copy link
Member

SimenB commented Jan 17, 2018

@LINKIWI @alexilyaev patch released

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

22.0.5 - passed folders to jest on CLI are not picked up
6 participants