Skip to content

Commit d46e49c

Browse files
authored
test(utils): uses util fixture for all git operations and cli executions (#1766)
1 parent d00cf03 commit d46e49c

File tree

9 files changed

+521
-827
lines changed

9 files changed

+521
-827
lines changed

tests/commands/test_bump_command.py

Lines changed: 271 additions & 469 deletions
Large diffs are not rendered by default.

tests/commands/test_changelog_command.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
from __future__ import annotations
22

33
import itertools
4-
import sys
54
from pathlib import Path
65
from textwrap import dedent
76
from typing import TYPE_CHECKING
87

98
import pytest
109
from jinja2 import FileSystemLoader
1110

12-
from commitizen import cli, git
11+
from commitizen import git
1312
from commitizen.commands.changelog import Changelog
1413
from commitizen.exceptions import (
1514
DryRunExit,
@@ -1692,6 +1691,7 @@ def test_export_changelog_template_from_plugin(
16921691
mock_plugin: BaseCommitizen,
16931692
changelog_format: ChangelogFormat,
16941693
tmp_path: Path,
1694+
util: UtilFixture,
16951695
):
16961696
project_root = Path(tmp_commitizen_project)
16971697
target = project_root / "changelog.jinja"
@@ -1700,10 +1700,7 @@ def test_export_changelog_template_from_plugin(
17001700
src.write_text(tpl)
17011701
mock_plugin.template_loader = FileSystemLoader(tmp_path)
17021702

1703-
args = ["cz", "changelog", "--export-template", str(target)]
1704-
1705-
mocker.patch.object(sys, "argv", args)
1706-
cli.main()
1703+
util.run_cli("changelog", "--export-template", str(target))
17071704

17081705
assert target.exists()
17091706
assert target.read_text() == tpl
@@ -1712,6 +1709,7 @@ def test_export_changelog_template_from_plugin(
17121709
def test_export_changelog_template_fails_when_template_has_no_filename(
17131710
mocker: MockFixture,
17141711
tmp_commitizen_project: Path,
1712+
util: UtilFixture,
17151713
):
17161714
project_root = Path(tmp_commitizen_project)
17171715
target = project_root / "changelog.jinja"
@@ -1725,11 +1723,8 @@ class FakeTemplate:
17251723
"commitizen.changelog.get_changelog_template", return_value=FakeTemplate()
17261724
)
17271725

1728-
args = ["cz", "changelog", "--export-template", str(target)]
1729-
mocker.patch.object(sys, "argv", args)
1730-
17311726
with pytest.raises(NotAllowed) as exc_info:
1732-
cli.main()
1727+
util.run_cli("changelog", "--export-template", str(target))
17331728

17341729
assert not target.exists()
17351730
assert "Template filename is not set" in str(exc_info.value)

tests/commands/test_check_command.py

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from __future__ import annotations
22

3-
import sys
43
from io import StringIO
54
from typing import TYPE_CHECKING, Any
65

76
import pytest
87

9-
from commitizen import cli, commands, git
8+
from commitizen import commands, git
109
from commitizen.cz import registry
1110
from commitizen.cz.base import BaseCommitizen, ValidationResult
1211
from commitizen.exceptions import (
@@ -15,15 +14,17 @@
1514
InvalidCommitMessageError,
1615
NoCommitsFoundError,
1716
)
18-
from tests.utils import create_file_and_commit
1917

2018
if 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

2829
COMMIT_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
)

tests/commands/test_common_command.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import sys
2-
31
import pytest
42
from pytest_mock import MockFixture
53

6-
from commitizen import cli
74
from commitizen.commands import Example, Info, ListCz, Schema
5+
from tests.utils import UtilFixture
86

97

108
@pytest.mark.parametrize(
@@ -24,11 +22,11 @@
2422
)
2523
@pytest.mark.usefixtures("python_version")
2624
def test_command_shows_description_when_use_help_option(
27-
mocker: MockFixture,
2825
capsys,
2926
file_regression,
3027
monkeypatch: pytest.MonkeyPatch,
3128
command: str,
29+
util: UtilFixture,
3230
):
3331
"""Test that the command shows the description when the help option is used.
3432
@@ -42,10 +40,8 @@ def test_command_shows_description_when_use_help_option(
4240
monkeypatch.setenv("NO_COLOR", "1")
4341
monkeypatch.setenv("PAGER", "cat")
4442

45-
testargs = ["cz", command, "--help"]
46-
mocker.patch.object(sys, "argv", testargs)
4743
with pytest.raises(SystemExit):
48-
cli.main()
44+
util.run_cli(command, "--help")
4945

5046
out, _ = capsys.readouterr()
5147
file_regression.check(out, extension=".txt")

0 commit comments

Comments
 (0)