Skip to content

Commit

Permalink
Restore original permissions in unit tests to allow pytest to clean up (
Browse files Browse the repository at this point in the history
#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(
  • Loading branch information
ichard26 authored Jul 13, 2024
1 parent 3228d76 commit 76daeb0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 13 deletions.
Empty file.
15 changes: 14 additions & 1 deletion tests/lib/filesystem.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
"""

import os
from contextlib import contextmanager
from functools import partial
from itertools import chain
from typing import Iterator, List, Set
from pathlib import Path
from typing import Iterator, List, Set, Union


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

return set(chain.from_iterable(join(*dirinfo) for dirinfo in os.walk(base)))


@contextmanager
def chmod(path: Union[str, Path], mode: int) -> Iterator[None]:
"""Contextmanager to temporarily update a path's mode."""
old_mode = os.stat(path).st_mode
try:
os.chmod(path, mode)
yield
finally:
os.chmod(path, old_mode)
22 changes: 10 additions & 12 deletions tests/unit/test_network_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pip._vendor.cachecontrol.caches import FileCache

from pip._internal.network.cache import SafeFileCache
from tests.lib.filesystem import chmod


@pytest.fixture(scope="function")
Expand Down Expand Up @@ -57,26 +58,23 @@ def test_cache_roundtrip_body(self, cache_tmpdir: Path) -> None:
def test_safe_get_no_perms(
self, cache_tmpdir: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
os.chmod(cache_tmpdir, 000)

monkeypatch.setattr(os.path, "exists", lambda x: True)

cache = SafeFileCache(os.fspath(cache_tmpdir))
cache.get("foo")
with chmod(cache_tmpdir, 000):
cache = SafeFileCache(os.fspath(cache_tmpdir))
cache.get("foo")

@pytest.mark.skipif("sys.platform == 'win32'")
def test_safe_set_no_perms(self, cache_tmpdir: Path) -> None:
os.chmod(cache_tmpdir, 000)

cache = SafeFileCache(os.fspath(cache_tmpdir))
cache.set("foo", b"bar")
with chmod(cache_tmpdir, 000):
cache = SafeFileCache(os.fspath(cache_tmpdir))
cache.set("foo", b"bar")

@pytest.mark.skipif("sys.platform == 'win32'")
def test_safe_delete_no_perms(self, cache_tmpdir: Path) -> None:
os.chmod(cache_tmpdir, 000)

cache = SafeFileCache(os.fspath(cache_tmpdir))
cache.delete("foo")
with chmod(cache_tmpdir, 000):
cache = SafeFileCache(os.fspath(cache_tmpdir))
cache.delete("foo")

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

0 comments on commit 76daeb0

Please sign in to comment.