| Josh Gao | 681f6b5 | 2016-07-20 11:23:20 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2 | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 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): | 
| Josh Gao | 681f6b5 | 2016-07-20 11:23:20 -0700 | [diff] [blame] | 16 | text = text.decode("utf-8") | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 17 | prefix = "    " | 
|  | 18 | return "\n".join([prefix + line for line in text.split("\n")]) | 
|  | 19 |  | 
|  | 20 |  | 
|  | 21 | def run_test(test_name, path): | 
|  | 22 | os.chdir(path) | 
|  | 23 | process = subprocess.Popen( | 
| Josh Gao | 658dbd9 | 2016-06-02 15:59:44 -0700 | [diff] [blame] | 24 | ["/bin/sh", "run.sh"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) | 
|  | 25 | (output, _) = process.communicate() | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 26 |  | 
|  | 27 | if os.path.exists("expected_fail"): | 
| Josh Gao | 681f6b5 | 2016-07-20 11:23:20 -0700 | [diff] [blame] | 28 | with open("expected_fail", "rb") as f: | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 29 | expected_output = f.read() | 
| Josh Gao | 681f6b5 | 2016-07-20 11:23:20 -0700 | [diff] [blame] | 30 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 39 | 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 |  | 
| Josh Gao | 9ab148c | 2016-08-15 14:19:05 -0700 | [diff] [blame] | 56 |  | 
|  | 57 | def usage(): | 
|  | 58 | print("Usage: run_tests.py [-f]") | 
|  | 59 | print("    -f\t\tdon't run slow tests") | 
|  | 60 | sys.exit(0) | 
|  | 61 |  | 
|  | 62 |  | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 63 | root_dir = os.path.dirname(os.path.realpath(__file__)) | 
|  | 64 | test_dir = os.path.join(root_dir, "tests") | 
|  | 65 | tests = os.listdir(test_dir) | 
| Josh Gao | 9ab148c | 2016-08-15 14:19:05 -0700 | [diff] [blame] | 66 | run_slow = True | 
|  | 67 |  | 
|  | 68 | if len(sys.argv) > 2: | 
|  | 69 | usage() | 
|  | 70 | elif len(sys.argv) == 2: | 
|  | 71 | if sys.argv[1] != "-f": | 
|  | 72 | usage() | 
|  | 73 | run_slow = False | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 74 |  | 
|  | 75 | success = True | 
|  | 76 | for test in sorted(tests): | 
| Josh Gao | 9ab148c | 2016-08-15 14:19:05 -0700 | [diff] [blame] | 77 | if test.startswith("slow") and not run_slow: | 
|  | 78 | continue | 
| Josh Gao | b0af100 | 2016-08-15 11:30:41 -0700 | [diff] [blame] | 79 | path = os.path.join(test_dir, test) | 
|  | 80 | if not os.path.isdir(path): | 
|  | 81 | continue | 
|  | 82 | if not run_test(test, path): | 
| Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 83 | success = False | 
|  | 84 |  | 
|  | 85 | sys.exit(0 if success else 1) |