Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | |
| 3 | import os |
| 4 | import subprocess |
| 5 | import sys |
| 6 | |
| 7 | red = '\033[91m' |
| 8 | green = '\033[92m' |
| 9 | bold = '\033[1m' |
| 10 | reset = '\033[0m' |
| 11 | prefix_pass = bold + "[" + green + "PASS" + reset + bold + "]" + reset |
| 12 | prefix_fail = bold + "[" + red + "FAIL" + reset + bold + "]" + reset |
| 13 | |
| 14 | |
| 15 | def indent(text, spaces=4): |
| 16 | prefix = " " |
| 17 | return "\n".join([prefix + line for line in text.split("\n")]) |
| 18 | |
| 19 | |
| 20 | def 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 | |
| 47 | root_dir = os.path.dirname(os.path.realpath(__file__)) |
| 48 | test_dir = os.path.join(root_dir, "tests") |
| 49 | tests = os.listdir(test_dir) |
| 50 | |
| 51 | success = True |
| 52 | for test in sorted(tests): |
| 53 | if not run_test(test, os.path.join(test_dir, test)): |
| 54 | success = False |
| 55 | |
| 56 | sys.exit(0 if success else 1) |