Skip to content

Commit 8cdf797

Browse files
Fix directories not cleaned up after test
The `test_rmtree_errorhandler_reraises_error` test creates a directory that the user is unable to read under pytest's `tmpdir`. Once the test session end pytest will fail to clean this up due to permissions error and emit a warning like: /home/user/src/pip/.nox/test-3-12/lib/python3.12/site-packages/_pytest/pathlib.py:98: PytestWarning: (rm_rf) error removing /tmp/pytest-of-user/garbage-23e907a9-3e54-40bd-89e4-d70150b10f61/test_rmtree_errorhandler_rerai0 <class 'OSError'>: [Errno 39] Directory not empty: 'test_rmtree_errorhandler_rerai0' warnings.warn( This restores behaviour from b5c1c76, that commit mentions fixing 'failing tests' though I'm not sure which failures that addresses and if they were related to restoring the file permissions. Also make consistent use of `pathlib.Path` methods rather than mixing them with `os.` methods
1 parent 66f4a5d commit 8cdf797

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

news/d475652f-fd8b-4be8-8057-411601597bee.trivial.rst

Whitespace-only changes.

tests/unit/test_utils.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,10 @@ def test_rmtree_errorhandler_reraises_error(tmpdir: Path) -> None:
248248
by the given unreadable directory.
249249
"""
250250
# Create directory without read permission
251-
subdir_path = tmpdir / "subdir"
252-
subdir_path.mkdir()
253-
path = str(subdir_path)
254-
os.chmod(path, stat.S_IWRITE)
251+
path = tmpdir / "subdir"
252+
path.mkdir()
253+
old_mode = path.stat().st_mode
254+
path.chmod(stat.S_IWRITE)
255255

256256
mock_func = Mock()
257257

@@ -267,6 +267,9 @@ def test_rmtree_errorhandler_reraises_error(tmpdir: Path) -> None:
267267
rmtree_errorhandler(
268268
mock_func, path, sys.exc_info() # type: ignore[arg-type]
269269
)
270+
finally:
271+
# Restore permissions to let pytest to clean up temp dirs
272+
path.chmod(old_mode)
270273

271274
mock_func.assert_not_called()
272275

0 commit comments

Comments
 (0)