π« Toolkit to help you get started with SDD or any other process!
fix(presets): start fresh on a non-UTF-8 preset registry (#3955)
Marsel Safin6e7818f83715
diff --git a/src/specify_cli/presets/__init__.py b/src/specify_cli/presets/__init__.pyindex 45c3456fe..cc98c4014 100644--- a/src/specify_cli/presets/__init__.py+++ b/src/specify_cli/presets/__init__.py@@ -550,7 +550,12 @@ class PresetRegistry:if not isinstance(data.get("presets"), dict):data["presets"] = {}return data- except (json.JSONDecodeError, FileNotFoundError):+ except (json.JSONDecodeError, UnicodeDecodeError, FileNotFoundError):+ # Corrupted or missing registry, start fresh. A registry whose+ # bytes cannot be decoded as UTF-8 is the same corruption class+ # as malformed JSON β only the exception type differs. OSError is+ # deliberately not caught: the data may be intact on disk, and+ # starting fresh would let a later _save() wipe it.return {"schema_version": self.SCHEMA_VERSION,"presets": {}diff --git a/tests/test_presets.py b/tests/test_presets.pyindex 6c6a1ed8f..41305518c 100644--- a/tests/test_presets.py+++ b/tests/test_presets.py@@ -515,6 +515,27 @@ class TestPresetRegistry:assert registry.list() == {}assert not registry.is_installed("test-pack")+ def test_load_starts_fresh_for_non_utf8_registry(self, temp_dir):+ """A registry file with undecodable bytes must start fresh, not raise.++ ``_load()`` already treats malformed JSON as "corrupted registry,+ start fresh", but a registry whose *bytes* cannot be decoded as UTF-8+ raised a raw ``UnicodeDecodeError`` from the same boundary β the same+ corruption class reaching a different exception type.+ """+ packs_dir = temp_dir / "packs"+ packs_dir.mkdir()+ (packs_dir / PresetRegistry.REGISTRY_FILE).write_bytes(+ b"\xff\xfe not utf-8 \xc3\x28"+ )++ registry = PresetRegistry(packs_dir)++ assert registry.data == {+ "schema_version": PresetRegistry.SCHEMA_VERSION,+ "presets": {},+ }+def test_add_and_get(self, temp_dir):"""Test adding and retrieving a pack."""packs_dir = temp_dir / "packs"