From 2a34cfae6742e114daa72adecc9e0b9227bf2774 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Tue, 14 Jun 2022 17:38:46 +0200 Subject: [PATCH] Introduce JUnitTreeOrException and ParsedJUnitFile --- python/publish/junit.py | 11 ++++++----- python/publish/trx.py | 10 +++++----- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/python/publish/junit.py b/python/publish/junit.py index 40b2ba55..4ae3972e 100644 --- a/python/publish/junit.py +++ b/python/publish/junit.py @@ -120,13 +120,15 @@ def close(self) -> Element: JUnitTree = etree.ElementTree +JUnitTreeOrException = Union[JUnitTree, BaseException] +ParsedJUnitFile = Tuple[str, JUnitTreeOrException] def parse_junit_xml_files(files: Iterable[str], drop_testcases: bool = False, - progress: Callable[[Tuple[str, Union[JUnitTree, BaseException]]], Tuple[str, Union[JUnitTree, BaseException]]] = lambda x: x) -> Iterable[Tuple[str, Union[JUnitTree, BaseException]]]: - """Parses junit xml files and returns aggregated statistics as a ParsedUnitTestResults.""" - def parse(path: str) -> Union[JUnitXml, BaseException]: + progress: Callable[[ParsedJUnitFile], ParsedJUnitFile] = lambda x: x) -> Iterable[ParsedJUnitFile]: + def parse(path: str) -> JUnitTreeOrException: + """Parses a junit xml file and returns either a JUnitTree or an Exception.""" if not os.path.exists(path): return FileNotFoundError(f'File does not exist.') if os.stat(path).st_size == 0: @@ -143,8 +145,7 @@ def parse(path: str) -> Union[JUnitXml, BaseException]: return [progress((result_file, parse(result_file))) for result_file in files] -def process_junit_xml_elems(trees: Iterable[Tuple[str, Union[JUnitTree, BaseException]]], - time_factor: float = 1.0) -> ParsedUnitTestResults: +def process_junit_xml_elems(trees: Iterable[ParsedJUnitFile], time_factor: float = 1.0) -> ParsedUnitTestResults: # TODO: move upstream into JUnitTree def create_junitxml(filepath: str, tree: JUnitTree) -> Union[JUnitXml, JUnitXmlError]: root_elem = tree.getroot() diff --git a/python/publish/trx.py b/python/publish/trx.py index 3a085f5a..77e3d31e 100644 --- a/python/publish/trx.py +++ b/python/publish/trx.py @@ -1,19 +1,19 @@ import os import pathlib -from typing import Iterable, Tuple, Union, Callable +from typing import Iterable, Callable from lxml import etree -from publish.junit import JUnitTree +from publish.junit import JUnitTreeOrException, ParsedJUnitFile with (pathlib.Path(__file__).parent / 'xslt' / 'trx-to-junit.xslt').open('r', encoding='utf-8') as r: transform_trx_to_junit = etree.XSLT(etree.parse(r)) def parse_trx_files(files: Iterable[str], - progress: Callable[[Tuple[str, Union[JUnitTree, BaseException]]], Tuple[str, Union[JUnitTree, BaseException]]] = lambda x: x) -> Iterable[Tuple[str, Union[JUnitTree, BaseException]]]: - """Parses trx files and returns aggregated statistics as a ParsedUnitTestResults.""" - def parse(path: str) -> Union[JUnitTree, BaseException]: + progress: Callable[[ParsedJUnitFile], ParsedJUnitFile] = lambda x: x) -> Iterable[ParsedJUnitFile]: + def parse(path: str) -> JUnitTreeOrException: + """Parses a trx file and returns either a JUnitTree or an Exception.""" if not os.path.exists(path): return FileNotFoundError(f'File does not exist.') if os.stat(path).st_size == 0: