|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import subprocess |
| 4 | +import sys |
| 5 | +import re |
| 6 | + |
| 7 | + |
| 8 | +class Options: |
| 9 | + def __init__(self): |
| 10 | + self.cmd_binary = sys.argv[1] |
| 11 | + self.test_name = sys.argv[2] |
| 12 | + self.cmd_options = sys.argv[3:] |
| 13 | + self.outfile = self.test_name + "-out-file.out" |
| 14 | + self.outfile_rules = self.test_name + "-out-file-rules.txt" |
| 15 | + |
| 16 | + def __str__(self): |
| 17 | + return """ |
| 18 | + cmd_binary: {}, |
| 19 | + test_name: {}, |
| 20 | + cmd_options: {}, |
| 21 | + outfile: {}, |
| 22 | + outfile_rules: {}, |
| 23 | + """.format(self.cmd_binary, |
| 24 | + self.test_name, |
| 25 | + self.cmd_options, |
| 26 | + self.outfile, |
| 27 | + self.outfile_rules) |
| 28 | + |
| 29 | + |
| 30 | +def try_compile_regex(regex_content): |
| 31 | + try: |
| 32 | + return re.compile(regex_content) |
| 33 | + except re.error: |
| 34 | + print("Bad regex: {}".format(regex_content)) |
| 35 | + raise |
| 36 | + |
| 37 | + |
| 38 | +def check_outfile(options): |
| 39 | + with open(options.outfile) as outfile_f: |
| 40 | + with open(options.outfile_rules) as outfile_rules_f: |
| 41 | + unprocessed_outfile = outfile_f.readlines() |
| 42 | + outfile_rules = [try_compile_regex(c) for c in outfile_rules_f.readlines()] |
| 43 | + for rule in outfile_rules: |
| 44 | + found = False |
| 45 | + while not found: |
| 46 | + if len(unprocessed_outfile) == 0: |
| 47 | + return False |
| 48 | + match = rule.match(unprocessed_outfile[0]) |
| 49 | + found = match is not None |
| 50 | + unprocessed_outfile = unprocessed_outfile[1:] |
| 51 | + return True |
| 52 | + |
| 53 | + |
| 54 | +def main(): |
| 55 | + options = Options() |
| 56 | + cmd_options = [options.outfile if item == '%out-file-name%' else item for item in |
| 57 | + options.cmd_options] |
| 58 | + cmd_line = [options.cmd_binary] + cmd_options |
| 59 | + print("Running: ", cmd_line) |
| 60 | + subprocess.run(cmd_line) |
| 61 | + print("\n--- Checking outfiles ---") |
| 62 | + if check_outfile(options): |
| 63 | + print("Output file matches.") |
| 64 | + sys.exit(0) |
| 65 | + else: |
| 66 | + print("Output file does not match.") |
| 67 | + sys.exit(1) |
| 68 | + |
| 69 | + |
| 70 | +if __name__ == "__main__": |
| 71 | + main() |
0 commit comments