Index: llvm/utils/lit/lit/builtin_commands/diff.py =================================================================== --- llvm/utils/lit/lit/builtin_commands/diff.py +++ llvm/utils/lit/lit/builtin_commands/diff.py @@ -24,10 +24,17 @@ return path, sorted(child_trees) def compareTwoFiles(flags, filepaths): + # FIXME: How can we restart stdin if the encoding is wrong? How can we + # read stdin with a different encoding or in binary mode in a way that's + # compatible with python 2 and 3? compare_bytes = False encoding = None filelines = [] + has_stdin = False for file in filepaths: + if file == "-": + has_stdin = True + continue try: with open(file, 'r') as f: filelines.append(f.readlines()) @@ -39,7 +46,15 @@ except: compare_bytes = True + if has_stdin and encoding: + sys.stderr.write("Unsupported: cannot compare '-' with encoding {}\n" + .format(encoding)) + sys.exit(1) + if compare_bytes: + if has_stdin: + sys.stderr.write("Unsupported: cannot compare '-' as binary\n") + sys.exit(1) return compareTwoBinaryFiles(flags, filepaths) else: return compareTwoTextFiles(flags, filepaths, encoding) @@ -71,7 +86,9 @@ def compareTwoTextFiles(flags, filepaths, encoding): filelines = [] for file in filepaths: - if encoding is None: + if file == "-": + filelines.append(sys.stdin.readlines()) + elif encoding is None: with open(file, 'r') as f: filelines.append(f.readlines()) else: @@ -205,10 +222,13 @@ 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) Index: llvm/utils/lit/tests/Inputs/shtest-shell/diff-pipes.txt =================================================================== --- llvm/utils/lit/tests/Inputs/shtest-shell/diff-pipes.txt +++ 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: echo foo | diff -u - %t.foo +# RUN: echo foo | diff -u %t.foo - +# RUN: echo bar | diff -u %t.foo - && false || true +# RUN: echo bar | diff -u - %t.foo && false || true + +# Check output and input pipes at the same time. +# RUN: echo foo | diff - %t.foo | FileCheck -allow-empty -check-prefix=EMPTY %s +# RUN: echo bar | diff -u %t.foo - | FileCheck %s && false || true + # Fail so lit will print output. # RUN: false Index: llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-7.txt =================================================================== --- /dev/null +++ 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 Index: llvm/utils/lit/tests/Inputs/shtest-shell/diff-r-error-8.txt =================================================================== --- /dev/null +++ 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 - Index: llvm/utils/lit/tests/max-failures.py =================================================================== --- llvm/utils/lit/tests/max-failures.py +++ llvm/utils/lit/tests/max-failures.py @@ -8,7 +8,7 @@ # # END. -# CHECK: Failing Tests (27) +# CHECK: Failing Tests (29) # CHECK: Failing Tests (1) # CHECK: Failing Tests (2) # CHECK: error: Option '--max-failures' requires positive integer Index: llvm/utils/lit/tests/shtest-shell.py =================================================================== --- llvm/utils/lit/tests/shtest-shell.py +++ llvm/utils/lit/tests/shtest-shell.py @@ -106,6 +106,51 @@ # CHECK-NOT: error # CHECK: $ "true" +# CHECK: $ "echo" "foo" +# CHECK: $ "diff" "-u" "-" "{{[^"]*}}.foo" +# CHECK-NOT: note +# CHECK-NOT: error + +# CHECK: $ "echo" "foo" +# CHECK: $ "diff" "-u" "{{[^"]*}}.foo" "-" +# CHECK-NOT: note +# CHECK-NOT: error + +# CHECK: $ "echo" "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: $ "echo" "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: $ "echo" "foo" +# CHECK: $ "diff" "-" "{{[^"]*}}.foo" +# CHECK-NOT: note +# CHECK-NOT: error +# CHECK: $ "FileCheck" +# CHECK-NOT: note +# CHECK-NOT: error + +# CHECK: $ "echo" "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: *** @@ -164,6 +209,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 # CHECK: FAIL: shtest-shell :: error-0.txt @@ -245,4 +304,4 @@ # CHECK: PASS: shtest-shell :: sequencing-0.txt # CHECK: XFAIL: shtest-shell :: sequencing-1.txt # CHECK: PASS: shtest-shell :: valid-shell.txt -# CHECK: Failing Tests (27) +# CHECK: Failing Tests (29)