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