#!/usr/bin/env python3 """Run the current project's real-UART cocotb scenario.""" from __future__ import annotations import os from pathlib import Path import sys import xml.etree.ElementTree as ET ROOT = Path(__file__).resolve().parents[2] TEST_DIR = Path(__file__).resolve().parent sys.path.insert(0, str(ROOT)) TEMP_ROOT = Path(os.environ.get("TEMP", ROOT / "tmp")) BUILD_DIR = TEMP_ROOT / "cocotb" / "prod2u_door_temp_uart_e2e" RESULTS_XML = BUILD_DIR / "results.xml" def rtl_sources() -> list[Path]: from tools.prod2u_p4_normal_v3_build import PRODUCTION_RTL_SOURCES return [ROOT / relative for relative in PRODUCTION_RTL_SOURCES] def results_xml_has_failures(results: Path) -> bool: root = ET.parse(results).getroot() return bool( root.findall(".//failure") or root.findall(".//error") or sum(int(node.attrib.get("failures", "0")) for node in root.findall(".//testsuite")) or sum(int(node.attrib.get("errors", "0")) for node in root.findall(".//testsuite")) or int(root.attrib.get("failures", "0")) or int(root.attrib.get("errors", "0")) ) def main() -> int: from cocotb_tools.runner import get_runner runner = get_runner("icarus") runner.build( sources=[*rtl_sources(), TEST_DIR / "prod2u_door_temp_uart_top.v"], hdl_toplevel="prod2u_door_temp_uart_top", build_dir=BUILD_DIR, build_args=["-I", str(ROOT)], always=True, verbose=False, ) results = Path( runner.test( hdl_toplevel="prod2u_door_temp_uart_top", test_module="test_prod2u_door_temp_uart_e2e", build_dir=BUILD_DIR, test_dir=TEST_DIR, results_xml=str(RESULTS_XML), ) ) return 1 if not results.is_file() or results_xml_has_failures(results) else 0 if __name__ == "__main__": raise SystemExit(main())