1
1
from __future__ import annotations
2
2
3
3
import json
4
+ import re
4
5
import subprocess
5
6
import unittest
6
7
from os import environ
7
8
from typing import Any
8
- from unittest .mock import call
9
+ from unittest .mock import patch
9
10
from urllib import request
10
11
from urllib .error import HTTPError
11
12
from urllib .error import URLError
@@ -470,7 +471,7 @@ def test_version_empty_via_env_var(tmpdir):
470
471
environ ['EMPTY_VERSION' ] = ''
471
472
with (
472
473
tmpdir .as_cwd (),
473
- unittest . mock . patch ('builtins.print' ) as mocked_print ,
474
+ patch ('builtins.print' ) as mocked_print ,
474
475
):
475
476
exec_cmd ('git' , 'init' )
476
477
f = tmpdir .join ('pseudo_commit_msg.txt' )
@@ -482,16 +483,18 @@ def test_version_empty_via_env_var(tmpdir):
482
483
'-u=http://banana' , '-p=pat_on_the_back' ,
483
484
),
484
485
) == 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' )
487
490
488
491
489
492
def test_version_multiple_via_env_var (tmpdir ):
490
493
cojira .request .urlopen = mocked_response
491
494
environ ['EMPTY_VERSION' ] = 'a,b,version'
492
495
with (
493
496
tmpdir .as_cwd (),
494
- unittest . mock . patch ('builtins.print' ) as mocked_print ,
497
+ patch ('builtins.print' ) as mocked_print ,
495
498
):
496
499
exec_cmd ('git' , 'init' )
497
500
f = tmpdir .join ('pseudo_commit_msg.txt' )
@@ -503,22 +506,27 @@ def test_version_multiple_via_env_var(tmpdir):
503
506
'-u=http://banana' , '-p=pat_on_the_back' ,
504
507
),
505
508
) == 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' )
508
511
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: ({\' version\' , \' a\' , \' b\' }))'
517
+ ) or
518
+ mocked_print .call_args_list [- 2 ].args [0 ].endswith (
519
+ '\t (allowed versions are: ({\' b\' , \' a\' , \' version\' }))'
520
+ ) or
521
+ mocked_print .call_args_list [- 2 ].args [0 ].endswith (
522
+ '\t (allowed versions are: ({\' b\' , \' version\' , \' a\' }))'
523
+ ) or
524
+ mocked_print .call_args_list [- 2 ].args [0 ].endswith (
525
+ '\t (allowed versions are: ({\' version\' , \' b\' , \' a\' }))'
526
+ )
521
527
)
528
+ assert mocked_print .call_args_list [- 1 ].args [0 ] \
529
+ .endswith ('Ticket is OK according to COJIRA rules' )
522
530
523
531
524
532
@pytest .mark .parametrize (
@@ -531,7 +539,6 @@ def test_version_multiple_via_env_var(tmpdir):
531
539
texts = [
532
540
'Lenient early exit, because no JIRA URI given' ,
533
541
],
534
- alts = [],
535
542
),
536
543
dict (
537
544
e = 4 ,
@@ -541,7 +548,6 @@ def test_version_multiple_via_env_var(tmpdir):
541
548
texts = [
542
549
'Could not reify ticket from commit message' ,
543
550
],
544
- alts = [],
545
551
),
546
552
dict (
547
553
e = 3 ,
@@ -552,23 +558,21 @@ def test_version_multiple_via_env_var(tmpdir):
552
558
'-u=http://banana' ,
553
559
),
554
560
texts = [
561
+ '\t \(allowed versions are: \({\' not-this-version\' }\)\)' ,
555
562
'Ticket has no fix version, but it is expected' ,
556
- '\t (allowed versions are: ({\' not-this-version\' }))' ,
557
563
],
558
- alts = [],
559
564
),
560
565
dict (
561
566
e = 1 ,
562
567
msg = 'ABC-123: Banana' ,
563
568
u = lambda _ : MockedResponse ('<this><is not="a"/><json/></this>' ),
564
569
args = ('pseudo_commit_msg.txt' , '-u=http://banana' ),
565
570
texts = [
571
+ '\t disallowed categories are: \({\' done\' }\)\)' ,
572
+ '\t \(allowed categories are: \(\),' ,
573
+ 'Ticket status category \("None"\) is not allowed' ,
566
574
'Ticket fix version not checked' ,
567
- 'Ticket status category ("None") is not allowed' ,
568
- '\t (allowed categories are: (),' ,
569
- '\t disallowed categories are: ({\' done\' }))' ,
570
575
],
571
- alts = [],
572
576
),
573
577
dict (
574
578
e = 2 ,
@@ -579,13 +583,8 @@ def test_version_multiple_via_env_var(tmpdir):
579
583
'-u=http://banana' ,
580
584
),
581
585
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
- ],
586
+ '\t \(allowed versions are: \({\' version[23]\' , \' version[23]\' }\)\)' ,
587
+ 'Fix version of ticket \("version"\) is not allowed' ,
589
588
],
590
589
),
591
590
dict (
@@ -597,34 +596,27 @@ def test_version_multiple_via_env_var(tmpdir):
597
596
'-u=http://banana' ,
598
597
),
599
598
texts = [
600
- 'Checking ticket "ABC-123"' ,
601
- 'Ticket fix version ("version") is allowed' ,
602
599
'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
- ],
600
+ '\t \(allowed versions are: \({\' version2?\' , \' version2?\' }\)\)' ,
601
+ 'Ticket fix version \("version"\) is allowed' ,
602
+ 'Checking ticket "ABC-123"' ,
609
603
],
610
604
),
611
605
],
612
606
)
613
607
def test_cojira_outputs (tmpdir , params ):
614
608
with (
615
609
tmpdir .as_cwd (),
616
- unittest . mock . patch ('builtins.print' ) as mocked_print ,
610
+ patch ('builtins.print' ) as mocked_print ,
617
611
):
618
612
if params ['u' ] is not None :
619
613
cojira .request .urlopen = params ['u' ]
620
614
exec_cmd ('git' , 'init' )
621
615
f = tmpdir .join ('pseudo_commit_msg.txt' )
622
616
f .write_text (params ['msg' ], encoding = 'utf-8' )
623
617
assert cojira .main (params ['args' ]) == params ['e' ]
618
+ index = - 1
624
619
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
- )
620
+ assert re .match (t , mocked_print .call_args_list [index ].args [0 ]) \
621
+ is not None
622
+ index -= 1
0 commit comments