Skip to content
Closed
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
10 changes: 7 additions & 3 deletions commitizen/commands/check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import re
import sys
from typing import Dict, Optional

from commitizen import factory, git, out
Expand Down Expand Up @@ -32,9 +33,12 @@ def __init__(self, config: BaseConfig, arguments: Dict[str, str], cwd=os.getcwd(
self.cz = factory.commiter_factory(self.config)

def _valid_command_argument(self):
if not (
bool(self.commit_msg_file) ^ bool(self.commit_msg) ^ bool(self.rev_range)
):
sources = (
Copy link
Member

Choose a reason for hiding this comment

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

The naming sources does not look straightforward to me. Currently, I do not have a better name for it. But we could at least add some comments to it.

Copy link
Member

Choose a reason for hiding this comment

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

it's the amount/number of params provided, so something like params_amount or number_params_provided could be

bool(self.commit_msg_file) + bool(self.commit_msg) + bool(self.rev_range)
)
if sources == 0 and not os.isatty(0):
self.commit_msg: Optional[str] = sys.stdin.read()
elif sources != 1:
raise InvalidCommandArgumentError(
(
"One and only one argument is required for check command! "
Expand Down
31 changes: 26 additions & 5 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from io import StringIO
from typing import List

import pytest
Expand Down Expand Up @@ -190,12 +191,12 @@ def test_check_a_range_of_git_commits_and_failed(config, mocker):
error_mock.assert_called_once()


@pytest.mark.parametrize(
"args", [{"rev_range": "HEAD~10..master", "commit_msg_file": "some_file"}, {}]
)
def test_check_command_with_invalid_argment(args, config):
def test_check_command_with_invalid_argment(config):
with pytest.raises(InvalidCommandArgumentError) as excinfo:
commands.Check(config=config, arguments=args)
commands.Check(
config=config,
arguments={"commit_msg_file": "some_file", "rev_range": "HEAD~10..master"},
)
assert "One and only one argument is required for check command!" in str(
excinfo.value
)
Expand Down Expand Up @@ -245,3 +246,23 @@ def test_check_command_with_invalid_message(config, mocker):
with pytest.raises(InvalidCommitMessageError):
check_cmd()
error_mock.assert_called_once()


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

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


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

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