Skip to content

Commit

Permalink
Correctly handle setting cwd to a pathlib.Path
Browse files Browse the repository at this point in the history
- fixes #1108
  • Loading branch information
mrbean-bremen committed Jan 8, 2025
1 parent 023294a commit 3852d0f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 4 additions & 1 deletion pyfakefs/fake_filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions pyfakefs/tests/fake_filesystem_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import contextlib
import errno
import os
import pathlib
import shutil
import stat
import sys
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit 3852d0f

Please sign in to comment.