Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/onedep_lib/schemas/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
27 changes: 27 additions & 0 deletions tests/schemas/test_local.py
Original file line number Diff line number Diff line change
@@ -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")