Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions website/fetch_github_stars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
48 changes: 48 additions & 0 deletions website/tests/test_fetch_github_stars.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,51 @@ def test_fetches_when_cache_is_stale(self, tmp_path, monkeypatch, capsys):
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