1212
1313
1414DEFAULT_MAX_LINES = 8
15- DEFAULT_MAX_CHARS = 8 * 80
15+ DEFAULT_MAX_CHARS = DEFAULT_MAX_LINES * 80
1616USAGE_MSG = "use '-vv' to show"
1717
1818
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 ]:
2220 """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+ )
2528 return explanation
2629
2730
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+
3044 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
3250
3351
3452def _truncate_explanation (
3553 input_lines : list [str ],
36- max_lines : int | None = None ,
37- max_chars : int | None = None ,
54+ max_lines : int ,
55+ max_chars : int ,
3856) -> list [str ]:
3957 """Truncate given list of strings that makes up the assertion explanation.
4058
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
4260 first, taking the truncation explanation into account. The remaining lines
4361 will be replaced by a usage message.
4462 """
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-
5063 # Check if truncation required
5164 input_char_count = len ("" .join (input_lines ))
5265 # The length of the truncation explanation depends on the number of lines
@@ -71,16 +84,23 @@ def _truncate_explanation(
7184 ):
7285 return input_lines
7386 # 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
7591 truncated_char = True
7692 # 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 :
7894 truncated_explanation = _truncate_by_char_count (
7995 truncated_explanation , max_chars
8096 )
8197 else :
8298 truncated_char = False
8399
100+ if truncated_explanation == input_lines :
101+ # No truncation happened, so we do not need to add any explanations
102+ return truncated_explanation
103+
84104 truncated_line_count = len (input_lines ) - len (truncated_explanation )
85105 if truncated_explanation [- 1 ]:
86106 # Add ellipsis and take into account part-truncated final line
0 commit comments