Skip to content

Commit 7e385bd

Browse files
committed
[lit] Extend internal diff to support -U
When using lit's internal shell, RUN lines like the following accidentally execute an external `diff` instead of lit's internal `diff`: ``` # RUN: program | diff -U1 file - ``` Such cases exist now, in `clang/test/Analysis` for example. We are preparing patches to ensure lit's internal `diff` is called in such cases, which will then fail because lit's internal `diff` doesn't recognize `-U` as a command-line option. This patch adds `-U` support. Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D68668 llvm-svn: 374814
1 parent 2b161cd commit 7e385bd

File tree

6 files changed

+142
-6
lines changed

6 files changed

+142
-6
lines changed

llvm/utils/lit/lit/TestRunner.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,15 @@ def executeBuiltinDiff(cmd, cmd_shenv):
365365
"""executeBuiltinDiff - Compare files line by line."""
366366
args = expand_glob_expressions(cmd.args, cmd_shenv.cwd)[1:]
367367
try:
368-
opts, args = getopt.gnu_getopt(args, "wbur", ["strip-trailing-cr"])
368+
opts, args = getopt.gnu_getopt(args, "wbuU:r", ["strip-trailing-cr"])
369369
except getopt.GetoptError as err:
370370
raise InternalShellError(cmd, "Unsupported: 'diff': %s" % str(err))
371371

372372
filelines, filepaths, dir_trees = ([] for i in range(3))
373373
ignore_all_space = False
374374
ignore_space_change = False
375375
unified_diff = False
376+
num_context_lines = 3
376377
recursive_diff = False
377378
strip_trailing_cr = False
378379
for o, a in opts:
@@ -382,6 +383,16 @@ def executeBuiltinDiff(cmd, cmd_shenv):
382383
ignore_space_change = True
383384
elif o == "-u":
384385
unified_diff = True
386+
elif o.startswith("-U"):
387+
unified_diff = True
388+
try:
389+
num_context_lines = int(a)
390+
if num_context_lines < 0:
391+
raise ValueException
392+
except:
393+
raise InternalShellError(cmd,
394+
"Error: invalid '-U' argument: {}\n"
395+
.format(a))
385396
elif o == "-r":
386397
recursive_diff = True
387398
elif o == "--strip-trailing-cr":
@@ -433,12 +444,16 @@ def compareTwoBinaryFiles(filepaths):
433444
exitCode = 0
434445
if hasattr(difflib, 'diff_bytes'):
435446
# python 3.5 or newer
436-
diffs = difflib.diff_bytes(difflib.unified_diff, filelines[0], filelines[1], filepaths[0].encode(), filepaths[1].encode())
447+
diffs = difflib.diff_bytes(difflib.unified_diff, filelines[0],
448+
filelines[1], filepaths[0].encode(),
449+
filepaths[1].encode(),
450+
n = num_context_lines)
437451
diffs = [diff.decode() for diff in diffs]
438452
else:
439453
# python 2.7
440454
func = difflib.unified_diff if unified_diff else difflib.context_diff
441-
diffs = func(filelines[0], filelines[1], filepaths[0], filepaths[1])
455+
diffs = func(filelines[0], filelines[1], filepaths[0], filepaths[1],
456+
n = num_context_lines)
442457

443458
for diff in diffs:
444459
stdout.write(diff)
@@ -471,7 +486,8 @@ def compose2(f, g):
471486
filelines[idx]= [f(line) for line in lines]
472487

473488
func = difflib.unified_diff if unified_diff else difflib.context_diff
474-
for diff in func(filelines[0], filelines[1], filepaths[0], filepaths[1]):
489+
for diff in func(filelines[0], filelines[1], filepaths[0], filepaths[1],
490+
n = num_context_lines):
475491
stdout.write(diff)
476492
exitCode = 1
477493
return exitCode
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Check bad -U argument.
2+
# RUN: echo foo > %t
3+
# RUN: diff -U 30.1 %t %t
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Check bad -U argument.
2+
# RUN: echo foo > %t
3+
# RUN: diff -U-1 %t %t
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
# Fail so lit will print output.
34+
# RUN: false

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#
99
# END.
1010

11-
# CHECK: Failing Tests (27)
11+
# CHECK: Failing Tests (30)
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

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,86 @@
149149

150150
# CHECK: PASS: shtest-shell :: diff-r.txt
151151

152+
# CHECK: FAIL: shtest-shell :: diff-unified-error-0.txt
153+
# CHECK: *** TEST 'shtest-shell :: diff-unified-error-0.txt' FAILED ***
154+
# CHECK: $ "diff" "-U" "30.1" "{{[^"]*}}" "{{[^"]*}}"
155+
# CHECK: # command stderr:
156+
# CHECK: Error: invalid '-U' argument: 30.1
157+
# CHECK: error: command failed with exit status: 127
158+
# CHECK: ***
159+
160+
# CHECK: FAIL: shtest-shell :: diff-unified-error-1.txt
161+
# CHECK: *** TEST 'shtest-shell :: diff-unified-error-1.txt' FAILED ***
162+
# CHECK: $ "diff" "-U-1" "{{[^"]*}}" "{{[^"]*}}"
163+
# CHECK: # command stderr:
164+
# CHECK: Error: invalid '-U' argument: -1
165+
# CHECK: error: command failed with exit status: 127
166+
# CHECK: ***
167+
168+
169+
# CHECK: FAIL: shtest-shell :: diff-unified.txt
170+
171+
# CHECK: *** TEST 'shtest-shell :: diff-unified.txt' FAILED ***
172+
173+
# CHECK: $ "diff" "-u" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
174+
# CHECK: # command output:
175+
# CHECK: @@ {{.*}} @@
176+
# CHECK-NEXT: 3
177+
# CHECK-NEXT: 4
178+
# CHECK-NEXT: 5
179+
# CHECK-NEXT: -6 foo
180+
# CHECK-NEXT: +6 bar
181+
# CHECK-NEXT: 7
182+
# CHECK-NEXT: 8
183+
# CHECK-NEXT: 9
184+
# CHECK-EMPTY:
185+
# CHECK-NEXT: error: command failed with exit status: 1
186+
# CHECK-NEXT: $ "true"
187+
188+
# CHECK: $ "diff" "-U" "2" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
189+
# CHECK: # command output:
190+
# CHECK: @@ {{.*}} @@
191+
# CHECK-NEXT: 4
192+
# CHECK-NEXT: 5
193+
# CHECK-NEXT: -6 foo
194+
# CHECK-NEXT: +6 bar
195+
# CHECK-NEXT: 7
196+
# CHECK-NEXT: 8
197+
# CHECK-EMPTY:
198+
# CHECK-NEXT: error: command failed with exit status: 1
199+
# CHECK-NEXT: $ "true"
200+
201+
# CHECK: $ "diff" "-U4" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
202+
# CHECK: # command output:
203+
# CHECK: @@ {{.*}} @@
204+
# CHECK-NEXT: 2
205+
# CHECK-NEXT: 3
206+
# CHECK-NEXT: 4
207+
# CHECK-NEXT: 5
208+
# CHECK-NEXT: -6 foo
209+
# CHECK-NEXT: +6 bar
210+
# CHECK-NEXT: 7
211+
# CHECK-NEXT: 8
212+
# CHECK-NEXT: 9
213+
# CHECK-NEXT: 10
214+
# CHECK-EMPTY:
215+
# CHECK-NEXT: error: command failed with exit status: 1
216+
# CHECK-NEXT: $ "true"
217+
218+
# CHECK: $ "diff" "-U0" "{{[^"]*}}.foo" "{{[^"]*}}.bar"
219+
# CHECK: # command output:
220+
# CHECK: @@ {{.*}} @@
221+
# CHECK-NEXT: -6 foo
222+
# CHECK-NEXT: +6 bar
223+
# CHECK-EMPTY:
224+
# CHECK-NEXT: error: command failed with exit status: 1
225+
# CHECK-NEXT: $ "true"
226+
227+
# CHECK: $ "false"
228+
229+
# CHECK: ***
230+
231+
152232
# CHECK: FAIL: shtest-shell :: error-0.txt
153233
# CHECK: *** TEST 'shtest-shell :: error-0.txt' FAILED ***
154234
# CHECK: $ "not-a-real-command"
@@ -228,4 +308,4 @@
228308
# CHECK: PASS: shtest-shell :: sequencing-0.txt
229309
# CHECK: XFAIL: shtest-shell :: sequencing-1.txt
230310
# CHECK: PASS: shtest-shell :: valid-shell.txt
231-
# CHECK: Failing Tests (27)
311+
# CHECK: Failing Tests (30)

0 commit comments

Comments
 (0)