Skip to content

Commit fc3c1e9

Browse files
crai0Lee-W
authored andcommitted
test(commit): create new test for backup file creation and use fixture for other retry test cases
1 parent 4f3931a commit fc3c1e9

File tree

1 file changed

+42
-70
lines changed

1 file changed

+42
-70
lines changed

‎tests/commands/test_commit_command.py

+42-70
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from commitizen import cmd, commands
88
from commitizen.cz.exceptions import CzException
9+
from commitizen.cz.utils import get_backup_file_path
910
from commitizen.exceptions import (
1011
CommitError,
1112
CustomError,
@@ -25,6 +26,12 @@ def staging_is_clean(mocker: MockFixture, tmp_git_project):
2526
return tmp_git_project
2627

2728

29+
@pytest.fixture
30+
def backup_file(tmp_git_project):
31+
with open(get_backup_file_path(), "w") as backup_file:
32+
backup_file.write("backup commit")
33+
34+
2835
@pytest.mark.usefixtures("staging_is_clean")
2936
def test_commit(config, mocker: MockFixture):
3037
prompt_mock = mocker.patch("questionary.prompt")
@@ -46,18 +53,7 @@ def test_commit(config, mocker: MockFixture):
4653

4754

4855
@pytest.mark.usefixtures("staging_is_clean")
49-
def test_commit_retry_fails_no_backup(config, mocker: MockFixture):
50-
commit_mock = mocker.patch("commitizen.git.commit")
51-
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
52-
53-
with pytest.raises(NoCommitBackupError) as excinfo:
54-
commands.Commit(config, {"retry": True})()
55-
56-
assert NoCommitBackupError.message in str(excinfo.value)
57-
58-
59-
@pytest.mark.usefixtures("staging_is_clean")
60-
def test_commit_retry_works(config, mocker: MockFixture):
56+
def test_commit_backup_on_failure(config, mocker: MockFixture):
6157
prompt_mock = mocker.patch("questionary.prompt")
6258
prompt_mock.return_value = {
6359
"prefix": "feat",
@@ -81,15 +77,32 @@ def test_commit_retry_works(config, mocker: MockFixture):
8177
error_mock.assert_called_once()
8278
assert os.path.isfile(temp_file)
8379

84-
# Previous commit failed, so retry should pick up the backup commit
85-
# commit_mock = mocker.patch("commitizen.git.commit")
80+
81+
@pytest.mark.usefixtures("staging_is_clean")
82+
def test_commit_retry_fails_no_backup(config, mocker: MockFixture):
83+
commit_mock = mocker.patch("commitizen.git.commit")
84+
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
85+
86+
with pytest.raises(NoCommitBackupError) as excinfo:
87+
commands.Commit(config, {"retry": True})()
88+
89+
assert NoCommitBackupError.message in str(excinfo.value)
90+
91+
92+
@pytest.mark.usefixtures("staging_is_clean", "backup_file")
93+
def test_commit_retry_works(config, mocker: MockFixture):
94+
prompt_mock = mocker.patch("questionary.prompt")
95+
96+
commit_mock = mocker.patch("commitizen.git.commit")
8697
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
8798
success_mock = mocker.patch("commitizen.out.success")
8899

89-
commands.Commit(config, {"retry": True})()
100+
commit_cmd = commands.Commit(config, {"retry": True})
101+
temp_file = commit_cmd.temp_file
102+
commit_cmd()
90103

91-
commit_mock.assert_called_with("feat: user created\n\ncloses #21", args="")
92-
prompt_mock.assert_called_once()
104+
commit_mock.assert_called_with("backup commit", args="")
105+
prompt_mock.assert_not_called()
93106
success_mock.assert_called_once()
94107
assert not os.path.isfile(temp_file)
95108

@@ -118,46 +131,26 @@ def test_commit_retry_after_failure_no_backup(config, mocker: MockFixture):
118131
success_mock.assert_called_once()
119132

120133

121-
@pytest.mark.usefixtures("staging_is_clean")
134+
@pytest.mark.usefixtures("staging_is_clean", "backup_file")
122135
def test_commit_retry_after_failure_works(config, mocker: MockFixture):
123136
prompt_mock = mocker.patch("questionary.prompt")
124-
prompt_mock.return_value = {
125-
"prefix": "feat",
126-
"subject": "user created",
127-
"scope": "",
128-
"is_breaking_change": False,
129-
"body": "closes #21",
130-
"footer": "",
131-
}
132137

133138
commit_mock = mocker.patch("commitizen.git.commit")
134-
commit_mock.return_value = cmd.Command("", "error", b"", b"", 9)
135-
error_mock = mocker.patch("commitizen.out.error")
136-
137-
with pytest.raises(CommitError):
138-
commit_cmd = commands.Commit(config, {})
139-
temp_file = commit_cmd.temp_file
140-
commit_cmd()
141-
142-
prompt_mock.assert_called_once()
143-
error_mock.assert_called_once()
144-
assert os.path.isfile(temp_file)
145-
146-
# Previous commit failed, so retry should pick up the backup commit
147-
# commit_mock = mocker.patch("commitizen.git.commit")
148139
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
149140
success_mock = mocker.patch("commitizen.out.success")
150141

151142
config.settings["retry_after_failure"] = True
152-
commands.Commit(config, {})()
143+
commit_cmd = commands.Commit(config, {})
144+
temp_file = commit_cmd.temp_file
145+
commit_cmd()
153146

154-
commit_mock.assert_called_with("feat: user created\n\ncloses #21", args="")
155-
prompt_mock.assert_called_once()
147+
commit_mock.assert_called_with("backup commit", args="")
148+
prompt_mock.assert_not_called()
156149
success_mock.assert_called_once()
157150
assert not os.path.isfile(temp_file)
158151

159152

160-
@pytest.mark.usefixtures("staging_is_clean")
153+
@pytest.mark.usefixtures("staging_is_clean", "backup_file")
161154
def test_commit_retry_after_failure_with_no_retry_works(config, mocker: MockFixture):
162155
prompt_mock = mocker.patch("questionary.prompt")
163156
prompt_mock.return_value = {
@@ -170,37 +163,16 @@ def test_commit_retry_after_failure_with_no_retry_works(config, mocker: MockFixt
170163
}
171164

172165
commit_mock = mocker.patch("commitizen.git.commit")
173-
commit_mock.return_value = cmd.Command("", "error", b"", b"", 9)
174-
error_mock = mocker.patch("commitizen.out.error")
175-
176-
with pytest.raises(CommitError):
177-
commit_cmd = commands.Commit(config, {})
178-
temp_file = commit_cmd.temp_file
179-
commit_cmd()
180-
181-
prompt_mock.assert_called_once()
182-
error_mock.assert_called_once()
183-
assert os.path.isfile(temp_file)
184-
185-
# provide different prompt to test that --no-retry ignore backup file
186-
prompt_mock = mocker.patch("questionary.prompt")
187-
prompt_mock.return_value = {
188-
"prefix": "feat",
189-
"subject": "user created",
190-
"scope": "",
191-
"is_breaking_change": False,
192-
"body": "closes #22",
193-
"footer": "",
194-
}
195-
196166
commit_mock.return_value = cmd.Command("success", "", b"", b"", 0)
197167
success_mock = mocker.patch("commitizen.out.success")
198168

199169
config.settings["retry_after_failure"] = True
200-
commands.Commit(config, {"no_retry": True})()
170+
commit_cmd = commands.Commit(config, {"no_retry": True})
171+
temp_file = commit_cmd.temp_file
172+
commit_cmd()
201173

202-
commit_mock.assert_called_with("feat: user created\n\ncloses #22", args="")
203-
prompt_mock.assert_called()
174+
commit_mock.assert_called_with("feat: user created\n\ncloses #21", args="")
175+
prompt_mock.assert_called_once()
204176
success_mock.assert_called_once()
205177
assert not os.path.isfile(temp_file)
206178

0 commit comments

Comments
 (0)