Skip to content

fscache: handle trailing slash in exists_case prefix #16772

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mypy/fscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def exists_case(self, path: str, prefix: str) -> bool:
if path in self.exists_case_cache:
return self.exists_case_cache[path]
head, tail = os.path.split(path)
prefix = prefix.rstrip(os.sep)
if not head.startswith(prefix) or not tail:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hm, sort of pre-existing, but I guess stripping is a little problematic for startswith, e.g. path=/foobar/baz, prefix=/foo. I guess it just means there ends up being another listdir

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yes, didn't think about that before. That's indeed pre-existing, but changed the condition a bit to catch that as well.

# Only perform the check for paths under prefix.
self.exists_case_cache[path] = True
Expand Down
15 changes: 15 additions & 0 deletions mypy/test/testfscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ def test_isfile_case_other_directory(self) -> None:
# this path is not under the prefix, case difference is fine.
assert self.isfile_case(os.path.join(other, "PKG/other_dir.py"))

def test_exists_case_1(self) -> None:
self.make_file("foo/bar/baz.py")
# Run twice to test both cached and non-cached code paths.
for i in range(2):
assert self.exists_case("foo/bar/baz.py", "foo")
assert self.exists_case("foo/bar", "foo")
assert not self.exists_case("foo/bar/non_existent1.py", "foo")
assert not self.exists_case("foo/bar/not_a_dir", "foo")
assert not self.exists_case("not_a_dir/not_a_subdir", "not_a_dir/")

def make_file(self, path: str, base: str | None = None) -> None:
if base is None:
base = self.tempdir
Expand All @@ -99,3 +109,8 @@ def make_file(self, path: str, base: str | None = None) -> None:

def isfile_case(self, path: str) -> bool:
return self.fscache.isfile_case(os.path.join(self.tempdir, path), self.tempdir)

def exists_case(self, path: str, prefix: str) -> bool:
return self.fscache.exists_case(
os.path.join(self.tempdir, path), os.path.join(self.tempdir, prefix)
)