Debian Codegraph @

src/cocotb_tools/check_results.py

raw symbols SCIP
1 # Copyright cocotb contributors
2 # Licensed under the Revised BSD License, see LICENSE for details.
3 # SPDX-License-Identifier: BSD-3-Clause
4 """Checks if a JUnit results file exists and whether there was failing tests."""
5
6 import argparse
7 import sys
8 from pathlib import Path
9 from typing import Tuple
10 from xml.etree import ElementTree
11
12
13 def get_results(results_xml_file: Path) -> Tuple[int, int]:
14 """Return number of tests and fails in *results_xml_file*.
15
16 Returns:
17 Tuple of number of tests and number of fails.
18
19 Raises:
20 RuntimeError: If *results_xml_file* is non-existent.
21 """
22
23 __tracebackhide__ = True # Hide the traceback when using PyTest.
24
25 if not results_xml_file.is_file():
26 raise RuntimeError(
27 f"ERROR: Simulation terminated abnormally. Results file {results_xml_file} not found."
28 )
29
30 num_tests = 0
31 num_failed = 0
32
33 tree = ElementTree.parse(results_xml_file)
34 for ts in tree.iter("testsuite"):
35 for tc in ts.iter("testcase"):
36 num_tests += 1
37 for _ in tc.iter("failure"):
38 num_failed += 1
39
40 return (num_tests, num_failed)
41
42
43 def _get_parser() -> argparse.ArgumentParser:
44 """Return the cmdline parser"""
45 parser = argparse.ArgumentParser(description=__doc__)
46 parser.add_argument(
47 "results_file", help="Path to XML file holding JUnit test results.", type=Path
48 )
49 return parser
50
51
52 def main() -> int:
53 parser = _get_parser()
54 args = parser.parse_args()
55
56 try:
57 (_, num_failed) = get_results(args.results_file)
58 except RuntimeError:
59 return 1
60 return num_failed
61
62
63 if __name__ == "__main__":
64 rc = main()
65 sys.exit(rc)