feat: validate message_length_limit is non-negative by SARAMALI15792 · Pull Request #1916 · commitizen-tools/commitizen · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions commitizen/commands/check.py
25 changes: 16 additions & 9 deletions commitizen/commands/commit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
CommitMessageLengthExceededError,
CustomError,
DryRunExit,
InvalidCommandArgumentError,
NoAnswersError,
NoCommitBackupError,
NotAGitProjectError,
Expand All @@ -35,7 +36,7 @@ class CommitArgs(TypedDict, total=False):
dry_run: bool
edit: bool
extra_cli_args: str
message_length_limit: int
message_length_limit: int | None
no_retry: bool
signoff: bool
write_message_to_file: Path | None
Expand All @@ -54,6 +55,17 @@ def __init__(self, config: BaseConfig, arguments: CommitArgs) -> None:
self.arguments = arguments
self.backup_file_path = get_backup_file_path()

message_length_limit = arguments.get("message_length_limit")
self.message_length_limit: int = (
message_length_limit
if message_length_limit is not None
else config.settings["message_length_limit"]
)
if self.message_length_limit < 0:
raise InvalidCommandArgumentError(
"message_length_limit must be a non-negative integer"
)

def _read_backup_message(self) -> str | None:
# Check the commit backup file exists
if not self.backup_file_path.is_file():
Expand Down Expand Up @@ -85,19 +97,14 @@ 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)
)
# By the contract, message_length_limit is set to 0 for no limit
if (
message_length_limit is None or message_length_limit <= 0
): # do nothing for no limit
if self.message_length_limit == 0:
return

subject = message.partition("\n")[0].strip()
if len(subject) > message_length_limit:
if len(subject) > self.message_length_limit:
raise CommitMessageLengthExceededError(
f"Length of commit message exceeds limit ({len(subject)}/{message_length_limit}), subject: '{subject}'"
f"Length of commit message exceeds limit ({len(subject)}/{self.message_length_limit}), subject: '{subject}'"
)

def manual_edit(self, message: str) -> str:
Expand Down
1 change: 1 addition & 0 deletions docs/config/check.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ List of prefixes that commitizen ignores when verifying messages.
- Default: `0` (no limit)

Maximum length of the commit message. Setting it to `0` disables the length limit.
This value must be a non-negative integer (`>= 0`).

!!! note
This option can be overridden by the `-l/--message-length-limit` command line argument.
11 changes: 11 additions & 0 deletions docs/config/commit.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ Sets the character encoding to be used when parsing commit messages.
- Default: `False`

Retries failed commit when running `cz commit`.

## `message_length_limit`

- Type: `int`
- Default: `0` (no limit)

Maximum length of the commit message. Setting it to `0` disables the length limit.
This value must be a non-negative integer (`>= 0`).

!!! note
This option can be overridden by the `-l/--message-length-limit` command line argument.
37 changes: 32 additions & 5 deletions tests/commands/test_check_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,23 +351,27 @@ def test_check_command_with_amend_prefix_default(config, success_mock):
success_mock.assert_called_once()


def test_check_command_with_config_message_length_limit(config, success_mock):
def test_check_command_with_config_message_length_limit_and_cli_none(
config, success_mock: MockType
):
message = "fix(scope): some commit message"
config.settings["message_length_limit"] = len(message) + 1
commands.Check(
config=config,
arguments={"message": message},
arguments={"message": message, "message_length_limit": None},
)()
success_mock.assert_called_once()


def test_check_command_with_config_message_length_limit_exceeded(config):
def test_check_command_with_config_message_length_limit_exceeded_and_cli_none(
config,
):
message = "fix(scope): some commit message"
config.settings["message_length_limit"] = len(message) - 1
with pytest.raises(CommitMessageLengthExceededError):
commands.Check(
config=config,
arguments={"message": message},
arguments={"message": message, "message_length_limit": None},
)()


Expand All @@ -376,7 +380,7 @@ def test_check_command_cli_overrides_config_message_length_limit(
):
message = "fix(scope): some commit message"
config.settings["message_length_limit"] = len(message) - 1
for message_length_limit in [len(message) + 1, 0]:
for message_length_limit in [len(message), 0]:
success_mock.reset_mock()
commands.Check(
config=config,
Expand All @@ -388,6 +392,29 @@ def test_check_command_cli_overrides_config_message_length_limit(
success_mock.assert_called_once()


def test_check_command_with_negative_cli_message_length_limit_raises(config):
with pytest.raises(InvalidCommandArgumentError):
commands.Check(
config=config,
arguments={
"message": "fix(scope): some commit message",
"message_length_limit": -1,
},
)


def test_check_command_with_negative_config_message_length_limit_raises(config):
config.settings["message_length_limit"] = -1
with pytest.raises(InvalidCommandArgumentError):
commands.Check(
config=config,
arguments={
"message": "fix(scope): some commit message",
"message_length_limit": None,
},
)


class ValidationCz(BaseCommitizen):
def questions(self) -> list[CzQuestion]:
return [
Expand Down
80 changes: 64 additions & 16 deletions tests/commands/test_commit_command.py
Loading