From 2424f3f4c2b0497a1761613a1e6ca43fd1e00783 Mon Sep 17 00:00:00 2001 From: Yu-Ting Hsiung Date: Wed, 14 Jan 2026 14:33:11 +0800 Subject: [PATCH] test: remove unreachable code in pytest.raises block, fix some malformed tests and extract fixtures --- tests/commands/conftest.py | 6 + tests/commands/test_check_command.py | 236 ++++++++------------------ tests/commands/test_commit_command.py | 173 +++++++------------ tests/test_cz_customize.py | 2 +- 4 files changed, 146 insertions(+), 271 deletions(-) diff --git a/tests/commands/conftest.py b/tests/commands/conftest.py index 4f9b5de3c..88fc89b7c 100644 --- a/tests/commands/conftest.py +++ b/tests/commands/conftest.py @@ -1,6 +1,7 @@ import os import pytest +from pytest_mock import MockerFixture from commitizen import defaults from commitizen.config import BaseConfig @@ -52,3 +53,8 @@ def changelog_path() -> str: @pytest.fixture() def config_path() -> str: return os.path.join(os.getcwd(), "pyproject.toml") + + +@pytest.fixture() +def success_mock(mocker: MockerFixture): + return mocker.patch("commitizen.out.success") diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index 382814892..4b15da6eb 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -19,7 +19,7 @@ import re from collections.abc import Mapping - from pytest_mock import MockFixture + from pytest_mock import MockFixture, MockType from commitizen.config.base_config import BaseConfig from commitizen.question import CzQuestion @@ -149,18 +149,12 @@ def test_check_conventional_commit_succeeds( ), ), ) -def test_check_no_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir): - with pytest.raises(InvalidCommitMessageError): - error_mock = mocker.patch("commitizen.out.error") - - tempfile = tmpdir.join("temp_commit_file") - tempfile.write(commit_msg) +def test_check_no_conventional_commit(commit_msg, config, tmpdir): + tempfile = tmpdir.join("temp_commit_file") + tempfile.write(commit_msg) - check_cmd = commands.Check( - config=config, arguments={"commit_msg_file": tempfile} - ) - check_cmd() - error_mock.assert_called_once() + with pytest.raises(InvalidCommitMessageError): + commands.Check(config=config, arguments={"commit_msg_file": tempfile})() @pytest.mark.parametrize( @@ -172,15 +166,10 @@ def test_check_no_conventional_commit(commit_msg, config, mocker: MockFixture, t "bump: 0.0.1 -> 1.0.0", ), ) -def test_check_conventional_commit(commit_msg, config, mocker: MockFixture, tmpdir): - success_mock = mocker.patch("commitizen.out.success") - +def test_check_conventional_commit(commit_msg, config, success_mock: MockType, tmpdir): tempfile = tmpdir.join("temp_commit_file") tempfile.write(commit_msg) - - check_cmd = commands.Check(config=config, arguments={"commit_msg_file": tempfile}) - - check_cmd() + commands.Check(config=config, arguments={"commit_msg_file": tempfile})() success_mock.assert_called_once() @@ -189,33 +178,26 @@ def test_check_command_when_commit_file_not_found(config): commands.Check(config=config, arguments={"commit_msg_file": "no_such_file"})() -def test_check_a_range_of_git_commits(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") +def test_check_a_range_of_git_commits( + config, success_mock: MockType, mocker: MockFixture +): mocker.patch( "commitizen.git.get_commits", return_value=_build_fake_git_commits(COMMIT_LOG) ) - check_cmd = commands.Check( - config=config, arguments={"rev_range": "HEAD~10..master"} - ) - - check_cmd() + commands.Check(config=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): - error_mock = mocker.patch("commitizen.out.error") mocker.patch( "commitizen.git.get_commits", return_value=_build_fake_git_commits(["This commit does not follow rule"]), ) - check_cmd = commands.Check( - config=config, arguments={"rev_range": "HEAD~10..master"} - ) - with pytest.raises(InvalidCommitMessageError): - check_cmd() - error_mock.assert_called_once() + with pytest.raises(InvalidCommitMessageError) as excinfo: + commands.Check(config=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): @@ -234,123 +216,80 @@ def test_check_command_with_invalid_argument(config): def test_check_command_with_empty_range(config: BaseConfig, util: UtilFixture): # must initialize git with a commit util.create_file_and_commit("feat: initial") - - check_cmd = commands.Check(config=config, arguments={"rev_range": "master..master"}) with pytest.raises(NoCommitsFoundError) as excinfo: - check_cmd() - + commands.Check(config=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): - ill_formated_commits_msgs = [ + ill_formatted_commits_msgs = [ "First commit does not follow rule", "Second commit does not follow rule", ("Third commit does not follow rule\nIll-formatted commit with body"), ] mocker.patch( "commitizen.git.get_commits", - return_value=_build_fake_git_commits(ill_formated_commits_msgs), - ) - check_cmd = commands.Check( - config=config, arguments={"rev_range": "HEAD~10..master"} + return_value=_build_fake_git_commits(ill_formatted_commits_msgs), ) with pytest.raises(InvalidCommitMessageError) as excinfo: - check_cmd() - assert all([msg in str(excinfo.value) for msg in ill_formated_commits_msgs]) + commands.Check(config=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, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") - check_cmd = commands.Check( +def test_check_command_with_valid_message( + config, success_mock: MockType, mocker: MockFixture +): + commands.Check( config=config, arguments={"message": "fix(scope): some commit message"} - ) - - check_cmd() + )() success_mock.assert_called_once() -def test_check_command_with_invalid_message(config, mocker: MockFixture): - error_mock = mocker.patch("commitizen.out.error") - check_cmd = commands.Check(config=config, arguments={"message": "bad commit"}) - +@pytest.mark.parametrize("message", ["bad commit", ""]) +def test_check_command_with_invalid_message(config, message): with pytest.raises(InvalidCommitMessageError): - check_cmd() - error_mock.assert_called_once() - - -def test_check_command_with_empty_message(config, mocker: MockFixture): - error_mock = mocker.patch("commitizen.out.error") - check_cmd = commands.Check(config=config, arguments={"message": ""}) - - with pytest.raises(InvalidCommitMessageError): - check_cmd() - error_mock.assert_called_once() - + commands.Check(config=config, arguments={"message": message})() -def test_check_command_with_allow_abort_arg(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") - check_cmd = commands.Check( - config=config, arguments={"message": "", "allow_abort": True} - ) - check_cmd() +def test_check_command_with_allow_abort_arg(config, success_mock): + commands.Check(config=config, arguments={"message": "", "allow_abort": True})() success_mock.assert_called_once() -def test_check_command_with_allow_abort_config(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") +def test_check_command_with_allow_abort_config(config, success_mock): config.settings["allow_abort"] = True - check_cmd = commands.Check(config=config, arguments={"message": ""}) - - check_cmd() + commands.Check(config=config, arguments={"message": ""})() success_mock.assert_called_once() -def test_check_command_override_allow_abort_config(config, mocker: MockFixture): - error_mock = mocker.patch("commitizen.out.error") +def test_check_command_override_allow_abort_config(config): config.settings["allow_abort"] = True - check_cmd = commands.Check( - config=config, arguments={"message": "", "allow_abort": False} - ) - with pytest.raises(InvalidCommitMessageError): - check_cmd() - error_mock.assert_called_once() + commands.Check(config=config, arguments={"message": "", "allow_abort": False})() -def test_check_command_with_allowed_prefixes_arg(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") - check_cmd = commands.Check( +def test_check_command_with_allowed_prefixes_arg(config, success_mock): + commands.Check( config=config, arguments={"message": "custom! test", "allowed_prefixes": ["custom!"]}, - ) - - check_cmd() + )() success_mock.assert_called_once() -def test_check_command_with_allowed_prefixes_config(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") +def test_check_command_with_allowed_prefixes_config(config, success_mock): config.settings["allowed_prefixes"] = ["custom!"] - check_cmd = commands.Check(config=config, arguments={"message": "custom! test"}) - - check_cmd() + commands.Check(config=config, arguments={"message": "custom! test"})() success_mock.assert_called_once() -def test_check_command_override_allowed_prefixes_config(config, mocker: MockFixture): - error_mock = mocker.patch("commitizen.out.error") +def test_check_command_override_allowed_prefixes_config(config): config.settings["allow_abort"] = ["fixup!"] - check_cmd = commands.Check( - config=config, - arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]}, - ) - with pytest.raises(InvalidCommitMessageError): - check_cmd() - error_mock.assert_called_once() + commands.Check( + config=config, + arguments={"message": "fixup! test", "allowed_prefixes": ["custom!"]}, + )() def test_check_command_with_pipe_message( @@ -424,93 +363,64 @@ def test_check_conventional_commit_succeed_with_git_diff( assert "Commit validation: successful!" in out -def test_check_command_with_message_length_limit(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") +def test_check_command_with_message_length_limit(config, success_mock): message = "fix(scope): some commit message" - check_cmd = commands.Check( + commands.Check( config=config, arguments={"message": message, "message_length_limit": len(message) + 1}, - ) - - check_cmd() + )() success_mock.assert_called_once() -def test_check_command_with_message_length_limit_exceeded(config, mocker: MockFixture): - error_mock = mocker.patch("commitizen.out.error") +def test_check_command_with_message_length_limit_exceeded(config): message = "fix(scope): some commit message" - check_cmd = commands.Check( - config=config, - arguments={"message": message, "message_length_limit": len(message) - 1}, - ) - with pytest.raises(CommitMessageLengthExceededError): - check_cmd() - error_mock.assert_called_once() - + commands.Check( + config=config, + arguments={"message": message, "message_length_limit": len(message) - 1}, + )() -def test_check_command_with_amend_prefix_default(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") - check_cmd = commands.Check(config=config, arguments={"message": "amend! test"}) - check_cmd() +def test_check_command_with_amend_prefix_default(config, success_mock): + commands.Check(config=config, arguments={"message": "amend! test"})() success_mock.assert_called_once() -def test_check_command_with_config_message_length_limit(config, mocker: MockFixture): - success_mock = mocker.patch("commitizen.out.success") +def test_check_command_with_config_message_length_limit(config, success_mock): message = "fix(scope): some commit message" - config.settings["message_length_limit"] = len(message) + 1 - - check_cmd = commands.Check( + commands.Check( config=config, arguments={"message": message}, - ) - - check_cmd() + )() success_mock.assert_called_once() -def test_check_command_with_config_message_length_limit_exceeded( - config, mocker: MockFixture -): - error_mock = mocker.patch("commitizen.out.error") +def test_check_command_with_config_message_length_limit_exceeded(config): message = "fix(scope): some commit message" - config.settings["message_length_limit"] = len(message) - 1 - - check_cmd = commands.Check( - config=config, - arguments={"message": message}, - ) - with pytest.raises(CommitMessageLengthExceededError): - check_cmd() - error_mock.assert_called_once() + commands.Check( + config=config, + arguments={"message": message}, + )() def test_check_command_cli_overrides_config_message_length_limit( - config, mocker: MockFixture + config, success_mock: MockType, mocker: MockFixture ): - success_mock = mocker.patch("commitizen.out.success") message = "fix(scope): some commit message" - config.settings["message_length_limit"] = len(message) - 1 - - check_cmd = commands.Check( - config=config, - arguments={"message": message, "message_length_limit": len(message) + 1}, - ) - - check_cmd() - success_mock.assert_called_once() - - success_mock.reset_mock() - check_cmd = commands.Check( - config=config, - arguments={"message": message, "message_length_limit": None}, - ) + for message_length_limit in [len(message) + 1, None]: + success_mock.reset_mock() + commands.Check( + config=config, + arguments={ + "message": message, + "message_length_limit": message_length_limit, + }, + )() + success_mock.assert_called_once() class ValidationCz(BaseCommitizen): diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index c987f4b3f..c17001292 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -2,7 +2,7 @@ from unittest.mock import ANY import pytest -from pytest_mock import MockFixture +from pytest_mock import MockFixture, MockType from commitizen import cmd, commands from commitizen.cz.exceptions import CzException @@ -20,6 +20,13 @@ ) +@pytest.fixture +def commit_mock(mocker: MockFixture): + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) + return commit_mock + + @pytest.fixture def staging_is_clean(mocker: MockFixture, tmp_git_project): is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") @@ -33,8 +40,8 @@ def backup_file(tmp_git_project): backup_file.write("backup commit") -@pytest.mark.usefixtures("staging_is_clean") -def test_commit(config, mocker: MockFixture): +@pytest.mark.usefixtures("staging_is_clean", "commit_mock") +def test_commit(config, success_mock: MockType, mocker: MockFixture): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -45,10 +52,6 @@ def test_commit(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - commands.Commit(config, {})() success_mock.assert_called_once() @@ -65,13 +68,14 @@ def test_commit_backup_on_failure(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("", "error", b"", b"", 9) + mocker.patch("commitizen.git.commit").return_value = cmd.Command( + "", "error", b"", b"", 9 + ) error_mock = mocker.patch("commitizen.out.error") + commit_cmd = commands.Commit(config, {}) + temp_file = commit_cmd.backup_file_path with pytest.raises(CommitError): - commit_cmd = commands.Commit(config, {}) - temp_file = commit_cmd.backup_file_path commit_cmd() prompt_mock.assert_called_once() @@ -79,11 +83,8 @@ def test_commit_backup_on_failure(config, mocker: MockFixture): assert os.path.isfile(temp_file) -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_retry_fails_no_backup(config, mocker: MockFixture): - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - +@pytest.mark.usefixtures("staging_is_clean", "commit_mock") +def test_commit_retry_fails_no_backup(config): with pytest.raises(NoCommitBackupError) as excinfo: commands.Commit(config, {"retry": True})() @@ -91,13 +92,11 @@ def test_commit_retry_fails_no_backup(config, mocker: MockFixture): @pytest.mark.usefixtures("staging_is_clean", "backup_file") -def test_commit_retry_works(config, mocker: MockFixture): +def test_commit_retry_works( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - commit_cmd = commands.Commit(config, {"retry": True}) temp_file = commit_cmd.backup_file_path commit_cmd() @@ -109,7 +108,9 @@ def test_commit_retry_works(config, mocker: MockFixture): @pytest.mark.usefixtures("staging_is_clean") -def test_commit_retry_after_failure_no_backup(config, mocker: MockFixture): +def test_commit_retry_after_failure_no_backup( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -120,10 +121,6 @@ def test_commit_retry_after_failure_no_backup(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["retry_after_failure"] = True commands.Commit(config, {})() @@ -133,13 +130,11 @@ def test_commit_retry_after_failure_no_backup(config, mocker: MockFixture): @pytest.mark.usefixtures("staging_is_clean", "backup_file") -def test_commit_retry_after_failure_works(config, mocker: MockFixture): +def test_commit_retry_after_failure_works( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["retry_after_failure"] = True commit_cmd = commands.Commit(config, {}) temp_file = commit_cmd.backup_file_path @@ -152,7 +147,9 @@ def test_commit_retry_after_failure_works(config, mocker: MockFixture): @pytest.mark.usefixtures("staging_is_clean", "backup_file") -def test_commit_retry_after_failure_with_no_retry_works(config, mocker: MockFixture): +def test_commit_retry_after_failure_with_no_retry_works( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -163,10 +160,6 @@ def test_commit_retry_after_failure_with_no_retry_works(config, mocker: MockFixt "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["retry_after_failure"] = True commit_cmd = commands.Commit(config, {"no_retry": True}) temp_file = commit_cmd.backup_file_path @@ -191,13 +184,12 @@ def test_commit_command_with_dry_run_option(config, mocker: MockFixture): } with pytest.raises(DryRunExit): - commit_cmd = commands.Commit(config, {"dry_run": True}) - commit_cmd() + commands.Commit(config, {"dry_run": True})() -@pytest.mark.usefixtures("staging_is_clean") +@pytest.mark.usefixtures("staging_is_clean", "commit_mock") def test_commit_command_with_write_message_to_file_option( - config, tmp_path, mocker: MockFixture + config, tmp_path, success_mock: MockType, mocker: MockFixture ): tmp_file = tmp_path / "message" @@ -211,10 +203,6 @@ def test_commit_command_with_write_message_to_file_option( "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - commands.Commit(config, {"write_message_to_file": tmp_file})() success_mock.assert_called_once() assert tmp_file.exists() @@ -236,12 +224,13 @@ def test_commit_command_with_invalid_write_message_to_file_option( } with pytest.raises(NotAllowed): - commit_cmd = commands.Commit(config, {"write_message_to_file": tmp_path}) - commit_cmd() + commands.Commit(config, {"write_message_to_file": tmp_path})() @pytest.mark.usefixtures("staging_is_clean") -def test_commit_command_with_signoff_option(config, mocker: MockFixture): +def test_commit_command_with_signoff_option( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -252,10 +241,6 @@ def test_commit_command_with_signoff_option(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - commands.Commit(config, {"signoff": True})() commit_mock.assert_called_once_with(ANY, args="-s") @@ -263,7 +248,9 @@ def test_commit_command_with_signoff_option(config, mocker: MockFixture): @pytest.mark.usefixtures("staging_is_clean") -def test_commit_command_with_always_signoff_enabled(config, mocker: MockFixture): +def test_commit_command_with_always_signoff_enabled( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -274,10 +261,6 @@ def test_commit_command_with_always_signoff_enabled(config, mocker: MockFixture) "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["always_signoff"] = True commands.Commit(config, {})() @@ -287,7 +270,7 @@ def test_commit_command_with_always_signoff_enabled(config, mocker: MockFixture) @pytest.mark.usefixtures("staging_is_clean") def test_commit_command_with_gpgsign_and_always_signoff_enabled( - config, mocker: MockFixture + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType ): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { @@ -299,10 +282,6 @@ def test_commit_command_with_gpgsign_and_always_signoff_enabled( "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["always_signoff"] = True commands.Commit(config, {"extra_cli_args": "-S"})() @@ -316,14 +295,15 @@ def test_commit_when_nothing_to_commit(config, mocker: MockFixture): is_staging_clean_mock.return_value = True with pytest.raises(NothingToCommitError) as excinfo: - commit_cmd = commands.Commit(config, {}) - commit_cmd() + commands.Commit(config, {})() assert "No files added to staging!" in str(excinfo.value) @pytest.mark.usefixtures("staging_is_clean") -def test_commit_with_allow_empty(config, mocker: MockFixture): +def test_commit_with_allow_empty( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -334,10 +314,6 @@ def test_commit_with_allow_empty(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - commands.Commit(config, {"extra_cli_args": "--allow-empty"})() commit_mock.assert_called_with( @@ -347,7 +323,9 @@ def test_commit_with_allow_empty(config, mocker: MockFixture): @pytest.mark.usefixtures("staging_is_clean") -def test_commit_with_signoff_and_allow_empty(config, mocker: MockFixture): +def test_commit_with_signoff_and_allow_empty( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -358,10 +336,6 @@ def test_commit_with_signoff_and_allow_empty(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["always_signoff"] = True commands.Commit(config, {"extra_cli_args": "--allow-empty"})() @@ -379,8 +353,7 @@ def test_commit_when_customized_expected_raised(config, mocker: MockFixture, cap prompt_mock.side_effect = _err with pytest.raises(CustomError) as excinfo: - commit_cmd = commands.Commit(config, {}) - commit_cmd() + commands.Commit(config, {})() # Assert only the content in the formatted text assert "This is the root custom err" in str(excinfo.value) @@ -395,8 +368,7 @@ def test_commit_when_non_customized_expected_raised( prompt_mock.side_effect = _err with pytest.raises(ValueError): - commit_cmd = commands.Commit(config, {}) - commit_cmd() + commands.Commit(config, {})() @pytest.mark.usefixtures("staging_is_clean") @@ -405,8 +377,7 @@ def test_commit_when_no_user_answer(config, mocker: MockFixture, capsys): prompt_mock.return_value = None with pytest.raises(NoAnswersError): - commit_cmd = commands.Commit(config, {}) - commit_cmd() + commands.Commit(config, {})() def test_commit_in_non_git_project(tmpdir, config): @@ -415,8 +386,10 @@ def test_commit_in_non_git_project(tmpdir, config): commands.Commit(config, {}) -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_command_with_all_option(config, mocker: MockFixture): +@pytest.mark.usefixtures("staging_is_clean", "commit_mock") +def test_commit_command_with_all_option( + config, success_mock: MockType, mocker: MockFixture +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -427,9 +400,6 @@ def test_commit_command_with_all_option(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") add_mock = mocker.patch("commitizen.git.add") commands.Commit(config, {"all": True})() add_mock.assert_called() @@ -437,7 +407,9 @@ def test_commit_command_with_all_option(config, mocker: MockFixture): @pytest.mark.usefixtures("staging_is_clean") -def test_commit_command_with_extra_args(config, mocker: MockFixture): +def test_commit_command_with_extra_args( + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType +): prompt_mock = mocker.patch("questionary.prompt") prompt_mock.return_value = { "prefix": "feat", @@ -448,16 +420,15 @@ def test_commit_command_with_extra_args(config, mocker: MockFixture): "footer": "", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") commands.Commit(config, {"extra_cli_args": "-- -extra-args1 -extra-arg2"})() commit_mock.assert_called_once_with(ANY, args="-- -extra-args1 -extra-arg2") success_mock.assert_called_once() -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_command_with_message_length_limit(config, mocker: MockFixture): +@pytest.mark.usefixtures("staging_is_clean", "commit_mock") +def test_commit_command_with_message_length_limit( + config, success_mock: MockType, mocker: MockFixture +): prompt_mock = mocker.patch("questionary.prompt") prefix = "feat" subject = "random subject" @@ -471,10 +442,6 @@ def test_commit_command_with_message_length_limit(config, mocker: MockFixture): "footer": "random footer", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - commands.Commit(config, {"message_length_limit": message_length})() success_mock.assert_called_once() @@ -504,9 +471,7 @@ def test_manual_edit(editor, config, mocker: MockFixture, tmp_path): commit_cmd.manual_edit(test_message) else: edited_message = commit_cmd.manual_edit(test_message) - subprocess_mock.assert_called_once_with(["vim", str(temp_file)]) - assert edited_message == test_message.strip() @@ -541,8 +506,10 @@ def test_commit_when_nothing_added_to_commit(config, mocker: MockFixture, out): error_mock.assert_called_once_with(out) -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_command_with_config_message_length_limit(config, mocker: MockFixture): +@pytest.mark.usefixtures("staging_is_clean", "commit_mock") +def test_commit_command_with_config_message_length_limit( + config, success_mock: MockType, mocker: MockFixture +): prompt_mock = mocker.patch("questionary.prompt") prefix = "feat" subject = "random subject" @@ -556,10 +523,6 @@ def test_commit_command_with_config_message_length_limit(config, mocker: MockFix "footer": "random footer", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["message_length_limit"] = message_length commands.Commit(config, {})() success_mock.assert_called_once() @@ -571,7 +534,7 @@ def test_commit_command_with_config_message_length_limit(config, mocker: MockFix @pytest.mark.usefixtures("staging_is_clean") def test_commit_command_cli_overrides_config_message_length_limit( - config, mocker: MockFixture + config, success_mock: MockType, mocker: MockFixture, commit_mock: MockType ): prompt_mock = mocker.patch("questionary.prompt") prefix = "feat" @@ -586,10 +549,6 @@ def test_commit_command_cli_overrides_config_message_length_limit( "footer": "random footer", } - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", b"", b"", 0) - success_mock = mocker.patch("commitizen.out.success") - config.settings["message_length_limit"] = message_length - 1 commands.Commit(config, {"message_length_limit": message_length})() diff --git a/tests/test_cz_customize.py b/tests/test_cz_customize.py index dd354d65e..676474f26 100644 --- a/tests/test_cz_customize.py +++ b/tests/test_cz_customize.py @@ -376,8 +376,8 @@ def config_with_unicode(request): def test_initialize_cz_customize_failed(): + config = BaseConfig() with pytest.raises(MissingCzCustomizeConfigError) as excinfo: - config = BaseConfig() _ = CustomizeCommitsCz(config) assert MissingCzCustomizeConfigError.message in str(excinfo.value)