55 lines · python
1#!/usr/bin/env python2 3import sys4import subprocess5import traceback6import json7 8data = json.load(sys.stdin)9testfile = sys.argv[1]10 11prefix = "CHECK: "12 13fails = 014passes = 015with open(testfile) as testfh:16 lineno = 017 for line in iter(testfh.readline, ""):18 lineno += 119 line = line.rstrip("\r\n")20 try:21 prefix_pos = line.index(prefix)22 except ValueError:23 continue24 check_expr = line[prefix_pos + len(prefix) :]25 26 try:27 exception = None28 result = eval(check_expr, {"data": data})29 except Exception:30 result = False31 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, expr=check_expr, exception=exception38 )39 )40 fails += 141 elif not result:42 sys.stderr.write(43 "{file}:{line:d}: check returned False: {expr}\n".format(44 file=testfile, line=lineno, expr=check_expr45 )46 )47 fails += 148 else:49 passes += 150 51if fails != 0:52 sys.exit("{} checks failed".format(fails))53else:54 sys.stdout.write("{} checks passed\n".format(passes))55