diff --git a/commitizen/cli.py b/commitizen/cli.py index f5b41a2e2f..d457d8b7ec 100644 --- a/commitizen/cli.py +++ b/commitizen/cli.py @@ -247,15 +247,21 @@ def main(): if args.version: warnings.warn( - "'cz --version' will be deprecated in next major version. " - "Please use 'cz version' command from your scripts" + ( + "'cz --version' will be deprecated in next major version. " + "Please use 'cz version' command from your scripts" + ), + category=DeprecationWarning, ) args.func = commands.Version if args.debug: warnings.warn( - "Debug will be deprecated in next major version. " - "Please remove it from your scripts" + ( + "Debug will be deprecated in next major version. " + "Please remove it from your scripts" + ), + category=DeprecationWarning, ) logging.getLogger("commitizen").setLevel(logging.DEBUG) diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index ccfb7a4791..beadc4b392 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -1,10 +1,11 @@ import os import re +import warnings from typing import Dict, Optional from commitizen import factory, git, out from commitizen.config import BaseConfig -from commitizen.error_codes import INVALID_COMMIT_MSG +from commitizen.error_codes import INVALID_COMMIT_MSG, NO_COMMITS_FOUND class Check: @@ -50,6 +51,10 @@ def __call__(self): """ commit_msgs = self._get_commit_messages() + if not commit_msgs: + warnings.warn(f"No commit found with range: '{self.rev_range}'") + raise SystemExit(NO_COMMITS_FOUND) + pattern = self.cz.schema_pattern() for commit_msg in commit_msgs: if not Check.validate_commit_message(commit_msg, pattern): diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index e5fe53b518..bfa3d6bd88 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -194,3 +194,11 @@ def test_check_command_with_invalid_argment(args, config, capsys): commands.Check(config=config, arguments=args) _, err = capsys.readouterr() assert "One and only one argument is required for check command!" in err + + +def test_check_command_with_empty_range(config, mocker, capsys): + check_cmd = commands.Check(config=config, arguments={"rev_range": "master..master"}) + with pytest.raises(SystemExit), pytest.warns(UserWarning) as record: + check_cmd() + + assert record[0].message.args[0] == "No commit found with range: 'master..master'" diff --git a/tests/test_cli.py b/tests/test_cli.py index e136096f37..4275fa23ce 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -55,10 +55,29 @@ def test_ls(mocker, capsys): assert isinstance(out, str) -def test_version(mocker, capsys): +def test_arg_version(mocker, capsys): testargs = ["cz", "--version"] mocker.patch.object(sys, "argv", testargs) - cli.main() - out, _ = capsys.readouterr() - assert out.strip() == __version__ + with pytest.warns(DeprecationWarning) as record: + cli.main() + out, _ = capsys.readouterr() + assert out.strip() == __version__ + + assert record[0].message.args[0] == ( + "'cz --version' will be deprecated in next major version. " + "Please use 'cz version' command from your scripts" + ) + + +def test_arg_debug(mocker): + testargs = ["cz", "--debug", "info"] + mocker.patch.object(sys, "argv", testargs) + + with pytest.warns(DeprecationWarning) as record: + cli.main() + + assert record[0].message.args[0] == ( + "Debug will be deprecated in next major version. " + "Please remove it from your scripts" + )