diff options
author | Joel E. Denny <jdenny.ornl@gmail.com> | 2019-10-17 14:03:06 +0000 |
---|---|---|
committer | Joel E. Denny <dennyje@ornl.gov> | 2019-10-29 15:13:53 -0400 |
commit | 7c1d536c21c7d98be350ffab8353e202c14dcc93 (patch) | |
tree | 3d0c48435c1b13ca303cc4d2164db524a8c85845 | |
parent | b163806cdc317ee160a2ce694958c088a949fa7d (diff) | |
download | bcm5719-llvm-7c1d536c21c7d98be350ffab8353e202c14dcc93.tar.gz bcm5719-llvm-7c1d536c21c7d98be350ffab8353e202c14dcc93.zip |
[lit] Extend internal diff to support `-` argument
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 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 `-` as a command-line option. This patch adds support for
`-` to mean stdin.
Reviewed By: probinson, rnk
Differential Revision: https://reviews.llvm.org/D67643
-rw-r--r-- | llvm/utils/lit/lit/builtin_commands/diff.py | 14 | ||||
-rw-r--r-- | llvm/utils/lit/tests/Inputs/shtest-shell/diff-encodings.txt | 6 | ||||
-rw-r--r-- | llvm/utils/lit/tests/Inputs/shtest-shell/diff-pipes.txt | 10 | ||||
-rw-r--r-- | llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-7.txt | 2 | ||||
-rw-r--r-- | llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-8.txt | 2 | ||||
-rw-r--r-- | llvm/utils/lit/tests/max-failures.py | 2 | ||||
-rw-r--r-- | llvm/utils/lit/tests/shtest-shell.py | 115 |
7 files changed, 146 insertions, 5 deletions
diff --git a/llvm/utils/lit/lit/builtin_commands/diff.py b/llvm/utils/lit/lit/builtin_commands/diff.py index a29070c68ef..77bf5d78ca5 100644 --- a/llvm/utils/lit/lit/builtin_commands/diff.py +++ b/llvm/utils/lit/lit/builtin_commands/diff.py @@ -32,8 +32,13 @@ def getDirTree(path, basedir=""): def compareTwoFiles(flags, filepaths): filelines = [] for file in filepaths: - with open(file, 'rb') as file_bin: - filelines.append(file_bin.readlines()) + if file == "-": + stdin_fileno = sys.stdin.fileno() + with os.fdopen(os.dup(stdin_fileno), 'rb') as stdin_bin: + filelines.append(stdin_bin.readlines()) + else: + with open(file, 'rb') as file_bin: + filelines.append(file_bin.readlines()) try: return compareTwoTextFiles(flags, filepaths, filelines, @@ -222,10 +227,13 @@ def main(argv): exitCode = 0 try: for file in args: - if not os.path.isabs(file): + if file != "-" and not os.path.isabs(file): file = os.path.realpath(os.path.join(os.getcwd(), file)) if flags.recursive_diff: + if file == "-": + sys.stderr.write("Error: cannot recursively compare '-'\n") + sys.exit(1) dir_trees.append(getDirTree(file)) else: filepaths.append(file) diff --git a/llvm/utils/lit/tests/Inputs/shtest-shell/diff-encodings.txt b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-encodings.txt index d8b9718a099..044908f7725 100644 --- a/llvm/utils/lit/tests/Inputs/shtest-shell/diff-encodings.txt +++ b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-encodings.txt @@ -5,5 +5,11 @@ # RUN: diff -u diff-in.utf8 diff-in.bin && false || true # RUN: diff -u diff-in.bin diff-in.utf8 && false || true +# RUN: cat diff-in.bin | diff -u - diff-in.bin +# RUN: cat diff-in.bin | diff -u diff-in.bin - +# RUN: cat diff-in.bin | diff -u diff-in.utf16 - && false || true +# RUN: cat diff-in.bin | diff -u diff-in.utf8 - && false || true +# RUN: cat diff-in.bin | diff -u - diff-in.utf8 && false || true + # Fail so lit will print output. # RUN: false diff --git a/llvm/utils/lit/tests/Inputs/shtest-shell/diff-pipes.txt b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-pipes.txt index ce0abca1661..ed8eee4dc09 100644 --- a/llvm/utils/lit/tests/Inputs/shtest-shell/diff-pipes.txt +++ b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-pipes.txt @@ -5,6 +5,16 @@ # RUN: diff %t.foo %t.foo | FileCheck -allow-empty -check-prefix=EMPTY %s # RUN: diff -u %t.foo %t.bar | FileCheck %s && false || true +# Check input pipe. +# RUN: cat %t.foo | diff -u - %t.foo +# RUN: cat %t.foo | diff -u %t.foo - +# RUN: cat %t.bar | diff -u %t.foo - && false || true +# RUN: cat %t.bar | diff -u - %t.foo && false || true + +# Check output and input pipes at the same time. +# RUN: cat %t.foo | diff - %t.foo | FileCheck -allow-empty -check-prefix=EMPTY %s +# RUN: cat %t.bar | diff -u %t.foo - | FileCheck %s && false || true + # Fail so lit will print output. # RUN: false diff --git a/llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-7.txt b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-7.txt new file mode 100644 index 00000000000..08c50447a97 --- /dev/null +++ b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-7.txt @@ -0,0 +1,2 @@ +# diff -r currently cannot handle stdin. +# RUN: diff -r - %t diff --git a/llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-8.txt b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-8.txt new file mode 100644 index 00000000000..26361a91e89 --- /dev/null +++ b/llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-8.txt @@ -0,0 +1,2 @@ +# diff -r currently cannot handle stdin. +# RUN: diff -r %t - diff --git a/llvm/utils/lit/tests/max-failures.py b/llvm/utils/lit/tests/max-failures.py index 3b85ae18611..f661980ac2b 100644 --- a/llvm/utils/lit/tests/max-failures.py +++ b/llvm/utils/lit/tests/max-failures.py @@ -8,7 +8,7 @@ # # END. -# CHECK: Failing Tests (31) +# CHECK: Failing Tests (33) # CHECK: Failing Tests (1) # CHECK: Failing Tests (2) # CHECK: error: argument --max-failures: requires positive integer, but found '0' diff --git a/llvm/utils/lit/tests/shtest-shell.py b/llvm/utils/lit/tests/shtest-shell.py index 150c2fe362c..443cc10ef3b 100644 --- a/llvm/utils/lit/tests/shtest-shell.py +++ b/llvm/utils/lit/tests/shtest-shell.py @@ -81,6 +81,60 @@ # CHECK: error: command failed with exit status: 1 # CHECK: $ "true" +# CHECK: $ "cat" "diff-in.bin" +# CHECK-NOT: error +# CHECK: $ "diff" "-u" "-" "diff-in.bin" +# CHECK-NOT: error + +# CHECK: $ "cat" "diff-in.bin" +# CHECK-NOT: error +# CHECK: $ "diff" "-u" "diff-in.bin" "-" +# CHECK-NOT: error + +# CHECK: $ "cat" "diff-in.bin" +# CHECK-NOT: error +# CHECK: $ "diff" "-u" "diff-in.utf16" "-" +# CHECK: # command output: +# CHECK-NEXT: --- +# CHECK-NEXT: +++ +# CHECK-NEXT: @@ +# CHECK-NEXT: {{^ .f.o.o.$}} +# CHECK-NEXT: {{^-.b.a.r.$}} +# CHECK-NEXT: {{^\+.b.a.r..}} +# CHECK-NEXT: {{^ .b.a.z.$}} +# CHECK: error: command failed with exit status: 1 +# CHECK: $ "true" + +# CHECK: $ "cat" "diff-in.bin" +# CHECK-NOT: error +# CHECK: $ "diff" "-u" "diff-in.utf8" "-" +# CHECK: # command output: +# CHECK-NEXT: --- +# CHECK-NEXT: +++ +# CHECK-NEXT: @@ +# CHECK-NEXT: -foo +# CHECK-NEXT: -bar +# CHECK-NEXT: -baz +# CHECK-NEXT: {{^\+.f.o.o.$}} +# CHECK-NEXT: {{^\+.b.a.r..}} +# CHECK-NEXT: {{^\+.b.a.z.$}} +# CHECK: error: command failed with exit status: 1 +# CHECK: $ "true" + +# CHECK: $ "diff" "-u" "-" "diff-in.utf8" +# CHECK: # command output: +# CHECK-NEXT: --- +# CHECK-NEXT: +++ +# CHECK-NEXT: @@ +# CHECK-NEXT: {{^\-.f.o.o.$}} +# CHECK-NEXT: {{^\-.b.a.r..}} +# CHECK-NEXT: {{^\-.b.a.z.$}} +# CHECK-NEXT: +foo +# CHECK-NEXT: +bar +# CHECK-NEXT: +baz +# CHECK: error: command failed with exit status: 1 +# CHECK: $ "true" + # CHECK: $ "false" # CHECK: *** @@ -158,6 +212,51 @@ # CHECK-NOT: error # CHECK: $ "true" +# CHECK: $ "cat" "{{[^"]*}}.foo" +# CHECK: $ "diff" "-u" "-" "{{[^"]*}}.foo" +# CHECK-NOT: note +# CHECK-NOT: error + +# CHECK: $ "cat" "{{[^"]*}}.foo" +# CHECK: $ "diff" "-u" "{{[^"]*}}.foo" "-" +# CHECK-NOT: note +# CHECK-NOT: error + +# CHECK: $ "cat" "{{[^"]*}}.bar" +# CHECK: $ "diff" "-u" "{{[^"]*}}.foo" "-" +# CHECK: # command output: +# CHECK: @@ +# CHECK-NEXT: -foo +# CHECK-NEXT: +bar +# CHECK: error: command failed with exit status: 1 +# CHECK: $ "true" + +# CHECK: $ "cat" "{{[^"]*}}.bar" +# CHECK: $ "diff" "-u" "-" "{{[^"]*}}.foo" +# CHECK: # command output: +# CHECK: @@ +# CHECK-NEXT: -bar +# CHECK-NEXT: +foo +# CHECK: error: command failed with exit status: 1 +# CHECK: $ "true" + +# CHECK: $ "cat" "{{[^"]*}}.foo" +# CHECK: $ "diff" "-" "{{[^"]*}}.foo" +# CHECK-NOT: note +# CHECK-NOT: error +# CHECK: $ "FileCheck" +# CHECK-NOT: note +# CHECK-NOT: error + +# CHECK: $ "cat" "{{[^"]*}}.bar" +# CHECK: $ "diff" "-u" "{{[^"]*}}.foo" "-" +# CHECK: note: command had no output on stdout or stderr +# CHECK: error: command failed with exit status: 1 +# CHECK: $ "FileCheck" +# CHECK-NOT: note +# CHECK-NOT: error +# CHECK: $ "true" + # CHECK: $ "false" # CHECK: *** @@ -216,6 +315,20 @@ # CHECK: File {{.*}}dir1{{.*}}extra_file is a regular empty file while file {{.*}}dir2{{.*}}extra_file is a directory # CHECK: error: command failed with exit status: 1 +# CHECK: FAIL: shtest-shell :: diff-r-error-7.txt +# CHECK: *** TEST 'shtest-shell :: diff-r-error-7.txt' FAILED *** +# CHECK: $ "diff" "-r" "-" "{{[^"]*}}" +# CHECK: # command stderr: +# CHECK: Error: cannot recursively compare '-' +# CHECK: error: command failed with exit status: 1 + +# CHECK: FAIL: shtest-shell :: diff-r-error-8.txt +# CHECK: *** TEST 'shtest-shell :: diff-r-error-8.txt' FAILED *** +# CHECK: $ "diff" "-r" "{{[^"]*}}" "-" +# CHECK: # command stderr: +# CHECK: Error: cannot recursively compare '-' +# CHECK: error: command failed with exit status: 1 + # CHECK: PASS: shtest-shell :: diff-r.txt @@ -438,4 +551,4 @@ # CHECK: *** # CHECK: PASS: shtest-shell :: valid-shell.txt -# CHECK: Failing Tests (31) +# CHECK: Failing Tests (33) |