Skip to content

Commit 9f30d12

Browse files
committed
refactor(init): improve test coverage
1 parent 2a7e8ed commit 9f30d12

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

commitizen/commands/init.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,8 @@ def _gen_pre_commit_cmd(self, hook_types: list[str]) -> str:
294294
"""Generate pre-commit command according to given hook types"""
295295
if not hook_types:
296296
raise ValueError("At least 1 hook type should be provided.")
297-
return "pre-commit install " + " ".join(
298-
f"--hook-type {ty}" for ty in hook_types
299-
)
297+
hook_str = " ".join(f"--hook-type {ty}" for ty in hook_types)
298+
return f"pre-commit install {hook_str}"
300299

301300
def _init_config(self, config_path: str):
302301
if "toml" in config_path:

tests/commands/test_init_command.py

+101
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,27 @@ def test_init_with_confirmed_tag_format(config, mocker: MockFixture, tmpdir):
292292

293293
def test_init_with_no_existing_tags(config, mocker: MockFixture, tmpdir):
294294
mocker.patch("commitizen.commands.init.get_tag_names", return_value=[])
295+
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value="v1.0.0")
296+
mocker.patch(
297+
"questionary.select",
298+
side_effect=[
299+
FakeQuestion("pyproject.toml"),
300+
FakeQuestion("cz_conventional_commits"),
301+
FakeQuestion("commitizen"),
302+
FakeQuestion("semver"),
303+
],
304+
)
305+
mocker.patch("questionary.confirm", return_value=FakeQuestion(False))
306+
mocker.patch("questionary.text", return_value=FakeQuestion("$version"))
307+
mocker.patch("questionary.checkbox", return_value=FakeQuestion(None))
308+
309+
with tmpdir.as_cwd():
310+
commands.Init(config)()
311+
with open("pyproject.toml", encoding="utf-8") as toml_file:
312+
assert 'version = "0.0.1"' in toml_file.read()
313+
314+
315+
def test_init_with_no_existing_latest_tag(config, mocker: MockFixture, tmpdir):
295316
mocker.patch("commitizen.commands.init.get_latest_tag_name", return_value=None)
296317
mocker.patch(
297318
"questionary.select",
@@ -358,3 +379,83 @@ def test_init_with_non_commitizen_version_provider(config, mocker: MockFixture,
358379
assert (
359380
'version = "0.0.1"' not in content
360381
) # Version should not be set for non-commitizen providers
382+
383+
384+
class TestVersionProviderDefault:
385+
def test_default_for_python_poetry(self, config, mocker: MockFixture):
386+
mock_project_info = mocker.Mock()
387+
mock_project_info.is_python = True
388+
mock_project_info.is_python_poetry = True
389+
mock_project_info.is_python_uv = False
390+
mocker.patch(
391+
"commitizen.commands.init.ProjectInfo", return_value=mock_project_info
392+
)
393+
init = commands.Init(config)
394+
assert init._version_provider_default_val == "poetry"
395+
396+
def test_default_for_python_uv(self, config, mocker: MockFixture):
397+
mock_project_info = mocker.Mock()
398+
mock_project_info.is_python = True
399+
mock_project_info.is_python_poetry = False
400+
mock_project_info.is_python_uv = True
401+
mocker.patch(
402+
"commitizen.commands.init.ProjectInfo", return_value=mock_project_info
403+
)
404+
init = commands.Init(config)
405+
assert init._version_provider_default_val == "uv"
406+
407+
def test_default_for_python_pep621(self, config, mocker: MockFixture):
408+
mock_project_info = mocker.Mock()
409+
mock_project_info.is_python = True
410+
mock_project_info.is_python_poetry = False
411+
mock_project_info.is_python_uv = False
412+
mocker.patch(
413+
"commitizen.commands.init.ProjectInfo", return_value=mock_project_info
414+
)
415+
init = commands.Init(config)
416+
assert init._version_provider_default_val == "pep621"
417+
418+
def test_default_for_rust_cargo(self, config, mocker: MockFixture):
419+
mock_project_info = mocker.Mock()
420+
mock_project_info.is_python = False
421+
mock_project_info.is_rust_cargo = True
422+
mocker.patch(
423+
"commitizen.commands.init.ProjectInfo", return_value=mock_project_info
424+
)
425+
init = commands.Init(config)
426+
assert init._version_provider_default_val == "cargo"
427+
428+
def test_default_for_npm_package(self, config, mocker: MockFixture):
429+
mock_project_info = mocker.Mock()
430+
mock_project_info.is_python = False
431+
mock_project_info.is_rust_cargo = False
432+
mock_project_info.is_npm_package = True
433+
mocker.patch(
434+
"commitizen.commands.init.ProjectInfo", return_value=mock_project_info
435+
)
436+
init = commands.Init(config)
437+
assert init._version_provider_default_val == "npm"
438+
439+
def test_default_for_php_composer(self, config, mocker: MockFixture):
440+
mock_project_info = mocker.Mock()
441+
mock_project_info.is_python = False
442+
mock_project_info.is_rust_cargo = False
443+
mock_project_info.is_npm_package = False
444+
mock_project_info.is_php_composer = True
445+
mocker.patch(
446+
"commitizen.commands.init.ProjectInfo", return_value=mock_project_info
447+
)
448+
init = commands.Init(config)
449+
assert init._version_provider_default_val == "composer"
450+
451+
def test_default_fallback_to_commitizen(self, config, mocker: MockFixture):
452+
mock_project_info = mocker.Mock()
453+
mock_project_info.is_python = False
454+
mock_project_info.is_rust_cargo = False
455+
mock_project_info.is_npm_package = False
456+
mock_project_info.is_php_composer = False
457+
mocker.patch(
458+
"commitizen.commands.init.ProjectInfo", return_value=mock_project_info
459+
)
460+
init = commands.Init(config)
461+
assert init._version_provider_default_val == "commitizen"

0 commit comments

Comments
 (0)