|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +import datetime |
| 5 | + |
| 6 | +import pytest |
| 7 | +import update_python_versions |
| 8 | + |
| 9 | +NOW = datetime.datetime(2026, 8, 2, tzinfo=datetime.timezone.utc) |
| 10 | +WORKFLOWS = ("pr-check.yml", "build.yml") |
| 11 | + |
| 12 | + |
| 13 | +def release(name="Python 3.15.0rc1", release_date="2026-08-01T00:00:00Z", **overrides): |
| 14 | + result = { |
| 15 | + "name": name, |
| 16 | + "release_date": release_date, |
| 17 | + "is_published": True, |
| 18 | + "pre_release": True, |
| 19 | + "version": 3, |
| 20 | + } |
| 21 | + result.update(overrides) |
| 22 | + return result |
| 23 | + |
| 24 | + |
| 25 | +def test_latest_release_candidate_filters_and_orders_releases(): |
| 26 | + releases = [ |
| 27 | + release("Python 3.9.0rc10"), |
| 28 | + release("Python 3.10.0rc2"), |
| 29 | + release("Python 3.11.0b1"), |
| 30 | + release("Python 3.11.0rc1", "2027-08-01T00:00:00Z"), |
| 31 | + release("Python 3.12.0rc1", is_published=False), |
| 32 | + release("Python 3.13.0rc1", pre_release=False), |
| 33 | + None, |
| 34 | + ] |
| 35 | + |
| 36 | + assert update_python_versions.latest_release_candidate(releases, NOW) == "3.10" |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize("release_date", [None, "invalid", "2026-08-01"]) |
| 40 | +def test_latest_release_candidate_rejects_malformed_dates(release_date): |
| 41 | + with pytest.raises(ValueError, match="release_date"): |
| 42 | + update_python_versions.latest_release_candidate( |
| 43 | + [release(release_date=release_date)], NOW |
| 44 | + ) |
| 45 | + |
| 46 | + |
| 47 | +def test_latest_release_candidate_requires_a_published_rc(): |
| 48 | + with pytest.raises(ValueError, match="released Python RC"): |
| 49 | + update_python_versions.latest_release_candidate( |
| 50 | + [release("Python 3.15.0"), release("Python 3.16.0a1")], NOW |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +@pytest.mark.parametrize("key", ["python", "python-version"]) |
| 55 | +def test_configured_versions_normalizes_explicit_series(tmp_path, key): |
| 56 | + workflow = tmp_path / "workflow.yml" |
| 57 | + workflow.write_text( |
| 58 | + f"{key}: ['3.10.9', '3.11-dev', '3.12.0rc1', '3.x']\n", encoding="utf-8" |
| 59 | + ) |
| 60 | + |
| 61 | + assert update_python_versions.configured_versions(workflow) == { |
| 62 | + "3.10", |
| 63 | + "3.11", |
| 64 | + "3.12", |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | +@pytest.mark.parametrize("text", ["matrix: {}\n", "python: ['3.x']\n"]) |
| 69 | +def test_configured_versions_requires_explicit_matrices(tmp_path, text): |
| 70 | + workflow = tmp_path / "workflow.yml" |
| 71 | + workflow.write_text(text, encoding="utf-8") |
| 72 | + |
| 73 | + with pytest.raises(RuntimeError, match="Python version"): |
| 74 | + update_python_versions.configured_versions(workflow) |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize("key", ["python", "python-version"]) |
| 78 | +@pytest.mark.parametrize( |
| 79 | + "first, second, expected", |
| 80 | + [ |
| 81 | + ("3.15", "3.14", set()), |
| 82 | + ("3.14", "3.15", set()), |
| 83 | + ("3.15.1", "3.15-dev", {"3.15"}), |
| 84 | + ], |
| 85 | +) |
| 86 | +def test_configured_versions_requires_coverage_in_each_matrix( |
| 87 | + tmp_path, key, first, second, expected |
| 88 | +): |
| 89 | + workflow = tmp_path / "workflow.yml" |
| 90 | + workflow.write_text( |
| 91 | + f"{key}: ['{first}']\n{key}: ['{second}']\n{key}: ['3.x']\n", |
| 92 | + encoding="utf-8", |
| 93 | + ) |
| 94 | + |
| 95 | + assert update_python_versions.configured_versions(workflow) == expected |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize( |
| 99 | + "pr_version, push_version, expected", |
| 100 | + [ |
| 101 | + ("3.14", "3.14", "3.15"), |
| 102 | + ("3.15", "3.14", "3.15"), |
| 103 | + ("3.14", "3.15", "3.15"), |
| 104 | + ("3.15", "3.15", None), |
| 105 | + ("3.15.1", "3.15.0-rc.2", None), |
| 106 | + ("3.150", "3.15", "3.15"), |
| 107 | + ], |
| 108 | +) |
| 109 | +def test_unconfigured_version(tmp_path, pr_version, push_version, expected): |
| 110 | + workflows = [tmp_path / name for name in WORKFLOWS] |
| 111 | + for workflow, version in zip(workflows, (pr_version, push_version)): |
| 112 | + workflow.write_text(f"python: ['{version}']\n", encoding="utf-8") |
| 113 | + |
| 114 | + assert ( |
| 115 | + update_python_versions.unconfigured_version([release()], workflows, NOW) |
| 116 | + == expected |
| 117 | + ) |
| 118 | + |
| 119 | + |
| 120 | +@pytest.mark.parametrize( |
| 121 | + "push_version, expected", [("3.14", "3.15\n"), ("3.15-dev", "")] |
| 122 | +) |
| 123 | +def test_main_checks_both_workflows( |
| 124 | + tmp_path, monkeypatch, capsys, push_version, expected |
| 125 | +): |
| 126 | + workflow_dir = tmp_path / ".github" / "workflows" |
| 127 | + workflow_dir.mkdir(parents=True) |
| 128 | + for name, version in zip(WORKFLOWS, ("3.15.1", push_version)): |
| 129 | + (workflow_dir / name).write_text(f"python: ['{version}']\n", encoding="utf-8") |
| 130 | + monkeypatch.setattr( |
| 131 | + update_python_versions, |
| 132 | + "__file__", |
| 133 | + str(tmp_path / "build" / "update_python_versions.py"), |
| 134 | + ) |
| 135 | + monkeypatch.setattr( |
| 136 | + update_python_versions, |
| 137 | + "fetch_releases", |
| 138 | + lambda: [release(release_date="2020-08-01T00:00:00Z")], |
| 139 | + ) |
| 140 | + |
| 141 | + update_python_versions.main() |
| 142 | + |
| 143 | + assert capsys.readouterr().out == expected |
0 commit comments