11from __future__ import annotations
22
3- import sys
43from io import StringIO
54from typing import TYPE_CHECKING , Any
65
76import pytest
87
9- from commitizen import cli , commands , git
8+ from commitizen import commands , git
109from commitizen .cz import registry
1110from commitizen .cz .base import BaseCommitizen , ValidationResult
1211from commitizen .exceptions import (
1514 InvalidCommitMessageError ,
1615 NoCommitsFoundError ,
1716)
18- from tests .utils import create_file_and_commit
1917
2018if TYPE_CHECKING :
2119 import re
2220 from collections .abc import Mapping
2321
2422 from pytest_mock import MockFixture
2523
24+ from commitizen .config .base_config import BaseConfig
2625 from commitizen .question import CzQuestion
26+ from tests .utils import UtilFixture
27+
2728
2829COMMIT_LOG = [
2930 "refactor: A code change that neither fixes a bug nor adds a feature" ,
@@ -68,74 +69,70 @@ def _build_fake_git_commits(commit_msgs: list[str]) -> list[git.GitCommit]:
6869 return [git .GitCommit ("test_rev" , commit_msg ) for commit_msg in commit_msgs ]
6970
7071
71- def test_check_jira_fails (mocker : MockFixture ):
72- testargs = ["cz" , "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" ]
73- mocker .patch .object (sys , "argv" , testargs )
72+ def test_check_jira_fails (mocker : MockFixture , util : UtilFixture ):
7473 mocker .patch (
7574 "commitizen.commands.check.open" ,
7675 mocker .mock_open (read_data = "random message for J-2 #fake_command blah" ),
7776 )
7877 with pytest .raises (InvalidCommitMessageError ) as excinfo :
79- cli . main ( )
78+ util . run_cli ( "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" )
8079 assert "commit validation: failed!" in str (excinfo .value )
8180
8281
83- def test_check_jira_command_after_issue_one_space (mocker : MockFixture , capsys ):
84- testargs = [ "cz" , "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" ]
85- mocker . patch . object ( sys , "argv" , testargs )
82+ def test_check_jira_command_after_issue_one_space (
83+ mocker : MockFixture , capsys , util : UtilFixture
84+ ):
8685 mocker .patch (
8786 "commitizen.commands.check.open" ,
8887 mocker .mock_open (read_data = "JR-23 #command some arguments etc" ),
8988 )
90- cli . main ( )
89+ util . run_cli ( "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" )
9190 out , _ = capsys .readouterr ()
9291 assert "Commit validation: successful!" in out
9392
9493
95- def test_check_jira_command_after_issue_two_spaces (mocker : MockFixture , capsys ):
96- testargs = [ "cz" , "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" ]
97- mocker . patch . object ( sys , "argv" , testargs )
94+ def test_check_jira_command_after_issue_two_spaces (
95+ mocker : MockFixture , capsys , util : UtilFixture
96+ ):
9897 mocker .patch (
9998 "commitizen.commands.check.open" ,
10099 mocker .mock_open (read_data = "JR-2 #command some arguments etc" ),
101100 )
102- cli . main ( )
101+ util . run_cli ( "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" )
103102 out , _ = capsys .readouterr ()
104103 assert "Commit validation: successful!" in out
105104
106105
107- def test_check_jira_text_between_issue_and_command (mocker : MockFixture , capsys ):
108- testargs = [ "cz" , "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" ]
109- mocker . patch . object ( sys , "argv" , testargs )
106+ def test_check_jira_text_between_issue_and_command (
107+ mocker : MockFixture , capsys , util : UtilFixture
108+ ):
110109 mocker .patch (
111110 "commitizen.commands.check.open" ,
112111 mocker .mock_open (read_data = "JR-234 some text #command some arguments etc" ),
113112 )
114- cli . main ( )
113+ util . run_cli ( "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" )
115114 out , _ = capsys .readouterr ()
116115 assert "Commit validation: successful!" in out
117116
118117
119- def test_check_jira_multiple_commands (mocker : MockFixture , capsys ):
120- testargs = ["cz" , "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" ]
121- mocker .patch .object (sys , "argv" , testargs )
118+ def test_check_jira_multiple_commands (mocker : MockFixture , capsys , util : UtilFixture ):
122119 mocker .patch (
123120 "commitizen.commands.check.open" ,
124121 mocker .mock_open (read_data = "JRA-23 some text #command1 args #command2 args" ),
125122 )
126- cli . main ( )
123+ util . run_cli ( "-n" , "cz_jira" , "check" , "--commit-msg-file" , "some_file" )
127124 out , _ = capsys .readouterr ()
128125 assert "Commit validation: successful!" in out
129126
130127
131- def test_check_conventional_commit_succeeds (mocker : MockFixture , capsys ):
132- testargs = [ "cz" , "check" , "--commit-msg-file" , "some_file" ]
133- mocker . patch . object ( sys , "argv" , testargs )
128+ def test_check_conventional_commit_succeeds (
129+ mocker : MockFixture , capsys , util : UtilFixture
130+ ):
134131 mocker .patch (
135132 "commitizen.commands.check.open" ,
136133 mocker .mock_open (read_data = "fix(scope): some commit message" ),
137134 )
138- cli . main ( )
135+ util . run_cli ( "check" , "--commit-msg-file" , "some_file" )
139136 out , _ = capsys .readouterr ()
140137 assert "Commit validation: successful!" in out
141138
@@ -234,9 +231,9 @@ def test_check_command_with_invalid_argument(config):
234231
235232
236233@pytest .mark .usefixtures ("tmp_commitizen_project" )
237- def test_check_command_with_empty_range (config , mocker : MockFixture ):
234+ def test_check_command_with_empty_range (config : BaseConfig , util : UtilFixture ):
238235 # must initialize git with a commit
239- create_file_and_commit ("feat: initial" )
236+ util . create_file_and_commit ("feat: initial" )
240237
241238 check_cmd = commands .Check (config = config , arguments = {"rev_range" : "master..master" })
242239 with pytest .raises (NoCommitsFoundError ) as excinfo :
@@ -356,29 +353,29 @@ def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixt
356353 error_mock .assert_called_once ()
357354
358355
359- def test_check_command_with_pipe_message (mocker : MockFixture , capsys ):
360- testargs = [ "cz" , "check" ]
361- mocker . patch . object ( sys , "argv" , testargs )
356+ def test_check_command_with_pipe_message (
357+ mocker : MockFixture , capsys , util : UtilFixture
358+ ):
362359 mocker .patch ("sys.stdin" , StringIO ("fix(scope): some commit message" ))
363360
364- cli . main ( )
361+ util . run_cli ( "check" )
365362 out , _ = capsys .readouterr ()
366363 assert "Commit validation: successful!" in out
367364
368365
369- def test_check_command_with_pipe_message_and_failed (mocker : MockFixture ):
370- testargs = [ "cz" , "check" ]
371- mocker . patch . object ( sys , "argv" , testargs )
366+ def test_check_command_with_pipe_message_and_failed (
367+ mocker : MockFixture , util : UtilFixture
368+ ):
372369 mocker .patch ("sys.stdin" , StringIO ("bad commit message" ))
373370
374371 with pytest .raises (InvalidCommitMessageError ) as excinfo :
375- cli . main ( )
372+ util . run_cli ( "check" )
376373 assert "commit validation: failed!" in str (excinfo .value )
377374
378375
379- def test_check_command_with_comment_in_message_file (mocker : MockFixture , capsys ):
380- testargs = [ "cz" , "check" , "--commit-msg-file" , "some_file" ]
381- mocker . patch . object ( sys , "argv" , testargs )
376+ def test_check_command_with_comment_in_message_file (
377+ mocker : MockFixture , capsys , util : UtilFixture
378+ ):
382379 mocker .patch (
383380 "commitizen.commands.check.open" ,
384381 mocker .mock_open (
@@ -391,12 +388,14 @@ def test_check_command_with_comment_in_message_file(mocker: MockFixture, capsys)
391388 "This pre-commit hook will check our commits automatically."
392389 ),
393390 )
394- cli . main ( )
391+ util . run_cli ( "check" , "--commit-msg-file" , "some_file" )
395392 out , _ = capsys .readouterr ()
396393 assert "Commit validation: successful!" in out
397394
398395
399- def test_check_conventional_commit_succeed_with_git_diff (mocker , capsys ):
396+ def test_check_conventional_commit_succeed_with_git_diff (
397+ mocker , capsys , util : UtilFixture
398+ ):
400399 commit_msg = (
401400 "feat: This is a test commit\n "
402401 "# Please enter the commit message for your changes. Lines starting\n "
@@ -416,13 +415,11 @@ def test_check_conventional_commit_succeed_with_git_diff(mocker, capsys):
416415 "@@ -92,3 +92,4 @@ class Command(BaseCommand):\n "
417416 '+ "this is a test"\n '
418417 )
419- testargs = ["cz" , "check" , "--commit-msg-file" , "some_file" ]
420- mocker .patch .object (sys , "argv" , testargs )
421418 mocker .patch (
422419 "commitizen.commands.check.open" ,
423420 mocker .mock_open (read_data = commit_msg ),
424421 )
425- cli . main ( )
422+ util . run_cli ( "check" , "--commit-msg-file" , "some_file" )
426423 out , _ = capsys .readouterr ()
427424 assert "Commit validation: successful!" in out
428425
@@ -600,44 +597,34 @@ def use_cz_custom_validator(mocker):
600597
601598
602599@pytest .mark .usefixtures ("use_cz_custom_validator" )
603- def test_check_command_with_custom_validator_succeed (mocker : MockFixture , capsys ):
604- testargs = [
605- "cz" ,
606- "--name" ,
607- "cz_custom_validator" ,
608- "check" ,
609- "--commit-msg-file" ,
610- "some_file" ,
611- ]
612- mocker .patch .object (sys , "argv" , testargs )
600+ def test_check_command_with_custom_validator_succeed (
601+ mocker : MockFixture , capsys , util : UtilFixture
602+ ):
613603 mocker .patch (
614604 "commitizen.commands.check.open" ,
615605 mocker .mock_open (read_data = "ABC-123: add commitizen pre-commit hook" ),
616606 )
617- cli .main ()
607+ util .run_cli (
608+ "--name" , "cz_custom_validator" , "check" , "--commit-msg-file" , "some_file"
609+ )
618610 out , _ = capsys .readouterr ()
619611 assert "Commit validation: successful!" in out
620612
621613
622614@pytest .mark .usefixtures ("use_cz_custom_validator" )
623- def test_check_command_with_custom_validator_failed (mocker : MockFixture ):
624- testargs = [
625- "cz" ,
626- "--name" ,
627- "cz_custom_validator" ,
628- "check" ,
629- "--commit-msg-file" ,
630- "some_file" ,
631- ]
632- mocker .patch .object (sys , "argv" , testargs )
615+ def test_check_command_with_custom_validator_failed (
616+ mocker : MockFixture , util : UtilFixture
617+ ):
633618 mocker .patch (
634619 "commitizen.commands.check.open" ,
635620 mocker .mock_open (
636621 read_data = "123-ABC issue id has wrong format and misses colon"
637622 ),
638623 )
639624 with pytest .raises (InvalidCommitMessageError ) as excinfo :
640- cli .main ()
625+ util .run_cli (
626+ "--name" , "cz_custom_validator" , "check" , "--commit-msg-file" , "some_file"
627+ )
641628 assert "commit validation: failed!" in str (excinfo .value ), (
642629 "Pattern validation unexpectedly passed"
643630 )
0 commit comments