Skip to content

Commit 01520eb

Browse files
jordanSuLee-W
authored andcommitted
feat(cz_check): cz check can read from a string input
1 parent f27baba commit 01520eb

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

‎commitizen/cli.py

+5
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,11 @@
195195
"help": "a range of git rev to check. e.g, master..HEAD",
196196
"exclusive_group": "group1",
197197
},
198+
{
199+
"name": ["-m", "--message"],
200+
"help": "commit message that needs to be checked",
201+
"exclusive_group": "group1",
202+
},
198203
],
199204
},
200205
{

‎commitizen/commands/check.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def __init__(self, config: BaseConfig, arguments: Dict[str, str], cwd=os.getcwd(
2323
cwd: Current work directory
2424
"""
2525
self.commit_msg_file: Optional[str] = arguments.get("commit_msg_file")
26+
self.commit_msg: Optional[str] = arguments.get("message")
2627
self.rev_range: Optional[str] = arguments.get("rev_range")
2728

2829
self._valid_command_argument()
@@ -31,7 +32,9 @@ def __init__(self, config: BaseConfig, arguments: Dict[str, str], cwd=os.getcwd(
3132
self.cz = factory.commiter_factory(self.config)
3233

3334
def _valid_command_argument(self):
34-
if bool(self.commit_msg_file) is bool(self.rev_range):
35+
if not (
36+
bool(self.commit_msg_file) ^ bool(self.commit_msg) ^ bool(self.rev_range)
37+
):
3538
raise InvalidCommandArgumentError(
3639
(
3740
"One and only one argument is required for check command! "
@@ -77,6 +80,8 @@ def _get_commits(self):
7780
commit_title = commit_file.readline()
7881
commit_body = commit_file.read()
7982
return [git.GitCommit(rev="", title=commit_title, body=commit_body)]
83+
elif self.commit_msg:
84+
return [git.GitCommit(rev="", title="", body=self.commit_msg)]
8085

8186
# Get commit messages from git log (--rev-range)
8287
return git.get_commits(end=self.rev_range)

‎tests/commands/test_check_command.py

+19
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,22 @@ def test_check_a_range_of_failed_git_commits(config, mocker):
226226
with pytest.raises(InvalidCommitMessageError) as excinfo:
227227
check_cmd()
228228
assert all([msg in str(excinfo.value) for msg in ill_formated_commits_msgs])
229+
230+
231+
def test_check_command_with_valid_message(config, mocker):
232+
success_mock = mocker.patch("commitizen.out.success")
233+
check_cmd = commands.Check(
234+
config=config, arguments={"message": "fix(scope): some commit message"}
235+
)
236+
237+
check_cmd()
238+
success_mock.assert_called_once()
239+
240+
241+
def test_check_command_with_invalid_message(config, mocker):
242+
error_mock = mocker.patch("commitizen.out.error")
243+
check_cmd = commands.Check(config=config, arguments={"message": "bad commit"})
244+
245+
with pytest.raises(InvalidCommitMessageError):
246+
check_cmd()
247+
error_mock.assert_called_once()

0 commit comments

Comments
 (0)