GRM-59: fix: use commit SHA URLs for badges to bypass Gitea cache
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from click import ClickException
|
||||
@@ -65,13 +65,82 @@ class TestPushToBadgesBranch:
|
||||
svg = badges_dir / "badge1.svg"
|
||||
svg.write_text("<svg></svg>")
|
||||
|
||||
with patch("subprocess.run") as mock_run:
|
||||
push_badges.push_to_badges_branch(str(badges_dir))
|
||||
sha_result = MagicMock()
|
||||
sha_result.stdout = "abc123\n"
|
||||
default_result = MagicMock()
|
||||
with patch(
|
||||
"subprocess.run",
|
||||
side_effect=[default_result] * 7 + [sha_result],
|
||||
) as mock_run:
|
||||
sha = push_badges.push_to_badges_branch(str(badges_dir))
|
||||
|
||||
# Should have called git config, checkout, rm, add, commit, push
|
||||
assert mock_run.call_count >= 6
|
||||
# Should have called git config x2, checkout, rm, add, commit, push, rev-parse
|
||||
assert mock_run.call_count >= 8
|
||||
# Verify the SVG was copied to cwd
|
||||
assert (tmp_path / "badge1.svg").exists()
|
||||
assert sha == "abc123"
|
||||
|
||||
|
||||
class TestUpdateBadgeUrls:
|
||||
def test_replaces_branch_url(self) -> None:
|
||||
content = "[]"
|
||||
result = push_badges.update_badge_urls(content, "abc123def456")
|
||||
assert "raw/commit/abc123def456/tests.svg" in result
|
||||
assert "raw/branch/badges" not in result
|
||||
|
||||
def test_replaces_commit_url(self) -> None:
|
||||
"""Old commit SHA URLs should be replaced with the new one."""
|
||||
old_sha = "aabb123456789012345678901234567890123456" # 40 hex chars
|
||||
new_sha = "ccdd123456789012345678901234567890123456" # 40 hex chars
|
||||
content = f"[]"
|
||||
result = push_badges.update_badge_urls(content, new_sha)
|
||||
assert f"raw/commit/{new_sha}/tests.svg" in result
|
||||
assert old_sha not in result
|
||||
|
||||
def test_no_badge_urls(self) -> None:
|
||||
content = "# No badges here\nJust text."
|
||||
result = push_badges.update_badge_urls(content, "abc123")
|
||||
assert result == content
|
||||
|
||||
def test_multiple_badges(self) -> None:
|
||||
content = (
|
||||
"[]\n"
|
||||
"[]\n"
|
||||
"[]"
|
||||
)
|
||||
result = push_badges.update_badge_urls(content, "abc123def456")
|
||||
assert result.count("raw/commit/abc123def456/") == 3
|
||||
assert "raw/branch/badges" not in result
|
||||
|
||||
def test_preserves_non_badge_urls(self) -> None:
|
||||
content = "[]"
|
||||
result = push_badges.update_badge_urls(content, "abc123")
|
||||
assert result == content
|
||||
|
||||
|
||||
class TestUpdateReadmeWithBadgeSha:
|
||||
def test_updates_readme(self, tmp_path: Path) -> None:
|
||||
readme = tmp_path / "README.md"
|
||||
readme.write_text("[]")
|
||||
with patch("subprocess.run"):
|
||||
push_badges.update_readme_with_badge_sha("abc123def456", repo_root=tmp_path)
|
||||
content = readme.read_text()
|
||||
assert "raw/commit/abc123def456/tests.svg" in content
|
||||
|
||||
def test_no_badge_urls_skips_commit(self, tmp_path: Path) -> None:
|
||||
readme = tmp_path / "README.md"
|
||||
readme.write_text("# No badges here")
|
||||
with patch("subprocess.run") as mock_run:
|
||||
push_badges.update_readme_with_badge_sha("abc123def456", repo_root=tmp_path)
|
||||
# Should checkout master but not commit/push
|
||||
calls = [list(c.args[0]) for c in mock_run.call_args_list]
|
||||
assert not any("commit" in c for c in calls)
|
||||
assert not any("push" in c for c in calls)
|
||||
|
||||
def test_missing_readme_skips(self, tmp_path: Path) -> None:
|
||||
with patch("subprocess.run"):
|
||||
push_badges.update_readme_with_badge_sha("abc123def456", repo_root=tmp_path)
|
||||
# Should not raise
|
||||
|
||||
|
||||
class TestMain:
|
||||
@@ -83,7 +152,7 @@ class TestMain:
|
||||
|
||||
runner = CliRunner()
|
||||
with patch("subprocess.run"):
|
||||
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir)])
|
||||
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir), "--no-readme-update"])
|
||||
assert result.exit_code == 0
|
||||
|
||||
def test_no_badges(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
@@ -93,7 +162,7 @@ class TestMain:
|
||||
|
||||
runner = CliRunner()
|
||||
with patch("subprocess.run"):
|
||||
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir)])
|
||||
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir), "--no-readme-update"])
|
||||
assert result.exit_code != 0
|
||||
|
||||
def test_custom_branch(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
@@ -106,7 +175,7 @@ class TestMain:
|
||||
with patch("subprocess.run") as mock_run:
|
||||
result = runner.invoke(
|
||||
push_badges.main,
|
||||
["--output-dir", str(badges_dir), "--branch", "develop"],
|
||||
["--output-dir", str(badges_dir), "--branch", "develop", "--no-readme-update"],
|
||||
)
|
||||
assert result.exit_code == 0
|
||||
# Verify fetch was called with the custom branch
|
||||
@@ -120,5 +189,37 @@ class TestMain:
|
||||
|
||||
runner = CliRunner()
|
||||
with patch("subprocess.run", side_effect=subprocess.CalledProcessError(1, "git fetch")):
|
||||
result = runner.invoke(push_badges.main, [])
|
||||
result = runner.invoke(push_badges.main, ["--no-readme-update"])
|
||||
assert result.exit_code != 0
|
||||
|
||||
def test_readme_update_called_by_default(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""Without --no-readme-update, update_readme_with_badge_sha is called."""
|
||||
monkeypatch.chdir(tmp_path)
|
||||
badges_dir = tmp_path / ".badges"
|
||||
badges_dir.mkdir()
|
||||
(badges_dir / "badge1.svg").touch()
|
||||
|
||||
runner = CliRunner()
|
||||
with (
|
||||
patch("subprocess.run"),
|
||||
patch("scripts.ci.push_badges.update_readme_with_badge_sha") as mock_update,
|
||||
):
|
||||
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir)])
|
||||
assert result.exit_code == 0
|
||||
mock_update.assert_called_once()
|
||||
|
||||
def test_readme_update_skipped_with_flag(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
|
||||
"""With --no-readme-update, update_readme_with_badge_sha is not called."""
|
||||
monkeypatch.chdir(tmp_path)
|
||||
badges_dir = tmp_path / ".badges"
|
||||
badges_dir.mkdir()
|
||||
(badges_dir / "badge1.svg").touch()
|
||||
|
||||
runner = CliRunner()
|
||||
with (
|
||||
patch("subprocess.run"),
|
||||
patch("scripts.ci.push_badges.update_readme_with_badge_sha") as mock_update,
|
||||
):
|
||||
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir), "--no-readme-update"])
|
||||
assert result.exit_code == 0
|
||||
mock_update.assert_not_called()
|
||||
|
||||
Reference in New Issue
Block a user