115 lines
4.2 KiB
Python
115 lines
4.2 KiB
Python
"""Unit tests for scripts/auto_merge.py."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
import requests
|
|
|
|
from scripts.auto_merge import (
|
|
CONVENTIONAL_RE,
|
|
GITEA_API,
|
|
TASK_ID_RE,
|
|
extract_task_id,
|
|
main,
|
|
merge_pr,
|
|
validate_pr_title,
|
|
)
|
|
|
|
|
|
class TestRegexes:
|
|
def test_task_id_re_matches(self) -> None:
|
|
assert TASK_ID_RE.search("GRM-19-fix-bug")
|
|
assert TASK_ID_RE.search("feature/GRM-42")
|
|
|
|
def test_task_id_re_no_match(self) -> None:
|
|
assert not TASK_ID_RE.search("feature-no-id")
|
|
|
|
def test_conventional_re_matches(self) -> None:
|
|
assert CONVENTIONAL_RE.match("feat: add feature")
|
|
assert CONVENTIONAL_RE.match("fix(api): handle timeout")
|
|
|
|
def test_conventional_re_rejects(self) -> None:
|
|
assert not CONVENTIONAL_RE.match("random message")
|
|
assert not CONVENTIONAL_RE.match("feat:")
|
|
|
|
|
|
class TestExtractTaskId:
|
|
def test_extracts_from_branch(self) -> None:
|
|
assert extract_task_id("GRM-19-fix-bug") == "GRM-19"
|
|
|
|
def test_extracts_from_feature_branch(self) -> None:
|
|
assert extract_task_id("feature/GRM-42-add-x") == "GRM-42"
|
|
|
|
def test_returns_empty_when_missing(self) -> None:
|
|
assert extract_task_id("feature-no-id") == ""
|
|
|
|
|
|
class TestValidatePrTitle:
|
|
def test_valid_title_passes(self) -> None:
|
|
validate_pr_title("fix: resolve timeout")
|
|
|
|
def test_valid_title_with_scope_passes(self) -> None:
|
|
validate_pr_title("feat(cli): add --url option")
|
|
|
|
def test_invalid_title_exits(self) -> None:
|
|
with pytest.raises(SystemExit) as exc:
|
|
validate_pr_title("random message")
|
|
assert exc.value.code == 1
|
|
|
|
|
|
class TestMergePr:
|
|
@patch("scripts.auto_merge.requests.post")
|
|
def test_successful_merge(self, mock_post: MagicMock) -> None:
|
|
mock_response = MagicMock()
|
|
mock_post.return_value = mock_response
|
|
merge_pr("tok", "owner/repo", "7", "GRM-19: fix: bug")
|
|
mock_post.assert_called_once()
|
|
args, kwargs = mock_post.call_args
|
|
assert kwargs["headers"]["Authorization"] == "token tok"
|
|
assert kwargs["json"]["Do"] == "squash"
|
|
assert kwargs["json"]["MergeTitleField"] == "GRM-19: fix: bug"
|
|
assert GITEA_API in args[0]
|
|
|
|
@patch("scripts.auto_merge.requests.post")
|
|
def test_merge_raises_on_http_error(self, mock_post: MagicMock) -> None:
|
|
mock_response = MagicMock()
|
|
mock_response.raise_for_status.side_effect = requests.HTTPError("500")
|
|
mock_post.return_value = mock_response
|
|
with pytest.raises(requests.HTTPError):
|
|
merge_pr("tok", "owner/repo", "7", "title")
|
|
|
|
|
|
class TestMain:
|
|
@patch.dict("os.environ", {"GITEA_TOKEN": "tok"})
|
|
@patch("scripts.auto_merge.merge_pr")
|
|
def test_successful_flow(self, mock_merge: MagicMock, capsys: pytest.CaptureFixture[str]) -> None:
|
|
main(["auto_merge.py", "GRM-19-fix-bug", "fix: resolve timeout", "owner/repo", "7"])
|
|
mock_merge.assert_called_once_with("tok", "owner/repo", "7", "GRM-19: fix: resolve timeout")
|
|
captured = capsys.readouterr()
|
|
assert "squash-merged" in captured.out
|
|
|
|
@patch.dict("os.environ", {"GITEA_TOKEN": ""}, clear=True)
|
|
def test_missing_token_exits(self) -> None:
|
|
with pytest.raises(SystemExit) as exc:
|
|
main(["auto_merge.py", "branch", "title", "repo", "1"])
|
|
assert exc.value.code == 1
|
|
|
|
@patch.dict("os.environ", {"GITEA_TOKEN": "tok"})
|
|
def test_missing_task_id_exits(self) -> None:
|
|
with pytest.raises(SystemExit) as exc:
|
|
main(["auto_merge.py", "feature-no-id", "fix: bug", "repo", "1"])
|
|
assert exc.value.code == 1
|
|
|
|
@patch.dict("os.environ", {"GITEA_TOKEN": "tok"})
|
|
def test_invalid_pr_title_exits(self) -> None:
|
|
with pytest.raises(SystemExit) as exc:
|
|
main(["auto_merge.py", "GRM-19-fix", "random title", "repo", "1"])
|
|
assert exc.value.code == 1
|
|
|
|
@patch.dict("os.environ", {"GITEA_TOKEN": "tok"})
|
|
@patch("scripts.auto_merge.merge_pr")
|
|
def test_merge_pr_failure_propagates(self, mock_merge: MagicMock) -> None:
|
|
mock_merge.side_effect = requests.HTTPError("500")
|
|
with pytest.raises(requests.HTTPError):
|
|
main(["auto_merge.py", "GRM-19-fix", "fix: bug", "repo", "1"])
|