mirror of
https://github.com/vinta/awesome-python.git
synced 2026-11-20 06:40:51 +00:00
fix(website): don't write an empty star cache when the first fetch fails
On a non-401 HTTP error, fetch_github_stars saved whatever it had and
exited 1. With no restored cache that is {}, which still passes the
deploy workflow's "Verify star data exists" step (file exists, valid
JSON), so the site deployed with no stars. Only save a non-empty cache,
so the verify step fails as intended; a restored cache is still saved.
This commit is contained in:
parent
234dee17f1
commit
2270461947
2 changed files with 52 additions and 2 deletions
|
|
@ -157,8 +157,10 @@ def main() -> None:
|
|||
if e.response.status_code == 401:
|
||||
print("Error: Invalid GITHUB_TOKEN.", file=sys.stderr)
|
||||
sys.exit(1)
|
||||
print("Saving partial cache and exiting.", file=sys.stderr)
|
||||
save_cache(cache)
|
||||
# An empty file would pass the deploy's "star data exists" check and ship a site without stars
|
||||
if cache:
|
||||
print("Saving partial cache and exiting.", file=sys.stderr)
|
||||
save_cache(cache)
|
||||
sys.exit(1)
|
||||
|
||||
for repo in batch:
|
||||
|
|
|
|||
|
|
@ -262,3 +262,51 @@ class TestMainSkipsFreshCache:
|
|||
assert "2 repos to fetch" in output
|
||||
assert "Done. Fetched 2 repos" in output
|
||||
mock_client.post.assert_called_once()
|
||||
|
||||
|
||||
class TestMainOnHttpError:
|
||||
"""Verify what main() leaves on disk when a batch fails with an HTTP error."""
|
||||
|
||||
def _run_with_502(self, tmp_path, monkeypatch, cache):
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import httpx2
|
||||
import pytest
|
||||
|
||||
from fetch_github_stars import main
|
||||
|
||||
readme = tmp_path / "README.md"
|
||||
readme.write_text("* [req](https://github.com/psf/requests) - HTTP.\n")
|
||||
monkeypatch.setattr("fetch_github_stars.README_PATH", readme)
|
||||
data_dir = tmp_path / "data"
|
||||
cache_file = data_dir / "github_stars.json"
|
||||
if cache is not None:
|
||||
data_dir.mkdir()
|
||||
cache_file.write_text(json.dumps(cache), encoding="utf-8")
|
||||
monkeypatch.setattr("fetch_github_stars.CACHE_FILE", cache_file)
|
||||
monkeypatch.setattr("fetch_github_stars.DATA_DIR", data_dir)
|
||||
monkeypatch.setenv("GITHUB_TOKEN", "fake-token")
|
||||
|
||||
request = httpx2.Request("POST", "https://api.github.com/graphql")
|
||||
mock_response = MagicMock()
|
||||
mock_response.raise_for_status.side_effect = httpx2.HTTPStatusError("Bad Gateway", request=request, response=httpx2.Response(502, request=request))
|
||||
mock_client = MagicMock()
|
||||
mock_client.__enter__ = MagicMock(return_value=mock_client)
|
||||
mock_client.__exit__ = MagicMock(return_value=False)
|
||||
mock_client.post.return_value = mock_response
|
||||
monkeypatch.setattr("fetch_github_stars.httpx2.Client", lambda **kwargs: mock_client)
|
||||
|
||||
with pytest.raises(SystemExit) as exc:
|
||||
main()
|
||||
assert exc.value.code == 1
|
||||
return cache_file
|
||||
|
||||
def test_writes_no_cache_file_when_nothing_was_fetched(self, tmp_path, monkeypatch):
|
||||
# An empty {} would pass the deploy workflow's "star data exists" check and ship a site with no stars
|
||||
cache_file = self._run_with_502(tmp_path, monkeypatch, cache=None)
|
||||
assert not cache_file.exists()
|
||||
|
||||
def test_keeps_a_restored_cache(self, tmp_path, monkeypatch):
|
||||
restored = {"psf/requests": {"stars": 52000, "owner": "psf", "last_commit_at": "2025-01-01T00:00:00+00:00", "fetched_at": "2025-01-01T00:00:00+00:00"}}
|
||||
cache_file = self._run_with_502(tmp_path, monkeypatch, cache=restored)
|
||||
assert json.loads(cache_file.read_text(encoding="utf-8")) == restored
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue