Skip to content
Open
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
14 changes: 0 additions & 14 deletions tests/commands/conftest.py
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

duplicated fixtures

Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,9 @@
import pytest
from pytest_mock import MockerFixture, MockType

from commitizen import defaults
from commitizen.config import BaseConfig
from commitizen.config.json_config import JsonConfig


@pytest.fixture
def config() -> BaseConfig:
_config = BaseConfig()
_config.settings.update({"name": defaults.DEFAULT_SETTINGS["name"]})
return _config


@pytest.fixture
def config_customize() -> JsonConfig:
json_string = r"""{
Expand Down Expand Up @@ -50,11 +41,6 @@ def changelog_path() -> str:
return os.path.join(os.getcwd(), "CHANGELOG.md")


@pytest.fixture
def config_path() -> str:
return os.path.join(os.getcwd(), "pyproject.toml")


@pytest.fixture
def success_mock(mocker: MockerFixture) -> MockType:
return mocker.patch("commitizen.out.success")
21 changes: 12 additions & 9 deletions tests/commands/test_changelog_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def test_changelog_incremental_keep_a_changelog_sample(
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.parametrize("dry_run", [True, False])
def test_changelog_hook(
mocker: MockFixture, config: BaseConfig, dry_run: bool, util: UtilFixture
mocker: MockFixture, default_config: BaseConfig, dry_run: bool, util: UtilFixture
):
changelog_hook_mock = mocker.Mock()
changelog_hook_mock.return_value = "cool changelog hook"
Expand All @@ -265,9 +265,10 @@ def test_changelog_hook(
util.create_file_and_commit("refactor: is in changelog")
util.create_file_and_commit("Merge into master")

config.settings["change_type_order"] = ["Refactor", "Feat"] # type: ignore[typeddict-unknown-key]
default_config.settings["change_type_order"] = ["Refactor", "Feat"] # type: ignore[typeddict-unknown-key]
changelog = Changelog(
config, {"unreleased_version": None, "incremental": True, "dry_run": dry_run}
default_config,
{"unreleased_version": None, "incremental": True, "dry_run": dry_run},
)
mocker.patch.object(changelog.cz, "changelog_hook", changelog_hook_mock)
if dry_run:
Expand Down Expand Up @@ -312,7 +313,7 @@ def test_changelog_hook_customize(

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_release_hook(
mocker: MockFixture, config: BaseConfig, util: UtilFixture
mocker: MockFixture, default_config: BaseConfig, util: UtilFixture
):
def changelog_release_hook(release: dict, tag: git.GitTag) -> dict:
return release
Expand All @@ -325,7 +326,8 @@ def changelog_release_hook(release: dict, tag: git.GitTag) -> dict:

# changelog = Changelog(config, {})
changelog = Changelog(
config, {"unreleased_version": None, "incremental": True, "dry_run": False}
default_config,
{"unreleased_version": None, "incremental": True, "dry_run": False},
)
mocker.patch.object(changelog.cz, "changelog_release_hook", changelog_release_hook)
spy = mocker.spy(changelog.cz, "changelog_release_hook")
Expand Down Expand Up @@ -1196,7 +1198,7 @@ def test_changelog_prerelease_rev_with_use_scheme_semver(

@pytest.mark.usefixtures("tmp_commitizen_project")
def test_changelog_uses_version_tags_for_header(
mocker: MockFixture, config: BaseConfig, util: UtilFixture
mocker: MockFixture, default_config: BaseConfig, util: UtilFixture
):
"""Tests that changelog headers always use version tags even if there are non-version tags

Expand All @@ -1211,7 +1213,8 @@ def test_changelog_uses_version_tags_for_header(
write_patch = mocker.patch("commitizen.commands.changelog.out.write")

changelog = Changelog(
config, {"dry_run": True, "incremental": True, "unreleased_version": None}
default_config,
{"dry_run": True, "incremental": True, "unreleased_version": None},
)

with pytest.raises(DryRunExit):
Expand All @@ -1227,7 +1230,7 @@ def test_changelog_uses_version_tags_for_header(
@pytest.mark.usefixtures("tmp_commitizen_project")
@pytest.mark.freeze_time("2022-02-13")
def test_changelog_from_current_version_tag_with_nonversion_tag(
mocker: MockFixture, config: BaseConfig, util: UtilFixture
mocker: MockFixture, default_config: BaseConfig, util: UtilFixture
):
"""Tests that changelog generation for a single version works even if
there is a non-version tag in the list of tags
Expand All @@ -1251,7 +1254,7 @@ def test_changelog_from_current_version_tag_with_nonversion_tag(

with pytest.raises(DryRunExit):
Changelog(
config,
default_config,
{
"dry_run": True,
"incremental": False,
Expand Down
117 changes: 66 additions & 51 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ def test_check_conventional_commit_succeeds(
),
),
)
def test_check_no_conventional_commit(commit_msg, config, tmpdir):
def test_check_no_conventional_commit(commit_msg, default_config, tmpdir):
tempfile = tmpdir.join("temp_commit_file")
tempfile.write(commit_msg)

with pytest.raises(InvalidCommitMessageError):
commands.Check(config=config, arguments={"commit_msg_file": tempfile})()
commands.Check(config=default_config, arguments={"commit_msg_file": tempfile})()


@pytest.mark.parametrize(
Expand All @@ -140,44 +140,50 @@ def test_check_no_conventional_commit(commit_msg, config, tmpdir):
"bump: 0.0.1 -> 1.0.0",
),
)
def test_check_conventional_commit(commit_msg, config, success_mock: MockType, tmpdir):
def test_check_conventional_commit(
commit_msg, default_config, success_mock: MockType, tmpdir
):
tempfile = tmpdir.join("temp_commit_file")
tempfile.write(commit_msg)
commands.Check(config=config, arguments={"commit_msg_file": tempfile})()
commands.Check(config=default_config, arguments={"commit_msg_file": tempfile})()
success_mock.assert_called_once()


def test_check_command_when_commit_file_not_found(config):
def test_check_command_when_commit_file_not_found(default_config):
with pytest.raises(FileNotFoundError):
commands.Check(config=config, arguments={"commit_msg_file": "no_such_file"})()
commands.Check(
config=default_config, arguments={"commit_msg_file": "no_such_file"}
)()


def test_check_a_range_of_git_commits(
config, success_mock: MockType, mocker: MockFixture
default_config, success_mock: MockType, mocker: MockFixture
):
mocker.patch(
"commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG)
)

commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
commands.Check(config=default_config, arguments={"rev_range": "HEAD~10..master"})()
success_mock.assert_called_once()


def test_check_a_range_of_git_commits_and_failed(config, mocker: MockFixture):
def test_check_a_range_of_git_commits_and_failed(default_config, mocker: MockFixture):
mocker.patch(
"commitizen.git.get_commits",
return_value=_build_fake_git_commits(["This commit does not follow rule"]),
)

with pytest.raises(InvalidCommitMessageError) as excinfo:
commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
commands.Check(
config=default_config, arguments={"rev_range": "HEAD~10..master"}
)()
assert "This commit does not follow rule" in str(excinfo.value)


def test_check_command_with_invalid_argument(config):
def test_check_command_with_invalid_argument(default_config):
with pytest.raises(InvalidCommandArgumentError) as excinfo:
commands.Check(
config=config,
config=default_config,
arguments={"commit_msg_file": "some_file", "rev_range": "HEAD~10..master"},
)
assert (
Expand All @@ -187,15 +193,17 @@ def test_check_command_with_invalid_argument(config):


@pytest.mark.usefixtures("tmp_commitizen_project")
def test_check_command_with_empty_range(config: BaseConfig, util: UtilFixture):
def test_check_command_with_empty_range(default_config: BaseConfig, util: UtilFixture):
# must initialize git with a commit
util.create_file_and_commit("feat: initial")
with pytest.raises(NoCommitsFoundError) as excinfo:
commands.Check(config=config, arguments={"rev_range": "master..master"})()
commands.Check(
config=default_config, arguments={"rev_range": "master..master"}
)()
assert "No commit found with range: 'master..master'" in str(excinfo)


def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
def test_check_a_range_of_failed_git_commits(default_config, mocker: MockFixture):
ill_formatted_commits_msgs = [
"First commit does not follow rule",
"Second commit does not follow rule",
Expand All @@ -207,59 +215,66 @@ def test_check_a_range_of_failed_git_commits(config, mocker: MockFixture):
)

with pytest.raises(InvalidCommitMessageError) as excinfo:
commands.Check(config=config, arguments={"rev_range": "HEAD~10..master"})()
commands.Check(
config=default_config, arguments={"rev_range": "HEAD~10..master"}
)()
assert all([msg in str(excinfo.value) for msg in ill_formatted_commits_msgs])


def test_check_command_with_valid_message(config, success_mock: MockType):
def test_check_command_with_valid_message(default_config, success_mock: MockType):
commands.Check(
config=config, arguments={"message": "fix(scope): some commit message"}
config=default_config,
arguments={"message": "fix(scope): some commit message"},
)()
success_mock.assert_called_once()


@pytest.mark.parametrize("message", ["bad commit", ""])
def test_check_command_with_invalid_message(config, message):
def test_check_command_with_invalid_message(default_config, message):
with pytest.raises(InvalidCommitMessageError):
commands.Check(config=config, arguments={"message": message})()
commands.Check(config=default_config, arguments={"message": message})()


def test_check_command_with_allow_abort_arg(config, success_mock):
commands.Check(config=config, arguments={"message": "", "allow_abort": True})()
def test_check_command_with_allow_abort_arg(default_config, success_mock):
commands.Check(
config=default_config, arguments={"message": "", "allow_abort": True}
)()
success_mock.assert_called_once()


def test_check_command_with_allow_abort_config(config, success_mock):
config.settings["allow_abort"] = True
commands.Check(config=config, arguments={"message": ""})()
def test_check_command_with_allow_abort_config(default_config, success_mock):
default_config.settings["allow_abort"] = True
commands.Check(config=default_config, arguments={"message": ""})()
success_mock.assert_called_once()


def test_check_command_override_allow_abort_config(config):
config.settings["allow_abort"] = True
def test_check_command_override_allow_abort_config(default_config):
default_config.settings["allow_abort"] = True
with pytest.raises(InvalidCommitMessageError):
commands.Check(config=config, arguments={"message": "", "allow_abort": False})()
commands.Check(
config=default_config, arguments={"message": "", "allow_abort": False}
)()


def test_check_command_with_allowed_prefixes_arg(config, success_mock):
def test_check_command_with_allowed_prefixes_arg(default_config, success_mock):
commands.Check(
config=config,
config=default_config,
arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]},
)()
success_mock.assert_called_once()


def test_check_command_with_allowed_prefixes_config(config, success_mock):
config.settings["allowed_prefixes"] = ["custom!"]
commands.Check(config=config, arguments={"message": "custom! test"})()
def test_check_command_with_allowed_prefixes_config(default_config, success_mock):
default_config.settings["allowed_prefixes"] = ["custom!"]
commands.Check(config=default_config, arguments={"message": "custom! test"})()
success_mock.assert_called_once()


def test_check_command_override_allowed_prefixes_config(config):
config.settings["allow_abort"] = ["fixup!"]
def test_check_command_override_allowed_prefixes_config(default_config):
default_config.settings["allow_abort"] = ["fixup!"]
with pytest.raises(InvalidCommitMessageError):
commands.Check(
config=config,
config=default_config,
arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]},
)()

Expand Down Expand Up @@ -336,58 +351,58 @@ def test_check_conventional_commit_succeed_with_git_diff(
assert "Commit validation: successful!" in out


def test_check_command_with_message_length_limit(config, success_mock):
def test_check_command_with_message_length_limit(default_config, success_mock):
message = "fix(scope): some commit message"
commands.Check(
config=config,
config=default_config,
arguments={"message": message, "message_length_limit": len(message) + 1},
)()
success_mock.assert_called_once()


def test_check_command_with_message_length_limit_exceeded(config):
def test_check_command_with_message_length_limit_exceeded(default_config):
message = "fix(scope): some commit message"
with pytest.raises(CommitMessageLengthExceededError):
commands.Check(
config=config,
config=default_config,
arguments={"message": message, "message_length_limit": len(message) - 1},
)()


def test_check_command_with_amend_prefix_default(config, success_mock):
commands.Check(config=config, arguments={"message": "amend! test"})()
def test_check_command_with_amend_prefix_default(default_config, success_mock):
commands.Check(config=default_config, arguments={"message": "amend! test"})()
success_mock.assert_called_once()


def test_check_command_with_config_message_length_limit(config, success_mock):
def test_check_command_with_config_message_length_limit(default_config, success_mock):
message = "fix(scope): some commit message"
config.settings["message_length_limit"] = len(message) + 1
default_config.settings["message_length_limit"] = len(message) + 1
commands.Check(
config=config,
config=default_config,
arguments={"message": message},
)()
success_mock.assert_called_once()


def test_check_command_with_config_message_length_limit_exceeded(config):
def test_check_command_with_config_message_length_limit_exceeded(default_config):
message = "fix(scope): some commit message"
config.settings["message_length_limit"] = len(message) - 1
default_config.settings["message_length_limit"] = len(message) - 1
with pytest.raises(CommitMessageLengthExceededError):
commands.Check(
config=config,
config=default_config,
arguments={"message": message},
)()


def test_check_command_cli_overrides_config_message_length_limit(
config, success_mock: MockType
default_config, success_mock: MockType
):
message = "fix(scope): some commit message"
config.settings["message_length_limit"] = len(message) - 1
default_config.settings["message_length_limit"] = len(message) - 1
for message_length_limit in [len(message) + 1, 0]:
success_mock.reset_mock()
commands.Check(
config=config,
config=default_config,
arguments={
"message": message,
"message_length_limit": message_length_limit,
Expand Down
Loading