Commit 2c190ab1 by Chad Crawford Committed by Bjorn Neergaard

fix: Add post init check for `FileCache` hash type.

parent c2b1fb3d
...@@ -100,6 +100,12 @@ class FileCache(Generic[T]): ...@@ -100,6 +100,12 @@ class FileCache(Generic[T]):
path: Path path: Path
hash_type: str = "sha256" hash_type: str = "sha256"
def __post_init__(self) -> None:
if self.hash_type not in _HASHES:
raise ValueError(
f"FileCache.hash_type is unknown value: '{self.hash_type}'."
)
def get(self, key: str) -> T | None: def get(self, key: str) -> T | None:
return self._get_payload(key) return self._get_payload(key)
......
...@@ -85,6 +85,12 @@ def cachy_dict_cache() -> CacheManager: ...@@ -85,6 +85,12 @@ def cachy_dict_cache() -> CacheManager:
return patch_cachy(cache) return patch_cachy(cache)
def test_cache_validates(repository_cache_dir: Path) -> None:
with pytest.raises(ValueError) as e:
FileCache(repository_cache_dir / "cache", hash_type="unknown")
assert str(e.value) == "FileCache.hash_type is unknown value: 'unknown'."
@pytest.mark.parametrize("cache_name", ["cachy_file_cache", "poetry_file_cache"]) @pytest.mark.parametrize("cache_name", ["cachy_file_cache", "poetry_file_cache"])
def test_cache_get_put_has(cache_name: str, request: FixtureRequest) -> None: def test_cache_get_put_has(cache_name: str, request: FixtureRequest) -> None:
cache = request.getfixturevalue(cache_name) cache = request.getfixturevalue(cache_name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment