|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""What a build's output contributes to changelogs/, and what it must not. |
| 3 | +
|
| 4 | +A leg produces four metadata files. Three belong in `changelogs/`; the fourth, |
| 5 | +`hashes_<winpyver>.md`, describes one build's binaries rather than the release |
| 6 | +and has never been kept there. Getting that wrong is quiet: the wrong file is |
| 7 | +committed and nothing complains, so the selection is pinned here. |
| 8 | +
|
| 9 | +The `_History.md` companions are written from the checkout rather than shipped |
| 10 | +by the build, because a history compares against the *previous* release, which |
| 11 | +only the repository has. |
| 12 | +""" |
| 13 | +import importlib.util |
| 14 | +import os |
| 15 | +import shutil |
| 16 | +import subprocess |
| 17 | +import sys |
| 18 | +from pathlib import Path |
| 19 | + |
| 20 | +import pytest |
| 21 | + |
| 22 | +REPO = Path(__file__).resolve().parents[1] |
| 23 | +SCRIPT = REPO / ".github/scripts/changelog_files.py" |
| 24 | + |
| 25 | +needs_script = pytest.mark.skipif(not SCRIPT.is_file(), reason="changelog_files.py is gone") |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="module") |
| 29 | +def changelog_files(): |
| 30 | + if not SCRIPT.is_file(): |
| 31 | + pytest.skip("changelog_files.py is gone") |
| 32 | + spec = importlib.util.spec_from_file_location("changelog_files", SCRIPT) |
| 33 | + module = importlib.util.module_from_spec(spec) |
| 34 | + spec.loader.exec_module(module) |
| 35 | + return module |
| 36 | + |
| 37 | + |
| 38 | +# what one leg of 2026-04 b1 actually uploaded, names verbatim |
| 39 | +LEG_OUTPUT = [ |
| 40 | + "WinPythonslim-64bit-3.15.0.5b1.md", |
| 41 | + "pylock.64-3_15_0_5slimb1.toml", |
| 42 | + "requir.64-3_15_0_5slimb1.txt", |
| 43 | + "hashes_3.15.0.5slimb1.md", |
| 44 | +] |
| 45 | + |
| 46 | + |
| 47 | +@needs_script |
| 48 | +class TestParseChangelogName: |
| 49 | + @pytest.mark.parametrize("name,expected", [ |
| 50 | + ("WinPythonslim-64bit-3.15.0.5b1.md", ("slim", 64, "3.15.0.5b1")), |
| 51 | + ("WinPythondotf-64bit-3.14.7.1b1.md", ("dotf", 64, "3.14.7.1b1")), |
| 52 | + ("WinPythondot-64bit-3.13.15.0.md", ("dot", 64, "3.13.15.0")), |
| 53 | + ("WinPython-64bit-3.9.8.0.md", ("", 64, "3.9.8.0")), # the unflavored ones |
| 54 | + ("WinPythondot-32bit-3.9.0.0b1.md", ("dot", 32, "3.9.0.0b1")), |
| 55 | + ]) |
| 56 | + def test_reads_flavor_architecture_and_version(self, changelog_files, name, expected): |
| 57 | + assert changelog_files.parse_changelog_name(name) == expected |
| 58 | + |
| 59 | + @pytest.mark.parametrize("name", [ |
| 60 | + "WinPythonslim-64bit-3.15.0.5b1_History.md", # the companion, not an index |
| 61 | + "hashes_3.15.0.5slimb1.md", |
| 62 | + "pylock.64-3_15_0_5slimb1.toml", |
| 63 | + "README.md", |
| 64 | + "WinPythonslim-64bit-.md", |
| 65 | + ]) |
| 66 | + def test_rejects_everything_that_is_not_a_package_index(self, changelog_files, name): |
| 67 | + assert changelog_files.parse_changelog_name(name) is None |
| 68 | + |
| 69 | + |
| 70 | +@needs_script |
| 71 | +class TestSelection: |
| 72 | + def test_hashes_are_left_behind(self, changelog_files, tmp_path): |
| 73 | + """They describe one build's binaries; changelogs/ has never held them.""" |
| 74 | + for name in LEG_OUTPUT: |
| 75 | + (tmp_path / name).write_text("", encoding="utf-8") |
| 76 | + chosen = sorted(p.name for p in changelog_files.files_to_file(tmp_path)) |
| 77 | + assert chosen == [ |
| 78 | + "WinPythonslim-64bit-3.15.0.5b1.md", |
| 79 | + "pylock.64-3_15_0_5slimb1.toml", |
| 80 | + "requir.64-3_15_0_5slimb1.txt", |
| 81 | + ] |
| 82 | + |
| 83 | + def test_directories_are_skipped(self, changelog_files, tmp_path): |
| 84 | + (tmp_path / "WinPythonslim-64bit-3.15.0.5b1.md").write_text("", encoding="utf-8") |
| 85 | + (tmp_path / "pylock.64-nested").mkdir() |
| 86 | + assert [p.name for p in changelog_files.files_to_file(tmp_path)] == [ |
| 87 | + "WinPythonslim-64bit-3.15.0.5b1.md" |
| 88 | + ] |
| 89 | + |
| 90 | + |
| 91 | +def run_script(*args, cwd, env=None): |
| 92 | + """The script by absolute path, from an unrelated directory. |
| 93 | +
|
| 94 | + It must not need to be run from the checkout: the workflow's own step and |
| 95 | + these tests both invoke it as a path, which puts .github/scripts on |
| 96 | + sys.path rather than the repository root. |
| 97 | + """ |
| 98 | + return subprocess.run( |
| 99 | + [sys.executable, str(SCRIPT), *map(str, args)], |
| 100 | + cwd=str(cwd), capture_output=True, text=True, env=env, |
| 101 | + ) |
| 102 | + |
| 103 | + |
| 104 | +@needs_script |
| 105 | +class TestEndToEnd: |
| 106 | + """Run the script the way the workflow runs it.""" |
| 107 | + |
| 108 | + @pytest.fixture |
| 109 | + def cycle(self, tmp_path): |
| 110 | + """A metadata directory and a changelogs/ holding one earlier release.""" |
| 111 | + source = tmp_path / "release_metadata" |
| 112 | + source.mkdir() |
| 113 | + changelogs = tmp_path / "changelogs" |
| 114 | + changelogs.mkdir() |
| 115 | + |
| 116 | + # a real package index makes a real diff; reuse two the repo already has |
| 117 | + previous = REPO / "changelogs" / "WinPythonslim-64bit-3.15.0.4.md" |
| 118 | + current = REPO / "changelogs" / "WinPythonslim-64bit-3.14.7.0.md" |
| 119 | + if not (previous.is_file() and current.is_file()): |
| 120 | + pytest.skip("the changelogs this test reads from are gone") |
| 121 | + shutil.copyfile(previous, changelogs / previous.name) |
| 122 | + shutil.copyfile(current, source / "WinPythonslim-64bit-3.15.0.5b1.md") |
| 123 | + (source / "pylock.64-3_15_0_5slimb1.toml").write_text("x", encoding="utf-8") |
| 124 | + (source / "requir.64-3_15_0_5slimb1.txt").write_text("x", encoding="utf-8") |
| 125 | + (source / "hashes_3.15.0.5slimb1.md").write_text("x", encoding="utf-8") |
| 126 | + return source, changelogs |
| 127 | + |
| 128 | + def test_wppm_comes_from_the_checkout(self, cycle, tmp_path): |
| 129 | + """A decoy wppm on PYTHONPATH must lose to the one being released. |
| 130 | +
|
| 131 | + Running a script puts the script's directory on sys.path, not the |
| 132 | + checkout root, so without a deliberate insert the import falls through |
| 133 | + to whatever else is reachable. On a machine with wppm installed that |
| 134 | + looks fine -- which is how it once slipped past a green local run -- |
| 135 | + and on a CI runner, which installs none, it is ModuleNotFoundError. |
| 136 | + The decoy makes the difference visible either way. |
| 137 | + """ |
| 138 | + source, changelogs = cycle |
| 139 | + decoy = tmp_path / "decoy" |
| 140 | + (decoy / "wppm").mkdir(parents=True) |
| 141 | + (decoy / "wppm" / "__init__.py").write_text( |
| 142 | + "raise RuntimeError('decoy wppm imported')", encoding="utf-8" |
| 143 | + ) |
| 144 | + env = {**os.environ, "PYTHONPATH": str(decoy)} |
| 145 | + proc = run_script(source, changelogs, cwd=tmp_path, env=env) |
| 146 | + assert proc.returncode == 0, proc.stderr |
| 147 | + assert "decoy" not in proc.stderr |
| 148 | + |
| 149 | + def test_files_the_three_and_writes_the_history(self, cycle, tmp_path): |
| 150 | + source, changelogs = cycle |
| 151 | + proc = run_script(source, changelogs, cwd=tmp_path) |
| 152 | + assert proc.returncode == 0, proc.stderr |
| 153 | + landed = sorted(p.name for p in changelogs.iterdir()) |
| 154 | + assert landed == [ |
| 155 | + "WinPythonslim-64bit-3.15.0.4.md", # was already there |
| 156 | + "WinPythonslim-64bit-3.15.0.5b1.md", # filed |
| 157 | + "WinPythonslim-64bit-3.15.0.5b1_History.md", # written |
| 158 | + "pylock.64-3_15_0_5slimb1.toml", |
| 159 | + "requir.64-3_15_0_5slimb1.txt", |
| 160 | + ] |
| 161 | + |
| 162 | + def test_the_history_names_the_release_it_compares_against(self, cycle, tmp_path): |
| 163 | + source, changelogs = cycle |
| 164 | + proc = run_script(source, changelogs, cwd=tmp_path) |
| 165 | + assert proc.returncode == 0, proc.stderr |
| 166 | + history = (changelogs / "WinPythonslim-64bit-3.15.0.5b1_History.md").read_text( |
| 167 | + encoding="utf-8" |
| 168 | + ) |
| 169 | + assert "since version 3.15.0.4slim" in history |
| 170 | + assert "3.15.0.5b1slim" in history |
| 171 | + |
| 172 | + def test_an_empty_metadata_directory_is_an_error(self, tmp_path): |
| 173 | + """Silence here would commit nothing and call it a success.""" |
| 174 | + source, changelogs = tmp_path / "src", tmp_path / "changelogs" |
| 175 | + source.mkdir() |
| 176 | + changelogs.mkdir() |
| 177 | + proc = run_script(source, changelogs, cwd=tmp_path) |
| 178 | + assert proc.returncode != 0 |
| 179 | + assert "held no changelog" in proc.stdout + proc.stderr |
| 180 | + |
| 181 | + def test_a_missing_directory_is_an_error(self, tmp_path): |
| 182 | + proc = run_script(tmp_path / "nope", tmp_path, cwd=tmp_path) |
| 183 | + assert proc.returncode != 0 |
| 184 | + assert "no such directory" in proc.stdout + proc.stderr |
0 commit comments