1
1
Fork 0
mirror of https://github.com/pypa/pip synced 2023-12-13 21:30:23 +01:00

Improve code reuse in TestSafeFileCache

Use a fixture creating a temporary directory for cache.
This commit is contained in:
ekristina 2019-05-04 17:11:44 -04:00
parent f8732ac06d
commit 2e5b4461f4
2 changed files with 19 additions and 19 deletions

1
news/6322.trivial Normal file
View file

@ -0,0 +1 @@
Use a fixture creating a temporary directory to improve code reuse in TestSafeFileCache (tests/unit/test_download.py)

View file

@ -20,6 +20,13 @@ from pip._internal.utils.hashes import Hashes
from tests.lib import create_file from tests.lib import create_file
@pytest.fixture(scope="function")
def cache_tmpdir(tmpdir):
cache_dir = tmpdir.join("cache")
cache_dir.makedirs()
yield cache_dir
def test_unpack_http_url_with_urllib_response_without_content_type(data): def test_unpack_http_url_with_urllib_response_without_content_type(data):
""" """
It should download and unpack files even if no Content-Type header exists It should download and unpack files even if no Content-Type header exists
@ -310,11 +317,9 @@ class TestSafeFileCache:
os.geteuid which is absent on Windows. os.geteuid which is absent on Windows.
""" """
def test_cache_roundtrip(self, tmpdir): def test_cache_roundtrip(self, cache_tmpdir):
cache_dir = tmpdir.join("test-cache")
cache_dir.makedirs()
cache = SafeFileCache(cache_dir) cache = SafeFileCache(cache_tmpdir)
assert cache.get("test key") is None assert cache.get("test key") is None
cache.set("test key", b"a test string") cache.set("test key", b"a test string")
assert cache.get("test key") == b"a test string" assert cache.get("test key") == b"a test string"
@ -322,32 +327,26 @@ class TestSafeFileCache:
assert cache.get("test key") is None assert cache.get("test key") is None
@pytest.mark.skipif("sys.platform == 'win32'") @pytest.mark.skipif("sys.platform == 'win32'")
def test_safe_get_no_perms(self, tmpdir, monkeypatch): def test_safe_get_no_perms(self, cache_tmpdir, monkeypatch):
cache_dir = tmpdir.join("unreadable-cache") os.chmod(cache_tmpdir, 000)
cache_dir.makedirs()
os.chmod(cache_dir, 000)
monkeypatch.setattr(os.path, "exists", lambda x: True) monkeypatch.setattr(os.path, "exists", lambda x: True)
cache = SafeFileCache(cache_dir) cache = SafeFileCache(cache_tmpdir)
cache.get("foo") cache.get("foo")
@pytest.mark.skipif("sys.platform == 'win32'") @pytest.mark.skipif("sys.platform == 'win32'")
def test_safe_set_no_perms(self, tmpdir): def test_safe_set_no_perms(self, cache_tmpdir):
cache_dir = tmpdir.join("unreadable-cache") os.chmod(cache_tmpdir, 000)
cache_dir.makedirs()
os.chmod(cache_dir, 000)
cache = SafeFileCache(cache_dir) cache = SafeFileCache(cache_tmpdir)
cache.set("foo", b"bar") cache.set("foo", b"bar")
@pytest.mark.skipif("sys.platform == 'win32'") @pytest.mark.skipif("sys.platform == 'win32'")
def test_safe_delete_no_perms(self, tmpdir): def test_safe_delete_no_perms(self, cache_tmpdir):
cache_dir = tmpdir.join("unreadable-cache") os.chmod(cache_tmpdir, 000)
cache_dir.makedirs()
os.chmod(cache_dir, 000)
cache = SafeFileCache(cache_dir) cache = SafeFileCache(cache_tmpdir)
cache.delete("foo") cache.delete("foo")