Skip to content

Commit 8d0a92a

Browse files
committed
test(add-tests-to-improve-code-coverage-for-commit.py): Add missing test cases to cover previously untested code paths in the commit command:
- Test ValueError with CzException context in _handle_questionary_prompt - Test manual_edit when editor executable is not found - Test multiline question handling when result is None with/without default values
1 parent 3b7b402 commit 8d0a92a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

tests/commands/test_commit_command.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,24 @@ def test_commit_when_customized_expected_raised(config, mocker: MockFixture, cap
327327
assert "This is the root custom err" in str(excinfo.value)
328328

329329

330+
@pytest.mark.usefixtures("staging_is_clean")
331+
def test_commit_when_customized_error_in_handle_questionary_prompt(
332+
config, mocker: MockFixture
333+
):
334+
"""Test ValueError with CzException context in _handle_questionary_prompt."""
335+
_err = ValueError("questionary error")
336+
_err.__context__ = CzException("Custom validation failed")
337+
338+
handle_prompt_mock = mocker.patch(
339+
"commitizen.commands.commit._handle_questionary_prompt"
340+
)
341+
handle_prompt_mock.side_effect = _err
342+
343+
with pytest.raises(ValueError):
344+
commit_cmd = commands.Commit(config, {})
345+
commit_cmd()
346+
347+
330348
@pytest.mark.usefixtures("staging_is_clean")
331349
def test_commit_when_non_customized_expected_raised(
332350
config, mocker: MockFixture, capsys
@@ -449,6 +467,18 @@ def test_manual_edit(editor, config, mocker: MockFixture, tmp_path):
449467
assert edited_message == test_message.strip()
450468

451469

470+
@pytest.mark.usefixtures("staging_is_clean")
471+
def test_manual_edit_editor_not_found(config, mocker: MockFixture):
472+
"""Test manual_edit raises error when editor executable is not found."""
473+
mocker.patch("commitizen.git.get_core_editor", return_value="nonexistent_editor")
474+
mocker.patch("shutil.which", return_value=None)
475+
476+
commit_cmd = commands.Commit(config, {"edit": True})
477+
478+
with pytest.raises(RuntimeError, match="Editor 'nonexistent_editor' not found."):
479+
commit_cmd.manual_edit("test message")
480+
481+
452482
@skip_below_py_3_13
453483
def test_commit_command_shows_description_when_use_help_option(
454484
mocker: MockFixture, capsys, file_regression

tests/commands/test_commit_multiline.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,45 @@ def failing_then_succeeding_filter(text):
150150
assert mock_text.call_count == 2 # Prompt shown twice
151151
mock_error.assert_called_once() # Error message displayed once
152152

153+
@patch("commitizen.out.info")
154+
@patch("questionary.text")
155+
def test_result_none_uses_default(self, mock_text, _mock_info, mock_cz_style):
156+
"""Test that when result is None, default value is used."""
157+
mock_instance = Mock()
158+
mock_instance.unsafe_ask.return_value = None
159+
mock_text.return_value = mock_instance
160+
161+
question = {
162+
"type": "input",
163+
"name": "scope",
164+
"message": "Scope",
165+
"multiline": True,
166+
"default": "default_value",
167+
}
168+
169+
result = _handle_multiline_question(question, mock_cz_style)
170+
171+
assert result == {"scope": "default_value"}
172+
173+
@patch("commitizen.out.info")
174+
@patch("questionary.text")
175+
def test_result_none_no_default(self, mock_text, _mock_info, mock_cz_style):
176+
"""Test that when result is None and no default, empty string is used."""
177+
mock_instance = Mock()
178+
mock_instance.unsafe_ask.return_value = None
179+
mock_text.return_value = mock_instance
180+
181+
question = {
182+
"type": "input",
183+
"name": "scope",
184+
"message": "Scope",
185+
"multiline": True,
186+
}
187+
188+
result = _handle_multiline_question(question, mock_cz_style)
189+
190+
assert result == {"scope": ""}
191+
153192

154193
class TestKeyBindings:
155194
"""Test key binding setup."""

0 commit comments

Comments
 (0)