Repository navigation
Speed up case sensitive is-file check in file system cache #10093
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| """Unit tests for file system cache.""" | ||
|
|
||
| import os | ||
| import shutil | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from typing import Optional | ||
|
|
||
| from mypy.fscache import FileSystemCache | ||
|
|
||
|
|
||
| class TestFileSystemCache(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| self.tempdir = tempfile.mkdtemp() | ||
| self.oldcwd = os.getcwd() | ||
| os.chdir(self.tempdir) | ||
| self.fscache = FileSystemCache() | ||
|
|
||
| def tearDown(self) -> None: | ||
| os.chdir(self.oldcwd) | ||
| shutil.rmtree(self.tempdir) | ||
|
|
||
| def test_isfile_case_1(self) -> None: | ||
| self.make_file('bar.py') | ||
| self.make_file('pkg/sub_package/__init__.py') | ||
| self.make_file('pkg/sub_package/foo.py') | ||
| # Run twice to test both cached and non-cached code paths. | ||
| for i in range(2): | ||
| assert self.isfile_case('bar.py') | ||
| assert self.isfile_case('pkg/sub_package/__init__.py') | ||
| assert self.isfile_case('pkg/sub_package/foo.py') | ||
| assert not self.isfile_case('non_existent.py') | ||
| assert not self.isfile_case('pkg/non_existent.py') | ||
| assert not self.isfile_case('pkg/') | ||
| assert not self.isfile_case('bar.py/') | ||
| for i in range(2): | ||
| assert not self.isfile_case('Bar.py') | ||
| assert not self.isfile_case('pkg/sub_package/__init__.PY') | ||
| assert not self.isfile_case('pkg/Sub_Package/foo.py') | ||
| assert not self.isfile_case('Pkg/sub_package/foo.py') | ||
|
|
||
| def test_isfile_case_2(self) -> None: | ||
| self.make_file('bar.py') | ||
| self.make_file('pkg/sub_package/__init__.py') | ||
| self.make_file('pkg/sub_package/foo.py') | ||
| # Run twice to test both cached and non-cached code paths. | ||
| # This reverses the order of checks from test_isfile_case_1. | ||
| for i in range(2): | ||
| assert not self.isfile_case('Bar.py') | ||
| assert not self.isfile_case('pkg/sub_package/__init__.PY') | ||
| assert not self.isfile_case('pkg/Sub_Package/foo.py') | ||
| assert not self.isfile_case('Pkg/sub_package/foo.py') | ||
| for i in range(2): | ||
| assert self.isfile_case('bar.py') | ||
| assert self.isfile_case('pkg/sub_package/__init__.py') | ||
| assert self.isfile_case('pkg/sub_package/foo.py') | ||
| assert not self.isfile_case('non_existent.py') | ||
| assert not self.isfile_case('pkg/non_existent.py') | ||
|
|
||
| def test_isfile_case_3(self) -> None: | ||
| self.make_file('bar.py') | ||
| self.make_file('pkg/sub_package/__init__.py') | ||
| self.make_file('pkg/sub_package/foo.py') | ||
| # Run twice to test both cached and non-cached code paths. | ||
| for i in range(2): | ||
| assert self.isfile_case('bar.py') | ||
| assert not self.isfile_case('non_existent.py') | ||
| assert not self.isfile_case('pkg/non_existent.py') | ||
| assert not self.isfile_case('Bar.py') | ||
| assert not self.isfile_case('pkg/sub_package/__init__.PY') | ||
| assert not self.isfile_case('pkg/Sub_Package/foo.py') | ||
| assert not self.isfile_case('Pkg/sub_package/foo.py') | ||
| assert self.isfile_case('pkg/sub_package/__init__.py') | ||
| assert self.isfile_case('pkg/sub_package/foo.py') | ||
|
|
||
| def test_isfile_case_other_directory(self) -> None: | ||
| self.make_file('bar.py') | ||
| with tempfile.TemporaryDirectory() as other: | ||
| self.make_file('other_dir.py', base=other) | ||
| self.make_file('pkg/other_dir.py', base=other) | ||
| assert self.isfile_case(os.path.join(other, 'other_dir.py')) | ||
| assert not self.isfile_case(os.path.join(other, 'Other_Dir.py')) | ||
| assert not self.isfile_case(os.path.join(other, 'bar.py')) | ||
| if sys.platform in ('win32', 'darwin'): | ||
| # We only check case for directories under our prefix, and since | ||
| # this path is not under the prefix, case difference is fine. | ||
| assert self.isfile_case(os.path.join(other, 'PKG/other_dir.py')) | ||
|
|
||
| def make_file(self, path: str, base: Optional[str] = None) -> None: | ||
| if base is None: | ||
| base = self.tempdir | ||
| fullpath = os.path.join(base, path) | ||
| os.makedirs(os.path.dirname(fullpath), exist_ok=True) | ||
| if not path.endswith('/'): | ||
| with open(fullpath, 'w') as f: | ||
| f.write('# test file') | ||
|
|
||
| def isfile_case(self, path: str) -> bool: | ||
| return self.fscache.isfile_case(os.path.join(self.tempdir, path), self.tempdir) | ||
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.
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.
Maybe add a third test case where you check the two intermingled? For example: