|
| 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 |
0 commit comments