Skip to content

Commit f3070c1

Browse files
jordanSuLee-W
authored andcommitted
feat(cz_check): cz check can read commit message from pipe
1 parent 492270e commit f3070c1

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

commitizen/commands/check.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os
22
import re
3+
import sys
34
from typing import Dict, Optional
45

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

3435
def _valid_command_argument(self):
35-
if not (
36-
bool(self.commit_msg_file) ^ bool(self.commit_msg) ^ bool(self.rev_range)
37-
):
36+
number_args_provided = (
37+
bool(self.commit_msg_file) + bool(self.commit_msg) + bool(self.rev_range)
38+
)
39+
if number_args_provided == 0 and not os.isatty(0):
40+
self.commit_msg: Optional[str] = sys.stdin.read()
41+
elif number_args_provided != 1:
3842
raise InvalidCommandArgumentError(
3943
(
4044
"One and only one argument is required for check command! "

tests/commands/test_check_command.py

+26-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from io import StringIO
23
from typing import List
34

45
import pytest
@@ -190,12 +191,12 @@ def test_check_a_range_of_git_commits_and_failed(config, mocker):
190191
error_mock.assert_called_once()
191192

192193

193-
@pytest.mark.parametrize(
194-
"args", [{"rev_range": "HEAD~10..master", "commit_msg_file": "some_file"}, {}]
195-
)
196-
def test_check_command_with_invalid_argment(args, config):
194+
def test_check_command_with_invalid_argment(config):
197195
with pytest.raises(InvalidCommandArgumentError) as excinfo:
198-
commands.Check(config=config, arguments=args)
196+
commands.Check(
197+
config=config,
198+
arguments={"commit_msg_file": "some_file", "rev_range": "HEAD~10..master"},
199+
)
199200
assert "One and only one argument is required for check command!" in str(
200201
excinfo.value
201202
)
@@ -245,3 +246,23 @@ def test_check_command_with_invalid_message(config, mocker):
245246
with pytest.raises(InvalidCommitMessageError):
246247
check_cmd()
247248
error_mock.assert_called_once()
249+
250+
251+
def test_check_command_with_pipe_message(mocker, capsys):
252+
testargs = ["cz", "check"]
253+
mocker.patch.object(sys, "argv", testargs)
254+
mocker.patch("sys.stdin", StringIO("fix(scope): some commit message"))
255+
256+
cli.main()
257+
out, _ = capsys.readouterr()
258+
assert "Commit validation: successful!" in out
259+
260+
261+
def test_check_command_with_pipe_message_and_failed(mocker):
262+
testargs = ["cz", "check"]
263+
mocker.patch.object(sys, "argv", testargs)
264+
mocker.patch("sys.stdin", StringIO("bad commit message"))
265+
266+
with pytest.raises(InvalidCommitMessageError) as excinfo:
267+
cli.main()
268+
assert "commit validation: failed!" in str(excinfo.value)

0 commit comments

Comments
 (0)