1
1
import json
2
2
import os
3
+ from typing import Any , Dict , List
3
4
4
5
import pytest
5
6
import yaml
@@ -22,7 +23,10 @@ def ask(self):
22
23
cz_hook_config = {
23
24
"repo" : "https://github.com/commitizen-tools/commitizen" ,
24
25
"rev" : f"v{ __version__ } " ,
25
- "hooks" : [{"id" : "commitizen" }],
26
+ "hooks" : [
27
+ {"id" : "commitizen" },
28
+ {"id" : "commitizen-branch" , "stages" : ["push" ]},
29
+ ],
26
30
}
27
31
28
32
expected_config = (
@@ -51,7 +55,8 @@ def test_init_without_setup_pre_commit_hook(tmpdir, mocker: MockFixture, config)
51
55
)
52
56
mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
53
57
mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
54
- mocker .patch ("questionary.confirm" , return_value = FakeQuestion (False ))
58
+ # Return None to skip hook installation
59
+ mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
55
60
56
61
with tmpdir .as_cwd ():
57
62
commands .Init (config )()
@@ -118,51 +123,55 @@ def default_choice(request, mocker: MockFixture):
118
123
)
119
124
mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
120
125
mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
126
+ mocker .patch (
127
+ "questionary.checkbox" ,
128
+ return_value = FakeQuestion (["commit-msg" , "pre-push" ]),
129
+ )
121
130
yield request .param
122
131
123
132
133
+ def check_cz_config (config : str ):
134
+ """
135
+ Cehck the content of commitizen config is as expected
136
+
137
+ Args:
138
+ config: The config path
139
+ """
140
+ with open (config , "r" ) as file :
141
+ if "json" in config :
142
+ assert json .load (file ) == EXPECTED_DICT_CONFIG
143
+ elif "yaml" in config :
144
+ assert yaml .load (file , Loader = yaml .FullLoader ) == EXPECTED_DICT_CONFIG
145
+ else :
146
+ config_data = file .read ()
147
+ assert config_data == expected_config
148
+
149
+
150
+ def check_pre_commit_config (expected : List [Dict [str , Any ]]):
151
+ """
152
+ Check the content of pre-commit config is as expected
153
+ """
154
+ with open (pre_commit_config_filename , "r" ) as pre_commit_file :
155
+ pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
156
+ assert pre_commit_config_data == {"repos" : expected }
157
+
158
+
124
159
@pytest .mark .usefixtures ("pre_commit_installed" )
125
160
class TestPreCommitCases :
126
161
def test_no_existing_pre_commit_conifg (_ , default_choice , tmpdir , config ):
127
162
with tmpdir .as_cwd ():
128
163
commands .Init (config )()
129
-
130
- with open (default_choice , "r" ) as file :
131
- if "json" in default_choice :
132
- assert json .load (file ) == EXPECTED_DICT_CONFIG
133
- elif "yaml" in default_choice :
134
- assert (
135
- yaml .load (file , Loader = yaml .FullLoader ) == EXPECTED_DICT_CONFIG
136
- )
137
- else :
138
- config_data = file .read ()
139
- assert config_data == expected_config
140
-
141
- with open (pre_commit_config_filename , "r" ) as pre_commit_file :
142
- pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
143
- assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
164
+ check_cz_config (default_choice )
165
+ check_pre_commit_config ([cz_hook_config ])
144
166
145
167
def test_empty_pre_commit_config (_ , default_choice , tmpdir , config ):
146
168
with tmpdir .as_cwd ():
147
169
p = tmpdir .join (pre_commit_config_filename )
148
170
p .write ("" )
149
171
150
172
commands .Init (config )()
151
-
152
- with open (default_choice , "r" ) as file :
153
- if "json" in default_choice :
154
- assert json .load (file ) == EXPECTED_DICT_CONFIG
155
- elif "yaml" in default_choice :
156
- assert (
157
- yaml .load (file , Loader = yaml .FullLoader ) == EXPECTED_DICT_CONFIG
158
- )
159
- else :
160
- config_data = file .read ()
161
- assert config_data == expected_config
162
-
163
- with open (pre_commit_config_filename , "r" ) as pre_commit_file :
164
- pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
165
- assert pre_commit_config_data == {"repos" : [cz_hook_config ]}
173
+ check_cz_config (default_choice )
174
+ check_pre_commit_config ([cz_hook_config ])
166
175
167
176
def test_pre_commit_config_without_cz_hook (_ , default_choice , tmpdir , config ):
168
177
existing_hook_config = {
@@ -176,47 +185,18 @@ def test_pre_commit_config_without_cz_hook(_, default_choice, tmpdir, config):
176
185
p .write (yaml .safe_dump ({"repos" : [existing_hook_config ]}))
177
186
178
187
commands .Init (config )()
179
-
180
- with open (default_choice , "r" ) as file :
181
- if "json" in default_choice :
182
- assert json .load (file ) == EXPECTED_DICT_CONFIG
183
- elif "yaml" in default_choice :
184
- assert (
185
- yaml .load (file , Loader = yaml .FullLoader ) == EXPECTED_DICT_CONFIG
186
- )
187
- else :
188
- config_data = file .read ()
189
- assert config_data == expected_config
190
-
191
- with open (pre_commit_config_filename , "r" ) as pre_commit_file :
192
- pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
193
- assert pre_commit_config_data == {
194
- "repos" : [existing_hook_config , cz_hook_config ]
195
- }
188
+ check_cz_config (default_choice )
189
+ check_pre_commit_config ([existing_hook_config , cz_hook_config ])
196
190
197
191
def test_cz_hook_exists_in_pre_commit_config (_ , default_choice , tmpdir , config ):
198
192
with tmpdir .as_cwd ():
199
193
p = tmpdir .join (pre_commit_config_filename )
200
194
p .write (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
201
195
202
196
commands .Init (config )()
203
-
204
- with open (default_choice , "r" ) as file :
205
- if "json" in default_choice :
206
- assert json .load (file ) == EXPECTED_DICT_CONFIG
207
- elif "yaml" in default_choice :
208
- assert (
209
- yaml .load (file , Loader = yaml .FullLoader ) == EXPECTED_DICT_CONFIG
210
- )
211
- else :
212
- config_data = file .read ()
213
- assert config_data == expected_config
214
-
215
- with open (pre_commit_config_filename , "r" ) as pre_commit_file :
216
- pre_commit_config_data = yaml .safe_load (pre_commit_file .read ())
217
-
197
+ check_cz_config (default_choice )
218
198
# check that config is not duplicated
219
- assert pre_commit_config_data == { "repos" : [cz_hook_config ]}
199
+ check_pre_commit_config ( [cz_hook_config ])
220
200
221
201
222
202
class TestNoPreCommitInstalled :
0 commit comments