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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ The released versions correspond to PyPI releases.

### Fixes

* fixed `Path.resolve(strict=False)` incorrectly resolving Windows
drive-relative paths against the drive root instead of the current directory
(see [#1324](https://github.com/pytest-dev/pyfakefs/issues/1324))
* fixed a crash if the stack limit was set to a low value
(see [#1313](https://github.com/pytest-dev/pyfakefs/issues/1313))
* fixed `os.ftruncate` to inspect the `FakeFileWrapper` returned by
Expand Down
17 changes: 13 additions & 4 deletions pyfakefs/fake_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,13 +413,22 @@ def _join_real_path(
if self.filesystem.starts_with_root_path(rest):
if self.filesystem.is_windows_fs:
drive, rest = self.filesystem.splitdrive(rest)
if not drive:
if drive and not self.filesystem.starts_with_sep(rest):
cwd = matching_string(path, self.filesystem.cwd)
drive, _ = self.filesystem.splitdrive(cwd)
path = drive + sep
cwd_drive, _ = self.filesystem.splitdrive(cwd)
if drive.lower() == cwd_drive.lower():
path = cwd
else:
path = drive + sep
else:
if not drive:
cwd = matching_string(path, self.filesystem.cwd)
drive, _ = self.filesystem.splitdrive(cwd)
path = drive + sep
rest = rest[1:]
else:
path = sep
rest = rest[1:]
rest = rest[1:]

while rest:
name, _, rest = rest.partition(sep)
Expand Down
25 changes: 25 additions & 0 deletions pyfakefs/tests/fake_pathlib_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,31 @@ def test_resolve(self):
self.path(self.os.path.realpath(self.make_path("antoine", "setup.py"))),
)

def test_resolve_drive_relative_path(self):
# Regression test for #1324: a drive prefix does not imply a root path.
self.check_windows_only()
cwd = self.make_path("test")
self.create_dir(cwd)
self.create_file(self.os.path.join(cwd, "test_data", "dummy1.xml"))
self.os.chdir(cwd)
expected = self.os.path.join(cwd, "test", "test_data", "dummy1.xml")
drive = self.path.cwd().drive
for prefix in (drive.lower(), drive.upper()):
path = self.path(f"{prefix}test\\test_data/dummy1.xml")
self.assert_equal_paths(path.resolve(strict=False), expected)

def test_resolve_drive_relative_existing_file(self):
self.check_windows_only()
cwd = self.make_path("test")
self.create_dir(cwd)
expected = self.os.path.join(cwd, "test_data", "dummy1.xml")
self.create_file(expected)
self.os.chdir(cwd)
drive = self.path.cwd().drive
for prefix in (drive.lower(), drive.upper()):
path = self.path(f"{prefix}test_data/dummy1.xml")
self.assert_equal_paths(path.resolve(strict=False), expected)

def test_stat_file_in_unreadable_dir(self):
self.check_posix_only()
dir_path = self.make_path("some_dir")
Expand Down
Loading