from __future__ import annotations from pathlib import Path from stewardship.discovery import build_catalog_index FIXTURES = Path(__file__).parent / "fixtures" def test_catalog_finds_all_positive_fixtures_and_is_deterministic(tmp_path, make_repo): make_repo("positive/infra-lxd") make_repo("positive/service-antivirus") make_repo("positive/software-example") make_repo("positive/project-example") catalog1, findings1 = build_catalog_index([tmp_path]) catalog2, _findings2 = build_catalog_index([tmp_path]) names = {r["ref"] for r in catalog1["spec"]["repositories"]} assert names == { "repo:infra-lxd", "repo:services-antivirus", "repo:example-software", "repo:example-project", } assert not any(f.severity == "error" for f in findings1) # Deterministic rebuild ignoring the volatile generatedAt timestamp. repos1 = catalog1["spec"]["repositories"] repos2 = catalog2["spec"]["repositories"] assert repos1 == repos2 assert catalog1["spec"]["relations"] == catalog2["spec"]["relations"] def test_catalog_matches_requirement_to_capability(tmp_path, make_repo): make_repo("positive/infra-lxd") make_repo("positive/service-antivirus") catalog, _ = build_catalog_index([tmp_path]) relations = catalog["spec"]["relations"] matches = [r for r in relations if r["type"] == "matches"] assert any( r["from"] == "requirement:services-antivirus/control-plane-container" and r["to"] == "capability:infra-lxd/container" for r in matches ) def test_catalog_detects_duplicate_repository_name(tmp_path, make_repo): first = make_repo("positive/infra-lxd", git=False) import shutil second_parent = tmp_path / "duplicate-holder" second_parent.mkdir() second = second_parent / "infra-lxd-copy" shutil.copytree(first, second) import subprocess for repo in (first, second): subprocess.run(["git", "init", "-q"], cwd=repo, check=True) subprocess.run(["git", "config", "user.email", "test@example.com"], cwd=repo, check=True) subprocess.run(["git", "config", "user.name", "Test"], cwd=repo, check=True) subprocess.run(["git", "add", "-A"], cwd=repo, check=True) subprocess.run(["git", "commit", "-q", "-m", "baseline"], cwd=repo, check=True) _, findings = build_catalog_index([tmp_path]) assert any(f.code == "CATALOG_DUPLICATE_REPOSITORY_NAME" for f in findings) def test_a_contract_inside_another_repository_is_not_a_repository(tmp_path, make_repo) -> None: """A fixture, example or vendored copy is content, not a peer. This project's own `tests/fixtures/positive/` holds four valid contracts. They appeared in the catalog beside production repositories and were indistinguishable from them, which corrupts every catalog-wide question. """ outer = make_repo("positive/project-example") nested = outer / "examples" / "vendored" (nested / "ops").mkdir(parents=True) (nested / "ops" / "contract.yaml").write_text( (FIXTURES / "positive" / "software-example" / "ops" / "contract.yaml").read_text( encoding="utf-8" ), encoding="utf-8", ) index, findings = build_catalog_index([outer]) refs = {entry["ref"] for entry in index["spec"]["repositories"]} assert refs == {"repo:example-project"} skipped = [f for f in findings if f.code == "CATALOG_NESTED_CONTRACT_SKIPPED"] assert len(skipped) == 1 assert "vendored" in skipped[0].path