Skip to content

Commit 63fbbd4

Browse files
authored
acc: support diffing files in diff.py (#2548)
Using it in #2532
1 parent ed88115 commit 63fbbd4

File tree

3 files changed

+35
-11
lines changed

3 files changed

+35
-11
lines changed

acceptance/bin/diff.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ def main():
3030
except re.error as e:
3131
print(f"Regex error for pattern {r}: {e}", file=sys.stderr)
3232

33-
files1 = [str(p.relative_to(d1)) for p in d1.rglob("*") if p.is_file()]
34-
files2 = [str(p.relative_to(d2)) for p in d2.rglob("*") if p.is_file()]
33+
if d1.is_dir() and d2.is_dir():
34+
files1 = [str(p.relative_to(d1)) for p in d1.rglob("*") if p.is_file()]
35+
files2 = [str(p.relative_to(d2)) for p in d2.rglob("*") if p.is_file()]
36+
else:
37+
assert d1.is_file(), d1
38+
assert d2.is_file(), d2
39+
diff_files(patterns, d1, d2)
40+
return
3541

3642
set1 = set(files1)
3743
set2 = set(files2)
@@ -44,13 +50,17 @@ def main():
4450
elif f not in set1:
4551
print(f"Only in {d2}: {f}")
4652
else:
47-
a = replaceAll(patterns, p1.read_text()).splitlines(True)
48-
b = replaceAll(patterns, p2.read_text()).splitlines(True)
49-
if a != b:
50-
p1_str = p1.as_posix()
51-
p2_str = p2.as_posix()
52-
for line in difflib.unified_diff(a, b, p1_str, p2_str, "", "", 2):
53-
print(line, end="")
53+
diff_files(patterns, p1, p2)
54+
55+
56+
def diff_files(patterns, p1, p2):
57+
a = replaceAll(patterns, p1.read_text()).splitlines(True)
58+
b = replaceAll(patterns, p2.read_text()).splitlines(True)
59+
if a != b:
60+
p1_str = p1.as_posix()
61+
p2_str = p2.as_posix()
62+
for line in difflib.unified_diff(a, b, p1_str, p2_str, "", "", 2):
63+
print(line, end="")
5464

5565

5666
if __name__ == "__main__":

acceptance/selftest/diff/output.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,14 @@ Only in ../out_dir_b: only_in_b
2424
+ "id": "[UUID]",
2525
"userName": "[USERNAME]"
2626
}
27+
28+
>>> diff.py out_dir_a/output.txt out_dir_b/output.txt
29+
--- out_dir_a/output.txt
30+
+++ out_dir_b/output.txt
31+
@@ -1,5 +1,5 @@
32+
Hello!
33+
{
34+
- "id": "[USERID]",
35+
+ "id": "[UUID]",
36+
"userName": "[USERNAME]"
37+
}

acceptance/selftest/diff/script

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ printf "\n\nFooter" >> out_dir_a/output.txt
1212
printf '{\n "id": "7d639bad-ac6d-4e6f-abd7-9522a86b0239",\n "userName": "[USERNAME]"\n}\n\nFooter' >> out_dir_b/output.txt
1313

1414
# Unlike regular diff, diff.py will apply replacements first before doing the comparison
15-
errcode trace diff.py out_dir_a out_dir_b
15+
trace diff.py out_dir_a out_dir_b
1616

1717
# Test that it works from non-root directory:
1818
cd out_dir_a
19-
errcode trace diff.py . ../out_dir_b
19+
trace diff.py . ../out_dir_b
2020
cd ..
2121

22+
# Test diffing files:
23+
trace diff.py out_dir_a/output.txt out_dir_b/output.txt
24+
2225
rm out_dir_a/only_in_a out_dir_b/only_in_b

0 commit comments

Comments
 (0)