Skip to content

Commit b86c6f4

Browse files
[truncate_explanation] Cut short by checking if we're going to truncate
Better check the theory and the len of the string to know if we're going to truncate rather than checking the full length of the result after the fact.
1 parent 49bb987 commit b86c6f4

1 file changed

Lines changed: 4 additions & 11 deletions

File tree

src/_pytest/assertion/truncate.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ def _truncate_explanation(
6666
When this function is launched we know max_lines > 0 or max_chars > 0
6767
because _get_truncation_parameters was called first.
6868
"""
69-
# Check if truncation required
70-
input_char_count = sum(len(line) for line in input_lines)
7169
# The length of the truncation explanation depends on the number of lines
7270
# removed but is at least 68 characters:
7371
# The real value is
@@ -82,11 +80,11 @@ def _truncate_explanation(
8280
tolerable_max_chars = (
8381
max_chars + 70 # 64 + 1 (for plural) + 2 (for '99') + 3 for '...'
8482
)
85-
# The truncation explanation add two lines to the output
86-
tolerable_max_lines = max_lines + 2
8783
if (
88-
len(input_lines) <= tolerable_max_lines
89-
and input_char_count <= tolerable_max_chars
84+
# The truncation explanation add two lines to the output
85+
max_lines == 0 or len(input_lines) <= max_lines + 2
86+
) and (
87+
max_chars == 0 or sum(len(line) for line in input_lines) <= tolerable_max_chars
9088
):
9189
return input_lines
9290
# Truncate first to max_lines, and then truncate to max_chars if necessary
@@ -103,11 +101,6 @@ def _truncate_explanation(
103101
truncated_explanation = _truncate_by_char_count(
104102
truncated_explanation, max_chars
105103
)
106-
107-
if truncated_explanation == input_lines:
108-
# No truncation happened, so we do not need to add any explanations
109-
return truncated_explanation
110-
111104
# Something was truncated, adding '...' at the end to show that
112105
truncated_explanation[-1] += "..."
113106
truncated_line_count = (

0 commit comments

Comments
 (0)