generated from actions/typescript-action
-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* test: 测试 git handler * test: 添加 git handler 测试 * test: 添加 github handler 测试(部分) * test: 添加 github handler 测试(完整) * test: 添加 issue handler 测试
- Loading branch information
Showing
5 changed files
with
1,543 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]), | ||
] | ||
) |
Oops, something went wrong.