Skip to content

Commit cb29efc

Browse files
CrazyBaranclaude
andcommitted
fix(ci): validate every catalog version, not only those of changed extensions
Address the "needs a closer look" items from the Copilot reviews on #4395. Invariant 2 skipped catalog entries without an in-repo directory before parsing their version, and never parsed the in-repo manifest version at all - PEP 440 validation lived only in Invariant 1, which sees just the IDs with changed files under extensions/<id>/. Two gaps followed: - a hosted (catalog-only) entry could carry `"version": "not-a-version"` and pass, although `extension update` skips exactly such entries when packaging.Version fails; - promoting an existing uncataloged directory by adding only its catalog entry never entered Invariant 1, so a matching invalid string in manifest and catalog passed the plain equality check. Invariant 2 now runs over every catalog entry: the catalog version must be a non-empty string that parses as PEP 440, and for entries with an in-repo directory the manifest version must parse as well before the equality check. The catalog-side parse alone closes the promotion gap (a valid catalog string plus equality forces a valid manifest string); the manifest parse makes the failure name the manifest precisely. Tests: `_run_guard` now omits the head argument by default, matching the workflow's one-argument invocation, with a separate test for the optional HEAD_REF; new cases cover the hosted-entry and both promotion shapes (the two invalid ones fail against the previous script) plus a valid promotion that must keep passing. The working-tree contract test gains the matching every-catalog-version-parses check. Refs #4345 Assisted-by: Claude Code (model: claude-fable-5-1) Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent f6f9dd9 commit cb29efc

3 files changed

Lines changed: 156 additions & 12 deletions

File tree

‎.github/scripts/check_extension_version_bump.py‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
version must itself parse as PEP 440 — including for a brand-new
1919
extension, since the CLI rejects a manifest whose version it cannot
2020
parse.
21-
2. The `version` in `extensions/catalog.json` must equal the manifest's
22-
`extension.version` (the catalog is what update checks compare
23-
against, and the update preflight rejects a manifest whose version
24-
differs from the catalog's).
21+
2. Every `version` in `extensions/catalog.json` must parse as PEP 440
22+
(`extension update` skips entries it cannot parse), and for entries
23+
with an in-repo directory it must equal a likewise-valid
24+
`extension.version` in the manifest (the catalog is what update
25+
checks compare against, and the update preflight rejects a manifest
26+
whose version differs from the catalog's). This runs over every
27+
catalog entry, so catalog-only (hosted) entries and catalog-only
28+
promotions of existing directories are covered too.
2529
2630
Usage:
2731
check_extension_version_bump.py BASE_REF [HEAD_REF]
@@ -172,18 +176,44 @@ def main(argv: list[str]) -> int:
172176
f"Installed copies only receive changes when the version is bumped."
173177
)
174178

175-
# -- Invariant 2: catalog.json version matches the manifest ------------
179+
# -- Invariant 2: catalog versions are valid and match the manifests ----
180+
# Runs over every catalog entry, changed or not: a catalog-only entry
181+
# (hosted elsewhere) never has files under extensions/<id>/, and a
182+
# catalog-only promotion of an existing uncataloged directory never
183+
# enters Invariant 1, so neither would otherwise have its version parsed.
176184
for ext_id, entry in sorted(catalog_entries.items()):
185+
catalog_version = entry.get("version") if isinstance(entry, dict) else None
186+
if not isinstance(catalog_version, str) or not catalog_version.strip():
187+
errors.append(f"{CATALOG_PATH}: entry '{ext_id}' has no string 'version'")
188+
continue
189+
# `extension update` skips a catalog entry whose version packaging
190+
# cannot parse, so an invalid catalog version is never offered.
191+
try:
192+
Version(catalog_version)
193+
except InvalidVersion as exc:
194+
errors.append(
195+
f"{CATALOG_PATH}: entry '{ext_id}' version {catalog_version!r} is not a "
196+
f"valid PEP 440 version ({exc}); `extension update` skips such entries."
197+
)
198+
continue
199+
177200
manifest_path = f"{EXTENSIONS_ROOT}/{ext_id}/extension.yml"
178201
head_manifest = _show(head_ref, manifest_path)
179202
if head_manifest is None:
180-
continue # catalog-only entry (e.g. hosted elsewhere)
203+
continue # catalog-only entry (e.g. hosted elsewhere); version checked above
181204
try:
182205
manifest_version = _manifest_version(head_manifest, f"{head_ref}:{manifest_path}")
183206
except ValueError as exc:
184207
errors.append(str(exc))
185208
continue
186-
catalog_version = entry.get("version")
209+
try:
210+
Version(manifest_version)
211+
except InvalidVersion as exc:
212+
errors.append(
213+
f"{manifest_path}: extension.version {manifest_version!r} is not a valid "
214+
f"PEP 440 version ({exc}); the CLI rejects this manifest."
215+
)
216+
continue
187217
if catalog_version != manifest_version:
188218
errors.append(
189219
f"{CATALOG_PATH}: entry '{ext_id}' has version {catalog_version!r} but "

‎tests/contract/test_bundled_extension_versions.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import pytest
2323
import yaml
24+
from packaging.version import InvalidVersion, Version
2425

2526
REPO_ROOT = Path(__file__).parents[2]
2627
EXTENSIONS_ROOT = REPO_ROOT / "extensions"
@@ -62,3 +63,21 @@ def test_bundled_entries_ship_an_extension_directory(ext_id: str):
6263
assert (EXTENSIONS_ROOT / ext_id / "extension.yml").is_file(), (
6364
f"catalog marks '{ext_id}' as bundled but extensions/{ext_id}/extension.yml is missing"
6465
)
66+
67+
68+
@pytest.mark.parametrize("ext_id", sorted(_catalog_entries()))
69+
def test_catalog_version_is_valid_pep440(ext_id: str):
70+
"""`extension update` parses each catalog version with packaging and skips
71+
entries it cannot parse, so every entry - bundled or hosted - must carry
72+
a valid version, independent of whether it has an in-repo directory."""
73+
version = _catalog_entries()[ext_id].get("version")
74+
assert isinstance(version, str) and version.strip(), (
75+
f"extensions/catalog.json entry '{ext_id}' has no string version"
76+
)
77+
try:
78+
Version(version)
79+
except InvalidVersion as exc:
80+
pytest.fail(
81+
f"extensions/catalog.json entry '{ext_id}' version {version!r} is not a valid "
82+
f"PEP 440 version ({exc}); `extension update` would skip it"
83+
)

‎tests/contract/test_extension_version_guard_script.py‎

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,19 @@ def _write_extension(repo: Path, ext_id: str, version: str, script_line: str) ->
4646
(ext_dir / "script.sh").write_text(f"{script_line}\n", encoding="utf-8")
4747

4848

49-
def _write_catalog(repo: Path, versions: dict[str, str]) -> None:
49+
def _write_catalog(repo: Path, versions: dict[str, str | dict]) -> None:
50+
"""Write catalog.json; a value is either a bundled entry's version string
51+
or a full entry dict (for e.g. hosted, non-bundled entries)."""
5052
(repo / "extensions").mkdir(exist_ok=True)
5153
payload = {
5254
"schema_version": "1.0",
5355
"extensions": {
54-
ext_id: {"id": ext_id, "version": version, "bundled": True}
55-
for ext_id, version in versions.items()
56+
ext_id: (
57+
{"id": ext_id, **spec}
58+
if isinstance(spec, dict)
59+
else {"id": ext_id, "version": spec, "bundled": True}
60+
)
61+
for ext_id, spec in versions.items()
5662
},
5763
}
5864
(repo / "extensions" / "catalog.json").write_text(
@@ -86,9 +92,14 @@ def guard_repo(tmp_path: Path) -> tuple[Path, str]:
8692
return repo, base_sha
8793

8894

89-
def _run_guard(repo: Path, base: str, head: str = "HEAD") -> subprocess.CompletedProcess:
95+
def _run_guard(
96+
repo: Path, base: str, head: str | None = None
97+
) -> subprocess.CompletedProcess:
98+
"""Invoke the guard as the workflow does: base ref only, head defaulting
99+
to HEAD inside the script. Pass *head* explicitly only to test the
100+
optional second argument."""
90101
return subprocess.run(
91-
[sys.executable, str(SCRIPT), base, head],
102+
[sys.executable, str(SCRIPT), base, *([head] if head else [])],
92103
cwd=repo,
93104
capture_output=True,
94105
text=True,
@@ -229,6 +240,90 @@ def test_new_cataloged_extension_passes_without_base_version(guard_repo):
229240
assert "all invariants hold" in result.stdout
230241

231242

243+
def test_explicit_head_argument_is_honored(guard_repo):
244+
"""The optional HEAD_REF argument must select the head to check: pointing
245+
it at the base commit yields an empty diff even though HEAD has an
246+
unbumped change."""
247+
repo, base = guard_repo
248+
_write_extension(repo, "demo", "1.0.0", "echo changed")
249+
_commit_all(repo, "content change without bump")
250+
251+
result = _run_guard(repo, base, head=base)
252+
assert result.returncode == 0, result.stdout + result.stderr
253+
assert "all invariants hold" in result.stdout
254+
255+
256+
def test_catalog_only_entry_with_unparseable_version_fails(guard_repo):
257+
"""A hosted (non-bundled) entry has no in-repo directory, so nothing under
258+
extensions/ changes and Invariant 1 never sees it; its catalog version
259+
must still parse because `extension update` skips entries it cannot."""
260+
repo, base = guard_repo
261+
_write_catalog(
262+
repo,
263+
{
264+
"demo": "1.0.0",
265+
"hosted": {
266+
"version": "not-a-version",
267+
"bundled": False,
268+
"download_url": "https://example.com/hosted-1.0.0.zip",
269+
},
270+
},
271+
)
272+
_commit_all(repo, "add hosted entry with invalid version")
273+
274+
result = _run_guard(repo, base)
275+
assert result.returncode == 1, result.stdout + result.stderr
276+
assert "entry 'hosted'" in result.stdout
277+
assert "is not a valid PEP 440 version" in result.stdout
278+
279+
280+
def test_catalog_only_promotion_with_unparseable_version_fails(guard_repo):
281+
"""Promoting an existing uncataloged directory by adding only its catalog
282+
entry changes nothing under extensions/<id>/, so Invariant 1 skips it;
283+
matching invalid strings must still fail because the CLI cannot use them.
284+
The catalog-version parse rejects the entry first."""
285+
repo, _ = guard_repo
286+
_write_extension(repo, "draft", "not-a-version", "echo draft")
287+
base = _commit_all(repo, "uncataloged draft with invalid version on base")
288+
289+
_write_catalog(repo, {"demo": "1.0.0", "draft": "not-a-version"})
290+
_commit_all(repo, "promote draft via catalog only")
291+
292+
result = _run_guard(repo, base)
293+
assert result.returncode == 1, result.stdout + result.stderr
294+
assert "entry 'draft'" in result.stdout
295+
assert "is not a valid PEP 440 version" in result.stdout
296+
297+
298+
def test_catalog_only_promotion_with_invalid_manifest_version_fails(guard_repo):
299+
"""Same promotion, but the catalog carries a valid version while the
300+
untouched in-repo manifest does not: Invariant 2 must parse the manifest
301+
itself (not only compare strings) and name the manifest in the error."""
302+
repo, _ = guard_repo
303+
_write_extension(repo, "draft", "not-a-version", "echo draft")
304+
base = _commit_all(repo, "uncataloged draft with invalid version on base")
305+
306+
_write_catalog(repo, {"demo": "1.0.0", "draft": "1.0.0"})
307+
_commit_all(repo, "promote draft with a valid catalog version only")
308+
309+
result = _run_guard(repo, base)
310+
assert result.returncode == 1, result.stdout + result.stderr
311+
assert "extensions/draft/extension.yml" in result.stdout
312+
assert "is not a valid PEP 440 version" in result.stdout
313+
314+
315+
def test_catalog_only_promotion_with_valid_version_passes(guard_repo):
316+
"""The uncataloged `scratch` fixture carries a valid 1.0.0; cataloging it
317+
without touching its directory is a legitimate promotion."""
318+
repo, base = guard_repo
319+
_write_catalog(repo, {"demo": "1.0.0", "scratch": "1.0.0"})
320+
_commit_all(repo, "promote scratch via catalog only")
321+
322+
result = _run_guard(repo, base)
323+
assert result.returncode == 0, result.stdout + result.stderr
324+
assert "all invariants hold" in result.stdout
325+
326+
232327
def test_unparseable_version_fails_closed(guard_repo):
233328
repo, base = guard_repo
234329
_write_extension(repo, "demo", "not-a-version", "echo changed")

0 commit comments

Comments
 (0)