Skip to content

Commit

Permalink
test(commands/commit): add a test for is_blank_commit_file
Browse files Browse the repository at this point in the history
  • Loading branch information
saygox authored and Lee-W committed Jan 30, 2025
1 parent e13b4f9 commit a7b3228
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
4 changes: 2 additions & 2 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def manual_edit(self, message: str) -> str:
def is_blank_commit_file(self, filename) -> bool:
if not exists(filename):
return True
with open(filename, "tr") as f:
for x in f:
with open(filename) as f:
for x in f.readlines():
if len(x) == 0 or x[0] == "#":
continue
elif x[0] != "\r" and x[0] != "\n":
Expand Down
54 changes: 54 additions & 0 deletions tests/commands/test_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ def test_commit_from_pre_commit_msg_hook(config, mocker, capsys):
"footer": "",
}

is_blank_commit_file_mock = mocker.patch(
"commitizen.commands.commit.Commit.is_blank_commit_file"
)
is_blank_commit_file_mock.return_value = True

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "", 0)

Expand All @@ -554,3 +559,52 @@ def test_commit_from_pre_commit_msg_hook(config, mocker, capsys):
out, _ = capsys.readouterr()
assert "Commit message is successful!" in out
commit_mock.assert_not_called()


def test_commit_with_msg_from_pre_commit_msg_hook(config, mocker, capsys):
testargs = ["cz", "commit", "--commit-msg-file", "some_file"]
mocker.patch.object(sys, "argv", testargs)

prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "",
"footer": "",
}

is_blank_commit_file_mock = mocker.patch(
"commitizen.commands.commit.Commit.is_blank_commit_file"
)
is_blank_commit_file_mock.return_value = False

commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "", 0)

cli.main()

prompt_mock.assert_not_called()
commit_mock.assert_not_called()


@pytest.mark.parametrize(
"isexist, commitmsg, returnvalue",
[
[False, "", True],
[True, "\n#test", True],
[True, "test: test\n#test", False],
[True, "#test: test\n#test", True],
],
)
def test_is_blank_commit_file(config, mocker, isexist, commitmsg, returnvalue):
exists_mock = mocker.patch("commitizen.commands.commit.exists")
exists_mock.return_value = isexist

reader_mock = mocker.mock_open(read_data=commitmsg)
mocker.patch("builtins.open", reader_mock)

commit_cmd = commands.Commit(config, {})
ret = commit_cmd.is_blank_commit_file("test")
assert ret == returnvalue
7 changes: 7 additions & 0 deletions tests/wrap_stdio/test_wrap_stdio.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

from commitizen import wrap_stdio


def test_import_sub_files():
import commitizen.wrap_stdio.linux # noqa: F401
import commitizen.wrap_stdio.unix # noqa: F401
import commitizen.wrap_stdio.windows # noqa: F401


if sys.platform == "win32": # pragma: no cover
pass
elif sys.platform == "linux":
Expand Down

0 comments on commit a7b3228

Please sign in to comment.