Skip to content

Commit 1053d01

Browse files
committed
test(commands/commit): add a test for is_blank_commit_file
1 parent 97876a5 commit 1053d01

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

commitizen/commands/commit.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ def prompt_commit_questions(self) -> str:
6565
def is_blank_commit_file(self, filename) -> bool:
6666
if not exists(filename):
6767
return True
68-
with open(filename, "tr") as f:
69-
for x in f:
68+
with open(filename, "r") as f:
69+
for x in f.readlines():
7070
if len(x) == 0 or x[0] == "#":
7171
continue
7272
elif x[0] != "\r" and x[0] != "\n":

tests/commands/test_commit_command.py

+54
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ def test_commit_from_pre_commit_msg_hook(config, mocker, capsys):
196196
"footer": "",
197197
}
198198

199+
is_blank_commit_file_mock = mocker.patch(
200+
"commitizen.commands.commit.Commit.is_blank_commit_file"
201+
)
202+
is_blank_commit_file_mock.return_value = True
203+
199204
commit_mock = mocker.patch("commitizen.git.commit")
200205
commit_mock.return_value = cmd.Command("success", "", "", "", 0)
201206

@@ -209,3 +214,52 @@ def test_commit_from_pre_commit_msg_hook(config, mocker, capsys):
209214
out, _ = capsys.readouterr()
210215
assert "Commit message is successful!" in out
211216
commit_mock.assert_not_called()
217+
218+
219+
def test_commit_with_msg_from_pre_commit_msg_hook(config, mocker, capsys):
220+
testargs = ["cz", "commit", "--commit-msg-file", "some_file"]
221+
mocker.patch.object(sys, "argv", testargs)
222+
223+
prompt_mock = mocker.patch("questionary.prompt")
224+
prompt_mock.return_value = {
225+
"prefix": "feat",
226+
"subject": "user created",
227+
"scope": "",
228+
"is_breaking_change": False,
229+
"body": "",
230+
"footer": "",
231+
}
232+
233+
is_blank_commit_file_mock = mocker.patch(
234+
"commitizen.commands.commit.Commit.is_blank_commit_file"
235+
)
236+
is_blank_commit_file_mock.return_value = False
237+
238+
commit_mock = mocker.patch("commitizen.git.commit")
239+
commit_mock.return_value = cmd.Command("success", "", "", "", 0)
240+
241+
cli.main()
242+
243+
prompt_mock.assert_not_called()
244+
commit_mock.assert_not_called()
245+
246+
247+
@pytest.mark.parametrize(
248+
"isexist, commitmsg, returnvalue",
249+
[
250+
[False, "", True],
251+
[True, "\n#test", True],
252+
[True, "test: test\n#test", False],
253+
[True, "#test: test\n#test", True],
254+
],
255+
)
256+
def test_is_blank_commit_file(config, mocker, isexist, commitmsg, returnvalue):
257+
exists_mock = mocker.patch("commitizen.commands.commit.exists")
258+
exists_mock.return_value = isexist
259+
260+
reader_mock = mocker.mock_open(read_data=commitmsg)
261+
mocker.patch("builtins.open", reader_mock)
262+
263+
commit_cmd = commands.Commit(config, {})
264+
ret = commit_cmd.is_blank_commit_file("test")
265+
assert ret == returnvalue

tests/wrap_stdio/test_wrap_stdio.py

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
from commitizen import wrap_stdio
44

5+
6+
def test_import_sub_files():
7+
import commitizen.wrap_stdio.linux # noqa: F401
8+
import commitizen.wrap_stdio.unix # noqa: F401
9+
import commitizen.wrap_stdio.windows # noqa: F401
10+
11+
512
if sys.platform == "win32": # pragma: no cover
613
pass
714
elif sys.platform == "linux":

0 commit comments

Comments
 (0)