|
1 | | -import asyncio |
| 1 | +import os |
| 2 | +from unittest import mock |
2 | 3 |
|
3 | 4 | from gidgethub import sansio |
| 5 | +import pytest |
4 | 6 |
|
5 | | -from miss_islington import delete_branch |
| 7 | +os.environ.setdefault("HEROKU_REDIS_MAROON_URL", "someurl") |
6 | 8 |
|
7 | | - |
8 | | -class FakeGH: |
9 | | - def __init__(self, *, getitem=None): |
10 | | - self._getitem_return = getitem |
11 | | - self.getitem_url = None |
12 | | - self.post_data = None |
13 | | - |
14 | | - async def getitem(self, url): |
15 | | - self.getitem_url = url |
16 | | - to_return = self._getitem_return[self.getitem_url] |
17 | | - return to_return |
18 | | - |
19 | | - async def delete(self, url): |
20 | | - self.delete_url = url |
| 9 | +from miss_islington import delete_branch, tasks |
21 | 10 |
|
22 | 11 |
|
23 | | -async def noop_sleep(delay, result=None): |
| 12 | +class FakeGH: |
24 | 13 | pass |
25 | 14 |
|
26 | 15 |
|
27 | | -async def test_branch_deleted_when_pr_merged(): |
| 16 | +async def test_branch_deletion_queued_when_pr_merged(): |
28 | 17 | data = { |
29 | 18 | "action": "closed", |
30 | 19 | "pull_request": { |
31 | 20 | "number": 5722, |
32 | 21 | "user": {"login": "miss-islington"}, |
33 | 22 | "merged": True, |
34 | | - "merged_by": {"login": "miss-islington"}, |
35 | | - "head": {"ref": "backport-17ab8f0-3.7"}, |
36 | | - }, |
37 | | - } |
38 | | - event = sansio.Event(data, event="pull_request", delivery_id="1") |
39 | | - |
40 | | - gh = FakeGH() |
41 | | - await delete_branch.router.dispatch(event, gh) |
42 | | - assert gh.post_data is None # does not leave a comment |
43 | | - assert ( |
44 | | - gh.delete_url |
45 | | - == f"/repos/miss-islington/cpython/git/refs/heads/{data['pull_request']['head']['ref']}" |
46 | | - ) |
47 | | - |
48 | | - |
49 | | -async def test_branch_deleted_and_thank_committer(): |
50 | | - data = { |
51 | | - "action": "closed", |
52 | | - "pull_request": { |
53 | | - "number": 5722, |
54 | | - "user": {"login": "miss-islington"}, |
55 | | - "merged": True, |
56 | | - "merged_by": {"login": "Mariatta"}, |
57 | | - "head": {"ref": "backport-17ab8f0-3.7"}, |
58 | | - }, |
59 | | - } |
60 | | - event = sansio.Event(data, event="pull_request", delivery_id="1") |
61 | | - |
62 | | - gh = FakeGH() |
63 | | - await delete_branch.router.dispatch(event, gh) |
64 | | - assert gh.post_data is None # does not leave a comment |
65 | | - assert ( |
66 | | - gh.delete_url |
67 | | - == f"/repos/miss-islington/cpython/git/refs/heads/{data['pull_request']['head']['ref']}" |
68 | | - ) |
69 | | - |
70 | | - |
71 | | -async def test_branch_deleted_and_thanks(): |
72 | | - data = { |
73 | | - "action": "closed", |
74 | | - "pull_request": { |
75 | | - "number": 5722, |
76 | | - "user": {"login": "miss-islington"}, |
77 | | - "merged": True, |
78 | | - "merged_by": {"login": "miss-islington"}, |
79 | | - "head": {"ref": "backport-17ab8f0-3.7"}, |
80 | | - }, |
81 | | - } |
82 | | - event = sansio.Event(data, event="pull_request", delivery_id="1") |
83 | | - |
84 | | - gh = FakeGH() |
85 | | - await delete_branch.router.dispatch(event, gh) |
86 | | - assert gh.post_data is None # does not leave a comment |
87 | | - assert ( |
88 | | - gh.delete_url |
89 | | - == f"/repos/miss-islington/cpython/git/refs/heads/{data['pull_request']['head']['ref']}" |
90 | | - ) |
91 | | - |
92 | | - |
93 | | -async def test_branch_deleted_when_pr_closed(monkeypatch): |
94 | | - data = { |
95 | | - "action": "closed", |
96 | | - "pull_request": { |
97 | | - "number": 5722, |
98 | | - "user": {"login": "miss-islington"}, |
99 | | - "merged": False, |
100 | | - "merged_by": {"login": None}, |
101 | 23 | "head": {"ref": "backport-17ab8f0-3.7"}, |
102 | 24 | "url": "https://api.github.com/repos/python/cpython/pulls/5722", |
103 | 25 | }, |
| 26 | + "installation": {"id": 123}, |
104 | 27 | } |
105 | 28 | event = sansio.Event(data, event="pull_request", delivery_id="1") |
106 | | - getitem = { |
107 | | - "https://api.github.com/repos/python/cpython/pulls/5722": {"state": "closed"}, |
108 | | - } |
109 | 29 |
|
110 | | - monkeypatch.setattr(asyncio, "sleep", noop_sleep) |
111 | | - gh = FakeGH(getitem=getitem) |
112 | | - await delete_branch.router.dispatch(event, gh) |
113 | | - assert gh.post_data is None # does not leave a comment |
114 | | - assert ( |
115 | | - gh.delete_url |
116 | | - == f"/repos/miss-islington/cpython/git/refs/heads/{data['pull_request']['head']['ref']}" |
117 | | - ) |
| 30 | + gh = FakeGH() |
| 31 | + with mock.patch.object(tasks.delete_branch_task, "delay") as mock_delay: |
| 32 | + await delete_branch.router.dispatch(event, gh) |
| 33 | + mock_delay.assert_called_once_with( |
| 34 | + "backport-17ab8f0-3.7", |
| 35 | + "https://api.github.com/repos/python/cpython/pulls/5722", |
| 36 | + True, |
| 37 | + installation_id=123 |
| 38 | + ) |
118 | 39 |
|
119 | 40 |
|
120 | | -async def test_branch_not_deleted_when_pr_closed_and_reopened(monkeypatch): |
| 41 | +async def test_branch_deletion_queued_when_pr_closed_not_merged(): |
121 | 42 | data = { |
122 | 43 | "action": "closed", |
123 | 44 | "pull_request": { |
124 | 45 | "number": 5722, |
125 | 46 | "user": {"login": "miss-islington"}, |
126 | 47 | "merged": False, |
127 | | - "merged_by": {"login": None}, |
128 | 48 | "head": {"ref": "backport-17ab8f0-3.7"}, |
129 | 49 | "url": "https://api.github.com/repos/python/cpython/pulls/5722", |
130 | 50 | }, |
| 51 | + "installation": {"id": 456}, |
131 | 52 | } |
132 | 53 | event = sansio.Event(data, event="pull_request", delivery_id="1") |
133 | | - getitem = { |
134 | | - "https://api.github.com/repos/python/cpython/pulls/5722": {"state": "opened"}, |
135 | | - } |
136 | 54 |
|
137 | | - monkeypatch.setattr(asyncio, "sleep", noop_sleep) |
138 | | - gh = FakeGH(getitem=getitem) |
139 | | - await delete_branch.router.dispatch(event, gh) |
140 | | - assert gh.post_data is None # does not leave a comment |
141 | | - assert not hasattr(gh, "delete_url") |
| 55 | + gh = FakeGH() |
| 56 | + with mock.patch.object(tasks.delete_branch_task, "delay") as mock_delay: |
| 57 | + await delete_branch.router.dispatch(event, gh) |
| 58 | + mock_delay.assert_called_once_with( |
| 59 | + "backport-17ab8f0-3.7", |
| 60 | + "https://api.github.com/repos/python/cpython/pulls/5722", |
| 61 | + False, |
| 62 | + installation_id=456 |
| 63 | + ) |
142 | 64 |
|
143 | 65 |
|
144 | | -async def test_ignore_non_miss_islingtons_prs(): |
| 66 | +async def test_ignore_non_miss_islington_prs(): |
145 | 67 | data = { |
146 | 68 | "action": "closed", |
147 | 69 | "pull_request": { |
148 | 70 | "number": 5722, |
149 | 71 | "user": {"login": "Mariatta"}, |
150 | 72 | "merged": True, |
151 | | - "merged_by": {"login": "Mariatta"}, |
152 | 73 | "head": {"ref": "backport-17ab8f0-3.7"}, |
| 74 | + "url": "https://api.github.com/repos/python/cpython/pulls/5722", |
153 | 75 | }, |
| 76 | + "installation": {"id": 123}, |
154 | 77 | } |
155 | 78 | event = sansio.Event(data, event="pull_request", delivery_id="1") |
| 79 | + |
156 | 80 | gh = FakeGH() |
157 | | - await delete_branch.router.dispatch(event, gh) |
158 | | - assert gh.post_data is None # does not leave a comment |
159 | | - assert not hasattr(gh, "delete_url") |
| 81 | + with mock.patch.object(tasks.delete_branch_task, "delay") as mock_delay: |
| 82 | + await delete_branch.router.dispatch(event, gh) |
| 83 | + mock_delay.assert_not_called() |
| 84 | + |
| 85 | + |
| 86 | +def test_git_delete_branch_success(): |
| 87 | + with mock.patch("subprocess.check_output") as mock_subprocess: |
| 88 | + tasks._git_delete_branch("backport-17ab8f0-3.7") |
| 89 | + mock_subprocess.assert_called_once_with( |
| 90 | + ["git", "push", "origin", "--delete", "backport-17ab8f0-3.7"], |
| 91 | + stderr=mock.ANY |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_git_delete_branch_failure(): |
| 96 | + with mock.patch("subprocess.check_output") as mock_subprocess: |
| 97 | + import subprocess |
| 98 | + mock_subprocess.side_effect = subprocess.CalledProcessError( |
| 99 | + 1, "git", output=b"error: unable to delete" |
| 100 | + ) |
| 101 | + with pytest.raises(subprocess.CalledProcessError): |
| 102 | + tasks._git_delete_branch("backport-17ab8f0-3.7") |
0 commit comments