Fix and improve test: message_length_limit
Description
This PR fixes how message_length_limit is resolved when the CLI flag is unset. Previously, the CLI argument could be present as None, causing arguments.get("message_length_limit", ...) to return None and skip the config fallback—so the configured limit (e.g. in pyproject.toml) was effectively ignored.
- self.max_msg_length = arguments.get(
- "message_length_limit", config.settings.get("message_length_limit", 0)
+
+ 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"]
)
Now the precedence is correctly enforced as CLI > config > default (0 = no limit) for both cz commit and cz check, and additional unit + integration tests are added to prevent regressions.
- unit tests:
test_commit_command.py,test_check_command.py - integration tests:
tests/test_cli_config_integration.py
Checklist
- [X] I have read the contributing guidelines
Was generative AI tooling used to co-author this PR?
- [X] Yes (please specify the tool below)
Generated-with: [Cursor] following the guidelines
Code Changes
- [X] Add test cases to all the changes you introduce
- [X] Run
uv run poe alllocally to ensure this change passes linter check and tests - [X] Manually test the changes:
- [X] Verify the feature/bug fix works as expected in real-world scenarios
- [X] Test edge cases and error conditions
- [X] Ensure backward compatibility is maintained
- [X] Document any manual testing steps performed
- [ ] Update the documentation for the changes
Expected Behavior
When message_length_limit is configured, cz commit / cz check should enforce it unless explicitly overridden via -l/--message-length-limit (including -l 0 to disable the limit).
Steps to Test This Pull Request
Steps to reproduce the behavior:
- Create a temp repo with a
pyproject.tomlcontaining:
[tool.commitizen]
name = "cz_conventional_commits"
message_length_limit = 10
- Confirm config is enforced (should fail):
cz check --message "feat: this is definitely too long" - Confirm CLI overrides config (should succeed):
cz check -l 0 --message "feat: this is definitely too long"
Additional Context
#1899
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 97.99%. Comparing base (33223b4) to head (f5c4b48).
:warning: Report is 7 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #1900 +/- ##
=======================================
Coverage 97.99% 97.99%
=======================================
Files 60 60
Lines 2689 2691 +2
=======================================
+ Hits 2635 2637 +2
Misses 54 54
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.