-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtest_cache.py
171 lines (144 loc) · 5.43 KB
/
test_cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import importlib
import sys
from pathlib import Path
import platformdirs
import pretend # type: ignore
import pytest
from packaging.version import Version
from pytest import MonkeyPatch
import pip_audit._cache as cache
from pip_audit._cache import _get_cache_dir, _get_pip_cache
def _patch_platformdirs(monkeypatch: MonkeyPatch, sys_platform: str) -> None:
"""Utility function to patch `platformdirs` in order to test cross-platforms."""
# Mocking OS host
monkeypatch.setattr(sys, "platform", sys_platform)
# We are forced to reload `platformdirs` to get the correct cache directory
# as cache definition is stored in the top level `__init__.py` file of the
# `platformdirs` package
importlib.reload(platformdirs)
# Setting directory-controlling environment variables to known state
if sys_platform == "win32":
monkeypatch.setenv("LOCALAPPDATA", "/tmp/AppData/Local")
elif sys_platform == "linux":
monkeypatch.setenv("XDG_CACHE_HOME", "/tmp/home/.cache")
@pytest.mark.parametrize(
"sys_platform",
[
pytest.param("linux", id="on Linux"),
pytest.param("win32", id="on Windows"),
pytest.param("darwin", id="on MacOS"),
],
)
def test_get_cache_dir(monkeypatch, sys_platform):
# Check cross-platforms
_patch_platformdirs(monkeypatch, sys_platform)
# When we supply a cache directory, always use that
cache_dir = Path("/tmp/foo/cache_dir")
assert _get_cache_dir(cache_dir) == cache_dir
cache_dir = Path("/fake/pip/cache/dir")
get_pip_cache = pretend.call_recorder(lambda: cache_dir)
monkeypatch.setattr(cache, "_get_pip_cache", get_pip_cache)
# When `pip cache dir` works, we use it. In this case, it's mocked.
assert _get_cache_dir(None, use_pip=True) == cache_dir
def test_get_pip_cache():
# Actually running `pip cache dir` gets us some path that ends with "http"
cache_dir = _get_pip_cache()
assert cache_dir.stem == "http"
@pytest.mark.parametrize(
"sys_platform,expected",
[
pytest.param(
"linux",
Path("/tmp") / "home" / ".cache" / "pip-audit",
id="on Linux",
),
pytest.param(
"win32",
Path("/tmp") / "AppData" / "Local" / "pip-audit" / "Cache",
id="on Windows",
),
pytest.param(
"darwin",
Path.home() / "Library" / "Caches" / "pip-audit",
id="on MacOS",
),
],
)
def test_get_cache_dir_do_not_use_pip(monkeypatch, sys_platform, expected):
# Check cross-platforms
_patch_platformdirs(monkeypatch, sys_platform)
# Even with None, we never use the pip cache if we're told not to.
assert _get_cache_dir(None, use_pip=False) == expected
@pytest.mark.parametrize(
"sys_platform,expected",
[
pytest.param(
"linux",
Path("/tmp") / "home" / ".cache" / "pip-audit",
id="on Linux",
),
pytest.param(
"win32",
Path("/tmp") / "AppData" / "Local" / "pip-audit" / "Cache",
id="on Windows",
),
pytest.param(
"darwin",
Path.home() / "Library" / "Caches" / "pip-audit",
id="on MacOS",
),
],
)
def test_get_cache_dir_pip_disabled_in_environment(monkeypatch, sys_platform, expected):
monkeypatch.setenv("PIP_NO_CACHE_DIR", "1")
# Check cross-platforms
_patch_platformdirs(monkeypatch, sys_platform)
# Even with use_pip=True, we avoid pip's cache if the environment tells us to.
assert _get_cache_dir(None, use_pip=True) == expected
@pytest.mark.parametrize(
"sys_platform,expected",
[
pytest.param(
"linux",
Path("/tmp") / "home" / ".cache" / "pip-audit",
id="on Linux",
),
pytest.param(
"win32",
Path("/tmp") / "AppData" / "Local" / "pip-audit" / "Cache",
id="on Windows",
),
pytest.param(
"darwin",
Path.home() / "Library" / "Caches" / "pip-audit",
id="on MacOS",
),
],
)
def test_get_cache_dir_old_pip(monkeypatch, sys_platform, expected):
# Check the case where we have an old `pip`
monkeypatch.setattr(cache, "_PIP_VERSION", Version("1.0.0"))
# Check cross-platforms
_patch_platformdirs(monkeypatch, sys_platform)
# In this case, we can't query `pip` to figure out where its HTTP cache is
# Instead, we use `~/.pip-audit-cache`
assert _get_cache_dir(None) == expected
def test_cache_warns_about_old_pip(monkeypatch, cache_dir):
monkeypatch.setattr(cache, "_PIP_VERSION", Version("1.0.0"))
logger = pretend.stub(warning=pretend.call_recorder(lambda s: None))
monkeypatch.setattr(cache, "logger", logger)
# If we supply a cache directory, we're not relying on finding the `pip` cache so no need to log
# a warning
_get_cache_dir(cache_dir)
assert len(logger.warning.calls) == 0
# However, if we're not specifying a cache directory, we'll try to call `pip cache dir`. If we
# have an old `pip`, then we should expect a warning to be logged
_get_cache_dir(None)
assert len(logger.warning.calls) == 1
def test_delete_legacy_cache_dir(monkeypatch, tmp_path):
legacy = tmp_path / "pip-audit-cache"
legacy.mkdir()
assert legacy.exists()
monkeypatch.setattr(cache, "_PIP_AUDIT_LEGACY_INTERNAL_CACHE", legacy)
_get_cache_dir(None, use_pip=False)
assert not legacy.exists()