11from __future__ import annotations
22
3+ import os
34import re
45import runpy
6+ import shutil
7+ import subprocess
8+ import sys
59from pathlib import Path
610
11+ import pytest
12+ import yaml
13+
714ROOT = Path (__file__ ).resolve ().parents [1 ]
815MAKEFILE = ROOT / "Makefile"
916TESTS_WORKFLOW = ROOT / ".github" / "workflows" / "tests.yml"
@@ -169,8 +176,9 @@ def test_prospective_contract_preparation_removes_api_key_before_uv() -> None:
169176 assert recipe .index ("unset OPENAI_API_KEY" ) < recipe .index ("uv run" )
170177
171178
172- def test_release_build_validates_before_executing_candidate_code () -> None :
173- build = _workflow_job ("build" , PUBLISH_WORKFLOW )
179+ @pytest .mark .parametrize ("job_name" , ["checks" , "build" ])
180+ def test_release_build_validates_before_executing_candidate_code (job_name : str ) -> None :
181+ build = _workflow_job (job_name , PUBLISH_WORKFLOW )
174182
175183 assert "contents: read" in build
176184 assert "id-token:" not in build
@@ -180,11 +188,143 @@ def test_release_build_validates_before_executing_candidate_code() -> None:
180188 assert build .count ("persist-credentials: false" ) == 2
181189 assert "fetch-depth: 0" in build
182190 validation = build .index ("python -I control/.github/scripts/verify_release.py" )
183- assert validation < build .index ("run: make sync" ) < build .index ("run: uv build" )
191+ candidate_command = (
192+ 'UV_PYTHON="$python_version" make sync tests' if job_name == "checks" else "run: uv build"
193+ )
194+ assert validation < build .index (candidate_command )
184195 assert ' --tag "$RELEASE_TAG" --expected-sha "$RELEASE_SHA"' in build
185196 assert "enable-cache: false" in build
186197
187198
199+ @pytest .mark .parametrize ("failed_check" , [None , "3.12:sync tests" , ":typecheck" ])
200+ def test_release_checks_fail_closed (tmp_path : Path , failed_check : str | None ) -> None :
201+ bash = shutil .which ("bash" )
202+ if bash is None :
203+ pytest .skip ("The publish workflow requires Bash." )
204+ workflow = yaml .safe_load (PUBLISH_WORKFLOW .read_text (encoding = "utf-8" ))
205+ jobs = workflow ["jobs" ]
206+ checks = jobs ["checks" ]
207+ build = jobs ["build" ]
208+ steps = checks ["steps" ]
209+ check_index = next (i for i , step in enumerate (steps ) if step ["name" ] == "Check release source" )
210+ check = steps [check_index ]
211+ package = next (step for step in build ["steps" ] if step .get ("run" ) == "uv build" )
212+ assert check ["working-directory" ] == package ["working-directory" ] == "release-source"
213+ assert build ["needs" ] == "checks"
214+ assert check ["shell" ] == "bash"
215+ assert check ["env" ] == {"OPENAI_API_KEY" : "fake-for-tests" , "UV_LOCKED" : "1" }
216+ for job in (checks , build , jobs ["publish" ]):
217+ assert "if" not in job and "continue-on-error" not in job
218+ for step in job ["steps" ]:
219+ assert "if" not in step and "continue-on-error" not in step
220+
221+ bin_dir = tmp_path / "bin"
222+ bin_dir .mkdir ()
223+ make = bin_dir / "make"
224+ make .write_text (
225+ "#!/bin/sh\n "
226+ 'check="${UV_PYTHON:-}:$*"\n '
227+ 'printf "%s\\ n" "$check" >> "$CHECK_LOG"\n '
228+ '[ "$check" != "$FAILED_CHECK" ]\n ' ,
229+ encoding = "utf-8" ,
230+ )
231+ make .chmod (0o755 )
232+ log = tmp_path / "checks.log"
233+ result = subprocess .run (
234+ [bash , "--noprofile" , "--norc" , "-e" , "-o" , "pipefail" , "-c" , check ["run" ]],
235+ cwd = tmp_path ,
236+ env = {
237+ "PATH" : f"{ bin_dir } { os .pathsep } { os .defpath } " ,
238+ "CHECK_LOG" : str (log ),
239+ "FAILED_CHECK" : failed_check or "" ,
240+ ** check ["env" ],
241+ },
242+ capture_output = True ,
243+ text = True ,
244+ timeout = 5 ,
245+ )
246+ expected = [f"3.{ minor } :sync tests" for minor in range (10 , 15 )] + [":typecheck" ]
247+ if failed_check is None :
248+ assert result .returncode == 0 , result .stderr
249+ assert log .read_text (encoding = "utf-8" ).splitlines () == expected
250+ else :
251+ assert result .returncode != 0
252+ assert (
253+ log .read_text (encoding = "utf-8" ).splitlines ()
254+ == expected [: expected .index (failed_check ) + 1 ]
255+ )
256+
257+
258+ def test_release_checks_reject_stale_lockfile (tmp_path : Path ) -> None :
259+ uv = shutil .which ("uv" )
260+ if uv is None :
261+ pytest .skip ("The publish workflow requires uv." )
262+ workflow = yaml .safe_load (PUBLISH_WORKFLOW .read_text (encoding = "utf-8" ))
263+ check = next (
264+ step
265+ for step in workflow ["jobs" ]["checks" ]["steps" ]
266+ if step ["name" ] == "Check release source"
267+ )
268+ dependency = tmp_path / "dependency"
269+ dependency .mkdir ()
270+ (dependency / "pyproject.toml" ).write_text (
271+ '[project]\n name = "fixture-dependency"\n version = "0.1.0"\n ' , encoding = "utf-8"
272+ )
273+ project = (
274+ '[project]\n name = "release-fixture"\n version = "0.1.0"\n '
275+ 'requires-python = ">=3.10"\n dependencies = []\n '
276+ '[tool.uv.sources]\n fixture-dependency = { path = "dependency" }\n '
277+ )
278+ pyproject = tmp_path / "pyproject.toml"
279+ pyproject .write_text (project , encoding = "utf-8" )
280+ env = {
281+ "PATH" : os .defpath ,
282+ "UV_PYTHON" : sys .executable ,
283+ "UV_CACHE_DIR" : str (tmp_path / "cache" ),
284+ }
285+ if "SYSTEMROOT" in os .environ :
286+ env ["SYSTEMROOT" ] = os .environ ["SYSTEMROOT" ]
287+ command = [uv , "--offline" , "--no-config" ]
288+ subprocess .run (command + ["lock" ], cwd = tmp_path , env = env , check = True , timeout = 15 )
289+ lockfile = tmp_path / "uv.lock"
290+ original_lock = lockfile .read_bytes ()
291+ # Exercise synchronization without building or installing either fixture package.
292+ sync = command + ["sync" , "--no-install-project" , "--no-install-package" , "fixture-dependency" ]
293+ env .update (check ["env" ])
294+ subprocess .run (sync , cwd = tmp_path , env = env , check = True , timeout = 15 )
295+ pyproject .write_text (
296+ project .replace ("dependencies = []" , 'dependencies = ["fixture-dependency"]' ),
297+ encoding = "utf-8" ,
298+ )
299+ result = subprocess .run (sync , cwd = tmp_path , env = env , capture_output = True , text = True , timeout = 15 )
300+ assert result .returncode != 0
301+ assert "lockfile" in result .stderr and "needs to be updated" in result .stderr
302+ assert lockfile .read_bytes () == original_lock
303+
304+
305+ def test_release_build_is_isolated_from_test_execution () -> None :
306+ workflow = yaml .safe_load (PUBLISH_WORKFLOW .read_text (encoding = "utf-8" ))
307+ checks = workflow ["jobs" ]["checks" ]
308+ build = workflow ["jobs" ]["build" ]
309+ # Separate GitHub-hosted jobs provide fresh runners, not just new directories.
310+ assert checks ["runs-on" ] == build ["runs-on" ] == "ubuntu-latest"
311+ assert checks ["permissions" ] == build ["permissions" ] == {"contents" : "read" }
312+ assert "outputs" not in checks
313+ assert build ["needs" ] == "checks"
314+ for job in (checks , build ):
315+ assert "env" not in job and "container" not in job
316+ for step in job ["steps" ]:
317+ action = step .get ("uses" , "" )
318+ assert not action .startswith (("actions/cache@" , "actions/download-artifact@" ))
319+ if action .startswith ("astral-sh/setup-uv@" ):
320+ assert step ["with" ]["enable-cache" ] is False
321+ if job is checks :
322+ assert not action .startswith ("actions/upload-artifact@" )
323+ build_commands = [step ["run" ] for step in build ["steps" ] if "run" in step ]
324+ assert len (build_commands ) == 2 # Provenance validation, then packaging; no test execution.
325+ assert build_commands [- 1 ] == "uv build"
326+
327+
188328def test_pypi_job_only_publishes_the_build_artifact () -> None :
189329 workflow = PUBLISH_WORKFLOW .read_text (encoding = "utf-8" )
190330 publish = _workflow_job ("publish" , PUBLISH_WORKFLOW )
0 commit comments