Skip to content

Commit

Permalink
Merge pull request #20 from diffplug/fixbuild
Browse files Browse the repository at this point in the history
build: fix failing test WithinMethodGCTest + better error reporting
  • Loading branch information
jknack authored Sep 11, 2023
2 parents adf443e + 090274e commit 43d7048
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.diffplug.selfie.junit5

import io.kotest.matchers.shouldBe
import java.io.StringReader
import java.util.regex.Matcher
import java.util.regex.Pattern
import javax.xml.parsers.DocumentBuilderFactory
Expand Down Expand Up @@ -194,7 +193,7 @@ open class Harness(subproject: String) {
buildLauncher.run()
return null
} catch (e: BuildException) {
return parseBuildException(task)
return parseBuildException(task, e)
}
}

Expand All @@ -204,27 +203,32 @@ open class Harness(subproject: String) {
*
* Parses the exception message as well as stacktrace.
*/
private fun parseBuildException(task: String): AssertionFailedError {
val xmlFile =
private fun parseBuildException(task: String, e: BuildException): AssertionFailedError {
val xpFactory = XPathFactory.newInstance()
val xPath = xpFactory.newXPath()
val failure =
subprojectFolder
.resolve("build")
.resolve("test-results")
.resolve(task)
.resolve("TEST-" + task.lowercase() + ".junit5.UT_" + javaClass.simpleName + ".xml")
val xml = FileSystem.SYSTEM.read(xmlFile) { readUtf8() }
val dbFactory = DocumentBuilderFactory.newInstance()
val dBuilder = dbFactory.newDocumentBuilder()
val xmlInput = InputSource(StringReader(xml))
val doc = dBuilder.parse(xmlInput)
val xpFactory = XPathFactory.newInstance()
val xPath = xpFactory.newXPath()

// <item type="T1" count="1">Value1</item>
val xpath = "/testsuite/testcase/failure"
val failures = xPath.evaluate(xpath, doc, XPathConstants.NODESET) as NodeList
val failure = failures.item(0)
val type = failure.attributes.getNamedItem("type").nodeValue
val message = failure.attributes.getNamedItem("message").nodeValue.replace("&#10;", "\n")
.toFile()
.walkTopDown()
.filter { it.isFile && it.name.endsWith(".xml") }
.mapNotNull {
val dbFactory = DocumentBuilderFactory.newInstance()
val dBuilder = dbFactory.newDocumentBuilder()
it.inputStream().use { source -> dBuilder.parse(InputSource(source)) }
}
.mapNotNull {
val xpath = "/testsuite/testcase/failure"
val failures = xPath.evaluate(xpath, it, XPathConstants.NODESET) as NodeList
failures.item(0)
}
.firstOrNull()
?: return AssertionFailedError("Unable to find exception: " + e.stackTraceToString())
val attributes = failure!!.attributes
val type = attributes.getNamedItem("type").nodeValue
val message = attributes.getNamedItem("message").nodeValue.replace("&#10;", "\n")
val lines = failure.textContent.replace(message, "").trim().split("\n")
val stacktrace: MutableList<StackTraceElement> = ArrayList()
val tracePattern =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,17 @@ class WithinMethodGCTest : Harness("undertest-junit5") {
ut_mirror().lineWith("root").uncomment()
ut_mirror().lineWith("selfie2()").commentOut()
ut_mirror().lineWith("selfie()").uncomment()
gradleWriteSS()
ut_snapshot()
.assertContent(
"""
╔═ selfie ═╗
root
╔═ selfie/leaf ═╗
maple
╔═ [end of file] ═╗
"""
.trimIndent())
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
╔═ selfie2/leaf ═╗
╔═ selfie ═╗
root
╔═ selfie/leaf ═╗
maple
╔═ [end of file] ═╗

0 comments on commit 43d7048

Please sign in to comment.