12
12
13
13
14
14
DEFAULT_MAX_LINES = 8
15
- DEFAULT_MAX_CHARS = 8 * 80
15
+ DEFAULT_MAX_CHARS = DEFAULT_MAX_LINES * 80
16
16
USAGE_MSG = "use '-vv' to show"
17
17
18
18
19
- def truncate_if_required (
20
- explanation : list [str ], item : Item , max_length : int | None = None
21
- ) -> list [str ]:
19
+ def truncate_if_required (explanation : list [str ], item : Item ) -> list [str ]:
22
20
"""Truncate this assertion explanation if the given test item is eligible."""
23
- if _should_truncate_item (item ):
24
- return _truncate_explanation (explanation )
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
+ )
25
28
return explanation
26
29
27
30
28
- def _should_truncate_item (item : Item ) -> bool :
29
- """Whether or not this test item is eligible for truncation."""
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
+
30
44
verbose = item .config .get_verbosity (Config .VERBOSITY_ASSERTIONS )
31
- return verbose < 2 and not util .running_on_ci ()
45
+
46
+ should_truncate = verbose < 2 and not util .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
32
50
33
51
34
52
def _truncate_explanation (
35
53
input_lines : list [str ],
36
- max_lines : int | None = None ,
37
- max_chars : int | None = None ,
54
+ max_lines : int ,
55
+ max_chars : int ,
38
56
) -> list [str ]:
39
57
"""Truncate given list of strings that makes up the assertion explanation.
40
58
41
- Truncates to either 8 lines , or 640 characters - whichever the input reaches
59
+ Truncates to either max_lines , or max_chars - whichever the input reaches
42
60
first, taking the truncation explanation into account. The remaining lines
43
61
will be replaced by a usage message.
44
62
"""
45
- if max_lines is None :
46
- max_lines = DEFAULT_MAX_LINES
47
- if max_chars is None :
48
- max_chars = DEFAULT_MAX_CHARS
49
-
50
63
# Check if truncation required
51
64
input_char_count = len ("" .join (input_lines ))
52
65
# The length of the truncation explanation depends on the number of lines
@@ -71,16 +84,23 @@ def _truncate_explanation(
71
84
):
72
85
return input_lines
73
86
# Truncate first to max_lines, and then truncate to max_chars if necessary
74
- truncated_explanation = input_lines [:max_lines ]
87
+ if max_lines > 0 :
88
+ truncated_explanation = input_lines [:max_lines ]
89
+ else :
90
+ truncated_explanation = input_lines
75
91
truncated_char = True
76
92
# We reevaluate the need to truncate chars following removal of some lines
77
- if len ("" .join (truncated_explanation )) > tolerable_max_chars :
93
+ if len ("" .join (truncated_explanation )) > tolerable_max_chars and max_chars > 0 :
78
94
truncated_explanation = _truncate_by_char_count (
79
95
truncated_explanation , max_chars
80
96
)
81
97
else :
82
98
truncated_char = False
83
99
100
+ if truncated_explanation == input_lines :
101
+ # No truncation happened, so we do not need to add any explanations
102
+ return truncated_explanation
103
+
84
104
truncated_line_count = len (input_lines ) - len (truncated_explanation )
85
105
if truncated_explanation [- 1 ]:
86
106
# Add ellipsis and take into account part-truncated final line
0 commit comments