Skip to content

Commit

Permalink
test: 添加 handlers 的测试 (#331)
Browse files Browse the repository at this point in the history
* test: 测试 git handler

* test: 添加 git handler 测试

* test: 添加 github handler 测试(部分)

* test: 添加 github handler 测试(完整)

* test: 添加 issue handler 测试
  • Loading branch information
he0119 authored Dec 20, 2024
1 parent 2f2f595 commit a89d86a
Show file tree
Hide file tree
Showing 5 changed files with 1,543 additions and 8 deletions.
8 changes: 1 addition & 7 deletions src/plugins/github/handlers/git.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,7 @@ def commit_and_push(self, message: str, branch_name: str, author: str):
user_email = f"{author}@users.noreply.github.com"
run_shell_command(["git", "config", "--global", "user.email", user_email])
run_shell_command(["git", "add", "-A"])
try:
run_shell_command(["git", "commit", "-m", message])
except Exception:
# 如果提交失败,因为是 pre-commit hooks 格式化代码导致的,所以需要再次提交
run_shell_command(["git", "add", "-A"])
run_shell_command(["git", "commit", "-m", message])

run_shell_command(["git", "commit", "-m", message])
try:
run_shell_command(["git", "fetch", "origin"])
r = run_shell_command(["git", "diff", f"origin/{branch_name}", branch_name])
Expand Down
120 changes: 120 additions & 0 deletions tests/plugins/github/handlers/test_git_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
from unittest.mock import _Call, call

import pytest
from pytest_mock import MockerFixture


@pytest.fixture
def mock_run_shell_command(mocker: MockerFixture):
return mocker.patch("src.plugins.github.handlers.git.run_shell_command")


async def test_checkout_branch(mock_run_shell_command):
from src.plugins.github.handlers.git import GitHandler

git_handler = GitHandler()
git_handler.checkout_branch("main")

mock_run_shell_command.assert_has_calls(
[
call(["git", "checkout", "main"]),
]
)


async def test_checkout_remote_branch(mock_run_shell_command):
from src.plugins.github.handlers.git import GitHandler

git_handler = GitHandler()
git_handler.checkout_remote_branch("main")

mock_run_shell_command.assert_has_calls(
[
call(["git", "fetch", "origin", "main"]),
call(["git", "checkout", "main"]),
]
)


async def test_commit_and_push(mock_run_shell_command):
from src.plugins.github.handlers.git import GitHandler

git_handler = GitHandler()
git_handler.commit_and_push("commit message", "main", "author")

mock_run_shell_command.assert_has_calls(
[
call(["git", "config", "--global", "user.name", "author"]),
call(
[
"git",
"config",
"--global",
"user.email",
"[email protected]",
]
),
call(["git", "add", "-A"]),
call(["git", "commit", "-m", "commit message"]),
call(["git", "fetch", "origin"]),
call(["git", "diff", "origin/main", "main"]),
_Call(("().stdout.__bool__", (), {})),
call(["git", "push", "origin", "main", "-f"]),
],
)


async def test_commit_and_push_diff_no_change(mock_run_shell_command):
"""本地分支与远程分支一致,跳过推送的情况"""
from src.plugins.github.handlers.git import GitHandler

# 本地分支与远程分支一致时 git diff 应该返回空字符串
mock_run_shell_command.return_value.stdout = ""

git_handler = GitHandler()
git_handler.commit_and_push("commit message", "main", "author")

mock_run_shell_command.assert_has_calls(
[
call(["git", "config", "--global", "user.name", "author"]),
call(
[
"git",
"config",
"--global",
"user.email",
"[email protected]",
]
),
call(["git", "add", "-A"]),
call(["git", "commit", "-m", "commit message"]),
call(["git", "fetch", "origin"]),
call(["git", "diff", "origin/main", "main"]),
],
)


async def test_delete_origin_branch(mock_run_shell_command):
from src.plugins.github.handlers.git import GitHandler

git_handler = GitHandler()
git_handler.delete_origin_branch("main")

mock_run_shell_command.assert_has_calls(
[
call(["git", "push", "origin", "--delete", "main"]),
]
)


async def test_switch_branch(mock_run_shell_command):
from src.plugins.github.handlers.git import GitHandler

git_handler = GitHandler()
git_handler.switch_branch("main")

mock_run_shell_command.assert_has_calls(
[
call(["git", "switch", "-C", "main"]),
]
)
Loading

0 comments on commit a89d86a

Please sign in to comment.