You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.4KB

  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. import os
  6. from typing import List
  7. from typing import Optional
  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 _running_on_ci()
  23. def _running_on_ci() -> bool:
  24. """Check if we're currently running on a CI system."""
  25. env_vars = ["CI", "BUILD_NUMBER"]
  26. return any(var in os.environ for var in env_vars)
  27. def _truncate_explanation(
  28. input_lines: List[str],
  29. max_lines: Optional[int] = None,
  30. max_chars: Optional[int] = None,
  31. ) -> List[str]:
  32. """Truncate given list of strings that makes up the assertion explanation.
  33. Truncates to either 8 lines, or 640 characters - whichever the input reaches
  34. first. The remaining lines will be replaced by a usage message.
  35. """
  36. if max_lines is None:
  37. max_lines = DEFAULT_MAX_LINES
  38. if max_chars is None:
  39. max_chars = DEFAULT_MAX_CHARS
  40. # Check if truncation required
  41. input_char_count = len("".join(input_lines))
  42. if len(input_lines) <= max_lines and input_char_count <= max_chars:
  43. return input_lines
  44. # Truncate first to max_lines, and then truncate to max_chars if max_chars
  45. # is exceeded.
  46. truncated_explanation = input_lines[:max_lines]
  47. truncated_explanation = _truncate_by_char_count(truncated_explanation, max_chars)
  48. # Add ellipsis to final line
  49. truncated_explanation[-1] = truncated_explanation[-1] + "..."
  50. # Append useful message to explanation
  51. truncated_line_count = len(input_lines) - len(truncated_explanation)
  52. truncated_line_count += 1 # Account for the part-truncated final line
  53. msg = "...Full output truncated"
  54. if truncated_line_count == 1:
  55. msg += f" ({truncated_line_count} line hidden)"
  56. else:
  57. msg += f" ({truncated_line_count} lines hidden)"
  58. msg += f", {USAGE_MSG}"
  59. truncated_explanation.extend(["", str(msg)])
  60. return truncated_explanation
  61. def _truncate_by_char_count(input_lines: List[str], max_chars: int) -> List[str]:
  62. # Check if truncation required
  63. if len("".join(input_lines)) <= max_chars:
  64. return input_lines
  65. # Find point at which input length exceeds total allowed length
  66. iterated_char_count = 0
  67. for iterated_index, input_line in enumerate(input_lines):
  68. if iterated_char_count + len(input_line) > max_chars:
  69. break
  70. iterated_char_count += len(input_line)
  71. # Create truncated explanation with modified final line
  72. truncated_result = input_lines[:iterated_index]
  73. final_line = input_lines[iterated_index]
  74. if final_line:
  75. final_line_truncate_point = max_chars - iterated_char_count
  76. final_line = final_line[:final_line_truncate_point]
  77. truncated_result.append(final_line)
  78. return truncated_result