|
1 | 1 | import logging |
| 2 | +import re |
2 | 3 | import textwrap |
3 | 4 | import warnings |
4 | 5 | from collections import defaultdict |
@@ -123,15 +124,32 @@ def _to_doctest_format(output: str) -> str: |
123 | 124 | lines = output.splitlines() |
124 | 125 | blankline_sentinel = "<BLANKLINE>" |
125 | 126 | transformed_lines = [line if line else blankline_sentinel for line in lines] |
126 | | - # In some pathological cases, this can crash an editor. |
| 127 | + # In some pathological cases, really long lines can crash an editor. |
127 | 128 | shortened_lines = [ |
128 | 129 | line if len(line) < 1000 else f"{line[:50]}...{line[-50:]}" |
129 | 130 | for line in transformed_lines |
130 | 131 | ] |
131 | 132 | # Again, only for the pathological cases. |
132 | 133 | if len(shortened_lines) > 1000: |
133 | 134 | shortened_lines = shortened_lines[:50] + ["..."] + shortened_lines[-50:] |
134 | | - return "\n".join(shortened_lines) |
| 135 | + output = "\n".join(shortened_lines) |
| 136 | + return _redact_volatile(output) |
| 137 | + |
| 138 | + |
| 139 | +def _redact_volatile(output: str) -> str: |
| 140 | + """ |
| 141 | + Replace some volatile values, like temp paths & memory locations. |
| 142 | +
|
| 143 | + >>> _redact_volatile("<__main__.A at 0x10b80ce50>") |
| 144 | + '<__main__.A at 0x...>' |
| 145 | +
|
| 146 | + >>> _redact_volatile("/tmp/abcd234/pytest-accept-test-temp-file-0.py") |
| 147 | + '/tmp/.../pytest-accept-test-temp-file-0.py' |
| 148 | +
|
| 149 | + """ |
| 150 | + mem_locations = re.sub(r" 0x[0-9a-fA-F]+", " 0x...", output) |
| 151 | + temp_paths = re.sub(r"/tmp/[0-9a-fA-F]+", "/tmp/...", mem_locations) |
| 152 | + return temp_paths |
135 | 153 |
|
136 | 154 |
|
137 | 155 | def pytest_sessionfinish(session, exitstatus): |
|
0 commit comments