Skip to content

Commit 6762f5a

Browse files
[truncate explanation] Fix edge case when we truncate due to max_chars
Previousely the added test case would output: "...Full output truncated (0 lines hidden), use '-vv' to show"
1 parent fe7e4d5 commit 6762f5a

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

src/_pytest/assertion/truncate.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,16 +101,15 @@ def _truncate_explanation(
101101
# No truncation happened, so we do not need to add any explanations
102102
return truncated_explanation
103103

104-
truncated_line_count = len(input_lines) - len(truncated_explanation)
105104
if truncated_explanation[-1]:
106105
# Add ellipsis and take into account part-truncated final line
107106
truncated_explanation[-1] = truncated_explanation[-1] + "..."
108-
if truncated_char:
109-
# It's possible that we did not remove any char from this line
110-
truncated_line_count += 1
111107
else:
112108
# Add proper ellipsis when we were able to fit a full line exactly
113109
truncated_explanation[-1] = "..."
110+
truncated_line_count = (
111+
len(input_lines) - len(truncated_explanation) + int(truncated_char)
112+
)
114113
return [
115114
*truncated_explanation,
116115
"",

testing/test_assertion.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,6 +1394,17 @@ def test_truncates_at_8_lines_when_there_is_one_line_to_remove(self) -> None:
13941394
assert result == expl
13951395
assert "truncated" not in result[-1]
13961396

1397+
def test_truncates_full_line_because_of_max_chars(self) -> None:
1398+
"""A line is fully truncated because of the max_chars value."""
1399+
expl = ["a" * 10, "b" * 71]
1400+
result = truncate._truncate_explanation(expl, max_lines=10, max_chars=10)
1401+
assert result == [
1402+
"a" * 10,
1403+
"...",
1404+
"",
1405+
"...Full output truncated (1 line hidden), use '-vv' to show",
1406+
]
1407+
13971408
def test_truncates_edgecase_when_truncation_message_makes_the_result_longer_for_chars(
13981409
self,
13991410
) -> None:

0 commit comments

Comments
 (0)