diff --git a/commitizen/cli.py b/commitizen/cli.py index 552ee36184..0cfbb460b0 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -179,6 +179,7 @@ def main(): parser.print_help(sys.stderr) raise SystemExit() + # This is for the command required constraint in 2.0 try: args = parser.parse_args() except TypeError: @@ -195,7 +196,7 @@ def main(): "'cz --version' will be deprecated in next major version. " "Please use 'cz version' command from your scripts" ) - logging.getLogger("commitizen").setLevel(logging.DEBUG) + args.func = commands.Version if args.debug: warnings.warn( @@ -205,7 +206,9 @@ def main(): logging.getLogger("commitizen").setLevel(logging.DEBUG) # TODO: This try block can be removed after command is required in 2.0 + # Handle the case that argument is given, but no command is provided try: args.func(conf, vars(args))() except AttributeError: out.error("Command is required") + raise SystemExit() diff --git a/commitizen/commands/bump.py b/commitizen/commands/bump.py index cf325447df..1d07548d7e 100644 --- a/commitizen/commands/bump.py +++ b/commitizen/commands/bump.py @@ -69,9 +69,11 @@ def __call__(self): try: current_version_instance: Version = Version(self.parameters["version"]) except TypeError: - out.error("[NO_VERSION_SPECIFIED]") - out.error("Check if current version is specified in config file, like:") - out.error("version = 0.4.3") + out.error( + "[NO_VERSION_SPECIFIED]\n" + "Check if current version is specified in config file, like:\n" + "version = 0.4.3\n" + ) raise SystemExit(NO_VERSION_SPECIFIED) # Initialize values from sources (conf) @@ -95,8 +97,7 @@ def __call__(self): # No commits, there is no need to create an empty tag. # Unless we previously had a prerelease. if not commits and not current_version_instance.is_prerelease: - out.error("[NO_COMMITS_FOUND]") - out.error("No new commits found.") + out.error("[NO_COMMITS_FOUND]\n" "No new commits found.") raise SystemExit(NO_COMMITS_FOUND) if increment is None: diff --git a/tests/commands/conftest.py b/tests/commands/conftest.py new file mode 100644 index 0000000000..cadf369f06 --- /dev/null +++ b/tests/commands/conftest.py @@ -0,0 +1,11 @@ +import pytest + +from commitizen import defaults +from commitizen.config import BaseConfig + + +@pytest.fixture() +def config(): + _config = BaseConfig() + _config.settings.update({"name": defaults.name}) + return _config diff --git a/tests/commands/test_bump_command.py b/tests/commands/test_bump_command.py new file mode 100644 index 0000000000..48ebeebfeb --- /dev/null +++ b/tests/commands/test_bump_command.py @@ -0,0 +1,130 @@ +import sys +import uuid +from pathlib import Path +from typing import Optional + +import pytest + +from commitizen import cli, cmd, git + + +@pytest.fixture(scope="function") +def tmp_git_project(tmpdir): + with tmpdir.as_cwd(): + with open("pyproject.toml", "w") as f: + f.write("[tool.commitizen]\n" 'version="0.1.0"') + + cmd.run("git init") + + yield + + +def create_file_and_commit(message: str, filename: Optional[str] = None): + if not filename: + filename = str(uuid.uuid4()) + + Path(f"./{filename}").touch() + cmd.run("git add .") + git.commit(message) + + +@pytest.mark.usefixtures("tmp_git_project") +def test_bump_command(mocker): + # MINOR + create_file_and_commit("feat: new file") + + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist("0.2.0") + assert tag_exists is True + + # PATCH + create_file_and_commit("fix: username exception") + + testargs = ["cz", "bump"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist("0.2.1") + assert tag_exists is True + + # PRERELEASE + create_file_and_commit("feat: location") + + testargs = ["cz", "bump", "--prerelease", "alpha"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist("0.3.0a0") + assert tag_exists is True + + # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE + testargs = ["cz", "bump"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist("0.3.0") + assert tag_exists is True + + # MAJOR + create_file_and_commit( + "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" + ) + + testargs = ["cz", "bump"] + mocker.patch.object(sys, "argv", testargs) + cli.main() + + tag_exists = git.tag_exist("1.0.0") + assert tag_exists is True + + +def test_bump_when_bumpping_is_not_support(mocker, capsys, tmpdir): + with tmpdir.as_cwd(): + with open("./pyproject.toml", "w") as f: + f.write("[tool.commitizen]\n" 'version="0.1.0"') + + cmd.run("git init") + create_file_and_commit( + "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" + ) + + testargs = ["cz", "-n", "cz_jira", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + + with pytest.raises(SystemExit): + cli.main() + + _, err = capsys.readouterr() + assert "'cz_jira' rule does not support bump" in err + + +def test_bump_is_not_specify(mocker, capsys, tmpdir): + mocker.patch.object(sys, "argv", ["cz", "bump"]) + + with pytest.raises(SystemExit): + with tmpdir.as_cwd(): + cli.main() + + expected_error_message = ( + "[NO_VERSION_SPECIFIED]\n" + "Check if current version is specified in config file, like:\n" + "version = 0.4.3\n" + ) + + _, err = capsys.readouterr() + assert expected_error_message in err + + +def test_bump_when_not_new_commit(mocker, capsys, tmp_git_project): + testargs = ["cz", "bump", "--yes"] + mocker.patch.object(sys, "argv", testargs) + + with pytest.raises(SystemExit): + cli.main() + + expected_error_message = "[NO_COMMITS_FOUND]\n" "No new commits found." + _, err = capsys.readouterr() + assert expected_error_message in err diff --git a/tests/test_check_command.py b/tests/commands/test_check_command.py similarity index 72% rename from tests/test_check_command.py rename to tests/commands/test_check_command.py index 45acb57d80..c5bed35e61 100644 --- a/tests/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -3,6 +3,7 @@ import pytest from commitizen import cli +from commitizen import commands def test_check_jira_fails(mocker, capsys): @@ -76,3 +77,34 @@ def test_check_conventional_commit_succeeds(mocker, capsys): cli.main() out, _ = capsys.readouterr() assert "Commit validation: successful!" in out + + +def test_check_no_conventional_commit(config, mocker, tmpdir): + with pytest.raises(SystemExit): + error_mock = mocker.patch("commitizen.out.error") + + tempfile = tmpdir.join("temp_commit_file") + tempfile.write("no conventional commit") + + check_cmd = commands.Check( + config=config, arguments={"commit_msg_file": tempfile} + ) + check_cmd() + error_mock.assert_called_once() + + +def test_check_conventional_commit(config, mocker, tmpdir): + success_mock = mocker.patch("commitizen.out.success") + + tempfile = tmpdir.join("temp_commit_file") + tempfile.write("feat(lang): added polish language") + + check_cmd = commands.Check(config=config, arguments={"commit_msg_file": tempfile}) + + check_cmd() + success_mock.assert_called_once() + + +def test_check_command_when_commit_file_not_found(config): + with pytest.raises(FileNotFoundError): + commands.Check(config=config, arguments={"commit_msg_file": ""})() diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py new file mode 100644 index 0000000000..97614696dd --- /dev/null +++ b/tests/commands/test_commit_command.py @@ -0,0 +1,125 @@ +import os + +import pytest + +from commitizen import cmd, commands +from commitizen.cz.exceptions import CzException + + +@pytest.fixture +def staging_is_clean(mocker): + is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") + is_staging_clean_mock.return_value = False + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit(config, mocker): + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "prefix": "feat", + "subject": "user created", + "scope": "", + "is_breaking_change": False, + "body": "", + "footer": "", + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", "", "") + success_mock = mocker.patch("commitizen.out.success") + + commands.Commit(config, {})() + success_mock.assert_called_once() + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_retry_fails_no_backup(config, mocker): + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", "", "") + + with pytest.raises(SystemExit): + commands.Commit(config, {"retry": True})() + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_retry_works(config, mocker): + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "prefix": "feat", + "subject": "user created", + "scope": "", + "is_breaking_change": False, + "body": "closes #21", + "footer": "", + } + + commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("", "error", "", "") + error_mock = mocker.patch("commitizen.out.error") + + with pytest.raises(SystemExit): + commit_cmd = commands.Commit(config, {}) + temp_file = commit_cmd.temp_file + commit_cmd() + + prompt_mock.assert_called_once() + error_mock.assert_called_once() + assert os.path.isfile(temp_file) + + # Previous commit failed, so retry should pick up the backup commit + # commit_mock = mocker.patch("commitizen.git.commit") + commit_mock.return_value = cmd.Command("success", "", "", "") + success_mock = mocker.patch("commitizen.out.success") + + commands.Commit(config, {"retry": True})() + + commit_mock.assert_called_with("feat: user created\n\ncloses #21") + prompt_mock.assert_called_once() + success_mock.assert_called_once() + assert not os.path.isfile(temp_file) + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_with_dry_run_option(config, mocker): + prompt_mock = mocker = mocker.patch("questionary.prompt") + prompt_mock.return_value = { + "prefix": "feat", + "subject": "user created", + "scope": "", + "is_breaking_change": False, + "body": "closes #57", + "footer": "", + } + + with pytest.raises(SystemExit): + commit_cmd = commands.Commit(config, {"dry_run": True}) + commit_cmd() + + +def test_commit_when_nothing_to_commit(config, mocker): + is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") + is_staging_clean_mock.return_value = True + + with pytest.raises(SystemExit) as err: + commit_cmd = commands.Commit(config, {}) + commit_cmd() + + assert err.value.code == commands.commit.NOTHING_TO_COMMIT + + +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_when_customized_expected_raised(config, mocker, capsys): + _err = ValueError() + _err.__context__ = CzException("This is the root custom err") + prompt_mock = mocker.patch("questionary.prompt") + prompt_mock.side_effect = _err + + with pytest.raises(SystemExit) as err: + commit_cmd = commands.Commit(config, {}) + commit_cmd() + + assert err.value.code == commands.commit.CUSTOM_ERROR + + # Assert only the content in the formatted text + captured = capsys.readouterr() + assert "This is the root custom err" in captured.err diff --git a/tests/commands/test_init_command.py b/tests/commands/test_init_command.py new file mode 100644 index 0000000000..1de8fef06a --- /dev/null +++ b/tests/commands/test_init_command.py @@ -0,0 +1,45 @@ +from commitizen import commands + + +class FakeQuestion: + def __init__(self, expected_return): + self.expected_return = expected_return + + def ask(self): + return self.expected_return + + +def test_init(tmpdir, mocker, config): + mocker.patch( + "questionary.select", + side_effect=[ + FakeQuestion("pyproject.toml"), + FakeQuestion("cz_conventional_commits"), + ], + ) + mocker.patch("questionary.confirm", return_value=FakeQuestion("y")) + mocker.patch("questionary.text", return_value=FakeQuestion("y")) + expected_config = ( + "[tool.commitizen]\n" + 'name = "cz_conventional_commits"\n' + 'version = "0.0.1"\n' + 'tag_format = "y"\n' + ) + + with tmpdir.as_cwd(): + commands.Init(config)() + + with open("pyproject.toml", "r") as toml_file: + config_data = toml_file.read() + + assert config_data == expected_config + + +def test_init_when_config_already_exists(config, capsys): + # Set config path + path = "tests/pyproject.toml" + config.add_path(path) + + commands.Init(config)() + captured = capsys.readouterr() + assert captured.out == f"Config file {path} already exists\n" diff --git a/tests/commands/test_other_commands.py b/tests/commands/test_other_commands.py new file mode 100644 index 0000000000..619b6743d1 --- /dev/null +++ b/tests/commands/test_other_commands.py @@ -0,0 +1,25 @@ +from commitizen import commands + + +def test_example(config, mocker): + write_mock = mocker.patch("commitizen.out.write") + commands.Example(config)() + write_mock.assert_called_once() + + +def test_info(config, mocker): + write_mock = mocker.patch("commitizen.out.write") + commands.Info(config)() + write_mock.assert_called_once() + + +def test_schema(config, mocker): + write_mock = mocker.patch("commitizen.out.write") + commands.Schema(config)() + write_mock.assert_called_once() + + +def test_list_cz(config, mocker): + write_mock = mocker.patch("commitizen.out.write") + commands.ListCz(config)() + write_mock.assert_called_once() diff --git a/tests/commands/test_version_command.py b/tests/commands/test_version_command.py new file mode 100644 index 0000000000..527c0b53f1 --- /dev/null +++ b/tests/commands/test_version_command.py @@ -0,0 +1,42 @@ +from commitizen import commands +from commitizen.__version__ import __version__ + + +def test_version_for_showing_project_version(config, capsys): + # No version exist + commands.Version(config, {"project": True, "commitizen": False, "verbose": False})() + captured = capsys.readouterr() + assert "No project information in this project." in captured.err + + config.settings["version"] = "v0.0.1" + commands.Version(config, {"project": True, "commitizen": False, "verbose": False})() + captured = capsys.readouterr() + assert "v0.0.1" in captured.out + + +def test_version_for_showing_commitizen_version(config, capsys): + commands.Version(config, {"project": False, "commitizen": True, "verbose": False})() + captured = capsys.readouterr() + assert f"{__version__}" in captured.out + + # default showing commitizen version + commands.Version( + config, {"project": False, "commitizen": False, "verbose": False} + )() + captured = capsys.readouterr() + assert f"{__version__}" in captured.out + + +def test_version_for_showing_both_versions(config, capsys): + commands.Version(config, {"project": False, "commitizen": False, "verbose": True})() + captured = capsys.readouterr() + assert f"Installed Commitizen Version: {__version__}" in captured.out + assert "No project information in this project." in captured.err + + config.settings["version"] = "v0.0.1" + commands.Version(config, {"project": False, "commitizen": False, "verbose": True})() + captured = capsys.readouterr() + expected_out = ( + f"Installed Commitizen Version: {__version__}\n" f"Project Version: v0.0.1" + ) + assert expected_out in captured.out diff --git a/tests/test_bump_command.py b/tests/test_bump_command.py deleted file mode 100644 index 209a5efe53..0000000000 --- a/tests/test_bump_command.py +++ /dev/null @@ -1,104 +0,0 @@ -import errno -import os -import shutil -import stat -import sys -import uuid -from pathlib import Path -from typing import Optional - -import pytest - -from commitizen import cli, cmd, git - - -def ReadOnlyException(Exception): - pass - - -# https://stackoverflow.com/questions/1213706/what-user-do-python-scripts-run-as-in-windows -def handleRemoveReadOnly(func, path, exc): - excvalue = exc[1] - if func in (os.rmdir, os.remove, shutil.rmtree) and excvalue.errno == errno.EACCESS: - os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.IRWXO) # 744 - func(path) - else: - raise ReadOnlyException - - -@pytest.fixture -def create_project(): - current_directory = os.getcwd() - tmp_proj_path = "tests/tmp-proj" - full_tmp_path = os.path.join(current_directory, tmp_proj_path) - if not os.path.exists(full_tmp_path): - os.makedirs(full_tmp_path) - os.chdir(full_tmp_path) - yield - os.chdir(current_directory) - shutil.rmtree(full_tmp_path, handleRemoveReadOnly) - - -def create_file_and_commit(message: str, filename: Optional[str] = None): - if not filename: - filename = str(uuid.uuid4()) - Path(f"./{filename}").touch() - cmd.run("git add .") - git.commit(message) - - -def test_bump_command(mocker, create_project): - with open("./pyproject.toml", "w") as f: - f.write("[tool.commitizen]\n" 'version="0.1.0"') - - cmd.run("git init") - - # MINOR - create_file_and_commit("feat: new file") - - testargs = ["cz", "bump", "--yes"] - mocker.patch.object(sys, "argv", testargs) - cli.main() - - tag_exists = git.tag_exist("0.2.0") - assert tag_exists is True - - # PATCH - create_file_and_commit("fix: username exception") - - testargs = ["cz", "bump"] - mocker.patch.object(sys, "argv", testargs) - cli.main() - - tag_exists = git.tag_exist("0.2.1") - assert tag_exists is True - - # PRERELEASE - create_file_and_commit("feat: location") - - testargs = ["cz", "bump", "--prerelease", "alpha"] - mocker.patch.object(sys, "argv", testargs) - cli.main() - - tag_exists = git.tag_exist("0.3.0a0") - assert tag_exists is True - - # PRERELEASE BUMP CREATES VERSION WITHOUT PRERELEASE - testargs = ["cz", "bump"] - mocker.patch.object(sys, "argv", testargs) - cli.main() - - tag_exists = git.tag_exist("0.3.0") - assert tag_exists is True - - # MAJOR - create_file_and_commit( - "feat: new user interface\n\nBREAKING CHANGE: age is no longer supported" - ) - - testargs = ["cz", "bump"] - mocker.patch.object(sys, "argv", testargs) - cli.main() - - tag_exists = git.tag_exist("1.0.0") - assert tag_exists is True diff --git a/tests/test_cli.py b/tests/test_cli.py index 619f1c5057..51638655b3 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -3,11 +3,46 @@ import pytest from commitizen import cli +from commitizen.__version__ import __version__ -def test_sysexit_no_argv(): +def test_sysexit_no_argv(mocker, capsys): + testargs = ["cz"] + mocker.patch.object(sys, "argv", testargs) + with pytest.raises(SystemExit): cli.main() + out, _ = capsys.readouterr() + assert out.startswith("usage") + + +def test_cz_with_arg_but_without_command(mocker, capsys): + testargs = ["cz", "--name", "cz_jira"] + mocker.patch.object(sys, "argv", testargs) + + with pytest.raises(SystemExit): + cli.main() + _, err = capsys.readouterr() + assert "Command is required" in err + + +def test_name(mocker, capsys): + testargs = ["cz", "-n", "cz_jira", "example"] + mocker.patch.object(sys, "argv", testargs) + + cli.main() + out, _ = capsys.readouterr() + assert out.startswith("JRA") + + +def test_name_default_value(tmpdir, mocker, capsys): + with tmpdir.as_cwd() as _: + testargs = ["cz", "example"] + mocker.patch.object(sys, "argv", testargs) + + cli.main() + out, _ = capsys.readouterr() + assert out.startswith("fix: correct minor typos in code") def test_ls(mocker, capsys): @@ -20,11 +55,10 @@ def test_ls(mocker, capsys): assert isinstance(out, str) -def test_version(mocker): +def test_version(mocker, capsys): testargs = ["cz", "--version"] mocker.patch.object(sys, "argv", testargs) - error_mock = mocker.patch("commitizen.out.error") cli.main() - - error_mock.assert_called_once() + out, _ = capsys.readouterr() + assert out.strip() == __version__ diff --git a/tests/test_commands.py b/tests/test_commands.py deleted file mode 100644 index c88a2716ba..0000000000 --- a/tests/test_commands.py +++ /dev/null @@ -1,259 +0,0 @@ -import contextlib -import os -import shutil -import tempfile -from unittest import mock - -import pytest - -from commitizen import cmd, commands, defaults -from commitizen.cz.exceptions import CzException -from commitizen.config import BaseConfig -from commitizen.__version__ import __version__ - - -@pytest.fixture() -def config(): - _config = BaseConfig() - _config.settings.update({"name": defaults.name}) - return _config - - -@pytest.fixture -def staging_is_clean(mocker): - is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") - is_staging_clean_mock.return_value = False - - -@contextlib.contextmanager -def get_temp_dir(): - temp_dir = tempfile.mkdtemp() - try: - yield temp_dir - finally: - shutil.rmtree(temp_dir) - - -@pytest.mark.usefixtures("staging_is_clean") -def test_commit(config, mocker): - prompt_mock = mocker.patch("questionary.prompt") - prompt_mock.return_value = { - "prefix": "feat", - "subject": "user created", - "scope": "", - "is_breaking_change": False, - "body": "", - "footer": "", - } - - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", "", "") - success_mock = mocker.patch("commitizen.out.success") - - commands.Commit(config, {})() - success_mock.assert_called_once() - - -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_retry_fails_no_backup(config, mocker): - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", "", "") - - with pytest.raises(SystemExit): - commands.Commit(config, {"retry": True})() - - -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_retry_works(config, mocker): - prompt_mock = mocker.patch("questionary.prompt") - prompt_mock.return_value = { - "prefix": "feat", - "subject": "user created", - "scope": "", - "is_breaking_change": False, - "body": "closes #21", - "footer": "", - } - - commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("", "error", "", "") - error_mock = mocker.patch("commitizen.out.error") - - with pytest.raises(SystemExit): - commit_cmd = commands.Commit(config, {}) - temp_file = commit_cmd.temp_file - commit_cmd() - - prompt_mock.assert_called_once() - error_mock.assert_called_once() - assert os.path.isfile(temp_file) - - # Previous commit failed, so retry should pick up the backup commit - # commit_mock = mocker.patch("commitizen.git.commit") - commit_mock.return_value = cmd.Command("success", "", "", "") - success_mock = mocker.patch("commitizen.out.success") - - commands.Commit(config, {"retry": True})() - - commit_mock.assert_called_with("feat: user created\n\ncloses #21") - prompt_mock.assert_called_once() - success_mock.assert_called_once() - assert not os.path.isfile(temp_file) - - -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_command_with_dry_run_option(config, mocker): - prompt_mock = mocker = mocker.patch("questionary.prompt") - prompt_mock.return_value = { - "prefix": "feat", - "subject": "user created", - "scope": "", - "is_breaking_change": False, - "body": "closes #57", - "footer": "", - } - - with pytest.raises(SystemExit): - commit_cmd = commands.Commit(config, {"dry_run": True}) - commit_cmd() - - -def test_commit_when_nothing_to_commit(config, mocker): - is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean") - is_staging_clean_mock.return_value = True - - with pytest.raises(SystemExit) as err: - commit_cmd = commands.Commit(config, {}) - commit_cmd() - - assert err.value.code == commands.commit.NOTHING_TO_COMMIT - - -@pytest.mark.usefixtures("staging_is_clean") -def test_commit_when_customized_expected_raised(config, mocker, capsys): - _err = ValueError() - _err.__context__ = CzException("This is the root custom err") - prompt_mock = mocker.patch("questionary.prompt") - prompt_mock.side_effect = _err - - with pytest.raises(SystemExit) as err: - commit_cmd = commands.Commit(config, {}) - commit_cmd() - - assert err.value.code == commands.commit.CUSTOM_ERROR - - # Assert only the content in the formatted text - captured = capsys.readouterr() - assert "This is the root custom err" in captured.err - - -def test_example(config): - with mock.patch("commitizen.out.write") as write_mock: - commands.Example(config)() - write_mock.assert_called_once() - - -def test_info(config): - with mock.patch("commitizen.out.write") as write_mock: - commands.Info(config)() - write_mock.assert_called_once() - - -def test_schema(config): - with mock.patch("commitizen.out.write") as write_mock: - commands.Schema(config)() - write_mock.assert_called_once() - - -def test_list_cz(config): - with mock.patch("commitizen.out.write") as mocked_write: - commands.ListCz(config)() - mocked_write.assert_called_once() - - -def test_version_for_showing_project_version(config, capsys): - # No version exist - commands.Version(config, {"project": True, "commitizen": False, "verbose": False})() - captured = capsys.readouterr() - assert "No project information in this project." in captured.err - - config.settings["version"] = "v0.0.1" - commands.Version(config, {"project": True, "commitizen": False, "verbose": False})() - captured = capsys.readouterr() - assert "v0.0.1" in captured.out - - -def test_version_for_showing_commitizen_version(config, capsys): - commands.Version(config, {"project": False, "commitizen": True, "verbose": False})() - captured = capsys.readouterr() - assert f"{__version__}" in captured.out - - # default showing commitizen version - commands.Version( - config, {"project": False, "commitizen": False, "verbose": False} - )() - captured = capsys.readouterr() - assert f"{__version__}" in captured.out - - -def test_version_for_showing_both_versions(config, capsys): - commands.Version(config, {"project": False, "commitizen": False, "verbose": True})() - captured = capsys.readouterr() - assert f"Installed Commitizen Version: {__version__}" in captured.out - assert "No project information in this project." in captured.err - - config.settings["version"] = "v0.0.1" - commands.Version(config, {"project": False, "commitizen": False, "verbose": True})() - captured = capsys.readouterr() - expected_out = ( - f"Installed Commitizen Version: {__version__}\n" f"Project Version: v0.0.1" - ) - assert expected_out in captured.out - - -def test_check_no_conventional_commit(config, mocker): - with pytest.raises(SystemExit): - error_mock = mocker.patch("commitizen.out.error") - - with get_temp_dir() as dir: - - tempfile = os.path.join(dir, "temp_commit_file") - with open(tempfile, "w") as f: - f.write("no conventional commit") - - check_cmd = commands.Check( - config=config, arguments={"commit_msg_file": tempfile} - ) - check_cmd() - error_mock.assert_called_once() - - -def test_check_conventional_commit(config, mocker): - success_mock = mocker.patch("commitizen.out.success") - with get_temp_dir() as dir: - - tempfile = os.path.join(dir, "temp_commit_file") - with open(tempfile, "w") as f: - f.write("feat(lang): added polish language") - - check_cmd = commands.Check( - config=config, arguments={"commit_msg_file": tempfile} - ) - - check_cmd() - success_mock.assert_called_once() - - -def test_check_command_when_commit_file_not_found(config): - with pytest.raises(FileNotFoundError): - commands.Check(config=config, arguments={"commit_msg_file": ""})() - - -def test_init_when_config_already_exists(config, capsys): - # Set config path - path = "tests/pyproject.toml" - config.add_path(path) - - commands.Init(config)() - captured = capsys.readouterr() - assert captured.out == f"Config file {path} already exists\n" diff --git a/tests/test_cz_utils.py b/tests/test_cz_utils.py new file mode 100644 index 0000000000..a8a56d762c --- /dev/null +++ b/tests/test_cz_utils.py @@ -0,0 +1,19 @@ +import pytest + +from commitizen.cz import utils, exceptions + + +def test_required_validator(): + assert utils.required_validator("test") == "test" + + with pytest.raises(exceptions.AnswerRequiredError): + utils.required_validator("") + + +def test_multiple_line_breaker(): + message = "this is the first line | and this is the second line " + result = utils.multiple_line_breaker(message) + assert result == "this is the first line\nand this is the second line" + + result = utils.multiple_line_breaker(message, "is") + assert result == "th\n\nthe first line | and th\n\nthe second line"