1
- # import pytest
1
+ import os
2
+ import pytest
2
3
from unittest import mock
4
+
3
5
from commitizen import defaults , commands , cmd
4
6
5
7
config = {"name" : defaults .name }
@@ -20,8 +22,53 @@ def test_commit(mocker):
20
22
commit_mock .return_value = cmd .Command ("success" , "" , "" , "" )
21
23
success_mock = mocker .patch ("commitizen.out.success" )
22
24
23
- commands .Commit (config )()
25
+ commands .Commit (config , {})()
26
+ success_mock .assert_called_once ()
27
+
28
+
29
+ def test_commit_retry_fails_no_backup (mocker ):
30
+ commit_mock = mocker .patch ("commitizen.git.commit" )
31
+ commit_mock .return_value = cmd .Command ("success" , "" , "" , "" )
32
+
33
+ with pytest .raises (SystemExit ):
34
+ commands .Commit (config , {"retry" : True })()
35
+
36
+
37
+ def test_commit_retry_works (mocker ):
38
+ prompt_mock = mocker .patch ("questionary.prompt" )
39
+ prompt_mock .return_value = {
40
+ "prefix" : "feat" ,
41
+ "subject" : "user created" ,
42
+ "scope" : "" ,
43
+ "is_breaking_change" : False ,
44
+ "body" : "closes #21" ,
45
+ "footer" : "" ,
46
+ }
47
+
48
+ commit_mock = mocker .patch ("commitizen.git.commit" )
49
+ commit_mock .return_value = cmd .Command ("" , "error" , "" , "" )
50
+ error_mock = mocker .patch ("commitizen.out.error" )
51
+
52
+ with pytest .raises (SystemExit ):
53
+ commit_cmd = commands .Commit (config , {})
54
+ temp_file = commit_cmd .temp_file
55
+ commit_cmd ()
56
+
57
+ prompt_mock .assert_called_once ()
58
+ error_mock .assert_called_once ()
59
+ assert os .path .isfile (temp_file )
60
+
61
+ # Previous commit failed, so retry should pick up the backup commit
62
+ # commit_mock = mocker.patch("commitizen.git.commit")
63
+ commit_mock .return_value = cmd .Command ("success" , "" , "" , "" )
64
+ success_mock = mocker .patch ("commitizen.out.success" )
65
+
66
+ commands .Commit (config , {"retry" : True })()
67
+
68
+ commit_mock .assert_called_with ("feat: user created\n \n closes #21" )
69
+ prompt_mock .assert_called_once ()
24
70
success_mock .assert_called_once ()
71
+ assert not os .path .isfile (temp_file )
25
72
26
73
27
74
def test_example ():
0 commit comments