1
+ import os
2
+
1
3
import pytest
4
+ import yaml
2
5
3
6
from commitizen import commands
7
+ from commitizen .__version__ import __version__
4
8
from commitizen .exceptions import NoAnswersError
5
9
6
10
@@ -12,31 +16,42 @@ def ask(self):
12
16
return self .expected_return
13
17
14
18
15
- def test_init (tmpdir , mocker , config ):
19
+ pre_commit_config_filename = ".pre-commit-config.yaml"
20
+ cz_hook_config = {
21
+ "repo" : "https://github.com/commitizen-tools/commitizen" ,
22
+ "rev" : f"v{ __version__ } " ,
23
+ "hooks" : [{"id" : "commitizen" , "stages" : ["commit-msg" ]}],
24
+ }
25
+
26
+ expected_config = (
27
+ "[tool.commitizen]\n "
28
+ 'name = "cz_conventional_commits"\n '
29
+ 'version = "0.0.1"\n '
30
+ 'tag_format = "$version"\n '
31
+ )
32
+
33
+
34
+ def test_init_without_setup_pre_commit_hook (tmpdir , mocker , config ):
16
35
mocker .patch (
17
36
"questionary.select" ,
18
37
side_effect = [
19
38
FakeQuestion ("pyproject.toml" ),
20
39
FakeQuestion ("cz_conventional_commits" ),
21
40
],
22
41
)
23
- mocker .patch ("questionary.confirm" , return_value = FakeQuestion ("y" ))
24
- mocker .patch ("questionary.text" , return_value = FakeQuestion ("y" ))
25
- expected_config = (
26
- "[tool.commitizen]\n "
27
- 'name = "cz_conventional_commits"\n '
28
- 'version = "0.0.1"\n '
29
- 'tag_format = "y"\n '
30
- )
42
+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
43
+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
44
+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (False ))
31
45
32
46
with tmpdir .as_cwd ():
33
47
commands .Init (config )()
34
48
35
49
with open ("pyproject.toml" , "r" ) as toml_file :
36
50
config_data = toml_file .read ()
37
-
38
51
assert config_data == expected_config
39
52
53
+ assert not os .path .isfile (pre_commit_config_filename )
54
+
40
55
41
56
def test_init_when_config_already_exists (config , capsys ):
42
57
# Set config path
@@ -67,3 +82,85 @@ def test_init_without_choosing_tag(config, mocker, tmpdir):
67
82
with tmpdir .as_cwd ():
68
83
with pytest .raises (NoAnswersError ):
69
84
commands .Init (config )()
85
+
86
+
87
+ class TestPreCommitCases :
88
+ @pytest .fixture (scope = "function" , autouse = True )
89
+ def default_choices (_ , mocker ):
90
+ mocker .patch (
91
+ "questionary.select" ,
92
+ side_effect = [
93
+ FakeQuestion ("pyproject.toml" ),
94
+ FakeQuestion ("cz_conventional_commits" ),
95
+ ],
96
+ )
97
+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
98
+ mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
99
+ mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
100
+
101
+ def test_no_existing_pre_commit_conifg (_ , tmpdir , config ):
102
+ with tmpdir .as_cwd ():
103
+ commands .Init (config )()
104
+
105
+ with open ("pyproject.toml" , "r" ) as toml_file :
106
+ config_data = toml_file .read ()
107
+ assert config_data == expected_config
108
+
109
+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
110
+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
111
+ assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
112
+
113
+ def test_empty_pre_commit_config (_ , tmpdir , config ):
114
+ with tmpdir .as_cwd ():
115
+ p = tmpdir .join (pre_commit_config_filename )
116
+ p .write ("" )
117
+
118
+ commands .Init (config )()
119
+
120
+ with open ("pyproject.toml" , "r" ) as toml_file :
121
+ config_data = toml_file .read ()
122
+ assert config_data == expected_config
123
+
124
+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
125
+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
126
+ assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
127
+
128
+ def test_pre_commit_config_without_cz_hook (_ , tmpdir , config ):
129
+ existing_hook_config = {
130
+ "repo" : "https://github.com/pre-commit/pre-commit-hooks" ,
131
+ "rev" : "v1.2.3" ,
132
+ "hooks" : [{"id" , "trailing-whitespace" }],
133
+ }
134
+
135
+ with tmpdir .as_cwd ():
136
+ p = tmpdir .join (pre_commit_config_filename )
137
+ p .write (yaml .safe_dump ({"repos" : [existing_hook_config ]}))
138
+
139
+ commands .Init (config )()
140
+
141
+ with open ("pyproject.toml" , "r" ) as toml_file :
142
+ config_data = toml_file .read ()
143
+ assert config_data == expected_config
144
+
145
+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
146
+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
147
+ assert pre_commit_config_data == {
148
+ "repos" : [existing_hook_config , cz_hook_config ]
149
+ }
150
+
151
+ def test_cz_hook_exists_in_pre_commit_config (_ , tmpdir , config ):
152
+ with tmpdir .as_cwd ():
153
+ p = tmpdir .join (pre_commit_config_filename )
154
+ p .write (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
155
+
156
+ commands .Init (config )()
157
+
158
+ with open ("pyproject.toml" , "r" ) as toml_file :
159
+ config_data = toml_file .read ()
160
+ assert config_data == expected_config
161
+
162
+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
163
+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
164
+
165
+ # check that config is not duplicated
166
+ assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
0 commit comments