|
| 1 | +#!/bin/env python3 |
| 2 | +import argparse |
| 3 | +from pathlib import Path |
| 4 | +from rich.console import Console |
| 5 | +from rich.table import Table |
| 6 | +from junitparser import JUnitXml, Failure, Error, Skipped |
| 7 | + |
| 8 | +err_console = Console(stderr=True) |
| 9 | +console = Console() |
| 10 | + |
| 11 | +failed_table = Table(show_lines=True) |
| 12 | +errored_table = Table(show_lines=True) |
| 13 | +skipped_table = Table(show_lines=True) |
| 14 | + |
| 15 | + |
| 16 | +def get_api_docs_url(test_xml_name, name): |
| 17 | + return f'https://cite.opengeospatial.org/te2/about/ogcapi-tiles-1.0/1.0/site/apidocs/index.html?{test_xml_name.replace(".","/")}.html ({name})' |
| 18 | + |
| 19 | + |
| 20 | +def add_table(table, tuples, title): |
| 21 | + table.add_column( |
| 22 | + "Case Name", justify="right", style="cyan", no_wrap=False, overflow="fold" |
| 23 | + ) |
| 24 | + table.add_column( |
| 25 | + "Error", justify="right", style="red", no_wrap=False, overflow="fold" |
| 26 | + ) |
| 27 | + table.add_column( |
| 28 | + "File", justify="right", style="magenta", no_wrap=False, overflow="fold" |
| 29 | + ) |
| 30 | + table.add_column( |
| 31 | + "Url", justify="right", style="orange1", no_wrap=False, overflow="fold" |
| 32 | + ) |
| 33 | + for case in tuples: |
| 34 | + table.add_row(case[1], case[2], str(case[0]), case[3]) |
| 35 | + |
| 36 | + table.title = title |
| 37 | + console.print(table) |
| 38 | + |
| 39 | + |
| 40 | +def main(result_dir, service_url, pretty_print, exit_on_fail): |
| 41 | + """ |
| 42 | + Parse junit result. |
| 43 | + """ |
| 44 | + |
| 45 | + failed_cases = [] |
| 46 | + failed_tuples = [] |
| 47 | + |
| 48 | + skipped_cases = [] |
| 49 | + skipped_tuples = [] |
| 50 | + |
| 51 | + errored_cases = [] |
| 52 | + errored_tuples = [] |
| 53 | + |
| 54 | + dir_path = Path(args.result_dir) |
| 55 | + for junit_test in dir_path.glob("**/**/TEST-org.opengis.cite.*.xml"): |
| 56 | + test_xml = JUnitXml.fromfile(str(junit_test)) |
| 57 | + |
| 58 | + failed = [ |
| 59 | + case |
| 60 | + for case in test_xml |
| 61 | + if any(isinstance(item, Failure) for item in case.result) |
| 62 | + ] |
| 63 | + failed_message = [result.message for fail in failed for result in fail.result] |
| 64 | + failed_tuples += [ |
| 65 | + ( |
| 66 | + junit_test.name, |
| 67 | + test_xml.name, |
| 68 | + result.message, |
| 69 | + get_api_docs_url(test_xml.name, fail.name), |
| 70 | + ) |
| 71 | + for fail in failed |
| 72 | + for result in fail.result |
| 73 | + ] |
| 74 | + if failed: |
| 75 | + fail_name = next(iter([fail.name for fail in failed]), "") |
| 76 | + failed_cases += ( |
| 77 | + [f"## {test_xml.name}"] |
| 78 | + + [""] |
| 79 | + + failed_message |
| 80 | + + [""] |
| 81 | + + [get_api_docs_url(test_xml.name, fail_name)] |
| 82 | + + [""] |
| 83 | + ) |
| 84 | + |
| 85 | + skipped = [ |
| 86 | + case |
| 87 | + for case in test_xml |
| 88 | + if any(isinstance(item, Skipped) for item in case.result) |
| 89 | + ] |
| 90 | + errored_or_skipped = [ |
| 91 | + case |
| 92 | + for case in test_xml |
| 93 | + if any(isinstance(item, Error) for item in case.result) |
| 94 | + ] |
| 95 | + if errored_or_skipped: |
| 96 | + for case in errored_or_skipped: |
| 97 | + # TestNG SkipExceptions are wrongfully marked as errored when using the JUnit reporter. |
| 98 | + # Turn these errored tests into skipped tests. |
| 99 | + if case.result[0].type == 'org.testng.SkipException': |
| 100 | + skipped += [case] |
| 101 | + else: |
| 102 | + # Not a SkipException so treat as errored |
| 103 | + errored = [case] |
| 104 | + errored_message = [ |
| 105 | + result.message for error in errored for result in error.result |
| 106 | + ] |
| 107 | + errored_tuples += [ |
| 108 | + ( |
| 109 | + junit_test.name, |
| 110 | + test_xml.name, |
| 111 | + result.message, |
| 112 | + get_api_docs_url(test_xml.name, error.name), |
| 113 | + ) |
| 114 | + for error in errored |
| 115 | + for result in error.result |
| 116 | + ] |
| 117 | + if errored: |
| 118 | + error_name = next(iter([error.name for error in errored]), "") |
| 119 | + errored_cases += ( |
| 120 | + [f"## {test_xml.name}"] |
| 121 | + + errored_message |
| 122 | + + [""] |
| 123 | + + [get_api_docs_url(test_xml.name, error_name)] |
| 124 | + + [""] |
| 125 | + ) |
| 126 | + |
| 127 | + # Handle skipped |
| 128 | + skipped_message = [result.message for skip in skipped for result in skip.result] |
| 129 | + skipped_tuples += [ |
| 130 | + ( |
| 131 | + junit_test.name, |
| 132 | + test_xml.name, |
| 133 | + result.message, |
| 134 | + get_api_docs_url(test_xml.name, skip.name), |
| 135 | + ) |
| 136 | + for skip in skipped |
| 137 | + for result in skip.result |
| 138 | + ] |
| 139 | + if skipped: |
| 140 | + skip_name = next(iter([skip.name for skip in skipped]), "") |
| 141 | + skipped_cases += ( |
| 142 | + [f"## {test_xml.name}"] |
| 143 | + + skipped_message |
| 144 | + + [""] |
| 145 | + + [get_api_docs_url(test_xml.name, skip_name)] |
| 146 | + + [""] |
| 147 | + ) |
| 148 | + |
| 149 | + if pretty_print: |
| 150 | + console.print( |
| 151 | + "ogcapi-tiles-1.0-1.1 Test Suite Run", |
| 152 | + style="bold italic underline", |
| 153 | + justify="center", |
| 154 | + ) |
| 155 | + console.print("\n") |
| 156 | + console.print(f"Output test run saved in: '{result_dir}'", justify="center") |
| 157 | + if service_url: |
| 158 | + console.print(f"Test instance: '{service_url}'", justify="center") |
| 159 | + console.print("\n") |
| 160 | + |
| 161 | + if failed_tuples: |
| 162 | + add_table( |
| 163 | + failed_table, failed_tuples, f"FAILED TEST CASES ({len(failed_tuples)})" |
| 164 | + ) |
| 165 | + |
| 166 | + if errored_tuples: |
| 167 | + add_table( |
| 168 | + errored_table, |
| 169 | + errored_tuples, |
| 170 | + f"ERRORED TEST CASES ({len(errored_tuples)})", |
| 171 | + ) |
| 172 | + |
| 173 | + if skipped_tuples: |
| 174 | + add_table( |
| 175 | + skipped_table, |
| 176 | + skipped_tuples, |
| 177 | + f"SKIPPED TEST CASES ({len(skipped_tuples)})", |
| 178 | + ) |
| 179 | + |
| 180 | + else: |
| 181 | + console.print("# ogcapi-tiles-1.0-1.1 Test Suite Run\n") |
| 182 | + console.print(f"- Output test run saved in: '{result_dir}'") |
| 183 | + if service_url: |
| 184 | + console.print(f"- Test instance: '{service_url}'") |
| 185 | + console.print("\n") |
| 186 | + |
| 187 | + if failed_cases: |
| 188 | + console.print("# FAILED TEST CASES\n", style="red") |
| 189 | + console.print("\n".join(failed_cases), style="red") |
| 190 | + |
| 191 | + if errored_cases: |
| 192 | + console.print("# ERRORED TEST CASES\n", style="orange1") |
| 193 | + console.print("\n".join(errored_cases), style="orange1") |
| 194 | + |
| 195 | + if skipped_cases: |
| 196 | + console.print("# SKIPPED TEST CASES\n", style="yellow") |
| 197 | + console.print("\n".join(skipped_cases), style="yellow") |
| 198 | + |
| 199 | + if failed_cases and exit_on_fail: |
| 200 | + exit(1) |
| 201 | + |
| 202 | + |
| 203 | +if __name__ == "__main__": |
| 204 | + parser = argparse.ArgumentParser(description="Parse OAF ETS results") |
| 205 | + parser.add_argument( |
| 206 | + "result_dir", type=str, help="Directory with the result to parse" |
| 207 | + ) |
| 208 | + parser.add_argument( |
| 209 | + "--service-url", help="Optional service url to print to console" |
| 210 | + ) |
| 211 | + parser.add_argument( |
| 212 | + "--pretty-print", action="store_true", help="Print with a better formatting" |
| 213 | + ) |
| 214 | + parser.add_argument( |
| 215 | + "--exit-on-fail", |
| 216 | + action="store_true", |
| 217 | + help="Force failing with exit code 1 when failed tests cases in result", |
| 218 | + ) |
| 219 | + args = parser.parse_args() |
| 220 | + |
| 221 | + dir_path = Path(args.result_dir) |
| 222 | + if not dir_path.exists(): |
| 223 | + err_console.print(f"test dir '{args.result_dir}' should exist") |
| 224 | + exit(1) |
| 225 | + |
| 226 | + main(args.result_dir, args.service_url, args.pretty_print, args.exit_on_fail) |
0 commit comments