blob: f5a31f2aa3a7e1fada5ccb08bf168e8fb9d39003 [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001#!/usr/bin/env python3
2
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):
16 prefix = " "
17 return "\n".join([prefix + line for line in text.split("\n")])
18
19
20def run_test(test_name, path):
21 os.chdir(path)
22 process = subprocess.Popen(
23 ["/bin/sh", "run.sh"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
24 (output, error) = process.communicate()
25
26 if os.path.exists("expected_fail"):
27 with open("expected_fail") as f:
28 expected_output = f.read()
29 if output != expected_output:
30 print("{} {}: expected output mismatch".format(
31 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 process.returncode != 0:
39 print("{} {}: unexpected failure:".format(prefix_fail, test_name))
40 print("")
41 print(indent(output))
42 return False
43
44 print("{} {}".format(prefix_pass, test_name))
45 return True
46
47root_dir = os.path.dirname(os.path.realpath(__file__))
48test_dir = os.path.join(root_dir, "tests")
49tests = os.listdir(test_dir)
50
51success = True
52for test in sorted(tests):
53 if not run_test(test, os.path.join(test_dir, test)):
54 success = False
55
56sys.exit(0 if success else 1)