truncate.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. """Utilities for truncating assertion output.
  2. Current default behaviour is to truncate assertion explanations at
  3. ~8 terminal lines, unless running in "-vv" mode or running on CI.
  4. """
  5. from typing import List
  6. from typing import Optional
  7. from _pytest.assertion import util
  8. from _pytest.nodes import Item
  9. DEFAULT_MAX_LINES = 8
  10. DEFAULT_MAX_CHARS = 8 * 80
  11. USAGE_MSG = "use '-vv' to show"
  12. def truncate_if_required(
  13. explanation: List[str], item: Item, max_length: Optional[int] = None
  14. ) -> List[str]:
  15. """Truncate this assertion explanation if the given test item is eligible."""
  16. if _should_truncate_item(item):
  17. return _truncate_explanation(explanation)
  18. return explanation
  19. def _should_truncate_item(item: Item) -> bool:
  20. """Whether or not this test item is eligible for truncation."""
  21. verbose = item.config.option.verbose
  22. return verbose < 2 and not util.running_on_ci()
  23. def _truncate_explanation(
  24. input_lines: List[str],
  25. max_lines: Optional[int] = None,
  26. max_chars: Optional[int] = None,
  27. ) -> List[str]:
  28. """Truncate given list of strings that makes up the assertion explanation.
  29. Truncates to either 8 lines, or 640 characters - whichever the input reaches
  30. first, taking the truncation explanation into account. The remaining lines
  31. will be replaced by a usage message.
  32. """
  33. if max_lines is None:
  34. max_lines = DEFAULT_MAX_LINES
  35. if max_chars is None:
  36. max_chars = DEFAULT_MAX_CHARS
  37. # Check if truncation required
  38. input_char_count = len("".join(input_lines))
  39. # The length of the truncation explanation depends on the number of lines
  40. # removed but is at least 68 characters:
  41. # The real value is
  42. # 64 (for the base message:
  43. # '...\n...Full output truncated (1 line hidden), use '-vv' to show")'
  44. # )
  45. # + 1 (for plural)
  46. # + int(math.log10(len(input_lines) - max_lines)) (number of hidden line, at least 1)
  47. # + 3 for the '...' added to the truncated line
  48. # But if there's more than 100 lines it's very likely that we're going to
  49. # truncate, so we don't need the exact value using log10.
  50. tolerable_max_chars = (
  51. max_chars + 70 # 64 + 1 (for plural) + 2 (for '99') + 3 for '...'
  52. )
  53. # The truncation explanation add two lines to the output
  54. tolerable_max_lines = max_lines + 2
  55. if (
  56. len(input_lines) <= tolerable_max_lines
  57. and input_char_count <= tolerable_max_chars
  58. ):
  59. return input_lines
  60. # Truncate first to max_lines, and then truncate to max_chars if necessary
  61. truncated_explanation = input_lines[:max_lines]
  62. truncated_char = True
  63. # We reevaluate the need to truncate chars following removal of some lines
  64. if len("".join(truncated_explanation)) > tolerable_max_chars:
  65. truncated_explanation = _truncate_by_char_count(
  66. truncated_explanation, max_chars
  67. )
  68. else:
  69. truncated_char = False
  70. truncated_line_count = len(input_lines) - len(truncated_explanation)
  71. if truncated_explanation[-1]:
  72. # Add ellipsis and take into account part-truncated final line
  73. truncated_explanation[-1] = truncated_explanation[-1] + "..."
  74. if truncated_char:
  75. # It's possible that we did not remove any char from this line
  76. truncated_line_count += 1
  77. else:
  78. # Add proper ellipsis when we were able to fit a full line exactly
  79. truncated_explanation[-1] = "..."
  80. return truncated_explanation + [
  81. "",
  82. f"...Full output truncated ({truncated_line_count} line"
  83. f"{'' if truncated_line_count == 1 else 's'} hidden), {USAGE_MSG}",
  84. ]
  85. def _truncate_by_char_count(input_lines: List[str], max_chars: int) -> List[str]:
  86. # Find point at which input length exceeds total allowed length
  87. iterated_char_count = 0
  88. for iterated_index, input_line in enumerate(input_lines):
  89. if iterated_char_count + len(input_line) > max_chars:
  90. break
  91. iterated_char_count += len(input_line)
  92. # Create truncated explanation with modified final line
  93. truncated_result = input_lines[:iterated_index]
  94. final_line = input_lines[iterated_index]
  95. if final_line:
  96. final_line_truncate_point = max_chars - iterated_char_count
  97. final_line = final_line[:final_line_truncate_point]
  98. truncated_result.append(final_line)
  99. return truncated_result