|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import re |
| 5 | +import sys |
| 6 | +from enum import Enum |
| 7 | + |
| 8 | + |
| 9 | +class ExitCode(Enum): |
| 10 | + PASS = 0 |
| 11 | + FAIL = 1 |
| 12 | + ERROR = 2 |
| 13 | + |
| 14 | + |
| 15 | +class Status(Enum): |
| 16 | + PASS = "pass" |
| 17 | + FAIL = "fail" |
| 18 | + ERROR = "error" |
| 19 | + |
| 20 | + |
| 21 | +TEST_FUNCTION = "ert-deftest" |
| 22 | +TEST_FAILED_FUNCTION = "ert-test-failed" |
| 23 | + |
| 24 | + |
| 25 | +def parse_test_functions(s: str): |
| 26 | + """ |
| 27 | + Retrieve test function names and code from regions like |
| 28 | + '(ert-deftest name-is-persistent |
| 29 | + "Test that robot name is persistent." |
| 30 | + (should (equal (robot-name *robbie*) |
| 31 | + (robot-name *robbie*))))' |
| 32 | + in the test file |
| 33 | + """ |
| 34 | + function_matches = re.finditer( |
| 35 | + fr"\({TEST_FUNCTION}\s+(?P<name>[\w-]+)\s+\(\)\s*(?P<docstring>\".*\")?\s*(?P<code>(?:\n.+)+)\)", |
| 36 | + s, |
| 37 | + ) |
| 38 | + names = [] |
| 39 | + code_pieces = [] |
| 40 | + for m in function_matches: |
| 41 | + names.append(m["name"]) |
| 42 | + code_pieces.append(m["code"].strip()) |
| 43 | + return names, code_pieces |
| 44 | + |
| 45 | + |
| 46 | +def parse_test_statuses(s: str): |
| 47 | + """ |
| 48 | + Retrieve test statuses from lines like |
| 49 | + 'passed 3/4 name-is-persistent (0.000049 sec)' |
| 50 | + in the test output |
| 51 | + """ |
| 52 | + status_matches = re.finditer( |
| 53 | + r"(?P<status>passed|FAILED)\s+(?P<number>\d+)\/\d+\s+(?P<name>[\w-]+)\s*(?:\(\d+\.\d+\ssec\))?", |
| 54 | + s, |
| 55 | + ) |
| 56 | + return { |
| 57 | + m["name"]: ( |
| 58 | + Status.PASS if m["status"].strip() == "passed" else Status.FAIL |
| 59 | + ) |
| 60 | + for m in status_matches |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | +def parse_test_message(name: str, s: str): |
| 65 | + """ |
| 66 | + Retrieve test messages from regions like |
| 67 | + 'Test name-can-be-reset condition: |
| 68 | + (wrong-type-argument hash-table-p nil) |
| 69 | + FAILED 2/4 name-can-be-reset' |
| 70 | + in the test output |
| 71 | + """ |
| 72 | + condition_matches = re.finditer( |
| 73 | + fr"Test\s{name}\scondition:\s+(?P<condition>\((?P<function>.+)(?:\n.+)+)FAILED\s+(?P<number>\d+)/\d+\s+{name}", |
| 74 | + s, |
| 75 | + ) |
| 76 | + try: |
| 77 | + cond_match = next(condition_matches) |
| 78 | + except StopIteration: |
| 79 | + return None, None |
| 80 | + message = cond_match["condition"].strip() |
| 81 | + # status is 'fail' if test condition starts with the test failed function |
| 82 | + # otherwise there is an error |
| 83 | + status = ( |
| 84 | + Status.FAIL |
| 85 | + if cond_match["function"] == TEST_FAILED_FUNCTION |
| 86 | + else Status.ERROR |
| 87 | + ) |
| 88 | + return message, status |
| 89 | + |
| 90 | + |
| 91 | +def parse_test_output(name: str, num: int, s: str): |
| 92 | + """ |
| 93 | + Retrieve test outputs from regions like |
| 94 | + 'Running 4 tests (2022-01-04 17:06:51+0200, selector ‘t’) |
| 95 | + "1DG190" |
| 96 | + passed 1/4 different-robots-have-different-names (0.000075 sec)' |
| 97 | + , |
| 98 | + ' passed 1/4 different-robots-have-different-names (0.000075 sec) |
| 99 | + "1XW454" |
| 100 | + passed 2/4 name-can-be-reset (0.000047 sec)' |
| 101 | + and |
| 102 | + ' passed 3/4 name-is-persistent (0.000049 sec) |
| 103 | + "1DG190" |
| 104 | + Test name-matches-expected-pattern backtrace:' |
| 105 | + in the test output |
| 106 | + """ |
| 107 | + status_line_regexp = fr"(?:passed|FAILED)\s+{num - 1}\/\d+\s+(?:[\w-]+)\s*(?:\(\d+\.\d+\ssec\))?" |
| 108 | + output_regexp = fr"(?P<output>(?:\n.*)+)\s*(?:passed\s+{num}|Test\s{name}\sbacktrace)" |
| 109 | + output_matches = re.finditer( |
| 110 | + (r"\)" if num == 1 else status_line_regexp) + output_regexp, s |
| 111 | + ) |
| 112 | + try: |
| 113 | + output_match = next(output_matches) |
| 114 | + except StopIteration: |
| 115 | + return None, None |
| 116 | + output = output_match["output"].strip() |
| 117 | + message = None |
| 118 | + # Output is limited to 500 chars |
| 119 | + if len(output) > 500: |
| 120 | + message = "Output was truncated. Please limit to 500 chars" |
| 121 | + output = output[:500] |
| 122 | + return output, message |
| 123 | + |
| 124 | + |
| 125 | +def run(test_file_path: str, test_output_file_path: str): |
| 126 | + exit_code = ExitCode.PASS |
| 127 | + with open(test_file_path, encoding="utf-8") as f: |
| 128 | + test_file_content = f.read() |
| 129 | + with open(test_output_file_path, encoding="utf-8") as f: |
| 130 | + test_output_file_content = f.read() |
| 131 | + names, code_pieces = parse_test_functions(test_file_content) |
| 132 | + name_to_number = {name: i + 1 for i, name in enumerate(sorted(names))} |
| 133 | + name_to_status = parse_test_statuses(test_output_file_content) |
| 134 | + status_to_exit_code = {Status(ec.name.lower()): ec for ec in ExitCode} |
| 135 | + tests = [] |
| 136 | + for name, code in zip(names, code_pieces): |
| 137 | + test = {} |
| 138 | + number = name_to_number[name] |
| 139 | + test["name"] = name |
| 140 | + test["test_code"] = code.strip() |
| 141 | + # get status from status line or assume it is syntax error if there is no one |
| 142 | + status = name_to_status.get(name, Status.ERROR) |
| 143 | + message = None |
| 144 | + condition_message, message_status = parse_test_message( |
| 145 | + name, test_output_file_content |
| 146 | + ) |
| 147 | + if condition_message: |
| 148 | + message, status = condition_message, message_status |
| 149 | + output, output_message = parse_test_output( |
| 150 | + name, int(number), test_output_file_content |
| 151 | + ) |
| 152 | + if output_message and status != Status.PASS: |
| 153 | + if message: |
| 154 | + message += "\n" + output_message |
| 155 | + else: |
| 156 | + message = output_message |
| 157 | + exit_code = max( |
| 158 | + exit_code, status_to_exit_code[status], key=lambda x: x.value |
| 159 | + ) |
| 160 | + test["status"] = status.value |
| 161 | + test["message"] = message |
| 162 | + if output: |
| 163 | + test["output"] = output |
| 164 | + tests.append(test) |
| 165 | + print(json.dumps(tests)) |
| 166 | + return exit_code |
| 167 | + |
| 168 | + |
| 169 | +if __name__ == "__main__": |
| 170 | + if len(sys.argv) < 3: |
| 171 | + print("./parse-tests.py <test-file> <test-output>", file=sys.stderr) |
| 172 | + sys.exit(ExitCode.ERROR.value) |
| 173 | + else: |
| 174 | + exit_code = run(*sys.argv[1:]) |
| 175 | + sys.exit(exit_code.value) |
0 commit comments