xref: /netbsd-src/external/mpl/bind/dist/bin/tests/system/convert-junit-to-trs.py (revision 7bdf38e5b7a28439665f2fdeff81e36913eef7dd)
1#!/usr/bin/env python3
2#
3# Copyright (C) Internet Systems Consortium, Inc. ("ISC")
4#
5# SPDX-License-Identifier: MPL-2.0
6#
7# Convert JUnit pytest output to automake .trs files
8
9import argparse
10import sys
11from xml.etree import ElementTree
12
13
14def junit_to_trs(junit_xml):
15    root = ElementTree.fromstring(junit_xml)
16    testcases = root.findall(".//testcase")
17
18    if len(testcases) < 1:
19        print(":test-result: ERROR convert-junit-to-trs.py")
20        return 99
21
22    has_fail = False
23    has_error = False
24    has_skipped = False
25    for testcase in testcases:
26        filename = f"{testcase.attrib['classname'].replace('.', '/')}.py"
27        name = f"{filename}::{testcase.attrib['name']}"
28        res = "PASS"
29        for node in testcase:
30            if node.tag == "failure":
31                res = "FAIL"
32                has_fail = True
33            elif node.tag == "error":
34                res = "ERROR"
35                has_error = True
36            elif node.tag == "skipped":
37                if node.attrib.get("type") == "pytest.xfail":
38                    res = "XFAIL"
39                else:
40                    res = "SKIP"
41                    has_skipped = True
42        print(f":test-result: {res} {name}")
43
44    if has_error:
45        return 99
46    if has_fail:
47        return 1
48    if has_skipped:
49        return 77
50    return 0
51
52
53def main():
54    parser = argparse.ArgumentParser(
55        description="Convert JUnit XML to Automake TRS and exit with "
56        "the appropriate Automake-compatible exit code."
57    )
58    parser.add_argument(
59        "junit_file",
60        type=argparse.FileType("r", encoding="utf-8"),
61        help="junit xml result file",
62    )
63    args = parser.parse_args()
64
65    junit_xml = args.junit_file.read()
66    sys.exit(junit_to_trs(junit_xml))
67
68
69if __name__ == "__main__":
70    main()
71