Skip to content

Commit 76daeb0

Browse files
authored
Restore original permissions in unit tests to allow pytest to clean up (#12847)
This is necessary to address warnings like these: /home/ichard26/dev/oss/pip/venv/lib/python3.12/site-packages/_pytest/pathlib.py:95: PytestWarning: (rm_rf) error removing /tmp-ram/pytest-of-ichard26/garbage-ae7b86cb-23ec-453e-a72e-fd0b70d07ba2/test_safe_delete_no_perms0 <class 'OSError'>: [Errno 39] Directory not empty: '/tmp-ram/pytest-of-ichard26/garbage-ae7b86cb-23ec-453e-a72e-fd0b70d07ba2/test_safe_delete_no_perms0' warnings.warn( /home/ichard26/dev/oss/pip/venv/lib/python3.12/site-packages/_pytest/pathlib.py:95: PytestWarning: (rm_rf) error removing /tmp-ram/pytest-of-ichard26/garbage-ae7b86cb-23ec-453e-a72e-fd0b70d07ba2/test_safe_set_no_perms0 <class 'OSError'>: [Errno 39] Directory not empty: '/tmp-ram/pytest-of-ichard26/garbage-ae7b86cb-23ec-453e-a72e-fd0b70d07ba2/test_safe_set_no_perms0' warnings.warn( /home/ichard26/dev/oss/pip/venv/lib/python3.12/site-packages/_pytest/pathlib.py:95: PytestWarning: (rm_rf) error removing /tmp-ram/pytest-of-ichard26/garbage-ae7b86cb-23ec-453e-a72e-fd0b70d07ba2/test_safe_get_no_perms0 <class 'OSError'>: [Errno 39] Directory not empty: '/tmp-ram/pytest-of-ichard26/garbage-ae7b86cb-23ec-453e-a72e-fd0b70d07ba2/test_safe_get_no_perms0' warnings.warn(
1 parent 3228d76 commit 76daeb0

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

news/d0281e66-f6f9-4fb6-ac44-b5a9d468d42b.trivial.rst

Whitespace-only changes.

tests/lib/filesystem.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
"""
33

44
import os
5+
from contextlib import contextmanager
56
from functools import partial
67
from itertools import chain
7-
from typing import Iterator, List, Set
8+
from pathlib import Path
9+
from typing import Iterator, List, Set, Union
810

911

1012
def get_filelist(base: str) -> Set[str]:
@@ -17,3 +19,14 @@ def join(dirpath: str, dirnames: List[str], filenames: List[str]) -> Iterator[st
1719
)
1820

1921
return set(chain.from_iterable(join(*dirinfo) for dirinfo in os.walk(base)))
22+
23+
24+
@contextmanager
25+
def chmod(path: Union[str, Path], mode: int) -> Iterator[None]:
26+
"""Contextmanager to temporarily update a path's mode."""
27+
old_mode = os.stat(path).st_mode
28+
try:
29+
os.chmod(path, mode)
30+
yield
31+
finally:
32+
os.chmod(path, old_mode)

tests/unit/test_network_cache.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pip._vendor.cachecontrol.caches import FileCache
88

99
from pip._internal.network.cache import SafeFileCache
10+
from tests.lib.filesystem import chmod
1011

1112

1213
@pytest.fixture(scope="function")
@@ -57,26 +58,23 @@ def test_cache_roundtrip_body(self, cache_tmpdir: Path) -> None:
5758
def test_safe_get_no_perms(
5859
self, cache_tmpdir: Path, monkeypatch: pytest.MonkeyPatch
5960
) -> None:
60-
os.chmod(cache_tmpdir, 000)
61-
6261
monkeypatch.setattr(os.path, "exists", lambda x: True)
6362

64-
cache = SafeFileCache(os.fspath(cache_tmpdir))
65-
cache.get("foo")
63+
with chmod(cache_tmpdir, 000):
64+
cache = SafeFileCache(os.fspath(cache_tmpdir))
65+
cache.get("foo")
6666

6767
@pytest.mark.skipif("sys.platform == 'win32'")
6868
def test_safe_set_no_perms(self, cache_tmpdir: Path) -> None:
69-
os.chmod(cache_tmpdir, 000)
70-
71-
cache = SafeFileCache(os.fspath(cache_tmpdir))
72-
cache.set("foo", b"bar")
69+
with chmod(cache_tmpdir, 000):
70+
cache = SafeFileCache(os.fspath(cache_tmpdir))
71+
cache.set("foo", b"bar")
7372

7473
@pytest.mark.skipif("sys.platform == 'win32'")
7574
def test_safe_delete_no_perms(self, cache_tmpdir: Path) -> None:
76-
os.chmod(cache_tmpdir, 000)
77-
78-
cache = SafeFileCache(os.fspath(cache_tmpdir))
79-
cache.delete("foo")
75+
with chmod(cache_tmpdir, 000):
76+
cache = SafeFileCache(os.fspath(cache_tmpdir))
77+
cache.delete("foo")
8078

8179
def test_cache_hashes_are_same(self, cache_tmpdir: Path) -> None:
8280
cache = SafeFileCache(os.fspath(cache_tmpdir))

0 commit comments

Comments
 (0)