|
1 | 1 | import os |
| 2 | +import sys |
2 | 3 |
|
3 | 4 | import pytest |
4 | 5 |
|
5 | | -from commitizen import cmd, commands |
| 6 | +from commitizen import cli, cmd, commands |
6 | 7 | from commitizen.cz.exceptions import CzException |
7 | 8 | from commitizen.exceptions import ( |
8 | 9 | CommitError, |
@@ -178,3 +179,41 @@ def test_commit_in_non_git_project(tmpdir, config): |
178 | 179 | with tmpdir.as_cwd(): |
179 | 180 | with pytest.raises(NotAGitProjectError): |
180 | 181 | commands.Commit(config, {}) |
| 182 | + |
| 183 | + |
| 184 | +def test_commit_from_pre_commit_msg_hook(config, mocker, capsys): |
| 185 | + testargs = ["cz", "commit", "--commit-msg-file", "some_file"] |
| 186 | + mocker.patch.object(sys, "argv", testargs) |
| 187 | + |
| 188 | + prompt_mock = mocker.patch("questionary.prompt") |
| 189 | + prompt_mock.return_value = { |
| 190 | + "prefix": "feat", |
| 191 | + "subject": "user created", |
| 192 | + "scope": "", |
| 193 | + "is_breaking_change": False, |
| 194 | + "body": "", |
| 195 | + "footer": "", |
| 196 | + } |
| 197 | + |
| 198 | + commit_mock = mocker.patch("commitizen.git.commit") |
| 199 | + mocker.patch("commitizen.commands.commit.WrapStdin") |
| 200 | + mocker.patch("os.open") |
| 201 | + reader_mock = mocker.mock_open(read_data="\n\n#test\n") |
| 202 | + mocker.patch("builtins.open", reader_mock, create=True) |
| 203 | + |
| 204 | + cli.main() |
| 205 | + |
| 206 | + out, _ = capsys.readouterr() |
| 207 | + assert "Commit message is successful!" in out |
| 208 | + commit_mock.assert_not_called() |
| 209 | + |
| 210 | + |
| 211 | +def test_WrapStdin(mocker): |
| 212 | + mocker.patch("os.open") |
| 213 | + reader_mock = mocker.mock_open(read_data="data") |
| 214 | + mocker.patch("builtins.open", reader_mock, create=True) |
| 215 | + |
| 216 | + wrap_stdin = commands.commit.WrapStdin() |
| 217 | + |
| 218 | + assert wrap_stdin.encoding == "UTF-8" |
| 219 | + assert wrap_stdin.read() == "data" |
0 commit comments