11
11
12
12
from commitizen import cli , commands
13
13
from commitizen .__version__ import __version__
14
+ from commitizen .config .base_config import BaseConfig
14
15
from commitizen .exceptions import InitFailedError , NoAnswersError
15
16
from tests .utils import skip_below_py_3_10
16
17
@@ -58,7 +59,9 @@ def unsafe_ask(self):
58
59
}
59
60
60
61
61
- def test_init_without_setup_pre_commit_hook (tmpdir , mocker : MockFixture , config ):
62
+ def test_init_without_setup_pre_commit_hook (
63
+ tmpdir , mocker : MockFixture , config : BaseConfig
64
+ ):
62
65
mocker .patch (
63
66
"questionary.select" ,
64
67
side_effect = [
@@ -72,7 +75,6 @@ def test_init_without_setup_pre_commit_hook(tmpdir, mocker: MockFixture, config)
72
75
mocker .patch ("questionary.text" , return_value = FakeQuestion ("$version" ))
73
76
# Return None to skip hook installation
74
77
mocker .patch ("questionary.checkbox" , return_value = FakeQuestion (None ))
75
-
76
78
with tmpdir .as_cwd ():
77
79
commands .Init (config )()
78
80
@@ -83,7 +85,7 @@ def test_init_without_setup_pre_commit_hook(tmpdir, mocker: MockFixture, config)
83
85
assert not os .path .isfile (pre_commit_config_filename )
84
86
85
87
86
- def test_init_when_config_already_exists (config , capsys ):
88
+ def test_init_when_config_already_exists (config : BaseConfig , capsys ):
87
89
# Set config path
88
90
path = os .sep .join (["tests" , "pyproject.toml" ])
89
91
config .path = path
@@ -93,7 +95,7 @@ def test_init_when_config_already_exists(config, capsys):
93
95
assert captured .out == f"Config file { path } already exists\n "
94
96
95
97
96
- def test_init_without_choosing_tag (config , mocker : MockFixture , tmpdir ):
98
+ def test_init_without_choosing_tag (config : BaseConfig , mocker : MockFixture , tmpdir ):
97
99
mocker .patch (
98
100
"commitizen.commands.init.get_tag_names" , return_value = ["0.0.2" , "0.0.1" ]
99
101
)
@@ -115,7 +117,7 @@ def test_init_without_choosing_tag(config, mocker: MockFixture, tmpdir):
115
117
commands .Init (config )()
116
118
117
119
118
- def test_executed_pre_commit_command (config ):
120
+ def test_executed_pre_commit_command (config : BaseConfig ):
119
121
init = commands .Init (config )
120
122
expected_cmd = "pre-commit install --hook-type commit-msg --hook-type pre-push"
121
123
assert init ._gen_pre_commit_cmd (["commit-msg" , "pre-push" ]) == expected_cmd
@@ -155,17 +157,14 @@ def default_choice(request, mocker: MockFixture):
155
157
yield request .param
156
158
157
159
158
- def check_cz_config (config : str ):
160
+ def check_cz_config (config_filepath : str ):
159
161
"""
160
162
Check the content of commitizen config is as expected
161
-
162
- Args:
163
- config: The config path
164
163
"""
165
- with open (config ) as file :
166
- if "json" in config :
164
+ with open (config_filepath ) as file :
165
+ if "json" in config_filepath :
167
166
assert json .load (file ) == EXPECTED_DICT_CONFIG
168
- elif "yaml" in config :
167
+ elif "yaml" in config_filepath :
169
168
assert yaml .load (file , Loader = yaml .FullLoader ) == EXPECTED_DICT_CONFIG
170
169
else :
171
170
config_data = file .read ()
@@ -183,13 +182,17 @@ def check_pre_commit_config(expected: list[dict[str, Any]]):
183
182
184
183
@pytest .mark .usefixtures ("pre_commit_installed" )
185
184
class TestPreCommitCases :
186
- def test_no_existing_pre_commit_config (_ , default_choice , tmpdir , config ):
185
+ def test_no_existing_pre_commit_config (
186
+ _ , default_choice : str , tmpdir , config : BaseConfig
187
+ ):
187
188
with tmpdir .as_cwd ():
188
189
commands .Init (config )()
189
190
check_cz_config (default_choice )
190
191
check_pre_commit_config ([cz_hook_config ])
191
192
192
- def test_empty_pre_commit_config (_ , default_choice , tmpdir , config ):
193
+ def test_empty_pre_commit_config (
194
+ _ , default_choice : str , tmpdir , config : BaseConfig
195
+ ):
193
196
with tmpdir .as_cwd ():
194
197
p = tmpdir .join (pre_commit_config_filename )
195
198
p .write ("" )
@@ -198,7 +201,9 @@ def test_empty_pre_commit_config(_, default_choice, tmpdir, config):
198
201
check_cz_config (default_choice )
199
202
check_pre_commit_config ([cz_hook_config ])
200
203
201
- def test_pre_commit_config_without_cz_hook (_ , default_choice , tmpdir , config ):
204
+ def test_pre_commit_config_without_cz_hook (
205
+ _ , default_choice : str , tmpdir , config : BaseConfig
206
+ ):
202
207
existing_hook_config = {
203
208
"repo" : "https://github.com/pre-commit/pre-commit-hooks" ,
204
209
"rev" : "v1.2.3" ,
@@ -213,7 +218,9 @@ def test_pre_commit_config_without_cz_hook(_, default_choice, tmpdir, config):
213
218
check_cz_config (default_choice )
214
219
check_pre_commit_config ([existing_hook_config , cz_hook_config ])
215
220
216
- def test_cz_hook_exists_in_pre_commit_config (_ , default_choice , tmpdir , config ):
221
+ def test_cz_hook_exists_in_pre_commit_config (
222
+ _ , default_choice : str , tmpdir , config : BaseConfig
223
+ ):
217
224
with tmpdir .as_cwd ():
218
225
p = tmpdir .join (pre_commit_config_filename )
219
226
p .write (yaml .safe_dump ({"repos" : [cz_hook_config ]}))
@@ -226,7 +233,7 @@ def test_cz_hook_exists_in_pre_commit_config(_, default_choice, tmpdir, config):
226
233
227
234
class TestNoPreCommitInstalled :
228
235
def test_pre_commit_not_installed (
229
- _ , mocker : MockFixture , config , default_choice , tmpdir
236
+ _ , mocker : MockFixture , config : BaseConfig , default_choice : str , tmpdir
230
237
):
231
238
# Assume `pre-commit` is not installed
232
239
mocker .patch (
@@ -238,7 +245,7 @@ def test_pre_commit_not_installed(
238
245
commands .Init (config )()
239
246
240
247
def test_pre_commit_exec_failed (
241
- _ , mocker : MockFixture , config , default_choice , tmpdir
248
+ _ , mocker : MockFixture , config : BaseConfig , default_choice : str , tmpdir
242
249
):
243
250
# Assume `pre-commit` is installed
244
251
mocker .patch (
@@ -256,29 +263,29 @@ def test_pre_commit_exec_failed(
256
263
257
264
258
265
class TestAskTagFormat :
259
- def test_confirm_v_tag_format (self , mocker : MockFixture , config ):
266
+ def test_confirm_v_tag_format (self , mocker : MockFixture , config : BaseConfig ):
260
267
init = commands .Init (config )
261
268
mocker .patch ("questionary.confirm" , return_value = FakeQuestion (True ))
262
269
263
270
result = init ._ask_tag_format ("v1.0.0" )
264
271
assert result == r"v$version"
265
272
266
- def test_reject_v_tag_format (self , mocker : MockFixture , config ):
273
+ def test_reject_v_tag_format (self , mocker : MockFixture , config : BaseConfig ):
267
274
init = commands .Init (config )
268
275
mocker .patch ("questionary.confirm" , return_value = FakeQuestion (False ))
269
276
mocker .patch ("questionary.text" , return_value = FakeQuestion ("custom-$version" ))
270
277
271
278
result = init ._ask_tag_format ("v1.0.0" )
272
279
assert result == "custom-$version"
273
280
274
- def test_non_v_tag_format (self , mocker : MockFixture , config ):
281
+ def test_non_v_tag_format (self , mocker : MockFixture , config : BaseConfig ):
275
282
init = commands .Init (config )
276
283
mocker .patch ("questionary.text" , return_value = FakeQuestion ("custom-$version" ))
277
284
278
285
result = init ._ask_tag_format ("1.0.0" )
279
286
assert result == "custom-$version"
280
287
281
- def test_empty_input_returns_default (self , mocker : MockFixture , config ):
288
+ def test_empty_input_returns_default (self , mocker : MockFixture , config : BaseConfig ):
282
289
init = commands .Init (config )
283
290
mocker .patch ("questionary.confirm" , return_value = FakeQuestion (False ))
284
291
mocker .patch ("questionary.text" , return_value = FakeQuestion ("" ))
@@ -300,7 +307,9 @@ def test_init_command_shows_description_when_use_help_option(
300
307
file_regression .check (out , extension = ".txt" )
301
308
302
309
303
- def test_init_with_confirmed_tag_format (config , mocker : MockFixture , tmpdir ):
310
+ def test_init_with_confirmed_tag_format (
311
+ config : BaseConfig , mocker : MockFixture , tmpdir
312
+ ):
304
313
mocker .patch (
305
314
"commitizen.commands.init.get_tag_names" , return_value = ["v0.0.2" , "v0.0.1" ]
306
315
)
@@ -324,7 +333,7 @@ def test_init_with_confirmed_tag_format(config, mocker: MockFixture, tmpdir):
324
333
assert 'tag_format = "v$version"' in toml_file .read ()
325
334
326
335
327
- def test_init_with_no_existing_tags (config , mocker : MockFixture , tmpdir ):
336
+ def test_init_with_no_existing_tags (config : BaseConfig , mocker : MockFixture , tmpdir ):
328
337
mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = [])
329
338
mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
330
339
mocker .patch (
@@ -346,7 +355,9 @@ def test_init_with_no_existing_tags(config, mocker: MockFixture, tmpdir):
346
355
assert 'version = "0.0.1"' in toml_file .read ()
347
356
348
357
349
- def test_init_with_no_existing_latest_tag (config , mocker : MockFixture , tmpdir ):
358
+ def test_init_with_no_existing_latest_tag (
359
+ config : BaseConfig , mocker : MockFixture , tmpdir
360
+ ):
350
361
mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = None )
351
362
mocker .patch (
352
363
"questionary.select" ,
@@ -367,7 +378,7 @@ def test_init_with_no_existing_latest_tag(config, mocker: MockFixture, tmpdir):
367
378
assert 'version = "0.0.1"' in toml_file .read ()
368
379
369
380
370
- def test_init_with_existing_tags (config , mocker : MockFixture , tmpdir ):
381
+ def test_init_with_existing_tags (config : BaseConfig , mocker : MockFixture , tmpdir ):
371
382
expected_tags = ["v1.0.0" , "v0.9.0" , "v0.8.0" ]
372
383
mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = expected_tags )
373
384
mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
@@ -391,7 +402,7 @@ def test_init_with_existing_tags(config, mocker: MockFixture, tmpdir):
391
402
assert 'version = "1.0.0"' in toml_file .read ()
392
403
393
404
394
- def test_init_with_valid_tag_selection (config , mocker : MockFixture , tmpdir ):
405
+ def test_init_with_valid_tag_selection (config : BaseConfig , mocker : MockFixture , tmpdir ):
395
406
expected_tags = ["v1.0.0" , "v0.9.0" , "v0.8.0" ]
396
407
mocker .patch ("commitizen.commands.init.get_tag_names" , return_value = expected_tags )
397
408
mocker .patch ("commitizen.commands.init.get_latest_tag_name" , return_value = "v1.0.0" )
@@ -422,7 +433,7 @@ def test_init_with_valid_tag_selection(config, mocker: MockFixture, tmpdir):
422
433
assert 'version_scheme = "semver"' in content
423
434
424
435
425
- def test_init_configuration_settings (tmpdir , mocker : MockFixture , config ):
436
+ def test_init_configuration_settings (tmpdir , mocker : MockFixture , config : BaseConfig ):
426
437
"""Test that all configuration settings are properly initialized."""
427
438
mocker .patch (
428
439
"questionary.select" ,
@@ -452,7 +463,9 @@ def test_init_configuration_settings(tmpdir, mocker: MockFixture, config):
452
463
assert "major_version_zero = true" in config_data
453
464
454
465
455
- def test_init_configuration_with_version_provider (tmpdir , mocker : MockFixture , config ):
466
+ def test_init_configuration_with_version_provider (
467
+ tmpdir , mocker : MockFixture , config : BaseConfig
468
+ ):
456
469
"""Test configuration initialization with a different version provider."""
457
470
mocker .patch (
458
471
"questionary.select" ,
0 commit comments