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 2 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.
15 changes: 12 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 @@ -48,6 +50,7 @@ def _truncate_explanation(input_lines, max_lines=None, max_chars=None):
max_chars = DEFAULT_MAX_CHARS

# Check if truncation required

erheron marked this conversation as resolved.
Show resolved Hide resolved
input_char_count = len("".join(input_lines))
if len(input_lines) <= max_lines and input_char_count <= max_chars:
return input_lines
Expand All @@ -62,12 +65,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)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a missing space (after comma)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the space was missing. I've commited 2 changes, haven't squashed yet

else:
msg += " ({} lines hidden)".format(truncated_line_count)
msg += ")"
msg += ", {}".format(USAGE_MSG)
truncated_explanation.extend(["", str(msg)])
return truncated_explanation
Expand Down