Skip to content

Commit 3852d0f

Browse files
committed
Correctly handle setting cwd to a pathlib.Path
- fixes #1108
1 parent 023294a commit 3852d0f

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ The released versions correspond to PyPI releases.
1717
### Fixes
1818
* fixed a problem with module and session scoped fixtures in Python 3.13
1919
(see [#1101](../../issues/1101))
20+
* fixed handling of `cwd` if set to a `pathlib.Path` (see [#1108](../../issues/1108))
2021

2122
## [Version 5.7.3](https://pypi.python.org/pypi/pyfakefs/5.7.3) (2024-12-15)
2223
Fixes a regression in version 5.7.3.

pyfakefs/fake_filesystem.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,10 @@ def cwd(self, value: str) -> None:
346346
"""Set the current working directory of the fake filesystem.
347347
Make sure a new drive or share is auto-mounted under Windows.
348348
"""
349-
self._cwd = value
349+
_cwd = make_string_path(value)
350+
self._cwd = _cwd.replace(
351+
matching_string(_cwd, os.sep), matching_string(_cwd, self.path_separator)
352+
)
350353
self._auto_mount_drive_if_needed(value)
351354

352355
@property

pyfakefs/tests/fake_filesystem_test.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import contextlib
1818
import errno
1919
import os
20+
import pathlib
2021
import shutil
2122
import stat
2223
import sys
@@ -231,6 +232,10 @@ def test_relative_path_forced_to_cwd(self):
231232
self.filesystem.cwd = "/foo"
232233
self.assertEqual("/foo/bar", self.filesystem.absnormpath(path))
233234

235+
def test_cwd_from_pathlib_path(self):
236+
self.filesystem.cwd = pathlib.Path("/foo/bar")
237+
self.assertEqual("/foo/bar", self.filesystem.cwd)
238+
234239
def test_absolute_path_remains_unchanged(self):
235240
path = "foo/bar"
236241
self.assertEqual(self.root_name + path, self.filesystem.absnormpath(path))

0 commit comments

Comments
 (0)