Skip to content

Commit 618e8bd

Browse files
committed
fix: preserve invalid catalog root validation
1 parent dec57ea commit 618e8bd

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

src/specify_cli/integrations/catalog.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ def add_catalog(self, url: str, name: Optional[str] = None) -> None:
475475
raise IntegrationValidationError(
476476
f"Failed to read catalog config {config_path}: {exc}"
477477
) from exc
478+
if raw is None:
479+
raw = {}
478480
if not isinstance(raw, dict):
479481
raise IntegrationValidationError(
480482
"Catalog config file is corrupted (expected a mapping)."

tests/integrations/test_integration_catalog.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,40 @@ def test_add_catalog_creates_config_file(self, tmp_path, monkeypatch):
702702
active = cat.get_active_catalogs()
703703
assert [e.name for e in active] == ["mine"]
704704

705+
def test_add_catalog_recovers_from_empty_config_file(self, tmp_path, monkeypatch):
706+
self._isolate(tmp_path, monkeypatch)
707+
cfg_path = tmp_path / ".specify" / "integration-catalogs.yml"
708+
cfg_path.write_text("", encoding="utf-8")
709+
710+
cat = IntegrationCatalog(tmp_path)
711+
cat.add_catalog("https://example.com/catalog.json")
712+
713+
data = yaml.safe_load(cfg_path.read_text(encoding="utf-8"))
714+
assert data["catalogs"] == [
715+
{
716+
"name": "catalog-1",
717+
"url": "https://example.com/catalog.json",
718+
"priority": 1,
719+
"install_allowed": True,
720+
"description": "",
721+
}
722+
]
723+
724+
@pytest.mark.parametrize("config_content", ["[]\n", "false\n", "0\n", "''\n"])
725+
def test_add_catalog_rejects_falsy_non_mapping_config_roots(
726+
self, tmp_path, monkeypatch, config_content
727+
):
728+
self._isolate(tmp_path, monkeypatch)
729+
cfg_path = tmp_path / ".specify" / "integration-catalogs.yml"
730+
cfg_path.write_text(config_content, encoding="utf-8")
731+
732+
cat = IntegrationCatalog(tmp_path)
733+
with pytest.raises(
734+
IntegrationValidationError,
735+
match="corrupted.*expected a mapping",
736+
):
737+
cat.add_catalog("https://example.com/catalog.json")
738+
705739
def test_add_catalog_auto_derives_name_and_priority(self, tmp_path, monkeypatch):
706740
self._isolate(tmp_path, monkeypatch)
707741
cat = IntegrationCatalog(tmp_path)

0 commit comments

Comments
 (0)