Skip to content

Commit 6d7bba1

Browse files
committed
test(commit): add coverage for message length limit precedence
1 parent 2be6bcb commit 6d7bba1

File tree

1 file changed

+48
-16
lines changed

1 file changed

+48
-16
lines changed

tests/commands/test_commit_command.py

Lines changed: 48 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -336,34 +336,66 @@ def test_commit_when_nothing_added_to_commit(config, mocker: MockFixture, out):
336336
error_mock.assert_called_once_with(out)
337337

338338

339-
@pytest.mark.usefixtures("staging_is_clean", "commit_mock")
340-
def test_commit_command_with_config_message_length_limit(
341-
config, success_mock: MockType, prompt_mock_feat: MockType
342-
):
339+
def _commit_first_line_len(prompt_mock_feat: MockType) -> int:
343340
prefix = prompt_mock_feat.return_value["prefix"]
344341
subject = prompt_mock_feat.return_value["subject"]
345-
message_length = len(f"{prefix}: {subject}")
342+
scope = prompt_mock_feat.return_value["scope"]
343+
344+
formatted_scope = f"({scope})" if scope else ""
345+
first_line = f"{prefix}{formatted_scope}: {subject}"
346+
return len(first_line)
346347

347-
commands.Commit(config, {"message_length_limit": message_length})()
348+
349+
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")
350+
def test_commit_message_length_cli_at_limit_succeeds(
351+
config, success_mock: MockType, prompt_mock_feat: MockType
352+
):
353+
message_len = _commit_first_line_len(prompt_mock_feat)
354+
commands.Commit(config, {"message_length_limit": message_len})()
348355
success_mock.assert_called_once()
349356

357+
358+
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")
359+
def test_commit_message_length_cli_below_limit_raises(
360+
config, prompt_mock_feat: MockType
361+
):
362+
message_len = _commit_first_line_len(prompt_mock_feat)
350363
with pytest.raises(CommitMessageLengthExceededError):
351-
commands.Commit(config, {"message_length_limit": message_length - 1})()
364+
commands.Commit(config, {"message_length_limit": message_len - 1})()
352365

353-
config.settings["message_length_limit"] = message_length
354-
success_mock.reset_mock()
355-
commands.Commit(config, {})()
366+
367+
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")
368+
def test_commit_message_length_uses_config_when_cli_unset(
369+
config, success_mock: MockType, prompt_mock_feat: MockType
370+
):
371+
config.settings["message_length_limit"] = _commit_first_line_len(prompt_mock_feat)
372+
commands.Commit(config, {"message_length_limit": None})()
356373
success_mock.assert_called_once()
357374

358-
config.settings["message_length_limit"] = message_length - 1
375+
376+
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")
377+
def test_commit_message_length_config_exceeded_when_cli_unset(
378+
config, prompt_mock_feat: MockType
379+
):
380+
config.settings["message_length_limit"] = _commit_first_line_len(prompt_mock_feat) - 1
359381
with pytest.raises(CommitMessageLengthExceededError):
360-
commands.Commit(config, {})()
382+
commands.Commit(config, {"message_length_limit": None})()
383+
361384

362-
# Test config message length limit is overridden by CLI argument
363-
success_mock.reset_mock()
364-
commands.Commit(config, {"message_length_limit": message_length})()
385+
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")
386+
def test_commit_message_length_cli_overrides_stricter_config(
387+
config, success_mock: MockType, prompt_mock_feat: MockType
388+
):
389+
message_len = _commit_first_line_len(prompt_mock_feat)
390+
config.settings["message_length_limit"] = message_len - 1
391+
commands.Commit(config, {"message_length_limit": message_len})()
365392
success_mock.assert_called_once()
366393

367-
success_mock.reset_mock()
394+
395+
@pytest.mark.usefixtures("staging_is_clean", "commit_mock", "prompt_mock_feat")
396+
def test_commit_message_length_cli_zero_disables_limit(
397+
config, success_mock: MockType, prompt_mock_feat: MockType
398+
):
399+
config.settings["message_length_limit"] = _commit_first_line_len(prompt_mock_feat) - 1
368400
commands.Commit(config, {"message_length_limit": 0})()
369401
success_mock.assert_called_once()

0 commit comments

Comments
 (0)