Skip to content

Commit be40510

Browse files
yjawLee-W
authored andcommitted
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 078412d commit be40510

2 files changed

Lines changed: 21 additions & 28 deletions

File tree

‎commitizen/commands/commit.py‎

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

107107
def _wrap_body(self, message: str) -> str:
108+
"""
109+
Wrap the body of the commit message to the specified length.
110+
"""
111+
108112
body_length_limit = self.arguments.get(
109-
"body_length_limit", self.config.settings.get("body_length_limit", 0)
113+
"body_length_limit", self.config.settings["body_length_limit"]
110114
)
111115
# By the contract, body_length_limit is set to 0 for no limit
112116
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
@@ -378,43 +378,37 @@ def test_commit_command_with_config_message_length_limit(
378378

379379
@pytest.mark.usefixtures("staging_is_clean")
380380
@pytest.mark.parametrize(
381-
("test_id", "body", "body_length_limit"),
381+
("body", "body_length_limit"),
382382
[
383-
# Basic wrapping - long line gets wrapped
384-
(
385-
"wrapping",
383+
pytest.param(
386384
"This is a very long line that exceeds 72 characters and should be automatically wrapped by the system to fit within the limit",
387385
72,
386+
id="wrapping",
388387
),
389-
# Line break preservation - multiple lines with \n
390-
(
391-
"preserves_line_breaks",
388+
pytest.param(
392389
"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",
393390
72,
391+
id="preserves_line_breaks",
394392
),
395-
# Disabled wrapping - limit = 0
396-
(
397-
"disabled",
393+
pytest.param(
398394
"This is a very long line that exceeds 72 characters and should NOT be wrapped when body_length_limit is set to 0",
399395
0,
396+
id="disabled",
400397
),
401-
# No body - empty string
402-
(
403-
"no_body",
398+
pytest.param(
404399
"",
405400
72,
401+
id="no_body",
406402
),
407403
],
408404
)
409405
def test_commit_command_body_length_limit(
410-
test_id,
411406
body,
412407
body_length_limit,
413408
config,
414409
success_mock: MockType,
415410
commit_mock,
416411
mocker: MockFixture,
417-
file_regression,
418412
):
419413
"""Parameterized test for body_length_limit feature with file regression."""
420414
mocker.patch(
@@ -429,24 +423,19 @@ def test_commit_command_body_length_limit(
429423
},
430424
)
431425

432-
config.settings["body_length_limit"] = body_length_limit
433-
commands.Commit(config, {})()
434-
426+
commands.Commit(config, {"body_length_limit": body_length_limit})()
435427
success_mock.assert_called_once()
436428
committed_message = commit_mock.call_args[0][0]
437429

438-
# File regression check - uses test_id to create separate files
439-
file_regression.check(
440-
committed_message,
441-
extension=".txt",
442-
basename=f"test_commit_command_body_length_limit_{test_id}",
443-
)
430+
lines = committed_message.split("\n")
431+
body_lines = lines[2:] # Skip subject and blank line
444432

445-
# Validate line lengths if limit is not 0
446433
if body_length_limit > 0:
447-
lines = committed_message.split("\n")
448-
body_lines = lines[2:] # Skip subject and blank line
449434
for line in body_lines:
450435
assert len(line) <= body_length_limit, (
451436
f"Line exceeds {body_length_limit} chars: '{line}' ({len(line)} chars)"
452437
)
438+
elif body_length_limit == 0:
439+
assert len(body_lines) == 1, (
440+
"Body should not be wrapped when body_length_limit is set to 0"
441+
)

0 commit comments

Comments
 (0)