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

fscache: handle trailing slash in exists_case prefix #16772

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion mypy/fscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ 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)
if not head.startswith(prefix) or not tail:
prefix = prefix.rstrip(os.sep)
if (prefix != "" and not (head.startswith(prefix + os.sep) or head == prefix)) or not tail:
# Only perform the check for paths under prefix.
self.exists_case_cache[path] = True
return True
Expand Down
19 changes: 19 additions & 0 deletions mypy/test/testfscache.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ 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(os.path.join("foo", "bar", "baz.py"))
# Run twice to test both cached and non-cached code paths.
for i in range(2):
assert self.exists_case(os.path.join("foo", "bar", "baz.py"), "foo")
assert self.exists_case(os.path.join("foo", "bar"), "foo")
assert self.exists_case(os.path.join("foo", "bar", "non_existent1.py"), "bar")
assert self.exists_case(os.path.join("foobar", "non_existent2.py"), "foo")
assert not self.exists_case(os.path.join("foo", "bar", "non_existent3.py"), "foo")
assert not self.exists_case(os.path.join("foo", "bar", "not_a_dir"), "foo")
assert not self.exists_case(
os.path.join("not_a_dir", "not_a_subdir"), "not_a_dir" + os.sep
)

def make_file(self, path: str, base: str | None = None) -> None:
if base is None:
base = self.tempdir
Expand All @@ -99,3 +113,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)
)