truncate.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. The remaining lines will be replaced by a usage message.
  31. """
  32. if max_lines is None:
  33. max_lines = DEFAULT_MAX_LINES
  34. if max_chars is None:
  35. max_chars = DEFAULT_MAX_CHARS
  36. # Check if truncation required
  37. input_char_count = len("".join(input_lines))
  38. if len(input_lines) <= max_lines and input_char_count <= max_chars:
  39. return input_lines
  40. # Truncate first to max_lines, and then truncate to max_chars if max_chars
  41. # is exceeded.
  42. truncated_explanation = input_lines[:max_lines]
  43. truncated_explanation = _truncate_by_char_count(truncated_explanation, max_chars)
  44. # Add ellipsis to final line
  45. truncated_explanation[-1] = truncated_explanation[-1] + "..."
  46. # Append useful message to explanation
  47. truncated_line_count = len(input_lines) - len(truncated_explanation)
  48. truncated_line_count += 1 # Account for the part-truncated final line
  49. msg = "...Full output truncated"
  50. if truncated_line_count == 1:
  51. msg += f" ({truncated_line_count} line hidden)"
  52. else:
  53. msg += f" ({truncated_line_count} lines hidden)"
  54. msg += f", {USAGE_MSG}"
  55. truncated_explanation.extend(["", str(msg)])
  56. return truncated_explanation
  57. def _truncate_by_char_count(input_lines: List[str], max_chars: int) -> List[str]:
  58. # Check if truncation required
  59. if len("".join(input_lines)) <= max_chars:
  60. return input_lines
  61. # Find point at which input length exceeds total allowed length
  62. iterated_char_count = 0
  63. for iterated_index, input_line in enumerate(input_lines):
  64. if iterated_char_count + len(input_line) > max_chars:
  65. break
  66. iterated_char_count += len(input_line)
  67. # Create truncated explanation with modified final line
  68. truncated_result = input_lines[:iterated_index]
  69. final_line = input_lines[iterated_index]
  70. if final_line:
  71. final_line_truncate_point = max_chars - iterated_char_count
  72. final_line = final_line[:final_line_truncate_point]
  73. truncated_result.append(final_line)
  74. return truncated_result