Index: utils/lit/lit/TestRunner.py =================================================================== --- utils/lit/lit/TestRunner.py +++ utils/lit/lit/TestRunner.py @@ -558,6 +558,43 @@ exitCode = 1 return ShellCommandResult(cmd, "", stderr.getvalue(), exitCode, False) +def executeBuiltinCat(cmd, cmd_shenv): + """ + executeBuiltinCat - Prints contents of a file or merge and print + contents of several files. + """ + opened_files = [] + stdin, stdout, stderr = processRedirects(cmd, subprocess.PIPE, cmd_shenv, + opened_files) + if stdin != subprocess.PIPE or stderr != subprocess.PIPE: + raise InternalShellError( + cmd, "stdin and stderr redirects not supported for cat") + + isRedirected = True + if stdout == subprocess.PIPE: + stdout = StringIO() + isRedirected = False + + args = expand_glob_expressions(cmd.args, cmd_shenv.cwd)[1:] + exitCode = 0 + errMsg = "" + try: + for filename in args: + fileToCat = open(filename,"r") + stdout.write(fileToCat.read()) + fileToCat.close(); + except IOError as error: + exitCode = 1 + errMsg = str(error); + + for (name, mode, f, path) in opened_files: + f.close() + + contents = ""; + if (isRedirected == False): + contents = stdout.getvalue(); + return ShellCommandResult(cmd, contents, errMsg, exitCode, False) + def processRedirects(cmd, stdin_source, cmd_shenv, opened_files): """Return the standard fds for cmd after applying redirects @@ -740,6 +777,14 @@ results.append(cmdResult) return cmdResult.exitCode + if cmd.commands[0].args[0] == 'cat': + if len(cmd.commands) != 1: + raise InternalShellError(cmd.commands[0], "Unsupported: 'cat' " + "cannot be part of a pipeline") + cmdResult = executeBuiltinCat(cmd.commands[0], shenv) + results.append(cmdResult) + return cmdResult.exitCode + procs = [] default_stdin = subprocess.PIPE stderrTempFiles = [] Index: utils/lit/tests/Inputs/shtest-shell/cat-error-0.txt =================================================================== --- utils/lit/tests/Inputs/shtest-shell/cat-error-0.txt +++ utils/lit/tests/Inputs/shtest-shell/cat-error-0.txt @@ -0,0 +1,3 @@ +# Check error on a unsupported cat (cannot be part of a pipeline). +# +# RUN: cat cat-error-0.txt | echo Output Index: utils/lit/tests/Inputs/shtest-shell/cat-error-1.txt =================================================================== --- utils/lit/tests/Inputs/shtest-shell/cat-error-1.txt +++ utils/lit/tests/Inputs/shtest-shell/cat-error-1.txt @@ -0,0 +1,3 @@ +# Check error on a unsupported cat (Unable to find input file). +# +# RUN: cat temp1.txt Index: utils/lit/tests/Inputs/shtest-shell/valid-shell.txt =================================================================== --- utils/lit/tests/Inputs/shtest-shell/valid-shell.txt +++ utils/lit/tests/Inputs/shtest-shell/valid-shell.txt @@ -85,3 +85,31 @@ # RUN: cd %T/dir1 && echo "hello" > temp1.txt # RUN: cd %T/dir2 && echo "hello" > temp2.txt # RUN: diff temp2.txt ../dir1/temp1.txt +# +# Check cat command with single file. +# +# RUN: rm -rf %T/testCat +# RUN: mkdir -p %T/testCat +# RUN: echo "abcdefgh" > %T/testCat/temp.write +# RUN: cat %T/testCat/temp.write > %T/testCat/tempcat.write +# RUN: "%{python}" %S/check_path.py file %T/testCat/tempcat.write > %T/testCat/path.out +# RUN: FileCheck --check-prefix=FILE-EXISTS < %T/testCat/path.out %s +# RUN: FileCheck --check-prefix=CAT_OUTPUT < %T/testCat/tempcat.write %s +# FILE-EXISTS: True +# CAT_OUTPUT: abcdefgh +# +# Check cat command with multiple files. +# +# RUN: rm -rf %T/testCat +# RUN: mkdir -p %T/testCat +# RUN: echo "abcdefgh" > %T/testCat/temp1.write +# RUN: echo "efghijkl" > %T/testCat/temp2.write +# RUN: echo "mnopqrst" > %T/testCat/temp3.write +# RUN: cat %T/testCat/temp1.write %T/testCat/temp2.write %T/testCat/temp3.write > %T/testCat/tempmulticat.write +# RUN: "%{python}" %S/check_path.py file %T/testCat/tempmulticat.write > %T/testCat/path.out +# RUN: FileCheck --check-prefix=MULTI-FILE-EXISTS < %T/testCat/path.out %s +# RUN: FileCheck --check-prefix=MULTI-CAT_OUTPUT < %T/testCat/tempmulticat.write %s +# MULTI-FILE-EXISTS: True +# MULTI-CAT_OUTPUT: abcdefgh +# MULTI-CAT_OUTPUT-NEXT: efghijkl +# MULTI-CAT_OUTPUT-NEXT: mnopqrst Index: utils/lit/tests/max-failures.py =================================================================== --- utils/lit/tests/max-failures.py +++ utils/lit/tests/max-failures.py @@ -8,7 +8,7 @@ # # END. -# CHECK: Failing Tests (24) +# CHECK: Failing Tests (26) # CHECK: Failing Tests (1) # CHECK: Failing Tests (2) # CHECK: error: Setting --max-failures to 0 does not have any effect. Index: utils/lit/tests/shtest-shell.py =================================================================== --- utils/lit/tests/shtest-shell.py +++ utils/lit/tests/shtest-shell.py @@ -10,6 +10,21 @@ # CHECK: -- Testing: +# CHECK: FAIL: shtest-shell :: cat-error-0.txt +# CHECK: *** TEST 'shtest-shell :: cat-error-0.txt' FAILED *** +# CHECK: $ "cat" "cat-error-0.txt" +# CHECK: # command stderr: +# CHECK: Unsupported: 'cat' cannot be part of a pipeline +# CHECK: error: command failed with exit status: 127 +# CHECK: *** + +# CHECK: FAIL: shtest-shell :: cat-error-1.txt +# CHECK: *** TEST 'shtest-shell :: cat-error-1.txt' FAILED *** +# CHECK: $ "cat" "temp1.txt" +# CHECK: # command stderr: +# CHECK: [Errno 2] No such file or directory: 'temp1.txt' +# CHECK: error: command failed with exit status: 1 +# CHECK: *** # CHECK: FAIL: shtest-shell :: diff-error-0.txt # CHECK: *** TEST 'shtest-shell :: diff-error-0.txt' FAILED *** @@ -204,4 +219,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 (24) +# CHECK: Failing Tests (26)