Skip to content

Commit

Permalink
tests: expect_exit.py tool to replace "test $?"
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislaw committed Apr 13, 2020
1 parent 142be2d commit 13e4500
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
2 changes: 2 additions & 0 deletions tests/integration/lit.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ config.substitutions.append(('%FILECHECK_TESTER_EXEC', "{}/tests/integration/too
cat_exec = "cat" if not is_windows() else "type"
config.substitutions.append(('%cat', cat_exec))

config.substitutions.append(('%expect_exit', 'python \"{}/tests/integration/tools/expect_exit.py\"'.format(current_dir)))

if real_only:
config.suffixes = ['.itest', '.itest-llvm', '.c']
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
; RUN: cat %S/filecheck.input | (%FILECHECK_EXEC %S/filecheck.check 2>&1; test $? = 2) | %FILECHECK_TESTER_EXEC %s --match-full-lines
; RUN: cat %S/filecheck.input | %expect_exit 2 %FILECHECK_EXEC "%S/filecheck.check" 2>&1 | %expect_exit 0 %FILECHECK_TESTER_EXEC "%s" --match-full-lines
; CHECK: {{^.*}}FileCheck{{(\.py)?$}}
; CHECK: {{.*}}filecheck.check:1:3: error: found 'CHECK-EMPTY' without previous 'CHECK: line{{$}}
; CHECK: {{^}}; CHECK-EMPTY:{{$}}
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/tools/expect_exit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# import os
import subprocess
import sys

if len(sys.argv) <= 2:
print("Expect arguments to be provided")
exit(1)

expected_exit_code_arg = sys.argv[1]
try:
expected_exit_code = int(expected_exit_code_arg)
if expected_exit_code < 0 or expected_exit_code > 127:
raise ValueError
except ValueError:
print("expected numeric exit code within range [0, 127]: {}".format(expected_exit_code_arg))
exit(1)

data = sys.stdin.readlines()

args = sys.argv.copy()

args.pop(0)
args.pop(0)

process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
for line in data:
process.stdin.write(line.encode())

output, error = process.communicate()

output_lines = output.decode('utf-8').split('\n')
del output_lines[-1]

for word in output_lines:
print(word)

if process.returncode == expected_exit_code:
exit(0)
else:
exit(1)

0 comments on commit 13e4500

Please sign in to comment.