blob: be87d6bc9e583e7cc7a37e6b24dad68474d3f8c6 [file] [log] [blame]
Josh Gao681f6b52016-07-20 11:23:20 -07001#!/usr/bin/env python2
Josh Gaobf8a2852016-05-27 11:59:09 -07002
3import os
4import subprocess
5import sys
6
7red = '\033[91m'
8green = '\033[92m'
9bold = '\033[1m'
10reset = '\033[0m'
11prefix_pass = bold + "[" + green + "PASS" + reset + bold + "]" + reset
12prefix_fail = bold + "[" + red + "FAIL" + reset + bold + "]" + reset
13
14
15def indent(text, spaces=4):
Josh Gao681f6b52016-07-20 11:23:20 -070016 text = text.decode("utf-8")
Josh Gaobf8a2852016-05-27 11:59:09 -070017 prefix = " "
18 return "\n".join([prefix + line for line in text.split("\n")])
19
20
21def run_test(test_name, path):
22 os.chdir(path)
23 process = subprocess.Popen(
Josh Gao658dbd92016-06-02 15:59:44 -070024 ["/bin/sh", "run.sh"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
25 (output, _) = process.communicate()
Josh Gaobf8a2852016-05-27 11:59:09 -070026
27 if os.path.exists("expected_fail"):
Josh Gao681f6b52016-07-20 11:23:20 -070028 with open("expected_fail", "rb") as f:
Josh Gaobf8a2852016-05-27 11:59:09 -070029 expected_output = f.read()
Josh Gao681f6b52016-07-20 11:23:20 -070030 if process.returncode == 0:
31 print("{} {}: unexpected success:".format(prefix_fail, test_name))
32 print("")
33 print(" Expected:")
34 print(indent(expected_output))
35 print(" Actual:")
36 print(indent(output))
37 return False
38 elif not output.endswith(expected_output):
Josh Gaobf8a2852016-05-27 11:59:09 -070039 print("{} {}: expected output mismatch".format(
40 prefix_fail, test_name))
41 print("")
42 print(" Expected:")
43 print(indent(expected_output))
44 print(" Actual:")
45 print(indent(output))
46 return False
47 elif process.returncode != 0:
48 print("{} {}: unexpected failure:".format(prefix_fail, test_name))
49 print("")
50 print(indent(output))
51 return False
52
53 print("{} {}".format(prefix_pass, test_name))
54 return True
55
56root_dir = os.path.dirname(os.path.realpath(__file__))
57test_dir = os.path.join(root_dir, "tests")
58tests = os.listdir(test_dir)
59
60success = True
61for test in sorted(tests):
Josh Gaob0af1002016-08-15 11:30:41 -070062 path = os.path.join(test_dir, test)
63 if not os.path.isdir(path):
64 continue
65 if not run_test(test, path):
Josh Gaobf8a2852016-05-27 11:59:09 -070066 success = False
67
68sys.exit(0 if success else 1)