[kde-linux/os-autoinst-distri-kdelinux] lib/sut: Set usable file paths in JUnit XML for reports
Thomas Duckworth <[email protected]>
| Newsgroups | gmane.comp.kde.cvs |
|---|---|
| Message-ID | <[email protected]> |
Git commit 16eec260aeb491d8f1431c118a306e1742cf1d20 by Thomas Duckworth. Committed on 15/08/2026 at 05:41. Pushed by tduck into branch 'master'. Set usable file paths in JUnit XML for reports Append `/extensions/openqa` to the beginning of file paths in JUnit XML files, so we're able to click through them in GitLab test reporting in order to view the test file that was run. M +27 -2 lib/sut/openqa_junit_xml.py https://invent.kde.org/kde-linux/os-autoinst-distri-kdelinux/-/commit/16eec260aeb491d8f1431c118a306e1742cf1d20 diff --git a/lib/sut/openqa_junit_xml.py b/lib/sut/openqa_junit_xml.py index 6245129..76cb912 100644 --- a/lib/sut/openqa_junit_xml.py +++ b/lib/sut/openqa_junit_xml.py @@ -2,19 +2,44 @@ # SPDX-FileCopyrightText: 2026 Thomas Duckworth <[email protected]> import unittest import xmlrunner +import xml.etree.ElementTree as ET import os import sys +from pathlib import Path -RESULTS_DIR = '/var/log/kde-linux-openqa' +_RESULTS_DIR = '/var/log/kde-linux-openqa' +_TEST_BASE_DIR = '/extensions/openqa' + + +def _label(xml: bytes) -> bytes: + root = ET.fromstring(xml) + for testcase in root.iter("testcase"): + original_path = testcase.get("file", "") + if original_path: + testcase.set( + "file", + str(Path(_TEST_BASE_DIR) / original_path.lstrip("/")), + ) + return ET.tostring(root, encoding="utf-8", xml_declaration=True) def run(test_class: type, name: str): """Run a unittest class and write JUnit XML to the expected results dir""" - output_dir = f'{RESULTS_DIR}/{name}' + output_dir = f'{_RESULTS_DIR}/{name}' output_path = f'{output_dir}/junit.xml' os.makedirs(output_dir, exist_ok=True) suite = unittest.TestLoader().loadTestsFromTestCase(test_class) with open(output_path, 'wb') as f: runner = xmlrunner.XMLTestRunner(output=f, verbosity=2) result = runner.run(suite) + + # Set correct file paths for tests + try: + with open(output_path, 'rb') as xml: + labelled_xml = _label(xml.read()) + with open(output_path, 'wb') as xml: + xml.write(labelled_xml) + except ET.ParseError as error: + print(f"skipping {name!r}: {error}") + sys.exit(0 if result.wasSuccessful() else 1)