Skip to content

Commit 602f9d0

Browse files
committed
Improve error output parsing in tests
1 parent 9c0e374 commit 602f9d0

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

tests/run.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import shutil
1313
import subprocess
1414
from tempfile import TemporaryDirectory
15+
from difflib import SequenceMatcher
1516

1617

1718
BASEDIR = Path(__file__).resolve().parent
@@ -180,17 +181,17 @@ def run_test(
180181
)
181182

182183
else:
183-
expected_lines = expected_output.splitlines()
184-
actual_lines = total_output.splitlines()
184+
expected_lines = list(reversed(expected_output.splitlines()))
185+
actual_lines = list(reversed([l.decode() for l in total_output.splitlines()]))
185186
msg = []
186187
mismatch = False
187-
for i in range(0, max(len(expected_lines), len(actual_lines))):
188+
while expected_lines or actual_lines:
188189
try:
189-
expected_line = expected_lines[i]
190+
expected_line = expected_lines.pop()
190191
except IndexError:
191192
expected_line = None
192193
try:
193-
actual_line = actual_lines[i].decode()
194+
actual_line = actual_lines.pop()
194195
except IndexError:
195196
actual_line = None
196197

@@ -205,8 +206,21 @@ def run_test(
205206
assert expected_line is not None
206207
assert actual_line is not None
207208
if not re.match(f"^{expected_line}$", actual_line):
208-
msg.append(f"{RED}- {expected_line}{NO_COLOR}")
209-
msg.append(f"{GREEN}+ {actual_line}{NO_COLOR}")
209+
# Try to detect if the mismatch is due to an additional/missing line,
210+
# or because of a small change in the expected line
211+
if (
212+
len(expected_lines) != len(actual_lines)
213+
and SequenceMatcher(None, expected_line, actual_line).ratio() < 0.7
214+
):
215+
if len(expected_lines) > len(actual_lines):
216+
msg.append(f"{RED}- {expected_line}{NO_COLOR}")
217+
actual_lines.append(actual_line)
218+
else:
219+
msg.append(f"{GREEN}+ {actual_line}{NO_COLOR}")
220+
expected_lines.append(expected_line)
221+
else:
222+
msg.append(f"{RED}- {expected_line}{NO_COLOR}")
223+
msg.append(f"{GREEN}+ {actual_line}{NO_COLOR}")
210224
mismatch = True
211225
else:
212226
msg.append(f"~ {actual_line}")

0 commit comments

Comments
 (0)