Commit f4edce12 authored by Joel E. Denny's avatar Joel E. Denny
Browse files

[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

llvm-svn: 374390
parent 19e6bb25
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -27,6 +27,11 @@ def getDirTree(path, basedir=""):
def compareTwoFiles(flags, filepaths):
    filelines = []
    for file in filepaths:
        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())

@@ -194,10 +199,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)
+6 −0
Original line number Diff line number Diff line
@@ -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
+10 −0
Original line number Diff line number Diff line
@@ -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

+2 −0
Original line number Diff line number Diff line
# diff -r currently cannot handle stdin.
# RUN: diff -r - %t
+2 −0
Original line number Diff line number Diff line
# diff -r currently cannot handle stdin.
# RUN: diff -r %t -
Loading