Skip to content

Commit 5e99856

Browse files
authored
Improve diff output when tests fail (#5152)
Use difflib's unified_diff to produce a unified diff between the expected and actual file. This has the benefit of including the files compared in the output which makes it easier to do further inspection on them.
1 parent a4e8c88 commit 5e99856

File tree

1 file changed

+8
-8
lines changed
  • test/migration-tool-tests/src/test/resources

1 file changed

+8
-8
lines changed

test/migration-tool-tests/src/test/resources/run-test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#!/usr/bin/env python
22

3+
from difflib import unified_diff
34
import subprocess
45
import filecmp
56
import os
67
import shutil, errno
78
import argparse
8-
import difflib
9+
import sys
910

1011
RESOURCE_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "resources")
1112
BEFORE_DIR = os.path.join(RESOURCE_DIR, "before")
@@ -34,14 +35,13 @@ def run_test(version):
3435

3536
def compare_directory(dcmp):
3637
if dcmp.diff_files or dcmp.left_only or dcmp.right_only:
38+
print("Unexpected diffs found!")
3739
for diff_file in dcmp.diff_files:
38-
file1 = open(dcmp.right + "/" + diff_file, 'r')
39-
file2 = open(dcmp.left + "/" + diff_file, 'r')
40-
diff = difflib.ndiff(file1.readlines(), file2.readlines())
41-
changes = [l for l in diff if l.startswith('+ ') or l.startswith('- ')]
42-
print("Diff found in file: " + diff_file)
43-
for change in changes:
44-
print(change)
40+
a = dcmp.right + "/" + diff_file
41+
a_contents = open(a, 'r').readlines()
42+
b = dcmp.left + "/" + diff_file
43+
b_contents = open(b, 'r').readlines()
44+
sys.stdout.writelines(unified_diff(a_contents, b_contents, fromfile=a, tofile=b))
4545

4646
return False
4747
for sub_dcmp in dcmp.subdirs.values():

0 commit comments

Comments
 (0)