Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Avoid truncation when longer output, ref. #6267 #6462

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelog/6267.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When output is close to truncation limit, we now write 'N lines hidden [1 truncated]',
instead of 'N+1 lines hidden' - last line which we show isn't really hidden always, neither it's always really truncated.
14 changes: 11 additions & 3 deletions src/_pytest/assertion/truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def _truncate_explanation(input_lines, max_lines=None, max_chars=None):

Truncates to either 8 lines, or 640 characters - whichever the input reaches
first. The remaining lines will be replaced by a usage message.

Check https://github.com/pytest-dev/pytest/issues/6267 for corner cases
"""

if max_lines is None:
Expand All @@ -62,12 +64,18 @@ def _truncate_explanation(input_lines, max_lines=None, max_chars=None):

# Append useful message to explanation
truncated_line_count = len(input_lines) - len(truncated_explanation)
truncated_line_count += 1 # Account for the part-truncated final line
msg = "...Full output truncated"
last_truncated = not (
input_lines[len(truncated_explanation) - 1] in truncated_explanation[-1]
)
if truncated_line_count == 1:
msg += " ({} line hidden)".format(truncated_line_count)
msg += " ({} line hidden".format(truncated_line_count)
else:
msg += " ({} lines hidden".format(truncated_line_count)
if last_truncated:
msg += ", 1 truncated)"
else:
msg += " ({} lines hidden)".format(truncated_line_count)
msg += ")"
msg += ", {}".format(USAGE_MSG)
truncated_explanation.extend(["", str(msg)])
return truncated_explanation
Expand Down