Skip to content

Commit bb343bc

Browse files
committed
test(init): cover installer missing at install time
The InitFailedError branch in _ask_hook_installer was flagged by codecov as the only uncovered line of the PR. Add a test where the installer is available during the hook-type question but disappears before the install step, so the guard is exercised instead of removed.
1 parent c9ea08e commit bb343bc

1 file changed

Lines changed: 27 additions & 1 deletion

File tree

tests/commands/test_init_command.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from commitizen import cmd, commands
1111
from commitizen.__version__ import __version__
12-
from commitizen.exceptions import NoAnswersError
12+
from commitizen.exceptions import InitFailedError, NoAnswersError
1313

1414
if TYPE_CHECKING:
1515
from pytest_mock import MockFixture
@@ -358,6 +358,32 @@ def test_uses_pre_commit_when_only_pre_commit_is_installed(
358358
]
359359
)
360360

361+
def test_fails_when_installer_disappears_between_prompt_and_install(
362+
self, mocker: MockFixture, config: BaseConfig, tmp_path, monkeypatch
363+
):
364+
_init_hook_answers(mocker)
365+
# First call (during _ask_hook_types) finds pre-commit; second call
366+
# (during _ask_hook_installer, after the config is written) finds none,
367+
# e.g. the tool was uninstalled or the PATH changed in between.
368+
mocker.patch(
369+
"commitizen.project_info.available_hook_installers",
370+
side_effect=[["pre-commit"], []],
371+
)
372+
run = mocker.patch(
373+
"commitizen.cmd.run",
374+
return_value=cmd.Command("", "", b"", b"", 0),
375+
)
376+
monkeypatch.chdir(tmp_path)
377+
378+
with pytest.raises(InitFailedError):
379+
commands.Init(config)()
380+
381+
# Other subprocess calls (git describe, git config) may still happen,
382+
# but no hook installer may have been invoked.
383+
assert not any(
384+
"--hook-type" in " ".join(call.args[0]) for call in run.call_args_list
385+
)
386+
361387

362388
class TestAskTagFormat:
363389
def test_confirm_v_tag_format(self, mocker: MockFixture, config: BaseConfig):

0 commit comments

Comments
 (0)