Files
grm/tests/unit/test_push_badges.py
T

70 lines
2.3 KiB
Python

from __future__ import annotations
from pathlib import Path
from unittest.mock import patch
import pytest
from click import ClickException
from click.testing import CliRunner
import scripts.ci.push_badges as push_badges
class TestGenerateBadges:
def test_success(self, tmp_path: Path) -> None:
output_dir = tmp_path / ".badges"
output_dir.mkdir()
(output_dir / "badge1.svg").touch()
with patch("subprocess.run") as mock_run:
push_badges.generate_badges(str(output_dir))
mock_run.assert_called_once()
def test_no_badges_generated(self, tmp_path: Path) -> None:
output_dir = tmp_path / ".badges"
output_dir.mkdir()
with patch("subprocess.run"):
with pytest.raises(ClickException, match="No badge SVG files generated"):
push_badges.generate_badges(str(output_dir))
class TestPushToBadgesBranch:
def test_success(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
badges_dir = tmp_path / ".badges"
badges_dir.mkdir()
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))
# Should have called git config, checkout, rm, add, commit, push
assert mock_run.call_count >= 6
# Verify the SVG was copied to cwd
assert (tmp_path / "badge1.svg").exists()
class TestMain:
def test_success(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
badges_dir = tmp_path / ".badges"
badges_dir.mkdir()
(badges_dir / "badge1.svg").touch()
runner = CliRunner()
with patch("subprocess.run"):
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir)])
assert result.exit_code == 0
def test_no_badges(self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.chdir(tmp_path)
badges_dir = tmp_path / ".badges"
badges_dir.mkdir()
runner = CliRunner()
with patch("subprocess.run"):
result = runner.invoke(push_badges.main, ["--output-dir", str(badges_dir)])
assert result.exit_code != 0