Skip to content

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

fix(presets): reject falsy non-mapping catalog roots (#4088)

Marsel Safineb5870085f67
diff --git a/src/specify_cli/presets/__init__.py b/src/specify_cli/presets/__init__.py
index b8c8f390d..181b1eeef 100644
--- a/src/specify_cli/presets/__init__.py
+++ b/src/specify_cli/presets/__init__.py
@@ -4299,11 +4299,13 @@ class PresetCatalog:
if not config_path.exists():
return None
try:
- data = yaml.safe_load(config_path.read_text(encoding="utf-8")) or {}
+ data = yaml.safe_load(config_path.read_text(encoding="utf-8"))
except (yaml.YAMLError, OSError, UnicodeError) as e:
raise PresetValidationError(
f"Failed to read catalog config {config_path}: {e}"
)
+ if data is None:
+ return None
if not isinstance(data, dict):
raise PresetValidationError(
f"Invalid catalog config {config_path}: expected a mapping at root, got {type(data).__name__}"
diff --git a/tests/test_presets.py b/tests/test_presets.py
index 8f9a2e358..749c96b78 100644
--- a/tests/test_presets.py
+++ b/tests/test_presets.py
@@ -3413,6 +3413,17 @@ class TestPresetCatalogMultiCatalog:
result = catalog._load_catalog_config(config_path)
assert result is None
+ @pytest.mark.parametrize("bad", [[], False, 0, ""])
+ def test_load_catalog_config_rejects_falsy_non_mapping_root(
+ self, project_dir, bad
+ ):
+ config_path = project_dir / ".specify" / "preset-catalogs.yml"
+ config_path.write_text(yaml.safe_dump(bad), encoding="utf-8")
+
+ catalog = PresetCatalog(project_dir)
+ with pytest.raises(PresetValidationError, match="expected a mapping"):
+ catalog._load_catalog_config(config_path)
+
def test_load_catalog_config_invalid_yaml(self, project_dir):
"""Test loading invalid YAML raises error."""
config_path = project_dir / ".specify" / "preset-catalogs.yml"