|
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,
|
@@ -158,3 +159,41 @@ def test_commit_in_non_git_project(tmpdir, config):
|
158 | 159 | with tmpdir.as_cwd():
|
159 | 160 | with pytest.raises(NotAGitProjectError):
|
160 | 161 | commands.Commit(config, {})
|
| 162 | + |
| 163 | + |
| 164 | +def test_commit_from_pre_commit_msg_hook(config, mocker, capsys): |
| 165 | + testargs = ["cz", "commit", "--commit-msg-file", "some_file"] |
| 166 | + mocker.patch.object(sys, "argv", testargs) |
| 167 | + |
| 168 | + prompt_mock = mocker.patch("questionary.prompt") |
| 169 | + prompt_mock.return_value = { |
| 170 | + "prefix": "feat", |
| 171 | + "subject": "user created", |
| 172 | + "scope": "", |
| 173 | + "is_breaking_change": False, |
| 174 | + "body": "", |
| 175 | + "footer": "", |
| 176 | + } |
| 177 | + |
| 178 | + commit_mock = mocker.patch("commitizen.git.commit") |
| 179 | + mocker.patch("commitizen.commands.commit.WrapStdin") |
| 180 | + mocker.patch("os.open") |
| 181 | + reader_mock = mocker.mock_open(read_data="\n\n#test\n") |
| 182 | + mocker.patch("builtins.open", reader_mock, create=True) |
| 183 | + |
| 184 | + cli.main() |
| 185 | + |
| 186 | + out, _ = capsys.readouterr() |
| 187 | + assert "Commit message is successful!" in out |
| 188 | + commit_mock.assert_not_called() |
| 189 | + |
| 190 | + |
| 191 | +def test_WrapStdin(mocker): |
| 192 | + mocker.patch("os.open") |
| 193 | + reader_mock = mocker.mock_open(read_data="data") |
| 194 | + mocker.patch("builtins.open", reader_mock, create=True) |
| 195 | + |
| 196 | + wrap_stdin = commands.commit.WrapStdin() |
| 197 | + |
| 198 | + assert wrap_stdin.encoding == "UTF-8" |
| 199 | + assert wrap_stdin.read() == "data" |
0 commit comments