diff --git a/CHANGES.md b/CHANGES.md index 951d2e54..b381f4a0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ The released versions correspond to PyPI releases. ### Fixes * fixed a problem with module and session scoped fixtures in Python 3.13 (see [#1101](../../issues/1101)) +* fixed handling of `cwd` if set to a `pathlib.Path` (see [#1108](../../issues/1108)) ## [Version 5.7.3](https://pypi.python.org/pypi/pyfakefs/5.7.3) (2024-12-15) Fixes a regression in version 5.7.3. diff --git a/pyfakefs/fake_filesystem.py b/pyfakefs/fake_filesystem.py index fde46a68..efaa1781 100644 --- a/pyfakefs/fake_filesystem.py +++ b/pyfakefs/fake_filesystem.py @@ -346,7 +346,10 @@ def cwd(self, value: str) -> None: """Set the current working directory of the fake filesystem. Make sure a new drive or share is auto-mounted under Windows. """ - self._cwd = value + _cwd = make_string_path(value) + self._cwd = _cwd.replace( + matching_string(_cwd, os.sep), matching_string(_cwd, self.path_separator) + ) self._auto_mount_drive_if_needed(value) @property diff --git a/pyfakefs/tests/fake_filesystem_test.py b/pyfakefs/tests/fake_filesystem_test.py index 12c05b8f..96f7046e 100644 --- a/pyfakefs/tests/fake_filesystem_test.py +++ b/pyfakefs/tests/fake_filesystem_test.py @@ -17,6 +17,7 @@ import contextlib import errno import os +import pathlib import shutil import stat import sys @@ -231,6 +232,10 @@ def test_relative_path_forced_to_cwd(self): self.filesystem.cwd = "/foo" self.assertEqual("/foo/bar", self.filesystem.absnormpath(path)) + def test_cwd_from_pathlib_path(self): + self.filesystem.cwd = pathlib.Path("/foo/bar") + self.assertEqual("/foo/bar", self.filesystem.cwd) + def test_absolute_path_remains_unchanged(self): path = "foo/bar" self.assertEqual(self.root_name + path, self.filesystem.absnormpath(path))