Skip to content

Commit 5a3c99e

Browse files
committed
ISSUE-21: Refactor tests checking output
1 parent c0235c8 commit 5a3c99e

File tree

5 files changed

+143
-141
lines changed

5 files changed

+143
-141
lines changed

tests/test_cojira.py

+49-53
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
from __future__ import annotations
22

33
import json
4+
import re
45
import subprocess
5-
import unittest
66
from os import environ
77
from typing import Any
8-
from unittest.mock import call
8+
from unittest.mock import patch
99
from urllib import request
1010
from urllib.error import HTTPError
1111
from urllib.error import URLError
@@ -461,7 +461,7 @@ def test_jira_pat_as_env_var(tmpdir):
461461
(
462462
'pseudo_commit_msg.txt', '-e=none',
463463
'-u=http://banana', '-p=$YYYJIRA_PAT',
464-
)
464+
),
465465
) == 0
466466

467467

@@ -470,55 +470,65 @@ def test_version_empty_via_env_var(tmpdir):
470470
environ['EMPTY_VERSION'] = ''
471471
with (
472472
tmpdir.as_cwd(),
473-
unittest.mock.patch('builtins.print') as mocked_print,
473+
patch('builtins.print') as mocked_print,
474474
):
475475
exec_cmd('git', 'init')
476476
f = tmpdir.join('pseudo_commit_msg.txt')
477477
f.write_text('ABC-123: Banana', encoding='utf-8')
478478
assert cojira.main(
479479
(
480480
'pseudo_commit_msg.txt',
481-
f'-v=$EMPTY_VERSION',
481+
'-v=$EMPTY_VERSION',
482482
'-u=http://banana', '-p=pat_on_the_back',
483483
),
484484
) == 0
485-
assert call('Ticket fix version not checked') \
486-
in mocked_print.mock_calls
485+
assert mocked_print.call_args_list[-2].args[0] \
486+
.endswith('Ticket fix version not checked')
487+
assert mocked_print.call_args_list[-1].args[0] \
488+
.endswith('Ticket is OK according to COJIRA rules')
487489

488490

489491
def test_version_multiple_via_env_var(tmpdir):
490492
cojira.request.urlopen = mocked_response
491493
environ['EMPTY_VERSION'] = 'a,b,version'
492494
with (
493495
tmpdir.as_cwd(),
494-
unittest.mock.patch('builtins.print') as mocked_print,
496+
patch('builtins.print') as mocked_print,
495497
):
496498
exec_cmd('git', 'init')
497499
f = tmpdir.join('pseudo_commit_msg.txt')
498500
f.write_text('ABC-123: Banana', encoding='utf-8')
499501
assert cojira.main(
500502
(
501503
'pseudo_commit_msg.txt',
502-
f'-v=$EMPTY_VERSION',
504+
'-v=$EMPTY_VERSION',
503505
'-u=http://banana', '-p=pat_on_the_back',
504506
),
505507
) == 0
506-
assert call('Ticket fix version ("version") is allowed') \
507-
in mocked_print.mock_calls
508+
assert mocked_print.call_args_list[-3].args[0] \
509+
.startswith('Ticket fix version ("version") is allowed')
508510
assert (
509-
call('\t(allowed versions are: ({\'a\', \'b\', \'version\'}))') \
510-
in mocked_print.mock_calls or
511-
call('\t(allowed versions are: ({\'a\', \'version\', \'b\'}))') \
512-
in mocked_print.mock_calls or
513-
call('\t(allowed versions are: ({\'version\', \'a\', \'b\'}))') \
514-
in mocked_print.mock_calls or
515-
call('\t(allowed versions are: ({\'b\', \'a\', \'version\'}))') \
516-
in mocked_print.mock_calls or
517-
call('\t(allowed versions are: ({\'b\', \'version\', \'a\'}))') \
518-
in mocked_print.mock_calls or
519-
call('\t(allowed versions are: ({\'version\', \'b\', \'a\'}))') \
520-
in mocked_print.mock_calls
511+
mocked_print.call_args_list[-2].args[0].endswith(
512+
'\t(allowed versions are: ({\'a\', \'b\', \'version\'}))',
513+
) or
514+
mocked_print.call_args_list[-2].args[0].endswith(
515+
'\t(allowed versions are: ({\'a\', \'version\', \'b\'}))',
516+
) or
517+
mocked_print.call_args_list[-2].args[0].endswith(
518+
'\t(allowed versions are: ({\'version\', \'a\', \'b\'}))',
519+
) or
520+
mocked_print.call_args_list[-2].args[0].endswith(
521+
'\t(allowed versions are: ({\'b\', \'a\', \'version\'}))',
522+
) or
523+
mocked_print.call_args_list[-2].args[0].endswith(
524+
'\t(allowed versions are: ({\'b\', \'version\', \'a\'}))',
525+
) or
526+
mocked_print.call_args_list[-2].args[0].endswith(
527+
'\t(allowed versions are: ({\'version\', \'b\', \'a\'}))',
528+
)
521529
)
530+
assert mocked_print.call_args_list[-1].args[0] \
531+
.endswith('Ticket is OK according to COJIRA rules')
522532

523533

524534
@pytest.mark.parametrize(
@@ -531,7 +541,6 @@ def test_version_multiple_via_env_var(tmpdir):
531541
texts=[
532542
'Lenient early exit, because no JIRA URI given',
533543
],
534-
alts=[],
535544
),
536545
dict(
537546
e=4,
@@ -541,7 +550,6 @@ def test_version_multiple_via_env_var(tmpdir):
541550
texts=[
542551
'Could not reify ticket from commit message',
543552
],
544-
alts=[],
545553
),
546554
dict(
547555
e=3,
@@ -552,23 +560,21 @@ def test_version_multiple_via_env_var(tmpdir):
552560
'-u=http://banana',
553561
),
554562
texts=[
563+
'\t\\(allowed versions are: \\({\'not-this-version\'}\\)\\)',
555564
'Ticket has no fix version, but it is expected',
556-
'\t(allowed versions are: ({\'not-this-version\'}))',
557565
],
558-
alts=[],
559566
),
560567
dict(
561568
e=1,
562569
msg='ABC-123: Banana',
563570
u=lambda _: MockedResponse('<this><is not="a"/><json/></this>'),
564571
args=('pseudo_commit_msg.txt', '-u=http://banana'),
565572
texts=[
573+
'\t disallowed categories are: \\({\'done\'}\\)\\)',
574+
'\t\\(allowed categories are: \\(\\),',
575+
r'Ticket status category \("None"\) is not allowed',
566576
'Ticket fix version not checked',
567-
'Ticket status category ("None") is not allowed',
568-
'\t(allowed categories are: (),',
569-
'\t disallowed categories are: ({\'done\'}))',
570577
],
571-
alts=[],
572578
),
573579
dict(
574580
e=2,
@@ -579,13 +585,9 @@ def test_version_multiple_via_env_var(tmpdir):
579585
'-u=http://banana',
580586
),
581587
texts=[
582-
'Fix version of ticket ("version") is not allowed',
583-
],
584-
alts=[
585-
[
586-
'\t(allowed versions are: ({\'version2\', \'version3\'}))',
587-
'\t(allowed versions are: ({\'version3\', \'version2\'}))',
588-
],
588+
'\t\\(allowed versions are: \\({\'version[23]\','
589+
' \'version[23]\'}\\)\\)',
590+
r'Fix version of ticket \("version"\) is not allowed',
589591
],
590592
),
591593
dict(
@@ -597,34 +599,28 @@ def test_version_multiple_via_env_var(tmpdir):
597599
'-u=http://banana',
598600
),
599601
texts=[
600-
'Checking ticket "ABC-123"',
601-
'Ticket fix version ("version") is allowed',
602602
'Ticket is OK according to COJIRA rules',
603-
],
604-
alts=[
605-
[
606-
'\t(allowed versions are: ({\'version2\', \'version\'}))',
607-
'\t(allowed versions are: ({\'version\', \'version2\'}))',
608-
],
603+
'\t\\(allowed versions are: \\({\'version2?\','
604+
' \'version2?\'}\\)\\)',
605+
r'Ticket fix version \("version"\) is allowed',
606+
'Checking ticket "ABC-123"',
609607
],
610608
),
611609
],
612610
)
613611
def test_cojira_outputs(tmpdir, params):
614612
with (
615613
tmpdir.as_cwd(),
616-
unittest.mock.patch('builtins.print') as mocked_print,
614+
patch('builtins.print') as mocked_print,
617615
):
618616
if params['u'] is not None:
619617
cojira.request.urlopen = params['u']
620618
exec_cmd('git', 'init')
621619
f = tmpdir.join('pseudo_commit_msg.txt')
622620
f.write_text(params['msg'], encoding='utf-8')
623621
assert cojira.main(params['args']) == params['e']
622+
index = -1
624623
for t in params['texts']:
625-
assert call(t) in mocked_print.mock_calls
626-
for p in params['alts']:
627-
assert (
628-
call(p[0]) in mocked_print.mock_calls or
629-
call(p[1]) in mocked_print.mock_calls
630-
)
624+
assert re.match(t, mocked_print.call_args_list[index].args[0]) \
625+
is not None
626+
index -= 1

tests/test_commiticketing.py

+48-42
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import random
44
import subprocess
5-
import unittest
65
from pathlib import Path
76
from typing import Any
8-
from unittest.mock import call
7+
from unittest.mock import patch
98

109
import pytest
1110
from typing_extensions import Buffer
@@ -166,13 +165,13 @@ def test_get_prefix(tmpdir, params):
166165

167166

168167
def test_not_reified(tmpdir):
169-
with unittest.mock.patch('builtins.print') as mocked_print:
168+
with patch('builtins.print') as mocked_print:
170169
with tmpdir.as_cwd():
171170
f = tmpdir.join('pseudo_commit_msg.txt')
172171
f.write_text('Abracadabra', encoding='utf-8')
173172
assert commiticketing.main((str(f),)) == 3
174-
assert call('Could not reify branch name.') \
175-
in mocked_print.mock_calls
173+
assert mocked_print.call_args_list[-1].args[0] \
174+
.endswith('Could not reify branch name.')
176175

177176

178177
@pytest.mark.parametrize('branch_name', test_set_2)
@@ -193,7 +192,7 @@ def test_out_of_scope_branch_default(tmpdir, branch_name):
193192
],
194193
)
195194
def test_out_of_scope_branch_non_default(tmpdir, params):
196-
with unittest.mock.patch('builtins.print') as mocked_print:
195+
with patch('builtins.print') as mocked_print:
197196
with tmpdir.as_cwd():
198197
exec_cmd('git', 'init')
199198
exec_cmd('git', 'checkout', '-b', params['branch'])
@@ -202,10 +201,11 @@ def test_out_of_scope_branch_non_default(tmpdir, params):
202201
assert commiticketing.main(
203202
('-b', params['proc'], '-t', params['leveled'], str(f)),
204203
) == 1
205-
assert call(
204+
assert mocked_print.call_args_list[-1].args[0] \
205+
.endswith(
206206
f"You wanted to commit to a branch [{params['branch']}], "
207207
'which does not correspond to the commiticketing setup.',
208-
) in mocked_print.mock_calls
208+
)
209209

210210

211211
@pytest.mark.parametrize(
@@ -216,17 +216,18 @@ def test_out_of_scope_branch_non_default(tmpdir, params):
216216
),
217217
)
218218
def test_in_scope_mismatched_default(tmpdir, branch_name):
219-
with unittest.mock.patch('builtins.print') as mocked_print:
219+
with patch('builtins.print') as mocked_print:
220220
with tmpdir.as_cwd():
221221
exec_cmd('git', 'init')
222222
exec_cmd('git', 'checkout', '-b', branch_name)
223223
f = tmpdir.join('pseudo_commit_msg.txt')
224224
f.write_text('Abracadabra', encoding='utf-8')
225225
assert commiticketing.main((str(f),)) == 2
226-
assert call(
227-
f'[{branch_name}] does not correspond to branch naming '
228-
'rules, consult guidelines.',
229-
) in mocked_print.mock_calls
226+
assert mocked_print.call_args_list[-1].args[0] \
227+
.endswith(
228+
f'[{branch_name}] does not correspond to branch naming '
229+
'rules, consult guidelines.',
230+
)
230231

231232

232233
@pytest.mark.parametrize(
@@ -264,22 +265,25 @@ def test_in_scope_mismatched_non_default(tmpdir, params):
264265
)
265266
def test_prefix_if_not_there(tmpdir, branch_name):
266267
prefix = commiticketing.get_prefix(branch_name, ['user', 'backup'])
267-
with tmpdir.as_cwd():
268-
with unittest.mock.patch('builtins.print') as mocked_print:
269-
exec_cmd('git', 'init')
270-
exec_cmd('git', 'checkout', '-b', branch_name)
271-
f = tmpdir.join('pseudo_commit_msg.txt')
272-
f.write_text('abracadabra', encoding='utf-8')
273-
assert commiticketing.main((str(f),)) == 0
274-
assert call('Commiticketing did not change your subject line.') \
275-
not in mocked_print.mock_calls
276-
assert call(
268+
with (
269+
tmpdir.as_cwd(),
270+
patch('builtins.print') as mocked_print,
271+
):
272+
exec_cmd('git', 'init')
273+
exec_cmd('git', 'checkout', '-b', branch_name)
274+
f = tmpdir.join('pseudo_commit_msg.txt')
275+
f.write_text('abracadabra', encoding='utf-8')
276+
assert commiticketing.main((str(f),)) == 0
277+
assert not mocked_print.call_args_list[-1].args[0] \
278+
.endswith('Commiticketing did not change your subject line.')
279+
assert mocked_print.call_args_list[-1].args[0] \
280+
.endswith(
277281
'Commiticketing prefixed your subject line '
278282
f'with [{prefix}: ] and made it sentence case'
279283
' after.',
280-
) in mocked_print.mock_calls
281-
with open(f) as msg:
282-
assert msg.readline() == f'{prefix}: Abracadabra'
284+
)
285+
with open(f) as msg:
286+
assert msg.readline() == f'{prefix}: Abracadabra'
283287

284288

285289
@pytest.mark.parametrize(
@@ -299,24 +303,26 @@ def test_prefix_if_not_there(tmpdir, branch_name):
299303
)
300304
def test_prefix_not_doubling(tmpdir, params):
301305
prefix = commiticketing.get_prefix(params['branch'], ['user', 'backup'])
302-
with tmpdir.as_cwd():
303-
with unittest.mock.patch('builtins.print') as mocked_print:
304-
exec_cmd('git', 'init')
305-
exec_cmd('git', 'checkout', '-b', params['branch'])
306-
f = tmpdir.join('pseudo_commit_msg.txt')
307-
f.write_text(
308-
f"{prefix}: {params['msg']}",
309-
encoding='utf-8',
310-
)
311-
assert commiticketing.main((str(f),)) == 0
312-
with open(f) as msg:
313-
assert msg.readline() == f'{prefix}: Abracadabra'
306+
with (
307+
tmpdir.as_cwd(),
308+
patch('builtins.print') as mocked_print,
309+
):
310+
exec_cmd('git', 'init')
311+
exec_cmd('git', 'checkout', '-b', params['branch'])
312+
f = tmpdir.join('pseudo_commit_msg.txt')
313+
f.write_text(
314+
f"{prefix}: {params['msg']}",
315+
encoding='utf-8',
316+
)
317+
assert commiticketing.main((str(f),)) == 0
318+
with open(f) as msg:
319+
assert msg.readline() == f'{prefix}: Abracadabra'
314320
if params['msg'] == 'Abracadabra':
315-
assert call('Commiticketing did not change your subject line.') \
316-
in mocked_print.mock_calls
321+
assert mocked_print.call_args_list[-1].args[0] \
322+
.endswith('Commiticketing did not change your subject line.')
317323
elif params['msg'] == 'abracadabra':
318-
assert call('Commiticketing did not change your subject line.') \
319-
not in mocked_print.mock_calls
324+
assert not mocked_print.call_args_list[-1].args[0] \
325+
.endswith('Commiticketing did not change your subject line.')
320326

321327

322328
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)