Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
740 changes: 271 additions & 469 deletions tests/commands/test_bump_command.py

Large diffs are not rendered by default.

15 changes: 5 additions & 10 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
from __future__ import annotations

import itertools
import sys
from pathlib import Path
from textwrap import dedent
from typing import TYPE_CHECKING

import pytest
from jinja2 import FileSystemLoader

from commitizen import cli, git
from commitizen import git
from commitizen.commands.changelog import Changelog
from commitizen.exceptions import (
DryRunExit,
Expand Down Expand Up @@ -1692,6 +1691,7 @@ def test_export_changelog_template_from_plugin(
mock_plugin: BaseCommitizen,
changelog_format: ChangelogFormat,
tmp_path: Path,
util: UtilFixture,
):
project_root = Path(tmp_commitizen_project)
target = project_root / "changelog.jinja"
Expand All @@ -1700,10 +1700,7 @@ def test_export_changelog_template_from_plugin(
src.write_text(tpl)
mock_plugin.template_loader = FileSystemLoader(tmp_path)

args = ["cz", "changelog", "--export-template", str(target)]

mocker.patch.object(sys, "argv", args)
cli.main()
util.run_cli("changelog", "--export-template", str(target))

assert target.exists()
assert target.read_text() == tpl
Expand All @@ -1712,6 +1709,7 @@ def test_export_changelog_template_from_plugin(
def test_export_changelog_template_fails_when_template_has_no_filename(
mocker: MockFixture,
tmp_commitizen_project: Path,
util: UtilFixture,
):
project_root = Path(tmp_commitizen_project)
target = project_root / "changelog.jinja"
Expand All @@ -1725,11 +1723,8 @@ class FakeTemplate:
"commitizen.changelog.get_changelog_template", return_value=FakeTemplate()
)

args = ["cz", "changelog", "--export-template", str(target)]
mocker.patch.object(sys, "argv", args)

with pytest.raises(NotAllowed) as exc_info:
cli.main()
util.run_cli("changelog", "--export-template", str(target))

assert not target.exists()
assert "Template filename is not set" in str(exc_info.value)
121 changes: 54 additions & 67 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from __future__ import annotations

import sys
from io import StringIO
from typing import TYPE_CHECKING, Any

import pytest

from commitizen import cli, commands, git
from commitizen import commands, git
from commitizen.cz import registry
from commitizen.cz.base import BaseCommitizen, ValidationResult
from commitizen.exceptions import (
Expand All @@ -15,15 +14,17 @@
InvalidCommitMessageError,
NoCommitsFoundError,
)
from tests.utils import create_file_and_commit

if TYPE_CHECKING:
import re
from collections.abc import Mapping

from pytest_mock import MockFixture

from commitizen.config.base_config import BaseConfig
from commitizen.question import CzQuestion
from tests.utils import UtilFixture


COMMIT_LOG = [
"refactor: A code change that neither fixes a bug nor adds a feature",
Expand Down Expand Up @@ -68,74 +69,70 @@ def _build_fake_git_commits(commit_msgs: list[str]) -> list[git.GitCommit]:
return [git.GitCommit("test_rev", commit_msg) for commit_msg in commit_msgs]


def test_check_jira_fails(mocker: MockFixture):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
def test_check_jira_fails(mocker: MockFixture, util: UtilFixture):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="random message for J-2 #fake_command blah"),
)
with pytest.raises(InvalidCommitMessageError) as excinfo:
cli.main()
util.run_cli("-n", "cz_jira", "check", "--commit-msg-file", "some_file")
assert "commit validation: failed!" in str(excinfo.value)


def test_check_jira_command_after_issue_one_space(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
def test_check_jira_command_after_issue_one_space(
mocker: MockFixture, capsys, util: UtilFixture
):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JR-23 #command some arguments etc"),
)
cli.main()
util.run_cli("-n", "cz_jira", "check", "--commit-msg-file", "some_file")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out


def test_check_jira_command_after_issue_two_spaces(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
def test_check_jira_command_after_issue_two_spaces(
mocker: MockFixture, capsys, util: UtilFixture
):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JR-2 #command some arguments etc"),
)
cli.main()
util.run_cli("-n", "cz_jira", "check", "--commit-msg-file", "some_file")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out


def test_check_jira_text_between_issue_and_command(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
def test_check_jira_text_between_issue_and_command(
mocker: MockFixture, capsys, util: UtilFixture
):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JR-234 some text #command some arguments etc"),
)
cli.main()
util.run_cli("-n", "cz_jira", "check", "--commit-msg-file", "some_file")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out


def test_check_jira_multiple_commands(mocker: MockFixture, capsys):
testargs = ["cz", "-n", "cz_jira", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
def test_check_jira_multiple_commands(mocker: MockFixture, capsys, util: UtilFixture):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="JRA-23 some text #command1 args #command2 args"),
)
cli.main()
util.run_cli("-n", "cz_jira", "check", "--commit-msg-file", "some_file")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out


def test_check_conventional_commit_succeeds(mocker: MockFixture, capsys):
testargs = ["cz", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
def test_check_conventional_commit_succeeds(
mocker: MockFixture, capsys, util: UtilFixture
):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="fix(scope): some commit message"),
)
cli.main()
util.run_cli("check", "--commit-msg-file", "some_file")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out

Expand Down Expand Up @@ -234,9 +231,9 @@ def test_check_command_with_invalid_argument(config):


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_check_command_with_empty_range(config, mocker: MockFixture):
def test_check_command_with_empty_range(config: BaseConfig, util: UtilFixture):
# must initialize git with a commit
create_file_and_commit("feat: initial")
util.create_file_and_commit("feat: initial")

check_cmd = commands.Check(config=config, arguments={"rev_range": "master..master"})
with pytest.raises(NoCommitsFoundError) as excinfo:
Expand Down Expand Up @@ -356,29 +353,29 @@ def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixt
error_mock.assert_called_once()


def test_check_command_with_pipe_message(mocker: MockFixture, capsys):
testargs = ["cz", "check"]
mocker.patch.object(sys, "argv", testargs)
def test_check_command_with_pipe_message(
mocker: MockFixture, capsys, util: UtilFixture
):
mocker.patch("sys.stdin", StringIO("fix(scope): some commit message"))

cli.main()
util.run_cli("check")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out


def test_check_command_with_pipe_message_and_failed(mocker: MockFixture):
testargs = ["cz", "check"]
mocker.patch.object(sys, "argv", testargs)
def test_check_command_with_pipe_message_and_failed(
mocker: MockFixture, util: UtilFixture
):
mocker.patch("sys.stdin", StringIO("bad commit message"))

with pytest.raises(InvalidCommitMessageError) as excinfo:
cli.main()
util.run_cli("check")
assert "commit validation: failed!" in str(excinfo.value)


def test_check_command_with_comment_in_message_file(mocker: MockFixture, capsys):
testargs = ["cz", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
def test_check_command_with_comment_in_message_file(
mocker: MockFixture, capsys, util: UtilFixture
):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(
Expand All @@ -391,12 +388,14 @@ def test_check_command_with_comment_in_message_file(mocker: MockFixture, capsys)
"This pre-commit hook will check our commits automatically."
),
)
cli.main()
util.run_cli("check", "--commit-msg-file", "some_file")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out


def test_check_conventional_commit_succeed_with_git_diff(mocker, capsys):
def test_check_conventional_commit_succeed_with_git_diff(
mocker, capsys, util: UtilFixture
):
commit_msg = (
"feat: This is a test commit\n"
"# Please enter the commit message for your changes. Lines starting\n"
Expand All @@ -416,13 +415,11 @@ def test_check_conventional_commit_succeed_with_git_diff(mocker, capsys):
"@@ -92,3 +92,4 @@ class Command(BaseCommand):\n"
'+ "this is a test"\n'
)
testargs = ["cz", "check", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data=commit_msg),
)
cli.main()
util.run_cli("check", "--commit-msg-file", "some_file")
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out

Expand Down Expand Up @@ -600,44 +597,34 @@ def use_cz_custom_validator(mocker):


@pytest.mark.usefixtures("use_cz_custom_validator")
def test_check_command_with_custom_validator_succeed(mocker: MockFixture, capsys):
testargs = [
"cz",
"--name",
"cz_custom_validator",
"check",
"--commit-msg-file",
"some_file",
]
mocker.patch.object(sys, "argv", testargs)
def test_check_command_with_custom_validator_succeed(
mocker: MockFixture, capsys, util: UtilFixture
):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(read_data="ABC-123: add commitizen pre-commit hook"),
)
cli.main()
util.run_cli(
"--name", "cz_custom_validator", "check", "--commit-msg-file", "some_file"
)
out, _ = capsys.readouterr()
assert "Commit validation: successful!" in out


@pytest.mark.usefixtures("use_cz_custom_validator")
def test_check_command_with_custom_validator_failed(mocker: MockFixture):
testargs = [
"cz",
"--name",
"cz_custom_validator",
"check",
"--commit-msg-file",
"some_file",
]
mocker.patch.object(sys, "argv", testargs)
def test_check_command_with_custom_validator_failed(
mocker: MockFixture, util: UtilFixture
):
mocker.patch(
"commitizen.commands.check.open",
mocker.mock_open(
read_data="123-ABC issue id has wrong format and misses colon"
),
)
with pytest.raises(InvalidCommitMessageError) as excinfo:
cli.main()
util.run_cli(
"--name", "cz_custom_validator", "check", "--commit-msg-file", "some_file"
)
assert "commit validation: failed!" in str(excinfo.value), (
"Pattern validation unexpectedly passed"
)
Expand Down
10 changes: 3 additions & 7 deletions tests/commands/test_common_command.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import sys

import pytest
from pytest_mock import MockFixture

from commitizen import cli
from commitizen.commands import Example, Info, ListCz, Schema
from tests.utils import UtilFixture


@pytest.mark.parametrize(
Expand All @@ -24,11 +22,11 @@
)
@pytest.mark.usefixtures("python_version")
def test_command_shows_description_when_use_help_option(
mocker: MockFixture,
capsys,
file_regression,
monkeypatch: pytest.MonkeyPatch,
command: str,
util: UtilFixture,
):
"""Test that the command shows the description when the help option is used.

Expand All @@ -42,10 +40,8 @@ def test_command_shows_description_when_use_help_option(
monkeypatch.setenv("NO_COLOR", "1")
monkeypatch.setenv("PAGER", "cat")

testargs = ["cz", command, "--help"]
mocker.patch.object(sys, "argv", testargs)
with pytest.raises(SystemExit):
cli.main()
util.run_cli(command, "--help")

out, _ = capsys.readouterr()
file_regression.check(out, extension=".txt")
Expand Down
Loading