Skip to content

Commit 17ea1af

Browse files
committed
ISSUE-21: Refactor tests checking output
1 parent 16f71de commit 17ea1af

File tree

4 files changed

+135
-133
lines changed

4 files changed

+135
-133
lines changed

tests/test_cojira.py

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from __future__ import annotations
22

33
import json
4+
import re
45
import subprocess
56
import unittest
67
from os import environ
78
from typing import Any
8-
from unittest.mock import call
9+
from unittest.mock import patch
910
from urllib import request
1011
from urllib.error import HTTPError
1112
from urllib.error import URLError
@@ -470,7 +471,7 @@ def test_version_empty_via_env_var(tmpdir):
470471
environ['EMPTY_VERSION'] = ''
471472
with (
472473
tmpdir.as_cwd(),
473-
unittest.mock.patch('builtins.print') as mocked_print,
474+
patch('builtins.print') as mocked_print,
474475
):
475476
exec_cmd('git', 'init')
476477
f = tmpdir.join('pseudo_commit_msg.txt')
@@ -482,16 +483,18 @@ def test_version_empty_via_env_var(tmpdir):
482483
'-u=http://banana', '-p=pat_on_the_back',
483484
),
484485
) == 0
485-
assert call('Ticket fix version not checked') \
486-
in mocked_print.mock_calls
486+
assert mocked_print.call_args_list[-2].args[0] \
487+
.endswith('Ticket fix version not checked')
488+
assert mocked_print.call_args_list[-1].args[0] \
489+
.endswith('Ticket is OK according to COJIRA rules')
487490

488491

489492
def test_version_multiple_via_env_var(tmpdir):
490493
cojira.request.urlopen = mocked_response
491494
environ['EMPTY_VERSION'] = 'a,b,version'
492495
with (
493496
tmpdir.as_cwd(),
494-
unittest.mock.patch('builtins.print') as mocked_print,
497+
patch('builtins.print') as mocked_print,
495498
):
496499
exec_cmd('git', 'init')
497500
f = tmpdir.join('pseudo_commit_msg.txt')
@@ -503,22 +506,30 @@ def test_version_multiple_via_env_var(tmpdir):
503506
'-u=http://banana', '-p=pat_on_the_back',
504507
),
505508
) == 0
506-
assert call('Ticket fix version ("version") is allowed') \
507-
in mocked_print.mock_calls
509+
assert mocked_print.call_args_list[-3].args[0] \
510+
.startswith('Ticket fix version ("version") is allowed')
508511
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
512+
mocked_print.call_args_list[-2].args[0].endswith(
513+
'\t(allowed versions are: ({\'a\', \'b\', \'version\'}))'
514+
) or
515+
mocked_print.call_args_list[-2].args[0].endswith(
516+
'\t(allowed versions are: ({\'a\', \'version\', \'b\'}))'
517+
) or
518+
mocked_print.call_args_list[-2].args[0].endswith(
519+
'\t(allowed versions are: ({\'version\', \'a\', \'b\'}))'
520+
) or
521+
mocked_print.call_args_list[-2].args[0].endswith(
522+
'\t(allowed versions are: ({\'b\', \'a\', \'version\'}))'
523+
) or
524+
mocked_print.call_args_list[-2].args[0].endswith(
525+
'\t(allowed versions are: ({\'b\', \'version\', \'a\'}))'
526+
) or
527+
mocked_print.call_args_list[-2].args[0].endswith(
528+
'\t(allowed versions are: ({\'version\', \'b\', \'a\'}))'
529+
)
521530
)
531+
assert mocked_print.call_args_list[-1].args[0] \
532+
.endswith('Ticket is OK according to COJIRA rules')
522533

523534

524535
@pytest.mark.parametrize(
@@ -531,7 +542,6 @@ def test_version_multiple_via_env_var(tmpdir):
531542
texts=[
532543
'Lenient early exit, because no JIRA URI given',
533544
],
534-
alts=[],
535545
),
536546
dict(
537547
e=4,
@@ -541,7 +551,6 @@ def test_version_multiple_via_env_var(tmpdir):
541551
texts=[
542552
'Could not reify ticket from commit message',
543553
],
544-
alts=[],
545554
),
546555
dict(
547556
e=3,
@@ -552,23 +561,21 @@ def test_version_multiple_via_env_var(tmpdir):
552561
'-u=http://banana',
553562
),
554563
texts=[
564+
'\t\(allowed versions are: \({\'not-this-version\'}\)\)',
555565
'Ticket has no fix version, but it is expected',
556-
'\t(allowed versions are: ({\'not-this-version\'}))',
557566
],
558-
alts=[],
559567
),
560568
dict(
561569
e=1,
562570
msg='ABC-123: Banana',
563571
u=lambda _: MockedResponse('<this><is not="a"/><json/></this>'),
564572
args=('pseudo_commit_msg.txt', '-u=http://banana'),
565573
texts=[
574+
'\t disallowed categories are: \({\'done\'}\)\)',
575+
'\t\(allowed categories are: \(\),',
576+
'Ticket status category \("None"\) is not allowed',
566577
'Ticket fix version not checked',
567-
'Ticket status category ("None") is not allowed',
568-
'\t(allowed categories are: (),',
569-
'\t disallowed categories are: ({\'done\'}))',
570578
],
571-
alts=[],
572579
),
573580
dict(
574581
e=2,
@@ -579,13 +586,8 @@ def test_version_multiple_via_env_var(tmpdir):
579586
'-u=http://banana',
580587
),
581588
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-
],
589+
'\t\(allowed versions are: \({\'version[23]\', \'version[23]\'}\)\)',
590+
'Fix version of ticket \("version"\) is not allowed',
589591
],
590592
),
591593
dict(
@@ -597,34 +599,27 @@ 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?\', \'version2?\'}\)\)',
604+
'Ticket fix version \("version"\) is allowed',
605+
'Checking ticket "ABC-123"',
609606
],
610607
),
611608
],
612609
)
613610
def test_cojira_outputs(tmpdir, params):
614611
with (
615612
tmpdir.as_cwd(),
616-
unittest.mock.patch('builtins.print') as mocked_print,
613+
patch('builtins.print') as mocked_print,
617614
):
618615
if params['u'] is not None:
619616
cojira.request.urlopen = params['u']
620617
exec_cmd('git', 'init')
621618
f = tmpdir.join('pseudo_commit_msg.txt')
622619
f.write_text(params['msg'], encoding='utf-8')
623620
assert cojira.main(params['args']) == params['e']
621+
index = -1
624622
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-
)
623+
assert re.match(t, mocked_print.call_args_list[index].args[0]) \
624+
is not None
625+
index -= 1

tests/test_commiticketing.py

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import unittest
66
from pathlib import Path
77
from typing import Any
8-
from unittest.mock import call
8+
from unittest.mock import patch
99

1010
import pytest
1111
from typing_extensions import Buffer
@@ -166,13 +166,13 @@ def test_get_prefix(tmpdir, params):
166166

167167

168168
def test_not_reified(tmpdir):
169-
with unittest.mock.patch('builtins.print') as mocked_print:
169+
with patch('builtins.print') as mocked_print:
170170
with tmpdir.as_cwd():
171171
f = tmpdir.join('pseudo_commit_msg.txt')
172172
f.write_text('Abracadabra', encoding='utf-8')
173173
assert commiticketing.main((str(f),)) == 3
174-
assert call('Could not reify branch name.') \
175-
in mocked_print.mock_calls
174+
assert mocked_print.call_args_list[-1].args[0] \
175+
.endswith('Could not reify branch name.')
176176

177177

178178
@pytest.mark.parametrize('branch_name', test_set_2)
@@ -193,7 +193,7 @@ def test_out_of_scope_branch_default(tmpdir, branch_name):
193193
],
194194
)
195195
def test_out_of_scope_branch_non_default(tmpdir, params):
196-
with unittest.mock.patch('builtins.print') as mocked_print:
196+
with patch('builtins.print') as mocked_print:
197197
with tmpdir.as_cwd():
198198
exec_cmd('git', 'init')
199199
exec_cmd('git', 'checkout', '-b', params['branch'])
@@ -202,10 +202,11 @@ def test_out_of_scope_branch_non_default(tmpdir, params):
202202
assert commiticketing.main(
203203
('-b', params['proc'], '-t', params['leveled'], str(f)),
204204
) == 1
205-
assert call(
205+
assert mocked_print.call_args_list[-1].args[0] \
206+
.endswith(
206207
f"You wanted to commit to a branch [{params['branch']}], "
207208
'which does not correspond to the commiticketing setup.',
208-
) in mocked_print.mock_calls
209+
)
209210

210211

211212
@pytest.mark.parametrize(
@@ -216,17 +217,18 @@ def test_out_of_scope_branch_non_default(tmpdir, params):
216217
),
217218
)
218219
def test_in_scope_mismatched_default(tmpdir, branch_name):
219-
with unittest.mock.patch('builtins.print') as mocked_print:
220+
with patch('builtins.print') as mocked_print:
220221
with tmpdir.as_cwd():
221222
exec_cmd('git', 'init')
222223
exec_cmd('git', 'checkout', '-b', branch_name)
223224
f = tmpdir.join('pseudo_commit_msg.txt')
224225
f.write_text('Abracadabra', encoding='utf-8')
225226
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
227+
assert mocked_print.call_args_list[-1].args[0] \
228+
.endswith(
229+
f'[{branch_name}] does not correspond to branch naming '
230+
'rules, consult guidelines.',
231+
)
230232

231233

232234
@pytest.mark.parametrize(
@@ -264,22 +266,25 @@ def test_in_scope_mismatched_non_default(tmpdir, params):
264266
)
265267
def test_prefix_if_not_there(tmpdir, branch_name):
266268
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(
269+
with (
270+
tmpdir.as_cwd(),
271+
patch('builtins.print') as mocked_print,
272+
):
273+
exec_cmd('git', 'init')
274+
exec_cmd('git', 'checkout', '-b', branch_name)
275+
f = tmpdir.join('pseudo_commit_msg.txt')
276+
f.write_text('abracadabra', encoding='utf-8')
277+
assert commiticketing.main((str(f),)) == 0
278+
assert not mocked_print.call_args_list[-1].args[0] \
279+
.endswith('Commiticketing did not change your subject line.')
280+
assert mocked_print.call_args_list[-1].args[0] \
281+
.endswith(
277282
'Commiticketing prefixed your subject line '
278283
f'with [{prefix}: ] and made it sentence case'
279284
' after.',
280-
) in mocked_print.mock_calls
281-
with open(f) as msg:
282-
assert msg.readline() == f'{prefix}: Abracadabra'
285+
)
286+
with open(f) as msg:
287+
assert msg.readline() == f'{prefix}: Abracadabra'
283288

284289

285290
@pytest.mark.parametrize(
@@ -299,24 +304,26 @@ def test_prefix_if_not_there(tmpdir, branch_name):
299304
)
300305
def test_prefix_not_doubling(tmpdir, params):
301306
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'
307+
with (
308+
tmpdir.as_cwd(),
309+
patch('builtins.print') as mocked_print,
310+
):
311+
exec_cmd('git', 'init')
312+
exec_cmd('git', 'checkout', '-b', params['branch'])
313+
f = tmpdir.join('pseudo_commit_msg.txt')
314+
f.write_text(
315+
f"{prefix}: {params['msg']}",
316+
encoding='utf-8',
317+
)
318+
assert commiticketing.main((str(f),)) == 0
319+
with open(f) as msg:
320+
assert msg.readline() == f'{prefix}: Abracadabra'
314321
if params['msg'] == 'Abracadabra':
315-
assert call('Commiticketing did not change your subject line.') \
316-
in mocked_print.mock_calls
322+
assert mocked_print.call_args_list[-1].args[0] \
323+
.endswith('Commiticketing did not change your subject line.')
317324
elif params['msg'] == 'abracadabra':
318-
assert call('Commiticketing did not change your subject line.') \
319-
not in mocked_print.mock_calls
325+
assert not mocked_print.call_args_list[-1].args[0] \
326+
.endswith('Commiticketing did not change your subject line.')
320327

321328

322329
@pytest.mark.parametrize(

0 commit comments

Comments
 (0)