1+ import org.w3c.dom.NodeList
2+ import java.nio.file.Path
3+ import javax.xml.parsers.DocumentBuilderFactory
4+ import javax.xml.xpath.XPathConstants
5+ import javax.xml.xpath.XPathFactory
6+ import kotlin.io.path.extension
7+ import kotlin.io.path.name
8+ import kotlin.io.path.walk
9+
10+ object JUnitGithubAnnotations {
11+ private val xqryTestcase by lazy {
12+ val xPath = XPathFactory .newInstance().newXPath()
13+ val expression = " /testsuite/testcase[./failure]"
14+ xPath.compile(expression)
15+ }
16+
17+ private val xqryFailureMessage by lazy {
18+ val xPath = XPathFactory .newInstance().newXPath()
19+ val expression = " ./failure/@message"
20+ xPath.compile(expression)
21+ }
22+
23+ @JvmStatic
24+ fun readAndPrint (f : Path ) {
25+ val builder = DocumentBuilderFactory .newInstance().newDocumentBuilder()
26+ val doc = builder.parse(f.toFile())
27+ val nodeList = xqryTestcase.evaluate(doc, XPathConstants .NODESET ) as NodeList
28+
29+ if (nodeList.length <= 0 ) {
30+ return
31+ }
32+
33+
34+ // module name assuming <mod>/build/test-results/test/<file>.xml
35+ val base = f.parent.parent.parent.parent.name
36+ for (i in 0 until nodeList.length) {
37+ val node = nodeList.item(i)
38+ // failure in test case
39+ val failure = xqryFailureMessage.evaluate(node, XPathConstants .STRING ) as String?
40+ val attributes = node.attributes
41+ val caseName = attributes.getNamedItem(" name" ).textContent
42+ val classname = attributes.getNamedItem(" classname" ).textContent
43+ val filename = " $base /${classname.replace(" ." , " /" )} "
44+ val message = failure?.take(80 ) ? : " "
45+ // ::error file={name},line={line},endLine={endLine}, ={title}::{message}
46+ print (" ::error title=Testcase-missed,file=$filename ::Error in test case '$caseName ' and $message " )
47+ }
48+ }
49+
50+ @JvmStatic
51+ fun readAndPrintAll (root : Path ) {
52+ val reports = root.fileSystem.getPathMatcher(" glob:TEST-*xml" )
53+ root.walk()
54+ .filter { it.extension == " xml" }
55+ .filter { reports.matches(it) }
56+ .forEach { readAndPrint(it) }
57+ }
58+ }
0 commit comments