| 1 |
"""Utilities for truncating assertion output. |
| 2 |
|
| 3 |
Current default behaviour is to truncate assertion explanations at |
| 4 |
terminal lines, unless running with an assertions verbosity level of at least 2 or running on CI. |
| 5 |
""" |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
from _pytest.compat import running_on_ci |
| 10 |
from _pytest.config import Config |
| 11 |
from _pytest.nodes import Item |
| 12 |
|
| 13 |
|
| 14 |
DEFAULT_MAX_LINES = 8 |
| 15 |
DEFAULT_MAX_CHARS = DEFAULT_MAX_LINES * 80 |
| 16 |
USAGE_MSG = "use '-vv' to show" |
| 17 |
|
| 18 |
|
| 19 |
def truncate_if_required(explanation: list[str], item: Item) -> list[str]: |
| 20 |
"""Truncate this assertion explanation if the given test item is eligible.""" |
| 21 |
should_truncate, max_lines, max_chars = _get_truncation_parameters(item) |
| 22 |
if should_truncate: |
| 23 |
return _truncate_explanation( |
| 24 |
explanation, |
| 25 |
max_lines=max_lines, |
| 26 |
max_chars=max_chars, |
| 27 |
) |
| 28 |
return explanation |
| 29 |
|
| 30 |
|
| 31 |
def _get_truncation_parameters(item: Item) -> tuple[bool, int, int]: |
| 32 |
"""Return the truncation parameters related to the given item, as (should truncate, max lines, max chars).""" |
| 33 |
# We do not need to truncate if one of conditions is met: |
| 34 |
# 1. Verbosity level is 2 or more; |
| 35 |
# 2. Test is being run in CI environment; |
| 36 |
# 3. Both truncation_limit_lines and truncation_limit_chars |
| 37 |
# .ini parameters are set to 0 explicitly. |
| 38 |
max_lines = item.config.getini("truncation_limit_lines") |
| 39 |
max_lines = int(max_lines if max_lines is not None else DEFAULT_MAX_LINES) |
| 40 |
|
| 41 |
max_chars = item.config.getini("truncation_limit_chars") |
| 42 |
max_chars = int(max_chars if max_chars is not None else DEFAULT_MAX_CHARS) |
| 43 |
|
| 44 |
verbose = item.config.get_verbosity(Config.VERBOSITY_ASSERTIONS) |
| 45 |
|
| 46 |
should_truncate = verbose < 2 and not running_on_ci() |
| 47 |
should_truncate = should_truncate and (max_lines > 0 or max_chars > 0) |
| 48 |
|
| 49 |
return should_truncate, max_lines, max_chars |
| 50 |
|
| 51 |
|
| 52 |
def _truncate_explanation( |
| 53 |
input_lines: list[str], |
| 54 |
max_lines: int, |
| 55 |
max_chars: int, |
| 56 |
) -> list[str]: |
| 57 |
"""Truncate given list of strings that makes up the assertion explanation. |
| 58 |
|
| 59 |
Truncates to either max_lines, or max_chars - whichever the input reaches |
| 60 |
first, taking the truncation explanation into account. The remaining lines |
| 61 |
will be replaced by a usage message. |
| 62 |
|
| 63 |
If max_chars=0, no truncation by character count is performed. |
| 64 |
If max_lines=0, no truncation by line count is performed. |
| 65 |
|
| 66 |
When this function is launched we know max_lines > 0 or max_chars > 0 |
| 67 |
because _get_truncation_parameters was called first. |
| 68 |
""" |
| 69 |
# The length of the truncation explanation depends on the number of lines |
| 70 |
# removed but is at least 68 characters: |
| 71 |
# The real value is |
| 72 |
# 64 (for the base message: |
| 73 |
# '...\n...Full output truncated (1 line hidden), use '-vv' to show")' |
| 74 |
# ) |
| 75 |
# + 1 (for plural) |
| 76 |
# + int(math.log10(len(input_lines) - max_lines)) (number of hidden line, at least 1) |
| 77 |
# + 3 for the '...' added to the truncated line |
| 78 |
# But if there's more than 100 lines it's very likely that we're going to |
| 79 |
# truncate, so we don't need the exact value using log10. |
| 80 |
tolerable_max_chars = ( |
| 81 |
max_chars + 70 # 64 + 1 (for plural) + 2 (for '99') + 3 for '...' |
| 82 |
) |
| 83 |
# The truncation explanation add two lines to the output |
| 84 |
if max_lines == 0 or len(input_lines) <= max_lines + 2: |
| 85 |
if max_chars == 0 or sum(len(s) for s in input_lines) <= tolerable_max_chars: |
| 86 |
return input_lines |
| 87 |
truncated_explanation = input_lines |
| 88 |
else: |
| 89 |
# Truncate first to max_lines, and then truncate to max_chars if necessary |
| 90 |
truncated_explanation = input_lines[:max_lines] |
| 91 |
# We reevaluate the need to truncate chars following removal of some lines |
| 92 |
need_to_truncate_char = ( |
| 93 |
max_chars > 0 |
| 94 |
and sum(len(e) for e in truncated_explanation) > tolerable_max_chars |
| 95 |
) |
| 96 |
if need_to_truncate_char: |
| 97 |
truncated_explanation = _truncate_by_char_count( |
| 98 |
truncated_explanation, max_chars |
| 99 |
) |
| 100 |
# Something was truncated, adding '...' at the end to show that |
| 101 |
truncated_explanation[-1] += "..." |
| 102 |
truncated_line_count = ( |
| 103 |
len(input_lines) - len(truncated_explanation) + int(need_to_truncate_char) |
| 104 |
) |
| 105 |
return [ |
| 106 |
*truncated_explanation, |
| 107 |
"", |
| 108 |
f"...Full output truncated ({truncated_line_count} line" |
| 109 |
f"{'' if truncated_line_count == 1 else 's'} hidden), {USAGE_MSG}", |
| 110 |
] |
| 111 |
|
| 112 |
|
| 113 |
def _truncate_by_char_count(input_lines: list[str], max_chars: int) -> list[str]: |
| 114 |
# Find point at which input length exceeds total allowed length |
| 115 |
iterated_char_count = 0 |
| 116 |
for iterated_index, input_line in enumerate(input_lines): |
| 117 |
if iterated_char_count + len(input_line) > max_chars: |
| 118 |
break |
| 119 |
iterated_char_count += len(input_line) |
| 120 |
|
| 121 |
# Create truncated explanation with modified final line |
| 122 |
truncated_result = input_lines[:iterated_index] |
| 123 |
final_line = input_lines[iterated_index] |
| 124 |
if final_line: |
| 125 |
final_line_truncate_point = max_chars - iterated_char_count |
| 126 |
final_line = final_line[:final_line_truncate_point] |
| 127 |
truncated_result.append(final_line) |
| 128 |
return truncated_result |