| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
""" |
| 6 |
Simple script to combine JUnit test results into a single XML file. |
| 7 |
""" |
| 8 |
|
| 9 |
import argparse |
| 10 |
import os |
| 11 |
import re |
| 12 |
import sys |
| 13 |
from pathlib import Path |
| 14 |
from typing import Iterable, Pattern |
| 15 |
from xml.etree import ElementTree as ET |
| 16 |
|
| 17 |
|
| 18 |
def _find_all(name: Pattern, path: Path) -> Iterable[Path]: |
| 19 |
for obj in path.iterdir(): |
| 20 |
if obj.is_file() and re.match(name, obj.name): |
| 21 |
yield obj |
| 22 |
elif obj.is_dir(): |
| 23 |
yield from _find_all(name, obj) |
| 24 |
|
| 25 |
|
| 26 |
def _get_parser() -> argparse.ArgumentParser: |
| 27 |
"""Return the cmdline parser""" |
| 28 |
parser = argparse.ArgumentParser( |
| 29 |
description=__doc__, formatter_class=argparse.ArgumentDefaultsHelpFormatter |
| 30 |
) |
| 31 |
parser.add_argument( |
| 32 |
"directories", |
| 33 |
nargs="*", |
| 34 |
type=lambda args: [Path(arg) for arg in args], |
| 35 |
default=[Path()], |
| 36 |
help="Directories to search for input files.", |
| 37 |
) |
| 38 |
parser.add_argument( |
| 39 |
"-i", |
| 40 |
"--input-filename", |
| 41 |
default=r"results.*\.xml", |
| 42 |
help="A regular expression to match input filenames.", |
| 43 |
) |
| 44 |
parser.add_argument( |
| 45 |
"-o", |
| 46 |
"--output-file", |
| 47 |
default="combined_results.xml", |
| 48 |
help="Path of output XML file.", |
| 49 |
) |
| 50 |
parser.add_argument( |
| 51 |
"--output-testsuites-name", |
| 52 |
default="results", |
| 53 |
help="Name of 'testsuites' element in output XML file.", |
| 54 |
) |
| 55 |
parser.add_argument( |
| 56 |
"--verbose", |
| 57 |
action="store_true", |
| 58 |
help="Enables verbose output.", |
| 59 |
) |
| 60 |
parser.add_argument( |
| 61 |
"--repo-root", |
| 62 |
type=Path, |
| 63 |
help="Specify root of cocotb repo the regression is run from (CI only).", |
| 64 |
) |
| 65 |
return parser |
| 66 |
|
| 67 |
|
| 68 |
def main() -> int: |
| 69 |
parser = _get_parser() |
| 70 |
args = parser.parse_args() |
| 71 |
rc = 0 |
| 72 |
|
| 73 |
result = ET.Element("testsuites", name=args.output_testsuites_name) |
| 74 |
|
| 75 |
input_pattern = re.compile(args.input_filename) |
| 76 |
|
| 77 |
for directory in args.directories: |
| 78 |
if args.verbose: |
| 79 |
print(f"Searching in {directory} for results.xml files.") |
| 80 |
for fname in _find_all(input_pattern, directory): |
| 81 |
if args.verbose: |
| 82 |
print(f"Reading file {fname}.") |
| 83 |
tree = ET.parse(fname) |
| 84 |
for ts in tree.iter("testsuite"): |
| 85 |
if args.verbose: |
| 86 |
print( |
| 87 |
"Testsuite name: {!r}, package: {!r}".format( |
| 88 |
ts.get("name"), ts.get("package") |
| 89 |
) |
| 90 |
) |
| 91 |
for existing in result: |
| 92 |
if (existing.get("name") == ts.get("name")) and ( |
| 93 |
existing.get("package") == ts.get("package") |
| 94 |
): |
| 95 |
if args.verbose: |
| 96 |
print( |
| 97 |
"Testsuite already exists in combined results. Extending it." |
| 98 |
) |
| 99 |
existing.extend(list(ts)) |
| 100 |
break |
| 101 |
else: |
| 102 |
if args.verbose: |
| 103 |
print( |
| 104 |
"Testsuite does not already exist in combined results. Adding it." |
| 105 |
) |
| 106 |
result.append(ts) |
| 107 |
|
| 108 |
testsuite_count = 0 |
| 109 |
testcase_count = 0 |
| 110 |
for testsuite in result.iter("testsuite"): |
| 111 |
testsuite_count += 1 |
| 112 |
for testcase in testsuite.iter("testcase"): |
| 113 |
testcase_count += 1 |
| 114 |
for _ in testcase.iter("failure"): |
| 115 |
rc = 1 |
| 116 |
print( |
| 117 |
"Failure in testsuite: '{}' classname: '{}' testcase: '{}' with parameters '{}'".format( |
| 118 |
testsuite.get("name"), |
| 119 |
testcase.get("classname"), |
| 120 |
testcase.get("name"), |
| 121 |
testsuite.get("package"), |
| 122 |
) |
| 123 |
) |
| 124 |
if ( |
| 125 |
os.getenv("GITHUB_ACTIONS") is not None |
| 126 |
and args.repo_root is not None |
| 127 |
): |
| 128 |
|
| 129 |
file = testcase.get("file") |
| 130 |
|
| 131 |
assert file is not None |
| 132 |
relative_file = Path(file).relative_to(args.repo_root) |
| 133 |
print( |
| 134 |
"::error file={},line={}::Test {}:{} failed".format( |
| 135 |
relative_file, |
| 136 |
testcase.get("lineno"), |
| 137 |
testcase.get("classname"), |
| 138 |
testcase.get("name"), |
| 139 |
) |
| 140 |
) |
| 141 |
|
| 142 |
print(f"Ran a total of {testsuite_count} TestSuites and {testcase_count} TestCases") |
| 143 |
|
| 144 |
if args.verbose: |
| 145 |
print(f"Writing combined results to {args.output_file}") |
| 146 |
ET.ElementTree(result).write(args.output_file, encoding="UTF-8") |
| 147 |
return rc |
| 148 |
|
| 149 |
|
| 150 |
if __name__ == "__main__": |
| 151 |
rc = main() |
| 152 |
sys.exit(rc) |