Skip to content

Commit

Permalink
Fix PurePosixPath.joinpath under Windows
Browse files Browse the repository at this point in the history
- fixes #1070
  • Loading branch information
mrbean-bremen committed Oct 8, 2024
1 parent ed752e0 commit 68c0a42
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 16 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ The released versions correspond to PyPI releases.
(see [#1053](../../issues/1053))
* `PurePosixPath` reported Windows reserved names as reserved in Python >= 3.12
(see [#1067](../../issues/1067))
* `PurePosixPath.joinpath()` incorrectly handled paths with drives under Windows in Python >= 3.12
(see [#1070](../../issues/1070))

## [Version 5.6.0](https://pypi.python.org/pypi/pyfakefs/5.6.0) (2024-07-12)
Adds preliminary Python 3.13 support.
Expand Down
17 changes: 8 additions & 9 deletions pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,18 +1306,17 @@ def joinpaths(self, *paths: AnyStr) -> AnyStr:
return paths[0]
if self.is_windows_fs:
return self._join_paths_with_drive_support(*file_paths)
joined_path_segments = []
path = file_paths[0]
sep = self.get_path_separator(file_paths[0])
for path_segment in file_paths:
if self._starts_with_root_path(path_segment):
for path_segment in file_paths[1:]:
if path_segment.startswith(sep) or not path:
# An absolute path
joined_path_segments = [path_segment]
path = path_segment
elif path.endswith(sep):
path += path_segment
else:
if joined_path_segments and not joined_path_segments[-1].endswith(sep):
joined_path_segments.append(sep)
if path_segment:
joined_path_segments.append(path_segment)
return matching_string(file_paths[0], "").join(joined_path_segments)
path += sep + path_segment
return path

@overload
def _path_components(self, path: str) -> List[str]: ...
Expand Down
11 changes: 4 additions & 7 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,10 @@ def test_joinpath(self):
self.assertEqual(
self.path("/foo").joinpath("bar", "baz"), self.path("/foo/bar/baz")
)
if os.name != "nt":
# under Windows, this does not work correctly at the moment
# we get "C:/Program Files" instead
self.assertEqual(
self.path("c:").joinpath("/Program Files"),
self.path("/Program Files"),
)
self.assertEqual(
self.path("c:").joinpath("/Program Files"),
self.path("/Program Files"),
)

def test_match(self):
self.assertTrue(self.path("a/b.py").match("*.py"))
Expand Down

0 comments on commit 68c0a42

Please sign in to comment.