Skip to content

💫 Toolkit to help you get started with SDD or any other process!

fix(workflows): reject malformed step config on add (#4087)

Marsel Safin3eec154aca7b
diff --git a/src/specify_cli/workflows/catalog.py b/src/specify_cli/workflows/catalog.py
index f494848a0..5fffa4b45 100644
--- a/src/specify_cli/workflows/catalog.py
+++ b/src/specify_cli/workflows/catalog.py
@@ -1396,12 +1396,14 @@ class StepCatalog:
data: dict[str, Any] = {"catalogs": []}
if config_path.exists():
try:
- raw = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {}
+ raw = yaml.safe_load(config_path.read_text(encoding="utf-8"))
except (yaml.YAMLError, OSError, UnicodeDecodeError) as exc:
raise StepValidationError(
f"Catalog config file is unreadable or malformed: {exc}"
) from exc
- if not isinstance(raw, dict):
+ if raw is None:
+ raw = {}
+ elif not isinstance(raw, dict):
raise StepValidationError(
"Catalog config file is corrupted (expected a mapping)."
)
diff --git a/tests/test_workflows.py b/tests/test_workflows.py
index 686298611..41e47e1a6 100644
--- a/tests/test_workflows.py
+++ b/tests/test_workflows.py
@@ -9102,6 +9102,23 @@ class TestStepCatalog:
assert len(data["catalogs"]) == 1
assert data["catalogs"][0]["url"] == "https://example.com/steps.json"
+ @pytest.mark.parametrize("bad", [[], False, 0, ""])
+ def test_add_catalog_rejects_falsy_non_mapping_config(
+ self, project_dir, bad
+ ):
+ from specify_cli.workflows.catalog import StepCatalog, StepValidationError
+
+ config_path = project_dir / ".specify" / "step-catalogs.yml"
+ original = yaml.safe_dump(bad)
+ config_path.write_text(original, encoding="utf-8")
+
+ with pytest.raises(StepValidationError, match="expected a mapping"):
+ StepCatalog(project_dir).add_catalog(
+ "https://example.com/steps.json", "my-steps"
+ )
+
+ assert config_path.read_text(encoding="utf-8") == original
+
def test_add_catalog_duplicate_rejected(self, project_dir):
from specify_cli.workflows.catalog import StepCatalog, StepValidationError