Skip to content

Commit 6764617

Browse files
committed
test: add tests for cached mode
1 parent 0eb86b1 commit 6764617

File tree

2 files changed

+130
-24
lines changed

2 files changed

+130
-24
lines changed

pre_commit_python_eol/check_eol.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ def _get_cached_release_cycle(cache_json: Path) -> list[PythonRelease]:
108108
)
109109

110110

111-
def get_cached_eol_versions() -> abc.Iterator[PythonRelease]:
111+
def get_cached_eol_versions(
112+
eol_versions_cache_json: Path = CACHED_EOL_VERSIONS,
113+
) -> abc.Iterator[PythonRelease]:
112114
"""Parse the locally cached EOL Python versions into `PythonRelease` instance(s)."""
113-
with CACHED_EOL_VERSIONS.open("r", encoding="utf-8") as f:
115+
with eol_versions_cache_json.open("r", encoding="utf-8") as f:
114116
contents = json.load(f)
115117

116118
return (PythonRelease.from_json(v, m) for v, m in contents.items())
@@ -127,7 +129,11 @@ def get_eol_versions(cache_json: Path = CACHED_RELEASE_CYCLE) -> abc.Iterator[Py
127129

128130

129131
def check_python_support(
130-
toml_file: Path, *, cached: bool = False, cache_json: Path = CACHED_RELEASE_CYCLE
132+
toml_file: Path,
133+
*,
134+
cached: bool = False,
135+
cycle_cache_json: Path = CACHED_RELEASE_CYCLE,
136+
eol_versions_cache_json: Path = CACHED_EOL_VERSIONS,
131137
) -> None:
132138
"""
133139
Check the input TOML's `requires-python` for overlap with EOL Python version(s).
@@ -145,9 +151,9 @@ def check_python_support(
145151
package_spec = specifiers.SpecifierSet(requires_python)
146152

147153
if cached:
148-
eol_versions = get_cached_eol_versions()
154+
eol_versions = get_cached_eol_versions(eol_versions_cache_json)
149155
else:
150-
eol_versions = get_eol_versions(cache_json)
156+
eol_versions = get_eol_versions(cycle_cache_json)
151157
eol_supported = [version for version in eol_versions if version.python_ver in package_spec]
152158

153159
if eol_supported:

tests/test_check_eol.py

Lines changed: 119 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,21 @@ def test_python_release_from_json() -> None:
5050
assert PythonRelease.from_json("3.14", metadata=sample_metadata) == truth_rel
5151

5252

53+
def test_python_release_to_json() -> None:
54+
sample_rel = PythonRelease(
55+
python_ver=version.Version("3.14"),
56+
status=ReleasePhase.PRERELEASE,
57+
end_of_life=dt.date(year=2030, month=10, day=1),
58+
)
59+
60+
truth_metadata = {
61+
"status": "prerelease",
62+
"end_of_life": "2030-10-01",
63+
}
64+
65+
assert sample_rel.to_json() == ("3.14", truth_metadata)
66+
67+
5368
# Intentionally out of expected order so sorting can be checked
5469
SAMPLE_JSON = """\
5570
{
@@ -123,27 +138,47 @@ def test_get_cached_release_cycle(tmp_path: Path) -> None:
123138
}
124139
"""
125140

141+
EOL_VERSIONS_CACHE_WITH_EOL = """\
142+
{
143+
"3.8": {
144+
"status": "end-of-life",
145+
"end_of_life": "2024-10-07"
146+
},
147+
"3.7": {
148+
"status": "end-of-life",
149+
"end_of_life": "2023-06-27"
150+
}
151+
}
152+
"""
153+
126154

127155
@pytest.fixture
128-
def path_with_cache(tmp_path: Path) -> tuple[Path, Path]:
129-
json_file = tmp_path / "cache.json"
130-
json_file.write_text(RELEASE_CACHE_WITH_EOL)
156+
def path_with_caches(tmp_path: Path) -> tuple[Path, Path, Path]:
157+
cycle_json_file = tmp_path / "cycle_cache.json"
158+
eol_versions_json_file = tmp_path / "eol_versions_cache.json"
131159

132-
return tmp_path, json_file
160+
cycle_json_file.write_text(RELEASE_CACHE_WITH_EOL)
161+
eol_versions_json_file.write_text(EOL_VERSIONS_CACHE_WITH_EOL)
162+
163+
return tmp_path, cycle_json_file, eol_versions_json_file
133164

134165

135166
SAMPLE_PYPROJECT_NO_VERSION = """\
136167
[project]
137168
"""
138169

139170

140-
def test_check_python_no_version_spec_raises(path_with_cache: tuple[Path, Path]) -> None:
141-
base_path, cache_path = path_with_cache
171+
def test_check_python_no_version_spec_raises(path_with_caches: tuple[Path, Path, Path]) -> None:
172+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
142173
pyproject = base_path / "pyproject.toml"
143174
pyproject.write_text(SAMPLE_PYPROJECT_NO_VERSION)
144175

145176
with pytest.raises(RequiresPythonNotFoundError):
146-
check_python_support(pyproject, cache_json=cache_path)
177+
check_python_support(
178+
pyproject,
179+
cycle_cache_json=cycle_cache_path,
180+
eol_versions_cache_json=eol_versions_cache_path,
181+
)
147182

148183

149184
SAMPLE_PYPROJECT_NO_EOL = """\
@@ -152,13 +187,17 @@ def test_check_python_no_version_spec_raises(path_with_cache: tuple[Path, Path])
152187
"""
153188

154189

155-
def test_check_python_support_no_eol(path_with_cache: tuple[Path, Path]) -> None:
156-
base_path, cache_path = path_with_cache
190+
def test_check_python_support_no_eol(path_with_caches: tuple[Path, Path, Path]) -> None:
191+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
157192
pyproject = base_path / "pyproject.toml"
158193
pyproject.write_text(SAMPLE_PYPROJECT_NO_EOL)
159194

160195
with time_machine.travel(dt.date(year=2025, month=5, day=1)):
161-
check_python_support(pyproject, cache_json=cache_path)
196+
check_python_support(
197+
pyproject,
198+
cycle_cache_json=cycle_cache_path,
199+
eol_versions_cache_json=eol_versions_cache_path,
200+
)
162201

163202

164203
SAMPLE_PYPROJECT_SINGLE_EOL = """\
@@ -167,14 +206,18 @@ def test_check_python_support_no_eol(path_with_cache: tuple[Path, Path]) -> None
167206
"""
168207

169208

170-
def test_check_python_support_single_eol_raises(path_with_cache: tuple[Path, Path]) -> None:
171-
base_path, cache_path = path_with_cache
209+
def test_check_python_support_single_eol_raises(path_with_caches: tuple[Path, Path, Path]) -> None:
210+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
172211
pyproject = base_path / "pyproject.toml"
173212
pyproject.write_text(SAMPLE_PYPROJECT_SINGLE_EOL)
174213

175214
with time_machine.travel(dt.date(year=2025, month=5, day=1)):
176215
with pytest.raises(EOLPythonError) as e:
177-
check_python_support(pyproject, cache_json=cache_path)
216+
check_python_support(
217+
pyproject,
218+
cycle_cache_json=cycle_cache_path,
219+
eol_versions_cache_json=eol_versions_cache_path,
220+
)
178221

179222
assert str(e.value).endswith("3.8")
180223

@@ -185,14 +228,20 @@ def test_check_python_support_single_eol_raises(path_with_cache: tuple[Path, Pat
185228
"""
186229

187230

188-
def test_check_python_support_single_eol_raises_by_date(path_with_cache: tuple[Path, Path]) -> None:
189-
base_path, cache_path = path_with_cache
231+
def test_check_python_support_single_eol_raises_by_date(
232+
path_with_caches: tuple[Path, Path, Path],
233+
) -> None:
234+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
190235
pyproject = base_path / "pyproject.toml"
191236
pyproject.write_text(SAMPLE_PYPROJECT_SINGLE_EOL_BY_DATE)
192237

193238
with time_machine.travel(dt.date(year=2031, month=11, day=1)):
194239
with pytest.raises(EOLPythonError) as e:
195-
check_python_support(pyproject, cache_json=cache_path)
240+
check_python_support(
241+
pyproject,
242+
cycle_cache_json=cycle_cache_path,
243+
eol_versions_cache_json=eol_versions_cache_path,
244+
)
196245

197246
assert str(e.value).endswith("3.14")
198247

@@ -203,13 +252,64 @@ def test_check_python_support_single_eol_raises_by_date(path_with_cache: tuple[P
203252
"""
204253

205254

206-
def test_check_python_support_multi_eol_raises(path_with_cache: tuple[Path, Path]) -> None:
207-
base_path, cache_path = path_with_cache
255+
def test_check_python_support_multi_eol_raises(path_with_caches: tuple[Path, Path, Path]) -> None:
256+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
208257
pyproject = base_path / "pyproject.toml"
209258
pyproject.write_text(SAMPLE_PYPROJECT_MULTI_EOL)
210259

211260
with time_machine.travel(dt.date(year=2025, month=5, day=1)):
212261
with pytest.raises(EOLPythonError) as e:
213-
check_python_support(pyproject, cache_json=cache_path)
262+
check_python_support(
263+
pyproject,
264+
cycle_cache_json=cycle_cache_path,
265+
eol_versions_cache_json=eol_versions_cache_path,
266+
)
214267

215268
assert str(e.value).endswith("3.7, 3.8")
269+
270+
271+
def test_check_cached_python_support_no_eol(path_with_caches: tuple[Path, Path, Path]) -> None:
272+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
273+
pyproject = base_path / "pyproject.toml"
274+
pyproject.write_text(SAMPLE_PYPROJECT_NO_EOL)
275+
276+
check_python_support(
277+
pyproject,
278+
cycle_cache_json=cycle_cache_path,
279+
eol_versions_cache_json=eol_versions_cache_path,
280+
cached=True,
281+
)
282+
283+
284+
def test_check_cached_python_support_single_eol_raises(
285+
path_with_caches: tuple[Path, Path, Path],
286+
) -> None:
287+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
288+
pyproject = base_path / "pyproject.toml"
289+
pyproject.write_text(SAMPLE_PYPROJECT_SINGLE_EOL)
290+
291+
with pytest.raises(EOLPythonError) as e:
292+
check_python_support(
293+
pyproject,
294+
cycle_cache_json=cycle_cache_path,
295+
eol_versions_cache_json=eol_versions_cache_path,
296+
cached=True,
297+
)
298+
299+
assert str(e.value).endswith("3.8")
300+
301+
302+
def test_check_cached_python_support_single_eol_no_raises_by_date(
303+
path_with_caches: tuple[Path, Path, Path],
304+
) -> None:
305+
base_path, cycle_cache_path, eol_versions_cache_path = path_with_caches
306+
pyproject = base_path / "pyproject.toml"
307+
pyproject.write_text(SAMPLE_PYPROJECT_SINGLE_EOL_BY_DATE)
308+
309+
with time_machine.travel(dt.date(year=2031, month=11, day=1)):
310+
check_python_support(
311+
pyproject,
312+
cycle_cache_json=cycle_cache_path,
313+
eol_versions_cache_json=eol_versions_cache_path,
314+
cached=True,
315+
)

0 commit comments

Comments
 (0)