Skip to content

Commit 6cd61c6

Browse files
test: cover git cache edge cases without network (#277)
* test: cover git cache edge cases without network Closes #267 Empty cache dir, corrupt metadata, invalid refresh-hours env, and refresh-mode decision matrix for CacheMeta. * test: address CodeRabbit feedback on git cache edge cases Cover empty existing cache dirs separately from missing paths, narrow corrupt-JSON exceptions to JSONDecodeError/CpaError, and assert fresh metadata does not refresh under stale mode. --------- Co-authored-by: AshSgDe29071999 <ashsgde29071999@users.noreply.github.com>
1 parent bba4586 commit 6cd61c6

1 file changed

Lines changed: 39 additions & 0 deletions

File tree

packages/create-python-app-core/tests/test_git_cache.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,42 @@ def test_missing_subdir_forces_refresh_when_meta_is_fresh(tmp_path: Path) -> Non
135135

136136
updated = download_repository(src, cache_root=cache, refresh="stale")
137137
assert (updated / "extensions" / "all-github-setup" / "ok.txt").is_file()
138+
139+
140+
def test_read_cache_meta_empty_dir(tmp_path: Path) -> None:
141+
from create_python_app_core.git_cache import read_cache_meta
142+
143+
empty = tmp_path / "empty-entry"
144+
empty.mkdir()
145+
assert read_cache_meta(empty) is None
146+
assert read_cache_meta(tmp_path / "missing") is None
147+
148+
149+
def test_read_cache_meta_corrupt_json(tmp_path: Path) -> None:
150+
from create_python_app_core.errors import CpaError
151+
from create_python_app_core.git_cache import meta_path, read_cache_meta
152+
153+
entry = tmp_path / "e"
154+
entry.mkdir()
155+
meta_path(entry).write_text("{not-json", encoding="utf-8")
156+
with pytest.raises((json.JSONDecodeError, CpaError)):
157+
read_cache_meta(entry)
158+
159+
160+
def test_refresh_after_hours_invalid_env(monkeypatch: pytest.MonkeyPatch) -> None:
161+
from create_python_app_core.git_cache import refresh_after_hours
162+
163+
monkeypatch.setenv("CPA_REFRESH_AFTER_HOURS", "not-a-number")
164+
assert refresh_after_hours() == 24.0
165+
166+
167+
def test_should_refresh_modes() -> None:
168+
from create_python_app_core.git_cache import CacheMeta, _should_refresh
169+
170+
meta = CacheMeta(url="u", ref="main", fetched_at=time.time(), commit="abc")
171+
assert _should_refresh(None, "stale") is True
172+
assert _should_refresh(meta, "manual") is False
173+
assert _should_refresh(meta, "stale") is False
174+
assert _should_refresh(meta, "always") is True
175+
old = CacheMeta(url="u", ref="main", fetched_at=time.time() - 100_000, commit="abc")
176+
assert _should_refresh(old, "stale") is True

0 commit comments

Comments
 (0)