Skip to content

Commit 92a8294

Browse files
committedOct 12, 2019
Reland r374392: [lit] Extend internal diff to support -U
To avoid breaking some tests, D66574, D68664, D67643, and D68668 landed together. However, D68664 introduced an issue now addressed by D68839, with which these are now all relanding. Differential Revision: https://reviews.llvm.org/D68668 llvm-svn: 374651
1 parent 32096a8 commit 92a8294

File tree

4 files changed

+136
-6
lines changed

4 files changed

+136
-6
lines changed
 

‎llvm/utils/lit/lit/builtin_commands/diff.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def __init__(self):
1010
self.ignore_all_space = False
1111
self.ignore_space_change = False
1212
self.unified_diff = False
13+
self.num_context_lines = 3
1314
self.recursive_diff = False
1415
self.strip_trailing_cr = False
1516

@@ -48,15 +49,19 @@ def compareTwoBinaryFiles(flags, filepaths, filelines):
4849
exitCode = 0
4950
if hasattr(difflib, 'diff_bytes'):
5051
# python 3.5 or newer
51-
diffs = difflib.diff_bytes(difflib.unified_diff, filelines[0], filelines[1], filepaths[0].encode(), filepaths[1].encode())
52+
diffs = difflib.diff_bytes(difflib.unified_diff, filelines[0],
53+
filelines[1], filepaths[0].encode(),
54+
filepaths[1].encode(),
55+
n = flags.num_context_lines)
5256
diffs = [diff.decode(errors="backslashreplace") for diff in diffs]
5357
else:
5458
# python 2.7
5559
if flags.unified_diff:
5660
func = difflib.unified_diff
5761
else:
5862
func = difflib.context_diff
59-
diffs = func(filelines[0], filelines[1], filepaths[0], filepaths[1])
63+
diffs = func(filelines[0], filelines[1], filepaths[0], filepaths[1],
64+
n = flags.num_context_lines)
6065

6166
for diff in diffs:
6267
sys.stdout.write(diff)
@@ -88,7 +93,8 @@ def compose2(f, g):
8893
filelines[idx]= [f(line) for line in lines]
8994

9095
func = difflib.unified_diff if flags.unified_diff else difflib.context_diff
91-
for diff in func(filelines[0], filelines[1], filepaths[0], filepaths[1]):
96+
for diff in func(filelines[0], filelines[1], filepaths[0], filepaths[1],
97+
n = flags.num_context_lines):
9298
sys.stdout.write(diff)
9399
exitCode = 1
94100
return exitCode
@@ -171,7 +177,7 @@ def compareDirTrees(flags, dir_trees, base_paths=["", ""]):
171177
def main(argv):
172178
args = argv[1:]
173179
try:
174-
opts, args = getopt.gnu_getopt(args, "wbur", ["strip-trailing-cr"])
180+
opts, args = getopt.gnu_getopt(args, "wbuU:r", ["strip-trailing-cr"])
175181
except getopt.GetoptError as err:
176182
sys.stderr.write("Unsupported: 'diff': %s\n" % str(err))
177183
sys.exit(1)
@@ -185,6 +191,16 @@ def main(argv):
185191
flags.ignore_space_change = True
186192
elif o == "-u":
187193
flags.unified_diff = True
194+
elif o.startswith("-U"):
195+
flags.unified_diff = True
196+
try:
197+
flags.num_context_lines = int(a)
198+
if flags.num_context_lines < 0:
199+
raise ValueException
200+
except:
201+
sys.stderr.write("Error: invalid '-U' argument: {}\n"
202+
.format(a))
203+
sys.exit(1)
188204
elif o == "-r":
189205
flags.recursive_diff = True
190206
elif o == "--strip-trailing-cr":
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# RUN: echo 1 > %t.foo
2+
# RUN: echo 2 >> %t.foo
3+
# RUN: echo 3 >> %t.foo
4+
# RUN: echo 4 >> %t.foo
5+
# RUN: echo 5 >> %t.foo
6+
# RUN: echo 6 foo >> %t.foo
7+
# RUN: echo 7 >> %t.foo
8+
# RUN: echo 8 >> %t.foo
9+
# RUN: echo 9 >> %t.foo
10+
# RUN: echo 10 >> %t.foo
11+
# RUN: echo 11 >> %t.foo
12+
13+
# RUN: echo 1 > %t.bar
14+
# RUN: echo 2 >> %t.bar
15+
# RUN: echo 3 >> %t.bar
16+
# RUN: echo 4 >> %t.bar
17+
# RUN: echo 5 >> %t.bar
18+
# RUN: echo 6 bar >> %t.bar
19+
# RUN: echo 7 >> %t.bar
20+
# RUN: echo 8 >> %t.bar
21+
# RUN: echo 9 >> %t.bar
22+
# RUN: echo 10 >> %t.bar
23+
# RUN: echo 11 >> %t.bar
24+
25+
# Default is 3 lines of context.
26+
# RUN: diff -u %t.foo %t.bar && false || true
27+
28+
# Override default of 3 lines of context.
29+
# RUN: diff -U 2 %t.foo %t.bar && false || true
30+
# RUN: diff -U4 %t.foo %t.bar && false || true
31+
# RUN: diff -U0 %t.foo %t.bar && false || true
32+
33+
# Check bad -U argument.
34+
# RUN: diff -U 30.1 %t.foo %t.foo && false || true
35+
# RUN: diff -U-1 %t.foo %t.foo && false || true
36+
37+
# Fail so lit will print output.
38+
# RUN: false

‎llvm/utils/lit/tests/max-failures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#
99
# END.
1010

11-
# CHECK: Failing Tests (30)
11+
# CHECK: Failing Tests (31)
1212
# CHECK: Failing Tests (1)
1313
# CHECK: Failing Tests (2)
1414
# CHECK: error: argument --max-failures: requires positive integer, but found '0'

‎llvm/utils/lit/tests/shtest-shell.py

+77-1
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,82 @@
331331

332332
# CHECK: PASS: shtest-shell :: diff-r.txt
333333

334+
335+
# CHECK: FAIL: shtest-shell :: diff-unified.txt
336+
337+
# CHECK: *** TEST 'shtest-shell :: diff-unified.txt' FAILED ***
338+
339+
# CHECK: $ "diff" "-u" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
340+
# CHECK: # command output:
341+
# CHECK: @@ {{.*}} @@
342+
# CHECK-NEXT: 3
343+
# CHECK-NEXT: 4
344+
# CHECK-NEXT: 5
345+
# CHECK-NEXT: -6 foo
346+
# CHECK-NEXT: +6 bar
347+
# CHECK-NEXT: 7
348+
# CHECK-NEXT: 8
349+
# CHECK-NEXT: 9
350+
# CHECK-EMPTY:
351+
# CHECK-NEXT: error: command failed with exit status: 1
352+
# CHECK-NEXT: $ "true"
353+
354+
# CHECK: $ "diff" "-U" "2" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
355+
# CHECK: # command output:
356+
# CHECK: @@ {{.*}} @@
357+
# CHECK-NEXT: 4
358+
# CHECK-NEXT: 5
359+
# CHECK-NEXT: -6 foo
360+
# CHECK-NEXT: +6 bar
361+
# CHECK-NEXT: 7
362+
# CHECK-NEXT: 8
363+
# CHECK-EMPTY:
364+
# CHECK-NEXT: error: command failed with exit status: 1
365+
# CHECK-NEXT: $ "true"
366+
367+
# CHECK: $ "diff" "-U4" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
368+
# CHECK: # command output:
369+
# CHECK: @@ {{.*}} @@
370+
# CHECK-NEXT: 2
371+
# CHECK-NEXT: 3
372+
# CHECK-NEXT: 4
373+
# CHECK-NEXT: 5
374+
# CHECK-NEXT: -6 foo
375+
# CHECK-NEXT: +6 bar
376+
# CHECK-NEXT: 7
377+
# CHECK-NEXT: 8
378+
# CHECK-NEXT: 9
379+
# CHECK-NEXT: 10
380+
# CHECK-EMPTY:
381+
# CHECK-NEXT: error: command failed with exit status: 1
382+
# CHECK-NEXT: $ "true"
383+
384+
# CHECK: $ "diff" "-U0" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
385+
# CHECK: # command output:
386+
# CHECK: @@ {{.*}} @@
387+
# CHECK-NEXT: -6 foo
388+
# CHECK-NEXT: +6 bar
389+
# CHECK-EMPTY:
390+
# CHECK-NEXT: error: command failed with exit status: 1
391+
# CHECK-NEXT: $ "true"
392+
393+
# CHECK: $ "diff" "-U" "30.1" "{{[^"]*}}" "{{[^"]*}}"
394+
# CHECK: # command stderr:
395+
# CHECK: Error: invalid '-U' argument: 30.1
396+
# CHECK: error: command failed with exit status: 1
397+
# CHECK: $ "true"
398+
399+
# CHECK: $ "diff" "-U-1" "{{[^"]*}}" "{{[^"]*}}"
400+
# CHECK: # command stderr:
401+
# CHECK: Error: invalid '-U' argument: -1
402+
# CHECK: error: command failed with exit status: 1
403+
# CHECK: $ "true"
404+
405+
# CHECK: $ "false"
406+
407+
# CHECK: ***
408+
409+
334410
# CHECK: FAIL: shtest-shell :: error-0.txt
335411
# CHECK: *** TEST 'shtest-shell :: error-0.txt' FAILED ***
336412
# CHECK: $ "not-a-real-command"
@@ -410,4 +486,4 @@
410486
# CHECK: PASS: shtest-shell :: sequencing-0.txt
411487
# CHECK: XFAIL: shtest-shell :: sequencing-1.txt
412488
# CHECK: PASS: shtest-shell :: valid-shell.txt
413-
# CHECK: Failing Tests (30)
489+
# CHECK: Failing Tests (31)

0 commit comments

Comments
 (0)
Please sign in to comment.