11from __future__ import annotations
22
33import json
4+ import re
45import subprocess
5- import unittest
66from os import environ
77from typing import Any
8- from unittest .mock import call
8+ from unittest .mock import patch
99from urllib import request
1010from urllib .error import HTTPError
1111from 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
489491def 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)
613611def 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
0 commit comments