diff --git a/commitizen/commands/check.py b/commitizen/commands/check.py index fbf707341f..aed39defc7 100644 --- a/commitizen/commands/check.py +++ b/commitizen/commands/check.py @@ -22,7 +22,7 @@ class CheckArgs(TypedDict, total=False): commit_msg: str rev_range: str allow_abort: bool - message_length_limit: int + message_length_limit: int | None allowed_prefixes: list[str] message: str use_default_range: bool @@ -51,9 +51,9 @@ def __init__(self, config: BaseConfig, arguments: CheckArgs, *args: object) -> N ) self.use_default_range = bool(arguments.get("use_default_range")) - self.max_msg_length = arguments.get( - "message_length_limit", config.settings.get("message_length_limit", 0) - ) + self.max_msg_length = arguments.get("message_length_limit") + if self.max_msg_length is None: + self.max_msg_length = config.settings.get("message_length_limit", 0) # we need to distinguish between None and [], which is a valid value allowed_prefixes = arguments.get("allowed_prefixes") diff --git a/commitizen/commands/commit.py b/commitizen/commands/commit.py index 970a92450a..af5302a9e9 100644 --- a/commitizen/commands/commit.py +++ b/commitizen/commands/commit.py @@ -37,7 +37,7 @@ class CommitArgs(TypedDict, total=False): dry_run: bool edit: bool extra_cli_args: list[str] - message_length_limit: int + message_length_limit: int | None body_length_limit: int | None no_retry: bool signoff: bool @@ -89,9 +89,10 @@ def _get_message_by_prompt_commit_questions(self) -> str: return message def _validate_subject_length(self, message: str) -> None: - message_length_limit = self.arguments.get( - "message_length_limit", self.config.settings.get("message_length_limit", 0) - ) + """Enforce the configured subject limit unless the CLI overrides it.""" + message_length_limit = self.arguments.get("message_length_limit") + if message_length_limit is None: + message_length_limit = self.config.settings.get("message_length_limit", 0) # By the contract, message_length_limit is set to 0 for no limit if ( message_length_limit is None or message_length_limit <= 0 diff --git a/docs/config/check.md b/docs/config/check.md index 2c8dda27b4..667b7ea779 100644 --- a/docs/config/check.md +++ b/docs/config/check.md @@ -24,4 +24,6 @@ List of prefixes that commitizen ignores when verifying messages. Maximum length of the commit message. Setting it to `0` disables the length limit. !!! note - This option can be overridden by the `-l/--message-length-limit` command line argument. + Both `cz commit` and `cz check` use this setting when `-l/--message-length-limit` + is omitted. An explicit CLI value overrides the configuration. Use `-l 0` to + disable the limit, even when a limit is configured. diff --git a/tests/commands/test_check_command.py b/tests/commands/test_check_command.py index 227fa5fa58..bf7e8a1768 100644 --- a/tests/commands/test_check_command.py +++ b/tests/commands/test_check_command.py @@ -479,6 +479,25 @@ def test_check_command_cli_overrides_config_message_length_limit( success_mock.assert_called_once() +def test_check_command_uses_configured_message_length_limit_when_cli_option_is_omitted( + config: BaseConfig, success_mock: MockType +) -> None: + """Reject an oversized subject when argparse supplies no CLI override.""" + # Arrange + message = "fix(scope): some commit message" + config.settings["message_length_limit"] = len(message) - 1 + + # Act + with pytest.raises(CommitMessageLengthExceededError): + commands.Check( + config, + {"message": message, "message_length_limit": None}, + )() + + # Assert + success_mock.assert_not_called() + + class ValidationCz(BaseCommitizen): def questions(self) -> list[CzQuestion]: return [ diff --git a/tests/commands/test_commit_command.py b/tests/commands/test_commit_command.py index 867c7d28a0..1bcf5bf80b 100644 --- a/tests/commands/test_commit_command.py +++ b/tests/commands/test_commit_command.py @@ -1,10 +1,12 @@ +from __future__ import annotations + import re import sys from pathlib import Path +from typing import TYPE_CHECKING from unittest.mock import ANY import pytest -from pytest_mock import MockFixture, MockType from commitizen import cli, cmd, commands from commitizen.cz.exceptions import CzException @@ -21,6 +23,11 @@ NothingToCommitError, ) +if TYPE_CHECKING: + from pytest_mock import MockFixture, MockType + + from commitizen.config import BaseConfig + @pytest.fixture def commit_mock(mocker: MockFixture): @@ -376,6 +383,26 @@ def test_commit_command_with_config_message_length_limit( success_mock.assert_called_once() +@pytest.mark.usefixtures("staging_is_clean") +def test_commit_command_uses_configured_message_length_limit_when_cli_option_is_omitted( + config: BaseConfig, + commit_mock: MockType, + prompt_mock_feat: MockType, +) -> None: + """Reject an oversized subject when argparse supplies no CLI override.""" + # Arrange + prefix = prompt_mock_feat.return_value["prefix"] + subject = prompt_mock_feat.return_value["subject"] + config.settings["message_length_limit"] = len(f"{prefix}: {subject}") - 1 + + # Act + with pytest.raises(CommitMessageLengthExceededError): + commands.Commit(config, {"message_length_limit": None})() + + # Assert + commit_mock.assert_not_called() + + @pytest.mark.usefixtures("staging_is_clean") @pytest.mark.parametrize( ("body", "body_length_limit"),