From 38baf01a34d2b0fcbee949306953d725652dc586 Mon Sep 17 00:00:00 2001 From: AshSgDe29071999 Date: Fri, 7 Aug 2026 18:34:01 +0530 Subject: [PATCH 1/2] 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. --- .../tests/test_git_cache.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/create-python-app-core/tests/test_git_cache.py b/packages/create-python-app-core/tests/test_git_cache.py index 40373de..767e315 100644 --- a/packages/create-python-app-core/tests/test_git_cache.py +++ b/packages/create-python-app-core/tests/test_git_cache.py @@ -135,3 +135,39 @@ def test_missing_subdir_forces_refresh_when_meta_is_fresh(tmp_path: Path) -> Non updated = download_repository(src, cache_root=cache, refresh="stale") assert (updated / "extensions" / "all-github-setup" / "ok.txt").is_file() + + +def test_read_cache_meta_empty_dir(tmp_path: Path) -> None: + from create_python_app_core.git_cache import read_cache_meta + + assert read_cache_meta(tmp_path / "missing") is None + + +def test_read_cache_meta_corrupt_json(tmp_path: Path) -> None: + from create_python_app_core.errors import CpaError + from create_python_app_core.git_cache import meta_path, read_cache_meta + + entry = tmp_path / "e" + entry.mkdir() + meta_path(entry).write_text("{not-json", encoding="utf-8") + with pytest.raises((json.JSONDecodeError, CpaError, TypeError, ValueError)): + # Current implementation lets JSONDecodeError propagate; either is acceptable. + read_cache_meta(entry) + + +def test_refresh_after_hours_invalid_env(monkeypatch: pytest.MonkeyPatch) -> None: + from create_python_app_core.git_cache import refresh_after_hours + + monkeypatch.setenv("CPA_REFRESH_AFTER_HOURS", "not-a-number") + assert refresh_after_hours() == 24.0 + + +def test_should_refresh_modes() -> None: + from create_python_app_core.git_cache import CacheMeta, _should_refresh + + meta = CacheMeta(url="u", ref="main", fetched_at=time.time(), commit="abc") + assert _should_refresh(None, "stale") is True + assert _should_refresh(meta, "manual") is False + assert _should_refresh(meta, "always") is True + old = CacheMeta(url="u", ref="main", fetched_at=time.time() - 100_000, commit="abc") + assert _should_refresh(old, "stale") is True From 5c512c3718ce41328d72cec4ea1cb6b879eba01d Mon Sep 17 00:00:00 2001 From: Ashay Date: Sat, 8 Aug 2026 18:51:06 +0530 Subject: [PATCH 2/2] 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. --- packages/create-python-app-core/tests/test_git_cache.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/create-python-app-core/tests/test_git_cache.py b/packages/create-python-app-core/tests/test_git_cache.py index 767e315..b201346 100644 --- a/packages/create-python-app-core/tests/test_git_cache.py +++ b/packages/create-python-app-core/tests/test_git_cache.py @@ -140,6 +140,9 @@ def test_missing_subdir_forces_refresh_when_meta_is_fresh(tmp_path: Path) -> Non def test_read_cache_meta_empty_dir(tmp_path: Path) -> None: from create_python_app_core.git_cache import read_cache_meta + empty = tmp_path / "empty-entry" + empty.mkdir() + assert read_cache_meta(empty) is None assert read_cache_meta(tmp_path / "missing") is None @@ -150,8 +153,7 @@ def test_read_cache_meta_corrupt_json(tmp_path: Path) -> None: entry = tmp_path / "e" entry.mkdir() meta_path(entry).write_text("{not-json", encoding="utf-8") - with pytest.raises((json.JSONDecodeError, CpaError, TypeError, ValueError)): - # Current implementation lets JSONDecodeError propagate; either is acceptable. + with pytest.raises((json.JSONDecodeError, CpaError)): read_cache_meta(entry) @@ -168,6 +170,7 @@ def test_should_refresh_modes() -> None: meta = CacheMeta(url="u", ref="main", fetched_at=time.time(), commit="abc") assert _should_refresh(None, "stale") is True assert _should_refresh(meta, "manual") is False + assert _should_refresh(meta, "stale") is False assert _should_refresh(meta, "always") is True old = CacheMeta(url="u", ref="main", fetched_at=time.time() - 100_000, commit="abc") assert _should_refresh(old, "stale") is True