Skip to content
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
15 changes: 9 additions & 6 deletions upath/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1728,8 +1728,10 @@ def glob(
)
if self._relative_base is not None:
self = self.absolute()
path_pattern = self.joinpath(pattern).path
sep = self.parser.sep
# joinpath drops a trailing separator, which restricts matches to directories
dirs_only = sep if isinstance(pattern, str) and pattern.endswith(sep) else ""
path_pattern = self.joinpath(pattern).path + dirs_only
base = self.path
for name in self.fs.glob(path_pattern):
name = name.removeprefix(base).removeprefix(sep)
Expand Down Expand Up @@ -1761,18 +1763,19 @@ def rglob(
if _FSSPEC_HAS_WORKING_GLOB is None:
_check_fsspec_has_working_glob()

sep = self.parser.sep
# joinpath drops a trailing separator, which restricts matches to directories
dirs_only = sep if isinstance(pattern, str) and pattern.endswith(sep) else ""
if _FSSPEC_HAS_WORKING_GLOB:
r_path_pattern = self.joinpath("**", pattern).path
sep = self.parser.sep
r_path_pattern = self.joinpath("**", pattern).path + dirs_only
base = self.path
for name in self.fs.glob(r_path_pattern):
name = name.removeprefix(base).removeprefix(sep)
yield self.joinpath(name)

else:
path_pattern = self.joinpath(pattern).path
r_path_pattern = self.joinpath("**", pattern).path
sep = self.parser.sep
path_pattern = self.joinpath(pattern).path + dirs_only
r_path_pattern = self.joinpath("**", pattern).path + dirs_only
base = self.path
seen = set()
for p in (path_pattern, r_path_pattern):
Expand Down
5 changes: 3 additions & 2 deletions upath/tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ def test_expanduser(self):
(
"*.txt",
"*",
"*/",
pytest.param(
"**/*.txt",
marks=(
Expand Down Expand Up @@ -478,8 +479,8 @@ def test_read_text_errors(self):
content = upath.read_text(encoding="ascii", errors="strict")
assert content == "hello world"

def test_rglob(self, pathlib_base):
pattern = "*.txt"
@pytest.mark.parametrize("pattern", ["*.txt", "*/"])
def test_rglob(self, pathlib_base, pattern):
result = [*self.path.rglob(pattern)]
expected = [*pathlib_base.rglob(pattern)]
assert len(result) == len(expected)
Expand Down