feat: validate message_length_limit is non-negative
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
CheckandCommit__init__to rejectmessage_length_limit < 0 - Logic fix: Changed condition from
<= 0to== 0for "no limit" check - Type updates: Changed
CommitArgsandCheckArgsto acceptint | Nonefor proper CLI override handling - Tests: Added comprehensive test coverage for negative value rejection in both commands
- Documentation: Updated
docs/config/check.mdanddocs/config/commit.mdto 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
0means "no limit"
Expected Behavior
message_length_limit < 0→ raisesInvalidCommandArgumentError❌message_length_limit = 0→ disables length limit ✅message_length_limit > 0→ enforces the specified limit ✅- CLI
Nonevalue → 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 alllocally 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
0as "no limit")
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.