Skip to content

Commit

Permalink
Fix problem with shutil.rmtree if emulating Windows under POSIX
Browse files Browse the repository at this point in the history
- the fd functions that are used for rmtree are not available under Windows
-> switched of the usage of these function in the fake fs
- fixes pytest-dev#979
  • Loading branch information
mrbean-bremen committed Mar 14, 2024
1 parent 67e3a56 commit 909690d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ The released versions correspond to PyPI releases.
* fixed handling of `dirfd` in `os.symlink` (see [#968](../../issues/968))
* add missing `follow_symlink` argument to `os.link` (see [#973](../../issues/973))
* fixed handling of missing attribute in `os.getxattr` (see [#971](../../issues/971))
* fixed permission problem with `shutil.rmtree` if emulating Windows under POSIX
(see [#979](../../issues/979))

### Enhancements
* added support for `O_NOFOLLOW` and `O_DIRECTORY` flags in `os.open`
Expand Down
7 changes: 7 additions & 0 deletions pyfakefs/fake_filesystem_shutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ def __init__(self, filesystem):
self.filesystem = filesystem
self._shutil_module = shutil

@property
def _use_fd_functions(self):
print("_use_fd_functions")
if self.filesystem.is_windows_fs:
return False
return self._shutil_module._use_fd_functions

def disk_usage(self, path):
"""Return the total, used and free disk space in bytes as named tuple
or placeholder holder values simulating unlimited space if not set.
Expand Down
4 changes: 4 additions & 0 deletions pyfakefs/fake_filesystem_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,10 @@ def setUp(self, doctester: Any = None) -> None:
if self.has_fcopy_file:
shutil._HAS_FCOPYFILE = False # type: ignore[attr-defined]

# do not use the fd functions, as they may not be available in the target OS
if hasattr(shutil, "_use_fd_functions"):
shutil._use_fd_functions = False # type: ignore[module-attr]

with warnings.catch_warnings():
# ignore warnings, see #542 and #614
warnings.filterwarnings("ignore")
Expand Down
9 changes: 9 additions & 0 deletions pyfakefs/tests/fake_filesystem_shutil_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ def error_handler(_, path, _error_info):
self.assertFalse(NonLocal.errorHandled)
self.assertEqual(NonLocal.errorPath, "")

def test_rmtree_in_windows(self):
# regression test for #979
self.check_windows_only()
base_path = self.make_path("foo", "bar")
self.os.makedirs(self.os.path.join(base_path, "res"))
self.assertTrue(self.os.path.exists(base_path))
shutil.rmtree(base_path)
self.assertFalse(self.os.path.exists(base_path))

def test_copy(self):
src_file = self.make_path("xyzzy")
dst_file = self.make_path("xyzzy_copy")
Expand Down

0 comments on commit 909690d

Please sign in to comment.