Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 26f0b1f

Browse files
committed
feat(cz_check): cz check can read commit message from pipe
1 parent b207e6d commit 26f0b1f

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

‎commitizen/commands/check.py‎

Lines changed: 8 additions & 3 deletions
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,13 @@ 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+
# The number of args that user provided
37+
number_args_provided = (
38+
bool(self.commit_msg_file) + bool(self.commit_msg) + bool(self.rev_range)
39+
)
40+
if number_args_provided == 0 and not os.isatty(0):
41+
self.commit_msg: Optional[str] = sys.stdin.read()
42+
elif number_args_provided != 1:
3843
raise InvalidCommandArgumentError(
3944
(
4045
"One and only one argument is required for check command! "

‎tests/commands/test_check_command.py‎

Lines changed: 26 additions & 5 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /