33from pytest_mock import MockerFixture
44
55from tests .plugins .github .event import get_mock_event
6- from tests .plugins .github .utils import get_github_bot
6+ from tests .plugins .github .utils import (
7+ assert_subprocess_run_calls ,
8+ get_github_bot ,
9+ mock_subprocess_run_with_side_effect ,
10+ should_call_apis ,
11+ )
712
813
914def get_issue_labels (labels : list [str ]):
@@ -28,50 +33,56 @@ def get_issue_labels(labels: list[str]):
2833
2934
3035async def test_config_auto_merge (
31- app : App , mocker : MockerFixture , mock_installation
36+ app : App , mocker : MockerFixture , mock_installation , mock_installation_token
3237) -> None :
3338 """测试审查后自动合并
3439
3540 可直接合并的情况
3641 """
3742 from src .plugins .github .plugins .config import auto_merge_matcher
3843
39- mock_subprocess_run = mocker . patch ( "subprocess.run" )
44+ mock_subprocess_run = mock_subprocess_run_with_side_effect ( mocker )
4045
4146 async with app .test_matcher () as ctx :
4247 adapter , bot = get_github_bot (ctx )
4348 event = get_mock_event (PullRequestReviewSubmitted )
4449 event .payload .pull_request .labels = get_issue_labels (["Config" , "Plugin" ])
4550
46- ctx .should_call_api (
47- "rest.apps.async_get_repo_installation" ,
48- {"owner" : "he0119" , "repo" : "action-test" },
49- mock_installation ,
50- )
51- ctx .should_call_api (
52- "rest.pulls.async_merge" ,
53- {
54- "owner" : "he0119" ,
55- "repo" : "action-test" ,
56- "pull_number" : 100 ,
57- "merge_method" : "rebase" ,
58- },
59- True ,
51+ should_call_apis (
52+ ctx ,
53+ [
54+ {
55+ "api" : "rest.apps.async_get_repo_installation" ,
56+ "result" : mock_installation ,
57+ },
58+ {
59+ "api" : "rest.apps.async_create_installation_access_token" ,
60+ "result" : mock_installation_token ,
61+ },
62+ {
63+ "api" : "rest.pulls.async_merge" ,
64+ "result" : True ,
65+ },
66+ ],
67+ [
68+ {"owner" : "he0119" , "repo" : "action-test" },
69+ {"installation_id" : mock_installation .parsed_data .id },
70+ {
71+ "owner" : "he0119" ,
72+ "repo" : "action-test" ,
73+ "pull_number" : 100 ,
74+ "merge_method" : "rebase" ,
75+ },
76+ ],
6077 )
6178
6279 ctx .receive_event (bot , event )
6380 ctx .should_pass_rule (auto_merge_matcher )
6481
6582 # 测试 git 命令
66- mock_subprocess_run .assert_has_calls (
67- [
68- mocker .call (
69- ["git" , "config" , "--global" , "safe.directory" , "*" ],
70- check = True ,
71- capture_output = True ,
72- ), # type: ignore
73- ],
74- any_order = True ,
83+ assert_subprocess_run_calls (
84+ mock_subprocess_run ,
85+ [["git" , "config" , "--global" , "safe.directory" , "*" ]],
7586 )
7687
7788
@@ -82,7 +93,7 @@ async def test_auto_merge_not_remove(app: App, mocker: MockerFixture) -> None:
8293 """
8394 from src .plugins .github .plugins .config import auto_merge_matcher
8495
85- mock_subprocess_run = mocker . patch ( "subprocess.run" )
96+ mock_subprocess_run = mock_subprocess_run_with_side_effect ( mocker )
8697
8798 async with app .test_matcher () as ctx :
8899 adapter , bot = get_github_bot (ctx )
@@ -103,7 +114,7 @@ async def test_auto_merge_not_member(app: App, mocker: MockerFixture) -> None:
103114 """
104115 from src .plugins .github .plugins .config import auto_merge_matcher
105116
106- mock_subprocess_run = mocker . patch ( "subprocess.run" )
117+ mock_subprocess_run = mock_subprocess_run_with_side_effect ( mocker )
107118
108119 async with app .test_matcher () as ctx :
109120 adapter , bot = get_github_bot (ctx )
@@ -125,7 +136,7 @@ async def test_auto_merge_not_approve(app: App, mocker: MockerFixture) -> None:
125136 """
126137 from src .plugins .github .plugins .config import auto_merge_matcher
127138
128- mock_subprocess_run = mocker . patch ( "subprocess.run" )
139+ mock_subprocess_run = mock_subprocess_run_with_side_effect ( mocker )
129140
130141 async with app .test_matcher () as ctx :
131142 adapter , bot = get_github_bot (ctx )
0 commit comments