|
| 1 | +import datetime as dt |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pytest |
| 5 | +import time_machine |
| 6 | +from packaging import version |
| 7 | + |
| 8 | +from pre_commit_python_eol.check_eol import ( |
| 9 | + EOLPythonError, |
| 10 | + PythonRelease, |
| 11 | + ReleasePhase, |
| 12 | + RequiresPythonNotFoundError, |
| 13 | + _get_cached_release_cycle, |
| 14 | + _parse_eol_date, |
| 15 | + check_python_support, |
| 16 | +) |
| 17 | + |
| 18 | +EOL_DATE_PARSE_CASES = ( |
| 19 | + ("2025-01-01", dt.date(year=2025, month=1, day=1)), |
| 20 | + ("2025-01", dt.date(year=2025, month=1, day=1)), |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize(("date_str", "truth_date"), EOL_DATE_PARSE_CASES) |
| 25 | +def test_parse_eol_date(date_str: str, truth_date: dt.date) -> None: |
| 26 | + assert _parse_eol_date(date_str) == truth_date |
| 27 | + |
| 28 | + |
| 29 | +def test_parse_eol_date_unknown_fmt_raises() -> None: |
| 30 | + with pytest.raises(ValueError, match="Unknown date format"): |
| 31 | + _ = _parse_eol_date("123456") |
| 32 | + |
| 33 | + |
| 34 | +def test_python_release_from_json() -> None: |
| 35 | + sample_metadata = { |
| 36 | + "branch": "3.14", |
| 37 | + "pep": 745, |
| 38 | + "status": "prerelease", |
| 39 | + "first_release": "2025-10-07", |
| 40 | + "end_of_life": "2030-10", |
| 41 | + "release_manager": "Hugo van Kemenade", |
| 42 | + } |
| 43 | + |
| 44 | + truth_rel = PythonRelease( |
| 45 | + python_ver=version.Version("3.14"), |
| 46 | + status=ReleasePhase.PRERELEASE, |
| 47 | + end_of_life=dt.date(year=2030, month=10, day=1), |
| 48 | + ) |
| 49 | + |
| 50 | + assert PythonRelease.from_json("3.14", metadata=sample_metadata) == truth_rel |
| 51 | + |
| 52 | + |
| 53 | +# Intentionally out of expected order so sorting can be checked |
| 54 | +SAMPLE_JSON = """\ |
| 55 | +{ |
| 56 | + "3.14": { |
| 57 | + "branch": "3.14", |
| 58 | + "pep": 745, |
| 59 | + "status": "prerelease", |
| 60 | + "first_release": "2025-10-07", |
| 61 | + "end_of_life": "2030-10", |
| 62 | + "release_manager": "Hugo van Kemenade" |
| 63 | + }, |
| 64 | + "3.15": { |
| 65 | + "branch": "main", |
| 66 | + "pep": 790, |
| 67 | + "status": "feature", |
| 68 | + "first_release": "2026-10-01", |
| 69 | + "end_of_life": "2031-10", |
| 70 | + "release_manager": "Hugo van Kemenade" |
| 71 | + } |
| 72 | +} |
| 73 | +""" |
| 74 | + |
| 75 | +TRUTH_RELEASE_CYCLE = [ |
| 76 | + PythonRelease( |
| 77 | + python_ver=version.Version("3.15"), |
| 78 | + status=ReleasePhase.FEATURE, |
| 79 | + end_of_life=dt.date(year=2031, month=10, day=1), |
| 80 | + ), |
| 81 | + PythonRelease( |
| 82 | + python_ver=version.Version("3.14"), |
| 83 | + status=ReleasePhase.PRERELEASE, |
| 84 | + end_of_life=dt.date(year=2030, month=10, day=1), |
| 85 | + ), |
| 86 | +] |
| 87 | + |
| 88 | + |
| 89 | +def test_get_cached_release_cycle(tmp_path: Path) -> None: |
| 90 | + json_file = tmp_path / "cache.json" |
| 91 | + json_file.write_text(SAMPLE_JSON) |
| 92 | + |
| 93 | + release_cycle = _get_cached_release_cycle(cache_json=json_file) |
| 94 | + assert release_cycle == TRUTH_RELEASE_CYCLE |
| 95 | + |
| 96 | + |
| 97 | +RELEASE_CACHE_WITH_EOL = """\ |
| 98 | +{ |
| 99 | + "3.14": { |
| 100 | + "branch": "3.14", |
| 101 | + "pep": 745, |
| 102 | + "status": "prerelease", |
| 103 | + "first_release": "2025-10-07", |
| 104 | + "end_of_life": "2030-10", |
| 105 | + "release_manager": "Hugo van Kemenade" |
| 106 | + }, |
| 107 | + "3.8": { |
| 108 | + "branch": "3.8", |
| 109 | + "pep": 569, |
| 110 | + "status": "end-of-life", |
| 111 | + "first_release": "2019-10-14", |
| 112 | + "end_of_life": "2024-10-07", |
| 113 | + "release_manager": "Lukasz Langa" |
| 114 | + }, |
| 115 | + "3.7": { |
| 116 | + "branch": "3.7", |
| 117 | + "pep": 537, |
| 118 | + "status": "end-of-life", |
| 119 | + "first_release": "2018-06-27", |
| 120 | + "end_of_life": "2023-06-27", |
| 121 | + "release_manager": "Ned Deily" |
| 122 | + } |
| 123 | +} |
| 124 | +""" |
| 125 | + |
| 126 | + |
| 127 | +@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) |
| 131 | + |
| 132 | + return tmp_path, json_file |
| 133 | + |
| 134 | + |
| 135 | +SAMPLE_PYPROJECT_NO_VERSION = """\ |
| 136 | +[project] |
| 137 | +""" |
| 138 | + |
| 139 | + |
| 140 | +def test_check_python_no_version_spec_raises(path_with_cache: tuple[Path, Path]) -> None: |
| 141 | + base_path, cache_path = path_with_cache |
| 142 | + pyproject = base_path / "pyproject.toml" |
| 143 | + pyproject.write_text(SAMPLE_PYPROJECT_NO_VERSION) |
| 144 | + |
| 145 | + with pytest.raises(RequiresPythonNotFoundError): |
| 146 | + check_python_support(pyproject, cache_json=cache_path) |
| 147 | + |
| 148 | + |
| 149 | +SAMPLE_PYPROJECT_NO_EOL = """\ |
| 150 | +[project] |
| 151 | +requires-python = ">=3.11" |
| 152 | +""" |
| 153 | + |
| 154 | + |
| 155 | +def test_check_python_support_no_eol(path_with_cache: tuple[Path, Path]) -> None: |
| 156 | + base_path, cache_path = path_with_cache |
| 157 | + pyproject = base_path / "pyproject.toml" |
| 158 | + pyproject.write_text(SAMPLE_PYPROJECT_NO_EOL) |
| 159 | + |
| 160 | + with time_machine.travel(dt.date(year=2025, month=5, day=1)): |
| 161 | + check_python_support(pyproject, cache_json=cache_path) |
| 162 | + |
| 163 | + |
| 164 | +SAMPLE_PYPROJECT_SINGLE_EOL = """\ |
| 165 | +[project] |
| 166 | +requires-python = ">=3.8" |
| 167 | +""" |
| 168 | + |
| 169 | + |
| 170 | +def test_check_python_support_single_eol_raises(path_with_cache: tuple[Path, Path]) -> None: |
| 171 | + base_path, cache_path = path_with_cache |
| 172 | + pyproject = base_path / "pyproject.toml" |
| 173 | + pyproject.write_text(SAMPLE_PYPROJECT_SINGLE_EOL) |
| 174 | + |
| 175 | + with time_machine.travel(dt.date(year=2025, month=5, day=1)): |
| 176 | + with pytest.raises(EOLPythonError) as e: |
| 177 | + check_python_support(pyproject, cache_json=cache_path) |
| 178 | + |
| 179 | + assert str(e.value).endswith("3.8") |
| 180 | + |
| 181 | + |
| 182 | +SAMPLE_PYPROJECT_SINGLE_EOL_BY_DATE = """\ |
| 183 | +[project] |
| 184 | +requires-python = ">=3.11" |
| 185 | +""" |
| 186 | + |
| 187 | + |
| 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 |
| 190 | + pyproject = base_path / "pyproject.toml" |
| 191 | + pyproject.write_text(SAMPLE_PYPROJECT_SINGLE_EOL_BY_DATE) |
| 192 | + |
| 193 | + with time_machine.travel(dt.date(year=2031, month=11, day=1)): |
| 194 | + with pytest.raises(EOLPythonError) as e: |
| 195 | + check_python_support(pyproject, cache_json=cache_path) |
| 196 | + |
| 197 | + assert str(e.value).endswith("3.14") |
| 198 | + |
| 199 | + |
| 200 | +SAMPLE_PYPROJECT_MULTI_EOL = """\ |
| 201 | +[project] |
| 202 | +requires-python = ">=3.7" |
| 203 | +""" |
| 204 | + |
| 205 | + |
| 206 | +def test_check_python_support_multi_eol_raises(path_with_cache: tuple[Path, Path]) -> None: |
| 207 | + base_path, cache_path = path_with_cache |
| 208 | + pyproject = base_path / "pyproject.toml" |
| 209 | + pyproject.write_text(SAMPLE_PYPROJECT_MULTI_EOL) |
| 210 | + |
| 211 | + with time_machine.travel(dt.date(year=2025, month=5, day=1)): |
| 212 | + with pytest.raises(EOLPythonError) as e: |
| 213 | + check_python_support(pyproject, cache_json=cache_path) |
| 214 | + |
| 215 | + assert str(e.value).endswith("3.7, 3.8") |
0 commit comments