Simon Tatham | 6a8c6ca | 2018-07-11 08:40:19 +0000 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import sys |
| 4 | import subprocess |
| 5 | import traceback |
| 6 | import json |
| 7 | |
| 8 | data = json.load(sys.stdin) |
| 9 | testfile = sys.argv[1] |
| 10 | |
| 11 | prefix = "CHECK: " |
| 12 | |
| 13 | fails = 0 |
| 14 | passes = 0 |
| 15 | with open(testfile) as testfh: |
| 16 | lineno = 0 |
| 17 | for line in iter(testfh.readline, ""): |
| 18 | lineno += 1 |
| 19 | line = line.rstrip("\r\n") |
| 20 | try: |
| 21 | prefix_pos = line.index(prefix) |
| 22 | except ValueError: |
| 23 | continue |
| 24 | check_expr = line[prefix_pos + len(prefix):] |
| 25 | |
| 26 | try: |
| 27 | exception = None |
| 28 | result = eval(check_expr, {"data":data}) |
| 29 | except Exception: |
| 30 | result = False |
| 31 | exception = traceback.format_exc().splitlines()[-1] |
| 32 | |
| 33 | if exception is not None: |
| 34 | sys.stderr.write( |
| 35 | "{file}:{line:d}: check threw exception: {expr}\n" |
| 36 | "{file}:{line:d}: exception was: {exception}\n".format( |
| 37 | file=testfile, line=lineno, |
| 38 | expr=check_expr, exception=exception)) |
| 39 | fails += 1 |
| 40 | elif not result: |
| 41 | sys.stderr.write( |
| 42 | "{file}:{line:d}: check returned False: {expr}\n".format( |
| 43 | file=testfile, line=lineno, expr=check_expr)) |
| 44 | fails += 1 |
| 45 | else: |
| 46 | passes += 1 |
| 47 | |
| 48 | if fails != 0: |
| 49 | sys.exit("{} checks failed".format(fails)) |
| 50 | else: |
| 51 | sys.stdout.write("{} checks passed\n".format(passes)) |