Skip to content

Commit f514bdd

Browse files
committed
refactor(commit): refactor code and test follow by suggestions
The test was previously passing even if the code accidentally skipped the argument setting, which was incorrect. I realized I was always overriding the configuration in the test. Now, I pass the argument setting during mocking, and it behaves as expected.
1 parent 005b422 commit f514bdd

File tree

2 files changed

+21
-28
lines changed

2 files changed

+21
-28
lines changed

commitizen/commands/commit.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,12 @@ def _validate_subject_length(self, message: str) -> None:
107107
)
108108

109109
def _wrap_body(self, message: str) -> str:
110+
"""
111+
Wrap the body of the commit message to the specified length.
112+
"""
113+
110114
body_length_limit = self.arguments.get(
111-
"body_length_limit", self.config.settings.get("body_length_limit", 0)
115+
"body_length_limit", self.config.settings["body_length_limit"]
112116
)
113117
# By the contract, body_length_limit is set to 0 for no limit
114118
if not body_length_limit or body_length_limit <= 0:

tests/commands/test_commit_command.py

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -369,43 +369,37 @@ def test_commit_command_with_config_message_length_limit(
369369

370370
@pytest.mark.usefixtures("staging_is_clean")
371371
@pytest.mark.parametrize(
372-
("test_id", "body", "body_length_limit"),
372+
("body", "body_length_limit"),
373373
[
374-
# Basic wrapping - long line gets wrapped
375-
(
376-
"wrapping",
374+
pytest.param(
377375
"This is a very long line that exceeds 72 characters and should be automatically wrapped by the system to fit within the limit",
378376
72,
377+
id="wrapping",
379378
),
380-
# Line break preservation - multiple lines with \n
381-
(
382-
"preserves_line_breaks",
379+
pytest.param(
383380
"Line1 that is very long and exceeds the limit\nLine2 that is very long and exceeds the limit\nLine3 that is very long and exceeds the limit",
384381
72,
382+
id="preserves_line_breaks",
385383
),
386-
# Disabled wrapping - limit = 0
387-
(
388-
"disabled",
384+
pytest.param(
389385
"This is a very long line that exceeds 72 characters and should NOT be wrapped when body_length_limit is set to 0",
390386
0,
387+
id="disabled",
391388
),
392-
# No body - empty string
393-
(
394-
"no_body",
389+
pytest.param(
395390
"",
396391
72,
392+
id="no_body",
397393
),
398394
],
399395
)
400396
def test_commit_command_body_length_limit(
401-
test_id,
402397
body,
403398
body_length_limit,
404399
config,
405400
success_mock: MockType,
406401
commit_mock,
407402
mocker: MockFixture,
408-
file_regression,
409403
):
410404
"""Parameterized test for body_length_limit feature with file regression."""
411405
mocker.patch(
@@ -420,24 +414,19 @@ def test_commit_command_body_length_limit(
420414
},
421415
)
422416

423-
config.settings["body_length_limit"] = body_length_limit
424-
commands.Commit(config, {})()
425-
417+
commands.Commit(config, {"body_length_limit": body_length_limit})()
426418
success_mock.assert_called_once()
427419
committed_message = commit_mock.call_args[0][0]
428420

429-
# File regression check - uses test_id to create separate files
430-
file_regression.check(
431-
committed_message,
432-
extension=".txt",
433-
basename=f"test_commit_command_body_length_limit_{test_id}",
434-
)
421+
lines = committed_message.split("\n")
422+
body_lines = lines[2:] # Skip subject and blank line
435423

436-
# Validate line lengths if limit is not 0
437424
if body_length_limit > 0:
438-
lines = committed_message.split("\n")
439-
body_lines = lines[2:] # Skip subject and blank line
440425
for line in body_lines:
441426
assert len(line) <= body_length_limit, (
442427
f"Line exceeds {body_length_limit} chars: '{line}' ({len(line)} chars)"
443428
)
429+
elif body_length_limit == 0:
430+
assert len(body_lines) == 1, (
431+
"Body should not be wrapped when body_length_limit is set to 0"
432+
)

0 commit comments

Comments
 (0)