Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 添加 handlers 的测试 #331

Merged
merged 5 commits into from
Dec 20, 2024
Merged
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
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
Loading