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 47c8a38
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 3 deletions.
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 | %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
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 $? = 1) | %FILECHECK_TESTER_EXEC %s --match-full-lines
; RUN: cat "%S/filecheck.input" | %expect_exit 1 %FILECHECK_EXEC "%S/filecheck.check" | %FILECHECK_TESTER_EXEC "%s" --match-full-lines
; CHECK: {{^.*}}FileCheck{{(\.py)?$}}
; CHECK: {{.*}}filecheck.check:2:15: error: CHECK-EMPTY: expected string not found in input{{$}}
; CHECK: {{^}}; CHECK-EMPTY:{{$}}
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 $? = 1) | %FILECHECK_TESTER_EXEC %s --strict-whitespace --match-full-lines
; RUN: cat "%S/filecheck.input" | %expect_exit 1 %FILECHECK_EXEC "%S/filecheck.check" | %FILECHECK_TESTER_EXEC "%s" --strict-whitespace --match-full-lines
; CHECK:{{^.*}}FileCheck{{(\.py)?$}}
; CHECK:{{.*}}filecheck.check:3:10: error: CHECK: expected string not found in input{{$}}
; CHECK:; CHECK: string2
Expand Down
49 changes: 49 additions & 0 deletions tests/integration/tools/expect_exit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import subprocess
import sys

if len(sys.argv) <= 2:
print("error: expect_exit: 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("error: expect_exit: expect 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)

# To capture the output from the subprocess we set up stderr to be written to
# stdout. This ensures that we see the output from the subprocess in the same
# order as we do in a shell however this does not allow us to capture what is
# actually stdout and what is stderr.
process = subprocess.Popen(args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
for line in data:
process.stdin.write(line.encode())

stdout, stderr = process.communicate()

output_lines = stdout.decode('utf-8').split('\n')
# The last '\n' never belongs to the output, it is produced by decode-split.
if len(output_lines) > 0:
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 47c8a38

Please sign in to comment.