commitizen icon indicating copy to clipboard operation
commitizen copied to clipboard

feat: validate message_length_limit is non-negative

Open SARAMALI15792 opened this issue 8 months ago • 1 comments

Description

Added validation for message_length_limit to ensure it accepts only non-negative integers (>= 0). Negative values now raise InvalidCommandArgumentError instead of being silently treated as "no limit".

This PR addresses the inconsistency identified in #1909 where negative values were accepted but had unclear behavior.

Root Cause

After the fix in #1813 changed the default from None to 0 for "no limit", the validation logic still checked <= 0 instead of == 0. This meant:

  • message_length_limit = 0 → no limit ✅ (intended)
  • message_length_limit = -1 → no limit ✅ (unintended, should error)

Changes

  • Validation: Added check in Check and Commit __init__ to reject message_length_limit < 0
  • Logic fix: Changed condition from <= 0 to == 0 for "no limit" check
  • Type updates: Changed CommitArgs and CheckArgs to accept int | None for proper CLI override handling
  • Tests: Added comprehensive test coverage for negative value rejection in both commands
  • Documentation: Updated docs/config/check.md and docs/config/commit.md to clarify non-negative requirement

Why This Is Safe

  • Default is 0 (no limit), so existing configs without this setting are unaffected
  • Negative values currently work as "no limit" but this is undocumented behavior
  • Users relying on negative values can simply change to 0 (same effect, clearer intent)
  • Aligns with the v4.13.0 design where 0 means "no limit"

Expected Behavior

  • message_length_limit < 0 → raises InvalidCommandArgumentError ❌
  • message_length_limit = 0 → disables length limit ✅
  • message_length_limit > 0 → enforces the specified limit ✅
  • CLI None value → falls back to config setting ✅

Steps to Test This Pull Request

# Test negative value raises error
echo "feat: hello" | cz check -l -1
# Expected: "message_length_limit must be a non-negative integer"

# Test zero disables limit
echo "feat: this is a very long message" | cz check -l 0
# Expected: "Commit validation: successful!"

# Test positive limit works
echo "feat: hello" | cz check -l 50
# Expected: "Commit validation: successful!"

# Run test suite
uv run poe test

Checklist

  • [x] Add test cases to all the changes introduced
  • [x] Run uv run poe all locally to ensure this change passes linter check and tests
  • [x] Manually test the changes:
    • [x] Verify negative values raise error
    • [x] Verify zero disables limit
    • [x] Verify positive limits work
    • [x] Verify CLI overrides config
    • [x] Verify config fallback works
  • [x] Update the documentation for the changes

Related

  • Fixes #1909
  • Related to #1908 (similar approach, independent implementation)
  • Builds on #1813 (the fix that standardized 0 as "no limit")

SARAMALI15792 avatar Mar 31 '26 06:03 SARAMALI15792

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 97.99%. Comparing base (7c21c21) to head (fdcdaec). :warning: Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1916   +/-   ##
=======================================
  Coverage   97.99%   97.99%           
=======================================
  Files          60       60           
  Lines        2689     2695    +6     
=======================================
+ Hits         2635     2641    +6     
  Misses         54       54           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

codecov[bot] avatar Mar 31 '26 06:03 codecov[bot]