-
-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathtest_commit_command.py
127 lines (98 loc) · 3.86 KB
/
test_commit_command.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import os
import pytest
from commitizen import cmd, commands
from commitizen.cz.exceptions import CzException
from commitizen.exceptions import (
CommitError,
CustomError,
NoCommitBackupError,
NothingToCommitError,
)
@pytest.fixture
def staging_is_clean(mocker):
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
is_staging_clean_mock.return_value = False
@pytest.mark.usefixtures("staging_is_clean")
def test_commit(config, mocker):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "",
"footer": "",
}
commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "")
success_mock = mocker.patch("commitizen.out.success")
commands.Commit(config, {})()
success_mock.assert_called_once()
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_retry_fails_no_backup(config, mocker):
commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "")
with pytest.raises(NoCommitBackupError):
commands.Commit(config, {"retry": True})()
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_retry_works(config, mocker):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "closes #21",
"footer": "",
}
commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("", "error", "", "")
error_mock = mocker.patch("commitizen.out.error")
with pytest.raises(CommitError):
commit_cmd = commands.Commit(config, {})
temp_file = commit_cmd.temp_file
commit_cmd()
prompt_mock.assert_called_once()
error_mock.assert_called_once()
assert os.path.isfile(temp_file)
# Previous commit failed, so retry should pick up the backup commit
# commit_mock = mocker.patch("commitizen.git.commit")
commit_mock.return_value = cmd.Command("success", "", "", "")
success_mock = mocker.patch("commitizen.out.success")
commands.Commit(config, {"retry": True})()
commit_mock.assert_called_with("feat: user created\n\ncloses #21")
prompt_mock.assert_called_once()
success_mock.assert_called_once()
assert not os.path.isfile(temp_file)
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_command_with_dry_run_option(config, mocker):
prompt_mock = mocker = mocker.patch("questionary.prompt")
prompt_mock.return_value = {
"prefix": "feat",
"subject": "user created",
"scope": "",
"is_breaking_change": False,
"body": "closes #57",
"footer": "",
}
with pytest.raises(NothingToCommitError):
commit_cmd = commands.Commit(config, {"dry_run": True})
commit_cmd()
def test_commit_when_nothing_to_commit(config, mocker):
is_staging_clean_mock = mocker.patch("commitizen.git.is_staging_clean")
is_staging_clean_mock.return_value = True
with pytest.raises(NothingToCommitError):
commit_cmd = commands.Commit(config, {})
commit_cmd()
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_when_customized_expected_raised(config, mocker, capsys):
_err = ValueError()
_err.__context__ = CzException("This is the root custom err")
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.side_effect = _err
with pytest.raises(CustomError):
commit_cmd = commands.Commit(config, {})
commit_cmd()
# Assert only the content in the formatted text
captured = capsys.readouterr()
assert "This is the root custom err" in captured.err