6
6
7
7
from commitizen import cmd , commands
8
8
from commitizen .cz .exceptions import CzException
9
+ from commitizen .cz .utils import get_backup_file_path
9
10
from commitizen .exceptions import (
10
11
CommitError ,
11
12
CustomError ,
@@ -25,6 +26,12 @@ def staging_is_clean(mocker: MockFixture, tmp_git_project):
25
26
return tmp_git_project
26
27
27
28
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
+
28
35
@pytest .mark .usefixtures ("staging_is_clean" )
29
36
def test_commit (config , mocker : MockFixture ):
30
37
prompt_mock = mocker .patch ("questionary.prompt" )
@@ -46,18 +53,7 @@ def test_commit(config, mocker: MockFixture):
46
53
47
54
48
55
@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 ):
61
57
prompt_mock = mocker .patch ("questionary.prompt" )
62
58
prompt_mock .return_value = {
63
59
"prefix" : "feat" ,
@@ -81,15 +77,32 @@ def test_commit_retry_works(config, mocker: MockFixture):
81
77
error_mock .assert_called_once ()
82
78
assert os .path .isfile (temp_file )
83
79
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" )
86
97
commit_mock .return_value = cmd .Command ("success" , "" , b"" , b"" , 0 )
87
98
success_mock = mocker .patch ("commitizen.out.success" )
88
99
89
- commands .Commit (config , {"retry" : True })()
100
+ commit_cmd = commands .Commit (config , {"retry" : True })
101
+ temp_file = commit_cmd .temp_file
102
+ commit_cmd ()
90
103
91
- commit_mock .assert_called_with ("feat: user created \n \n closes #21 " , args = "" )
92
- prompt_mock .assert_called_once ()
104
+ commit_mock .assert_called_with ("backup commit " , args = "" )
105
+ prompt_mock .assert_not_called ()
93
106
success_mock .assert_called_once ()
94
107
assert not os .path .isfile (temp_file )
95
108
@@ -118,46 +131,26 @@ def test_commit_retry_after_failure_no_backup(config, mocker: MockFixture):
118
131
success_mock .assert_called_once ()
119
132
120
133
121
- @pytest .mark .usefixtures ("staging_is_clean" )
134
+ @pytest .mark .usefixtures ("staging_is_clean" , "backup_file" )
122
135
def test_commit_retry_after_failure_works (config , mocker : MockFixture ):
123
136
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
- }
132
137
133
138
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")
148
139
commit_mock .return_value = cmd .Command ("success" , "" , b"" , b"" , 0 )
149
140
success_mock = mocker .patch ("commitizen.out.success" )
150
141
151
142
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 ()
153
146
154
- commit_mock .assert_called_with ("feat: user created \n \n closes #21 " , args = "" )
155
- prompt_mock .assert_called_once ()
147
+ commit_mock .assert_called_with ("backup commit " , args = "" )
148
+ prompt_mock .assert_not_called ()
156
149
success_mock .assert_called_once ()
157
150
assert not os .path .isfile (temp_file )
158
151
159
152
160
- @pytest .mark .usefixtures ("staging_is_clean" )
153
+ @pytest .mark .usefixtures ("staging_is_clean" , "backup_file" )
161
154
def test_commit_retry_after_failure_with_no_retry_works (config , mocker : MockFixture ):
162
155
prompt_mock = mocker .patch ("questionary.prompt" )
163
156
prompt_mock .return_value = {
@@ -170,37 +163,16 @@ def test_commit_retry_after_failure_with_no_retry_works(config, mocker: MockFixt
170
163
}
171
164
172
165
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
-
196
166
commit_mock .return_value = cmd .Command ("success" , "" , b"" , b"" , 0 )
197
167
success_mock = mocker .patch ("commitizen.out.success" )
198
168
199
169
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 ()
201
173
202
- commit_mock .assert_called_with ("feat: user created\n \n closes #22 " , args = "" )
203
- prompt_mock .assert_called ()
174
+ commit_mock .assert_called_with ("feat: user created\n \n closes #21 " , args = "" )
175
+ prompt_mock .assert_called_once ()
204
176
success_mock .assert_called_once ()
205
177
assert not os .path .isfile (temp_file )
206
178
0 commit comments