Skip to content

Commit 8be2838

Browse files
chore: add a merge_request() pytest fixture and use it
Added a pytest.fixture for merge_request(). Use this fixture in tools/functional/api/test_merge_requests.py
1 parent df9b5f9 commit 8be2838

File tree

2 files changed

+105
-82
lines changed

2 files changed

+105
-82
lines changed

tools/functional/api/test_merge_requests.py

+34-82
Original file line numberDiff line numberDiff line change
@@ -100,58 +100,18 @@ def test_merge_request_merge(project):
100100
mr.merge()
101101

102102

103-
def merge_request_create_helper(
104-
*,
105-
project: gitlab.v4.objects.Project,
106-
source_branch: str,
107-
wait_for_sidekiq,
108-
branch_will_be_deleted: bool,
109-
**kwargs,
110-
):
111-
# Wait for processes to be done before we start...
112-
# NOTE(jlvillal): Sometimes the CI would give a "500 Internal Server
113-
# Error". Hoping that waiting until all other processes are done will help
114-
# with that.
115-
result = wait_for_sidekiq(timeout=60)
116-
assert result is True, "sidekiq process should have terminated but did not"
117-
118-
project.branches.create({"branch": source_branch, "ref": "master"})
119-
120-
# NOTE(jlvillal): Must create a commit in the new branch before we can
121-
# create an MR that will work.
122-
project.files.create(
123-
{
124-
"file_path": f"README.{source_branch}",
125-
"branch": source_branch,
126-
"content": "Initial content",
127-
"commit_message": "New commit in new branch",
128-
}
129-
)
130-
131-
mr = project.mergerequests.create(
132-
{
133-
"source_branch": source_branch,
134-
"target_branch": "master",
135-
"title": "Should remove source branch",
136-
"remove_source_branch": True,
137-
}
138-
)
139-
140-
result = wait_for_sidekiq(timeout=60)
141-
assert result is True, "sidekiq process should have terminated but did not"
142-
143-
mr_iid = mr.iid
144-
for _ in range(60):
145-
mr = project.mergerequests.get(mr_iid)
146-
if mr.merge_status != "checking":
147-
break
148-
time.sleep(0.5)
149-
assert mr.merge_status != "checking"
103+
def test_merge_request_should_remove_source_branch(
104+
project, merge_request, wait_for_sidekiq
105+
) -> None:
106+
"""Test to ensure
107+
https://github.com/python-gitlab/python-gitlab/issues/1120 is fixed.
108+
Bug reported that they could not use 'should_remove_source_branch' in
109+
mr.merge() call"""
150110

151-
# Ensure we can get the MR branch
152-
project.branches.get(source_branch)
111+
source_branch = "remove_source_branch"
112+
mr = merge_request(source_branch=source_branch)
153113

154-
mr.merge(**kwargs)
114+
mr.merge(should_remove_source_branch=True)
155115

156116
result = wait_for_sidekiq(timeout=60)
157117
assert result is True, "sidekiq process should have terminated but did not"
@@ -166,48 +126,40 @@ def merge_request_create_helper(
166126
assert mr.merged_at is not None
167127
time.sleep(0.5)
168128

169-
if branch_will_be_deleted:
170-
# Ensure we can NOT get the MR branch
171-
with pytest.raises(gitlab.exceptions.GitlabGetError):
172-
project.branches.get(source_branch)
173-
174-
175-
def test_merge_request_should_remove_source_branch(
176-
project: gitlab.v4.objects.Project, wait_for_sidekiq
177-
):
178-
"""Test to ensure
179-
https://github.com/python-gitlab/python-gitlab/issues/1120 is fixed.
180-
Bug reported that they could not use 'should_remove_source_branch' in
181-
mr.merge() call"""
182-
183-
source_branch = "remove_source_branch"
184-
185-
merge_request_create_helper(
186-
project=project,
187-
source_branch=source_branch,
188-
wait_for_sidekiq=wait_for_sidekiq,
189-
branch_will_be_deleted=True,
190-
should_remove_source_branch=True,
191-
)
129+
# Ensure we can NOT get the MR branch
130+
with pytest.raises(gitlab.exceptions.GitlabGetError):
131+
project.branches.get(source_branch)
192132

193133

194134
def test_merge_request_large_commit_message(
195-
project: gitlab.v4.objects.Project, wait_for_sidekiq
196-
):
135+
project, merge_request, wait_for_sidekiq
136+
) -> None:
197137
"""Test to ensure https://github.com/python-gitlab/python-gitlab/issues/1452
198138
is fixed.
199139
Bug reported that very long 'merge_commit_message' in mr.merge() would
200140
cause an error: 414 Request too large
201141
"""
142+
202143
source_branch = "large_commit_message"
144+
mr = merge_request(source_branch=source_branch)
203145

204146
merge_commit_message = "large_message\r\n" * 1_000
205147
assert len(merge_commit_message) > 10_000
206148

207-
merge_request_create_helper(
208-
project=project,
209-
source_branch=source_branch,
210-
wait_for_sidekiq=wait_for_sidekiq,
211-
branch_will_be_deleted=False,
212-
merge_commit_message=merge_commit_message,
213-
)
149+
mr.merge(merge_commit_message=merge_commit_message)
150+
151+
result = wait_for_sidekiq(timeout=60)
152+
assert result is True, "sidekiq process should have terminated but did not"
153+
154+
# Wait until it is merged
155+
mr_iid = mr.iid
156+
for _ in range(60):
157+
mr = project.mergerequests.get(mr_iid)
158+
if mr.merged_at is not None:
159+
break
160+
time.sleep(0.5)
161+
assert mr.merged_at is not None
162+
time.sleep(0.5)
163+
164+
# Ensure we can get the MR branch
165+
project.branches.get(source_branch)

tools/functional/conftest.py

+71
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,77 @@ def project(gl):
201201
print(f"Project already deleted: {e}")
202202

203203

204+
@pytest.fixture(scope="function")
205+
def merge_request(project, wait_for_sidekiq):
206+
"""Fixture used to create a merge_request.
207+
208+
It will create a branch, add a commit to the branch, and then create a
209+
merge request against project.default_branch. The MR will be returned.
210+
211+
When finished any created merge requests and branches will be deleted.
212+
213+
NOTE: No attempt is made to restore project.default_branch to its previous
214+
state. So if the merge request is merged then its content will be in the
215+
project.default_branch branch.
216+
"""
217+
218+
to_delete = []
219+
220+
def _merge_request(*, source_branch: str):
221+
# Wait for processes to be done before we start...
222+
# NOTE(jlvillal): Sometimes the CI would give a "500 Internal Server
223+
# Error". Hoping that waiting until all other processes are done will
224+
# help with that.
225+
result = wait_for_sidekiq(timeout=60)
226+
assert result is True, "sidekiq process should have terminated but did not"
227+
228+
project.refresh() # Gets us the current default branch
229+
project.branches.create(
230+
{"branch": source_branch, "ref": project.default_branch}
231+
)
232+
# NOTE(jlvillal): Must create a commit in the new branch before we can
233+
# create an MR that will work.
234+
project.files.create(
235+
{
236+
"file_path": f"README.{source_branch}",
237+
"branch": source_branch,
238+
"content": "Initial content",
239+
"commit_message": "New commit in new branch",
240+
}
241+
)
242+
mr = project.mergerequests.create(
243+
{
244+
"source_branch": source_branch,
245+
"target_branch": project.default_branch,
246+
"title": "Should remove source branch",
247+
"remove_source_branch": True,
248+
}
249+
)
250+
result = wait_for_sidekiq(timeout=60)
251+
assert result is True, "sidekiq process should have terminated but did not"
252+
253+
mr_iid = mr.iid
254+
for _ in range(60):
255+
mr = project.mergerequests.get(mr_iid)
256+
if mr.merge_status != "checking":
257+
break
258+
time.sleep(0.5)
259+
assert mr.merge_status != "checking"
260+
261+
to_delete.append((mr.iid, source_branch))
262+
return mr
263+
264+
yield _merge_request
265+
266+
for mr_iid, source_branch in to_delete:
267+
project.mergerequests.delete(mr_iid)
268+
try:
269+
project.branches.delete(source_branch)
270+
except gitlab.exceptions.GitlabDeleteError:
271+
# Ignore if branch was already deleted
272+
pass
273+
274+
204275
@pytest.fixture(scope="module")
205276
def project_file(project):
206277
"""File fixture for tests requiring a project with files and branches."""

0 commit comments

Comments
 (0)