Skip to content

Commit

Permalink
test deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
tlambert03 committed Sep 30, 2023
1 parent dff9ed5 commit 7638cfc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/pyconify/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def svg_cache() -> MutableMapping[str, bytes]: # pragma: no cover
else:
try:
_SVG_CACHE = _SVGCache()
_delete_stale_svgs()
_delete_stale_svgs(_SVG_CACHE)
except Exception:
_SVG_CACHE = {}
return _SVG_CACHE
Expand Down Expand Up @@ -106,13 +106,13 @@ def __contains__(self, _key: object) -> bool:
return self.path_for(_key).exists() if isinstance(_key, str) else False


def _delete_stale_svgs() -> None:
def _delete_stale_svgs(cache: MutableMapping) -> None: # pragma: no cover
"""Remove all SVG files with an outdated last_modified date from the cache."""
from .api import last_modified

last_modified_dates = last_modified()
for key in svg_cache():
for key in list(cache):
with suppress(ValueError):
prefix, *_, cached_last_mod = key.split(DELIM)
if int(cached_last_mod) < last_modified_dates.get(prefix, 0):
del svg_cache()[key]
del cache[key]
6 changes: 6 additions & 0 deletions tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ def test_cache_dir(monkeypatch) -> None:
some_path = Path("/some/path").expanduser().resolve()
monkeypatch.setattr(_cache, "PYCONIFY_CACHE", str(some_path))
assert get_cache_directory() == some_path


def test_delete_stale() -> None:
cache = {"fa_0": b""}
_cache._delete_stale_svgs(cache)
assert not cache

0 comments on commit 7638cfc

Please sign in to comment.