66
77from commitizen import cmd , commands
88from commitizen .cz .exceptions import CzException
9+ from commitizen .cz .utils import get_backup_file_path
910from 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" )
2936def 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 \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 ()
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" )
122135def 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 \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 ()
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" )
161154def 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 \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 ()
204176 success_mock .assert_called_once ()
205177 assert not os .path .isfile (temp_file )
206178
0 commit comments