Coverage for packages/workshop/src/livery/workshop/_verify.py: 95%

Measured by coverage.py 7.16.2 on CPython 3.14 while the suites of tests, packages/workshop, packages/extensions/ty, packages/extensions/ruff, packages/extensions/python, packages/extensions/pytest, packages/extensions/pyrefly, packages/extensions/nanobind, packages/extensions/mypy, packages/extensions/mkdocstrings, packages/extensions/lfs, packages/extensions/docs, packages/extensions/cpp, packages/extensions/coverage, packages/extensions/conan, packages/extensions/cmake, packages/extensions/claude, packages/extensions/clang-tidy, packages/extensions/clang-format, packages/extensions/changelog, packages/extensions/basedpyright ran.

1"""The verify phase's flow: a member's published wheel against its own tests.
2
3The pairing under test is the one a consumer gets: the wheel the
4index serves, installed into a plain virtual environment beside the
5tests recorded for that same version, so the tree is checked out at
6the release tag, never at main, and the package must import from
7site-packages, never from the workspace. A red verification means a
8dependency release broke the installed configuration or the index
9artifact drifted; the failure path files or extends the marker
10issue through the forge, searching for the marker first, so a red
11night is one issue with one comment per failure, never a pile.
12
13Reach for [livery.workshop._verify.verify_flow][]; `fm release.verify`
14is its verb, and the nightly point schedules it through the
15contract's ``[[ci.schedule]]`` seam.
16"""
17
18from __future__ import annotations(empty)
19
20import contextlib(empty)
21import os(empty)
22import re(empty)
23import shutil(empty)
24from dataclasses import dataclass(empty)
25from pathlib import Path(empty)
26from typing import TYPE_CHECKING, Protocol(empty)
27
28from livery import git(empty)
29from livery.footman import fail, scratch_dir(empty)
30
31if TYPE_CHECKING:(empty)
32 from livery.forge import Repository
33 from livery.workshop._packages import Package
34
35#: The issue the failure path files or extends; the search key.
36MARKER = "nightly: released-wheels verification failed"(empty)
37
38_RELEASE_RE = re.compile(r"^\d+\.\d+\.\d+$")(empty)
39
40
41class Registry(Protocol):(empty)
42 """The one probe the verification needs; livery.forge.SimpleRegistry fits."""
43
44 def versions(self, name: str) -> tuple[str, ...]:(empty)
45 """The published versions of *name*."""
46 ...
47
48
49@dataclass(frozen=True)(empty)
50class Verification:(empty)
51 """What one verification is about.
52
53 Attributes:
54 member: The member's directory name under ``packages/``.
55 dist: Its distribution name.
56 version: The released version under test.
57 extras: The extras to install with it, comma-joined; empty for none.
58 """
59
60 member: str(empty)
61 dist: str(empty)
62 version: str(empty)
63 extras: str = ""(empty)
64
65 @property(empty)
66 def tag(self) -> str:(empty)
67 """The receipt tag the tree is checked out at."""
68 return f"packages/{self.member}/v{self.version}"
69
70 @property(empty)
71 def requirement(self) -> str:(empty)
72 """The exact requirement the plain environment installs."""
73 extras = f"[{self.extras}]" if self.extras else ""
74 return f"{self.dist}{extras}=={self.version}"
75
76
77def latest_release(registry: Registry, dist: str) -> str:(empty)
78 """The newest final release the index serves for *dist*; refuses when none.
79
80 Only ``x.y.z`` versions count: a dev or pre-release is never the
81 consumer's pairing.
82 """
83 finals = [v for v in registry.versions(dist) if _RELEASE_RE.match(v)]
84 if not finals:
85 fail(f"the index serves no released version of {dist}: nothing to verify")
86 return max(finals, key=lambda v: tuple(int(part) for part in v.split(".")))
87
88
89def run_url() -> str:(empty)
90 """The run's page from the runner's environment; empty outside CI."""
91 server = os.environ.get("GITHUB_SERVER_URL", "")
92 repository = os.environ.get("GITHUB_REPOSITORY", "")
93 run_id = os.environ.get("GITHUB_RUN_ID", "")
94 if server and repository and run_id:94 ↛ 96line 94 didn't jump to line 96 because the condition on line 94 was always true
95 return f"{server}/{repository}/actions/runs/{run_id}"
96 return ""
97
98
99def report_failure(repo: Repository, verification: Verification, *, url: str) -> str:(empty)
100 """File or extend the marker issue; the line saying which, or the refusal."""
101 from livery.forge import ForgeError
102
103 evidence = f"{verification.requirement} failed its verification" + (
104 f": {url}" if url else ""
105 )
106 try:
107 existing = repo.issue.search(MARKER)
108 if existing:
109 repo.issue.comment(existing[0].number, f"Failed again: {evidence}")
110 return f" commented on #{existing[0].number}: {evidence}"
111 created = repo.issue.create(
112 MARKER,
113 body=(
114 "The released-wheels verification is red: the wheel on the"
115 " index does not pass its own recorded tests in the consumer"
116 f" configuration.\n\n{evidence}\n\nThe verification installs"
117 " the released wheel into a plain virtual environment and runs"
118 " the member's tests at the release tag. A red verification"
119 " means a dependency release broke the installed configuration,"
120 " or the index artifact drifted; the run's logs carry the"
121 " failing tests."
122 ),
123 )
124 return f" filed #{created.number}: {MARKER}"
125 except ForgeError as error:
126 return f" the forge refused the issue: {error}"
127
128
129def verify_flow((empty)
130 root: Path,
131 package: Package,
132 *,
133 python: str,
134 registry: Registry,
135 index: str,
136 extras: str = "",
137 repo: Repository | None = None,
138) -> None:
139 """Verify *package*'s latest release on *python*; red files the issue and fails.
140
141 The release is the newest final version *registry* serves; the
142 tree is a temporary worktree at its receipt tag, refused when
143 the checkout lacks the tag. The install and the test run are the
144 package's ``verify`` phase, through
145 [livery.workshop._lifecycle.verify][]. With *repo*, a red
146 verification files or extends the marker issue before failing.
147 """
148 from livery.workshop import _lifecycle
149
150 member = package.member
151 verification = Verification(
152 member, package.name, latest_release(registry, package.name), extras
153 )
154 if verification.tag not in git.tags(root):
155 fail(
156 f"the checkout carries no tag {verification.tag}: fetch the tags"
157 " (git fetch --tags), or the release is not this repository's"
158 )
159 print(
160 f" verifying {verification.requirement} at {verification.tag} on python"
161 f" {python}"
162 )
163 scratch = scratch_dir("verify")
164 tree = scratch / "tree"
165 try:
166 git.worktree_add(root, tree, verification.tag, detach=True)
167 except git.GitError as error:
168 fail(str(error))
169 try:
170 code = _lifecycle.verify(
171 package,
172 root,
173 tree=tree,
174 version=verification.version,
175 python=python,
176 index=index,
177 extras=extras,
178 )
179 finally:
180 with contextlib.suppress(git.GitError):
181 git.worktree_remove(root, tree)
182 shutil.rmtree(scratch, ignore_errors=True)
183 if code == 0:
184 print(
185 f" green: {verification.requirement} passes its own tests from"
186 " site-packages"
187 )
188 return
189 if repo is not None:
190 print(report_failure(repo, verification, url=run_url()))
191 fail(f"the verification of {verification.requirement} is red (exit {code})")