-
-
Notifications
You must be signed in to change notification settings - Fork 175
/
trx.py
28 lines (21 loc) · 1 KB
/
trx.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
import pathlib
from typing import Iterable, Callable
from lxml import etree
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[[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:
return Exception(f'File is empty.')
try:
trx = etree.parse(path)
return transform_trx_to_junit(trx)
except BaseException as e:
return e
return [progress((result_file, parse(result_file))) for result_file in files]