Skip to content

Commit a21a34f

Browse files
CrazyBaranclaude
andcommitted
test(extensions): pin real-data update delivery and example bundle pins
Address Copilot review on github#4394. The version bumps are only useful if a copy installed before them is actually offered an update, and until now nothing proved that with the real data: unit tests drive the local-package update route with synthetic extensions, and the version contract only checks catalog/manifest equality. Add tests/contract/test_bundled_extension_update_delivery.py, which installs each bundled extension's real source with its manifest rewritten to the pre-bump 1.0.0, runs `extension update` against the real catalog entry and the real bundled copy (download must never be attempted), and asserts the install reaches the catalog version. The three extensions that had drifted at 1.0.0 when github#4345 was filed (agent-context, git, assess) must stay bumped past it - reverting one is a failure, not a skip - while an unbumped extension (bug) skips. A companion test asserts an install already at the bundled version is reported up to date, so fresh installs cannot loop. Fixtures now derive their pins from the bundled manifest, so the four changed examples/bundles/*/bundle.yml pins were no longer exercised by any test even though BundleExtensionPrimitive enforces exact pins. Add a contract test that parses every example bundle and checks each bundled extension pin against its manifest version; it fails when an example is left on a previous version. Refs github#4345 Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 7dd6dbe commit a21a34f

2 files changed

Lines changed: 182 additions & 0 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
"""Contract tests: previously stale bundled installs are now offered an update.
2+
3+
Every bundled extension shipped at 1.0.0 from its creation while its content
4+
kept changing, so installed copies were reported "Up to date" forever
5+
(#4345). The version bumps in extensions/*/extension.yml and the synced
6+
extensions/catalog.json are what finally make `specify extension update`
7+
offer those installs a newer version, and the local-package route installs
8+
it from the copy bundled with the running spec-kit.
9+
10+
Unit tests cover that route with synthetic extensions and versions, and the
11+
version contract only checks catalog/manifest equality. These tests close
12+
the gap with the real data: install each bundled extension's real source
13+
at the pre-bump 1.0.0, run `extension update` against the real catalog
14+
entry and the real bundled copy, and assert it reaches the catalog version.
15+
"""
16+
17+
from __future__ import annotations
18+
19+
import json
20+
import shutil
21+
from pathlib import Path
22+
from unittest.mock import patch
23+
24+
import pytest
25+
import yaml
26+
from packaging.version import Version
27+
from typer.testing import CliRunner
28+
29+
REPO_ROOT = Path(__file__).parents[2]
30+
EXTENSIONS_ROOT = REPO_ROOT / "extensions"
31+
PRE_BUMP_VERSION = "1.0.0"
32+
# Bundled extensions whose content had drifted while still declaring 1.0.0
33+
# when #4345 was filed. Their catalog version must stay above PRE_BUMP_VERSION
34+
# so a copy installed before the fix is actually offered an update; a version
35+
# that is not bumped past it here is a regression, not a skip.
36+
DRIFTED_BEFORE_BUMP = frozenset({"agent-context", "assess", "git"})
37+
38+
39+
def _catalog_entries() -> dict[str, dict]:
40+
catalog = json.loads((EXTENSIONS_ROOT / "catalog.json").read_text(encoding="utf-8"))
41+
return catalog["extensions"]
42+
43+
44+
def _bundled_ids() -> list[str]:
45+
return sorted(
46+
ext_id
47+
for ext_id, entry in _catalog_entries().items()
48+
if entry.get("bundled")
49+
and not entry.get("download_url")
50+
and (EXTENSIONS_ROOT / ext_id / "extension.yml").is_file()
51+
)
52+
53+
54+
def _make_project(tmp_path: Path) -> Path:
55+
project_dir = tmp_path / "project"
56+
project_dir.mkdir()
57+
(project_dir / ".specify").mkdir()
58+
(project_dir / ".claude" / "skills").mkdir(parents=True)
59+
return project_dir
60+
61+
62+
def _stale_copy(tmp_path: Path, ext_id: str, version: str) -> Path:
63+
"""The real bundled source with only its manifest version rewritten."""
64+
source = tmp_path / "stale" / ext_id
65+
shutil.copytree(EXTENSIONS_ROOT / ext_id, source)
66+
manifest_path = source / "extension.yml"
67+
data = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
68+
data["extension"]["version"] = version
69+
manifest_path.write_text(yaml.safe_dump(data, sort_keys=False), encoding="utf-8")
70+
return source
71+
72+
73+
def _run_update(project_dir: Path, ext_id: str):
74+
from specify_cli import app
75+
from specify_cli.extensions import ExtensionCatalog
76+
77+
catalog_info = dict(_catalog_entries()[ext_id])
78+
catalog_info.setdefault("_install_allowed", True)
79+
# Real catalog entry and real bundled copy; only the network fetch of the
80+
# catalog is replaced, and downloading must never be attempted for a
81+
# bundled extension.
82+
with patch.object(Path, "cwd", return_value=project_dir), \
83+
patch.object(ExtensionCatalog, "get_extension_info", return_value=catalog_info), \
84+
patch.object(
85+
ExtensionCatalog,
86+
"download_extension",
87+
side_effect=AssertionError("bundled update must not download"),
88+
):
89+
return CliRunner().invoke(
90+
app, ["extension", "update", ext_id], input="y\n", catch_exceptions=True
91+
)
92+
93+
94+
def test_bundled_ids_present():
95+
assert _bundled_ids(), "expected bundled extensions with in-repo sources"
96+
97+
98+
@pytest.mark.parametrize("ext_id", _bundled_ids())
99+
def test_stale_bundled_install_is_updated_to_catalog_version(tmp_path: Path, ext_id: str):
100+
from specify_cli._assets import get_speckit_version
101+
from specify_cli.extensions import ExtensionManager
102+
103+
catalog_version = Version(_catalog_entries()[ext_id]["version"])
104+
if catalog_version <= Version(PRE_BUMP_VERSION):
105+
assert ext_id not in DRIFTED_BEFORE_BUMP, (
106+
f"'{ext_id}' drifted at {PRE_BUMP_VERSION} before #4345 but the catalog still "
107+
f"advertises {catalog_version}; installs made before the fix would never be "
108+
f"offered the shipped changes"
109+
)
110+
pytest.skip(f"'{ext_id}' has not been bumped past {PRE_BUMP_VERSION}; nothing to deliver")
111+
112+
project_dir = _make_project(tmp_path)
113+
stale_source = _stale_copy(tmp_path, ext_id, PRE_BUMP_VERSION)
114+
manager = ExtensionManager(project_dir)
115+
manager.install_from_directory(stale_source, get_speckit_version())
116+
assert manager.registry.get(ext_id)["version"] == PRE_BUMP_VERSION
117+
118+
result = _run_update(project_dir, ext_id)
119+
120+
flat = " ".join(result.output.split())
121+
assert result.exit_code == 0, result.output
122+
assert f"Updated to v{catalog_version}" in flat, flat
123+
assert "Up to date" not in flat, flat
124+
assert ExtensionManager(project_dir).registry.get(ext_id)["version"] == str(catalog_version)
125+
126+
127+
@pytest.mark.parametrize("ext_id", _bundled_ids())
128+
def test_current_bundled_install_is_up_to_date(tmp_path: Path, ext_id: str):
129+
"""The bumped catalog must not re-offer an update to an install that
130+
already carries the bundled version, or every fresh install would loop."""
131+
from specify_cli._assets import get_speckit_version
132+
from specify_cli.extensions import ExtensionManager
133+
134+
catalog_version = _catalog_entries()[ext_id]["version"]
135+
project_dir = _make_project(tmp_path)
136+
ExtensionManager(project_dir).install_from_directory(
137+
EXTENSIONS_ROOT / ext_id, get_speckit_version()
138+
)
139+
140+
result = _run_update(project_dir, ext_id)
141+
142+
flat = " ".join(result.output.split())
143+
assert result.exit_code == 0, result.output
144+
assert f"Up to date (v{catalog_version})" in flat, flat
145+
assert ExtensionManager(project_dir).registry.get(ext_id)["version"] == catalog_version

‎tests/contract/test_bundled_extension_versions.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
REPO_ROOT = Path(__file__).parents[2]
2727
EXTENSIONS_ROOT = REPO_ROOT / "extensions"
28+
EXAMPLE_BUNDLES_ROOT = REPO_ROOT / "examples" / "bundles"
2829

2930

3031
def _catalog_entries() -> dict[str, dict]:
@@ -81,3 +82,39 @@ def test_catalog_version_is_valid_pep440(ext_id: str):
8182
f"extensions/catalog.json entry '{ext_id}' version {version!r} is not a valid "
8283
f"PEP 440 version ({exc}); `extension update` would skip it"
8384
)
85+
86+
87+
def _example_bundle_extension_pins() -> list[tuple[str, str, str]]:
88+
"""(bundle name, extension id, pinned version) for every example bundle."""
89+
pins = []
90+
for manifest_path in sorted(EXAMPLE_BUNDLES_ROOT.glob("*/bundle.yml")):
91+
data = yaml.safe_load(manifest_path.read_text(encoding="utf-8"))
92+
for entry in (data.get("provides") or {}).get("extensions") or []:
93+
pins.append((manifest_path.parent.name, entry["id"], str(entry.get("version"))))
94+
return pins
95+
96+
97+
def test_example_bundles_pin_extensions():
98+
assert _example_bundle_extension_pins(), "expected at least one extension pin in examples/bundles"
99+
100+
101+
@pytest.mark.parametrize(
102+
"bundle_name,ext_id,pinned",
103+
_example_bundle_extension_pins(),
104+
ids=lambda v: v if isinstance(v, str) else str(v),
105+
)
106+
def test_example_bundle_pins_match_bundled_manifest(bundle_name: str, ext_id: str, pinned: str):
107+
"""``BundleExtensionPrimitive`` refuses to install when the resolved
108+
extension version differs from the bundle's pin, so an example bundle that
109+
still pins a bundled extension's previous version fails for every user who
110+
installs it. Test fixtures derive their pins from the bundled manifest and
111+
therefore cannot catch this; the shipped examples must be checked directly."""
112+
manifest_path = EXTENSIONS_ROOT / ext_id / "extension.yml"
113+
if not manifest_path.is_file():
114+
pytest.skip(f"'{ext_id}' is not a bundled extension; pin resolves via the catalog at install time")
115+
manifest_version = _manifest_version(ext_id)
116+
assert Version(pinned) == Version(manifest_version), (
117+
f"examples/bundles/{bundle_name}/bundle.yml pins '{ext_id}' to {pinned} but "
118+
f"{manifest_path.relative_to(REPO_ROOT)} declares {manifest_version}; the bundler "
119+
f"enforces exact pins, so installing this example would fail"
120+
)

0 commit comments

Comments
 (0)