From 090274e1ff775a3d406bc8fd13077fea5894ac6b Mon Sep 17 00:00:00 2001 From: Edgar Espina Date: Sun, 10 Sep 2023 12:38:11 -0300 Subject: [PATCH] build: fix failing test WithinMethodGCTest + better error reporting - We weren't synching test state with snapshhot for WithinMethodGCTest - On Harness find the first failing test and report that. So `ReadWriteTest` was failing bc the `UT_WithinMethodGCTest` error --- .../com/diffplug/selfie/junit5/Harness.kt | 42 ++++++++++--------- .../selfie/junit5/WithinMethodGCTest.kt | 12 ++++++ .../__snapshots__/UT_WithinMethodGCTest.ss | 4 +- 3 files changed, 38 insertions(+), 20 deletions(-) diff --git a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/Harness.kt b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/Harness.kt index 1980958f..56301dd2 100644 --- a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/Harness.kt +++ b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/Harness.kt @@ -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 @@ -194,7 +193,7 @@ open class Harness(subproject: String) { buildLauncher.run() return null } catch (e: BuildException) { - return parseBuildException(task) + return parseBuildException(task, e) } } @@ -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() - - // Value1 - 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(" ", "\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(" ", "\n") val lines = failure.textContent.replace(message, "").trim().split("\n") val stacktrace: MutableList = ArrayList() val tracePattern = diff --git a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt index cafcbb27..ab05eeca 100644 --- a/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt +++ b/selfie-runner-junit5/src/test/kotlin/com/diffplug/selfie/junit5/WithinMethodGCTest.kt @@ -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()) } } diff --git a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_WithinMethodGCTest.ss b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_WithinMethodGCTest.ss index 1dd2c432..aed42119 100644 --- a/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_WithinMethodGCTest.ss +++ b/undertest-junit5/src/test/kotlin/undertest/junit5/__snapshots__/UT_WithinMethodGCTest.ss @@ -1,3 +1,5 @@ -╔═ selfie2/leaf ═╗ +╔═ selfie ═╗ +root +╔═ selfie/leaf ═╗ maple ╔═ [end of file] ═╗