Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b76301d

Browse files
bearomorphismLee-W
authored andcommitted
test(Init): add type hint and rename variable
1 parent cc981fc commit b76301d

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

tests/commands/test_init_command.py

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from commitizen import cli, commands
1313
from commitizen.__version__ import __version__
14+
from commitizen.config.base_config import BaseConfig
1415
from commitizen.exceptions import InitFailedError, NoAnswersError
1516
from tests.utils import skip_below_py_3_10
1617

@@ -58,7 +59,9 @@ def unsafe_ask(self):
5859
}
5960

6061

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+
):
6265
mocker.patch(
6366
"questionary.select",
6467
side_effect=[
@@ -72,7 +75,6 @@ def test_init_without_setup_pre_commit_hook(tmpdir, mocker: MockFixture, config)
7275
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
7376
# Return None to skip hook installation
7477
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))
75-
7678
with tmpdir.as_cwd():
7779
commands.Init(config)()
7880

@@ -83,7 +85,7 @@ def test_init_without_setup_pre_commit_hook(tmpdir, mocker: MockFixture, config)
8385
assert not os.path.isfile(pre_commit_config_filename)
8486

8587

86-
def test_init_when_config_already_exists(config, capsys):
88+
def test_init_when_config_already_exists(config: BaseConfig, capsys):
8789
# Set config path
8890
path = os.sep.join(["tests", "pyproject.toml"])
8991
config.path = path
@@ -93,7 +95,7 @@ def test_init_when_config_already_exists(config, capsys):
9395
assert captured.out == f"Config file {path} already exists\n"
9496

9597

96-
def test_init_without_choosing_tag(config, mocker: MockFixture, tmpdir):
98+
def test_init_without_choosing_tag(config: BaseConfig, mocker: MockFixture, tmpdir):
9799
mocker.patch(
98100
"commitizen.commands.init.get_tag_names", return_value=["0.0.2", "0.0.1"]
99101
)
@@ -115,7 +117,7 @@ def test_init_without_choosing_tag(config, mocker: MockFixture, tmpdir):
115117
commands.Init(config)()
116118

117119

118-
def test_executed_pre_commit_command(config):
120+
def test_executed_pre_commit_command(config: BaseConfig):
119121
init = commands.Init(config)
120122
expected_cmd = "pre-commit install --hook-type commit-msg --hook-type pre-push"
121123
assert init._gen_pre_commit_cmd(["commit-msg", "pre-push"]) == expected_cmd
@@ -155,17 +157,14 @@ def default_choice(request, mocker: MockFixture):
155157
yield request.param
156158

157159

158-
def check_cz_config(config: str):
160+
def check_cz_config(config_filepath: str):
159161
"""
160162
Check the content of commitizen config is as expected
161-
162-
Args:
163-
config: The config path
164163
"""
165-
with open(config) as file:
166-
if "json" in config:
164+
with open(config_filepath) as file:
165+
if "json" in config_filepath:
167166
assert json.load(file) == EXPECTED_DICT_CONFIG
168-
elif "yaml" in config:
167+
elif "yaml" in config_filepath:
169168
assert yaml.load(file, Loader=yaml.FullLoader) == EXPECTED_DICT_CONFIG
170169
else:
171170
config_data = file.read()
@@ -183,13 +182,17 @@ def check_pre_commit_config(expected: list[dict[str, Any]]):
183182

184183
@pytest.mark.usefixtures("pre_commit_installed")
185184
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+
):
187188
with tmpdir.as_cwd():
188189
commands.Init(config)()
189190
check_cz_config(default_choice)
190191
check_pre_commit_config([cz_hook_config])
191192

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+
):
193196
with tmpdir.as_cwd():
194197
p = tmpdir.join(pre_commit_config_filename)
195198
p.write("")
@@ -198,7 +201,9 @@ def test_empty_pre_commit_config(_, default_choice, tmpdir, config):
198201
check_cz_config(default_choice)
199202
check_pre_commit_config([cz_hook_config])
200203

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+
):
202207
existing_hook_config = {
203208
"repo": "https://github.com/pre-commit/pre-commit-hooks",
204209
"rev": "v1.2.3",
@@ -213,7 +218,9 @@ def test_pre_commit_config_without_cz_hook(_, default_choice, tmpdir, config):
213218
check_cz_config(default_choice)
214219
check_pre_commit_config([existing_hook_config, cz_hook_config])
215220

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+
):
217224
with tmpdir.as_cwd():
218225
p = tmpdir.join(pre_commit_config_filename)
219226
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):
226233

227234
class TestNoPreCommitInstalled:
228235
def test_pre_commit_not_installed(
229-
_, mocker: MockFixture, config, default_choice, tmpdir
236+
_, mocker: MockFixture, config: BaseConfig, default_choice: str, tmpdir
230237
):
231238
# Assume `pre-commit` is not installed
232239
mocker.patch(
@@ -238,7 +245,7 @@ def test_pre_commit_not_installed(
238245
commands.Init(config)()
239246

240247
def test_pre_commit_exec_failed(
241-
_, mocker: MockFixture, config, default_choice, tmpdir
248+
_, mocker: MockFixture, config: BaseConfig, default_choice: str, tmpdir
242249
):
243250
# Assume `pre-commit` is installed
244251
mocker.patch(
@@ -256,29 +263,29 @@ def test_pre_commit_exec_failed(
256263

257264

258265
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):
260267
init = commands.Init(config)
261268
mocker.patch("questionary.confirm", return_value=FakeQuestion(True))
262269

263270
result = init._ask_tag_format("v1.0.0")
264271
assert result == r"v$version"
265272

266-
def test_reject_v_tag_format(self, mocker: MockFixture, config):
273+
def test_reject_v_tag_format(self, mocker: MockFixture, config: BaseConfig):
267274
init = commands.Init(config)
268275
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
269276
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version"))
270277

271278
result = init._ask_tag_format("v1.0.0")
272279
assert result == "custom-$version"
273280

274-
def test_non_v_tag_format(self, mocker: MockFixture, config):
281+
def test_non_v_tag_format(self, mocker: MockFixture, config: BaseConfig):
275282
init = commands.Init(config)
276283
mocker.patch("questionary.text", return_value=FakeQuestion("custom-$version"))
277284

278285
result = init._ask_tag_format("1.0.0")
279286
assert result == "custom-$version"
280287

281-
def test_empty_input_returns_default(self, mocker: MockFixture, config):
288+
def test_empty_input_returns_default(self, mocker: MockFixture, config: BaseConfig):
282289
init = commands.Init(config)
283290
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
284291
mocker.patch("questionary.text", return_value=FakeQuestion(""))
@@ -300,7 +307,9 @@ def test_init_command_shows_description_when_use_help_option(
300307
file_regression.check(out, extension=".txt")
301308

302309

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+
):
304313
mocker.patch(
305314
"commitizen.commands.init.get_tag_names", return_value=["v0.0.2", "v0.0.1"]
306315
)
@@ -324,7 +333,7 @@ def test_init_with_confirmed_tag_format(config, mocker: MockFixture, tmpdir):
324333
assert 'tag_format = "v$version"' in toml_file.read()
325334

326335

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):
328337
mocker.patch("commitizen.commands.init.get_tag_names", return_value=[])
329338
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0")
330339
mocker.patch(
@@ -346,7 +355,9 @@ def test_init_with_no_existing_tags(config, mocker: MockFixture, tmpdir):
346355
assert 'version = "0.0.1"' in toml_file.read()
347356

348357

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+
):
350361
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value=None)
351362
mocker.patch(
352363
"questionary.select",
@@ -367,7 +378,7 @@ def test_init_with_no_existing_latest_tag(config, mocker: MockFixture, tmpdir):
367378
assert 'version = "0.0.1"' in toml_file.read()
368379

369380

370-
def test_init_with_existing_tags(config, mocker: MockFixture, tmpdir):
381+
def test_init_with_existing_tags(config: BaseConfig, mocker: MockFixture, tmpdir):
371382
expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"]
372383
mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags)
373384
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):
391402
assert 'version = "1.0.0"' in toml_file.read()
392403

393404

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):
395406
expected_tags = ["v1.0.0", "v0.9.0", "v0.8.0"]
396407
mocker.patch("commitizen.commands.init.get_tag_names", return_value=expected_tags)
397408
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):
422433
assert 'version_scheme = "semver"' in content
423434

424435

425-
def test_init_configuration_settings(tmpdir, mocker: MockFixture, config):
436+
def test_init_configuration_settings(tmpdir, mocker: MockFixture, config: BaseConfig):
426437
"""Test that all configuration settings are properly initialized."""
427438
mocker.patch(
428439
"questionary.select",
@@ -452,7 +463,9 @@ def test_init_configuration_settings(tmpdir, mocker: MockFixture, config):
452463
assert "major_version_zero = true" in config_data
453464

454465

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+
):
456469
"""Test configuration initialization with a different version provider."""
457470
mocker.patch(
458471
"questionary.select",

0 commit comments

Comments
 (0)