Skip to content

Commit 3f0ba15

Browse files
authored
Redact volatile parts from outputted strings (#30)
1 parent 7f828af commit 3f0ba15

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

pytest_accept/doctest_plugin.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import re
23
import textwrap
34
import warnings
45
from collections import defaultdict
@@ -123,15 +124,32 @@ def _to_doctest_format(output: str) -> str:
123124
lines = output.splitlines()
124125
blankline_sentinel = "<BLANKLINE>"
125126
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.
127128
shortened_lines = [
128129
line if len(line) < 1000 else f"{line[:50]}...{line[-50:]}"
129130
for line in transformed_lines
130131
]
131132
# Again, only for the pathological cases.
132133
if len(shortened_lines) > 1000:
133134
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
135153

136154

137155
def pytest_sessionfinish(session, exitstatus):

0 commit comments

Comments
 (0)