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,30 @@ 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: ({\' 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
+ )
521
530
)
531
+ assert mocked_print .call_args_list [- 1 ].args [0 ] \
532
+ .endswith ('Ticket is OK according to COJIRA rules' )
522
533
523
534
524
535
@pytest .mark .parametrize (
@@ -531,7 +542,6 @@ def test_version_multiple_via_env_var(tmpdir):
531
542
texts = [
532
543
'Lenient early exit, because no JIRA URI given' ,
533
544
],
534
- alts = [],
535
545
),
536
546
dict (
537
547
e = 4 ,
@@ -541,7 +551,6 @@ def test_version_multiple_via_env_var(tmpdir):
541
551
texts = [
542
552
'Could not reify ticket from commit message' ,
543
553
],
544
- alts = [],
545
554
),
546
555
dict (
547
556
e = 3 ,
@@ -552,23 +561,21 @@ def test_version_multiple_via_env_var(tmpdir):
552
561
'-u=http://banana' ,
553
562
),
554
563
texts = [
564
+ '\t \(allowed versions are: \({\' not-this-version\' }\)\)' ,
555
565
'Ticket has no fix version, but it is expected' ,
556
- '\t (allowed versions are: ({\' not-this-version\' }))' ,
557
566
],
558
- alts = [],
559
567
),
560
568
dict (
561
569
e = 1 ,
562
570
msg = 'ABC-123: Banana' ,
563
571
u = lambda _ : MockedResponse ('<this><is not="a"/><json/></this>' ),
564
572
args = ('pseudo_commit_msg.txt' , '-u=http://banana' ),
565
573
texts = [
574
+ '\t disallowed categories are: \({\' done\' }\)\)' ,
575
+ '\t \(allowed categories are: \(\),' ,
576
+ 'Ticket status category \("None"\) is not allowed' ,
566
577
'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
578
],
571
- alts = [],
572
579
),
573
580
dict (
574
581
e = 2 ,
@@ -579,13 +586,8 @@ def test_version_multiple_via_env_var(tmpdir):
579
586
'-u=http://banana' ,
580
587
),
581
588
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' ,
589
591
],
590
592
),
591
593
dict (
@@ -597,34 +599,27 @@ def test_version_multiple_via_env_var(tmpdir):
597
599
'-u=http://banana' ,
598
600
),
599
601
texts = [
600
- 'Checking ticket "ABC-123"' ,
601
- 'Ticket fix version ("version") is allowed' ,
602
602
'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"' ,
609
606
],
610
607
),
611
608
],
612
609
)
613
610
def test_cojira_outputs (tmpdir , params ):
614
611
with (
615
612
tmpdir .as_cwd (),
616
- unittest . mock . patch ('builtins.print' ) as mocked_print ,
613
+ patch ('builtins.print' ) as mocked_print ,
617
614
):
618
615
if params ['u' ] is not None :
619
616
cojira .request .urlopen = params ['u' ]
620
617
exec_cmd ('git' , 'init' )
621
618
f = tmpdir .join ('pseudo_commit_msg.txt' )
622
619
f .write_text (params ['msg' ], encoding = 'utf-8' )
623
620
assert cojira .main (params ['args' ]) == params ['e' ]
621
+ index = - 1
624
622
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
0 commit comments