-
-
Notifications
You must be signed in to change notification settings - Fork 276
/
Copy pathtest_commit_command.py
202 lines (154 loc) · 6.17 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import os
import pytest
from commitizen import cmd, commands
from commitizen.cz.exceptions import CzException
from commitizen.exceptions import (
CommitError,
CustomError,
DryRunExit,
NoAnswersError,
NoCommitBackupError,
NotAGitProjectError,
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", "", "", "", 0)
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", "", "", "", 0)
with pytest.raises(NoCommitBackupError) as excinfo:
commands.Commit(config, {"retry": True})()
assert NoCommitBackupError.message in str(excinfo.value)
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_allow_empty(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("success", "", "", "", 0)
success_mock = mocker.patch("commitizen.out.success")
commands.Commit(config, {"allow_empty": True})()
commit_mock.assert_called_with("feat: user created\n\ncloses #21", "--allow-empty")
success_mock.assert_called_once()
@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", "", "", 9)
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", "", "", "", 0)
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(DryRunExit):
commit_cmd = commands.Commit(config, {"dry_run": True})
commit_cmd()
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_command_with_signoff_option(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", "", "", "", 0)
success_mock = mocker.patch("commitizen.out.success")
commands.Commit(config, {"signoff": True})()
success_mock.assert_called_once()
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) as excinfo:
commit_cmd = commands.Commit(config, {})
commit_cmd()
assert "No files added to staging!" in str(excinfo.value)
@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) as excinfo:
commit_cmd = commands.Commit(config, {})
commit_cmd()
# Assert only the content in the formatted text
assert "This is the root custom err" in str(excinfo.value)
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_when_non_customized_expected_raised(config, mocker, capsys):
_err = ValueError()
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.side_effect = _err
with pytest.raises(ValueError):
commit_cmd = commands.Commit(config, {})
commit_cmd()
@pytest.mark.usefixtures("staging_is_clean")
def test_commit_when_no_user_answer(config, mocker, capsys):
prompt_mock = mocker.patch("questionary.prompt")
prompt_mock.return_value = None
with pytest.raises(NoAnswersError):
commit_cmd = commands.Commit(config, {})
commit_cmd()
def test_commit_in_non_git_project(tmpdir, config):
with tmpdir.as_cwd():
with pytest.raises(NotAGitProjectError):
commands.Commit(config, {})