diff --git a/src/onedep_lib/schemas/local.py b/src/onedep_lib/schemas/local.py index 50db9d8..53f19f1 100644 --- a/src/onedep_lib/schemas/local.py +++ b/src/onedep_lib/schemas/local.py @@ -13,6 +13,11 @@ def __init__(self, cache_dir: Path) -> None: def get_schema(self, schema_name: str) -> dict: cache_path = self._cache_dir / f"{schema_name}.json" if cache_path.exists(): - with cache_path.open() as f: - return json.load(f) + try: + with cache_path.open() as f: + return json.load(f) + except (ValueError, OSError) as exc: + raise SchemaError( + f"Schema '{schema_name}' is corrupted or unreadable: {exc}" + ) from exc raise SchemaError(f"Schema '{schema_name}' not available") diff --git a/tests/schemas/test_local.py b/tests/schemas/test_local.py new file mode 100644 index 0000000..023e103 --- /dev/null +++ b/tests/schemas/test_local.py @@ -0,0 +1,27 @@ +import json +from pathlib import Path + +import pytest + +from onedep_lib.exceptions import SchemaError +from onedep_lib.schemas.local import LocalSchemaProvider + +SAMPLE_SCHEMA = {"$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object"} + + +def test_reads_schema_from_cache(tmp_path: Path): + (tmp_path / "required_files.json").write_text(json.dumps(SAMPLE_SCHEMA)) + assert LocalSchemaProvider(tmp_path).get_schema("required_files") == SAMPLE_SCHEMA + + +def test_missing_schema_raises_schema_error(tmp_path: Path): + with pytest.raises(SchemaError): + LocalSchemaProvider(tmp_path).get_schema("absent") + + +def test_corrupted_schema_raises_schema_error(tmp_path: Path): + # A corrupted cache file must surface as SchemaError (the provider contract), + # not a raw json.JSONDecodeError - matching RemoteSchemaProvider. + (tmp_path / "required_files.json").write_text("{ not valid json") + with pytest.raises(SchemaError): + LocalSchemaProvider(tmp_path).get_schema("required_files")