From cea516175f5962b71e14c3782d483d5c37bb18ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannik=20Thei=C3=9F?= Date: Wed, 8 Jan 2020 11:46:19 +0100 Subject: [PATCH 1/2] introduce SysProp sbt.testing.legacyreport either create test reports with legacy file names (legacyreport=true) or with standard file names (legacyreport=false or omitted) but not both as suggested in #4451 --- main/src/main/scala/sbt/internal/SysProp.scala | 1 + .../main/scala/sbt/plugins/JUnitXmlReportPlugin.scala | 7 ++++++- .../src/main/scala/sbt/JUnitXmlTestsListener.scala | 11 ++++++----- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/main/src/main/scala/sbt/internal/SysProp.scala b/main/src/main/scala/sbt/internal/SysProp.scala index bbd0785a2..557fadb2d 100644 --- a/main/src/main/scala/sbt/internal/SysProp.scala +++ b/main/src/main/scala/sbt/internal/SysProp.scala @@ -70,6 +70,7 @@ object SysProp { def client: Boolean = getOrFalse("sbt.client") def ci: Boolean = getOrFalse("sbt.ci") def allowRootDir: Boolean = getOrFalse("sbt.rootdir") + def legacyTestReport: Boolean = getOrFalse("sbt.testing.legacyreport") def watchMode: String = sys.props.get("sbt.watch.mode").getOrElse("auto") diff --git a/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala b/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala index 842c8dd76..6537c9e2e 100644 --- a/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala +++ b/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala @@ -10,6 +10,7 @@ package plugins import Def.Setting import Keys._ +import sbt.internal.SysProp /** * An experimental plugin that adds the ability for junit-xml to be generated. @@ -30,6 +31,10 @@ object JUnitXmlReportPlugin extends AutoPlugin { // It might be a good idea to derive this setting into specific test scopes. override lazy val projectSettings: Seq[Setting[_]] = Seq( - testListeners += new JUnitXmlTestsListener(target.value.getAbsolutePath, streams.value.log) + testListeners += new JUnitXmlTestsListener( + target.value.getAbsolutePath, + streams.value.log, + SysProp.legacyTestReport + ) ) } diff --git a/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala b/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala index cb8db7338..c92765186 100644 --- a/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala +++ b/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala @@ -33,9 +33,10 @@ import sbt.protocol.testing.TestResult * report format. * @param outputDir path to the dir in which a folder with results is generated */ -class JUnitXmlTestsListener(val outputDir: String, logger: Logger) extends TestsListener { +class JUnitXmlTestsListener(val outputDir: String, logger: Logger, legacyTestReport: Boolean) + extends TestsListener { // This constructor is for binary compatibility with older versions of sbt. - def this(outputDir: String) = this(outputDir, null) + def this(outputDir: String) = this(outputDir, null, false) /**Current hostname so we know which machine executed the tests*/ val hostname: String = { @@ -248,14 +249,14 @@ class JUnitXmlTestsListener(val outputDir: String, logger: Logger) extends Tests d.truncatedTo(ChronoUnit.SECONDS).format(DateTimeFormatter.ISO_LOCAL_DATE_TIME) private def writeSuite(): Unit = { - val legacyFile = + val file = if (legacyTestReport) { new File(targetDir, s"${normalizeName(withTestSuite(_.name))}.xml").getAbsolutePath - val file = + } else { new File(targetDir, s"TEST-${normalizeName(withTestSuite(_.name))}.xml").getAbsolutePath + } // TODO would be nice to have a logger and log this with level debug // System.err.println("Writing JUnit XML test report: " + file) val testSuiteResult = withTestSuite(_.stop()) - XML.save(legacyFile, testSuiteResult, "UTF-8", xmlDecl = true, null) XML.save(file, testSuiteResult, "UTF-8", xmlDecl = true, null) testSuite.remove() } From af245d24943e70d1c96d57490dee9f8cc34ce617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannik=20Thei=C3=9F?= Date: Wed, 8 Jan 2020 15:29:48 +0100 Subject: [PATCH 2/2] SysProp sbt.testing.legacyreport: fix integration tests, JUnitXmlTestsListener backward binary compatibility --- .../src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala | 4 ++-- sbt/src/sbt-test/tests/junit-xml-report/build.sbt | 8 ++++---- testing/src/main/scala/sbt/JUnitXmlTestsListener.scala | 7 ++++--- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala b/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala index 6537c9e2e..39baa07a6 100644 --- a/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala +++ b/main/src/main/scala/sbt/plugins/JUnitXmlReportPlugin.scala @@ -33,8 +33,8 @@ object JUnitXmlReportPlugin extends AutoPlugin { Seq( testListeners += new JUnitXmlTestsListener( target.value.getAbsolutePath, - streams.value.log, - SysProp.legacyTestReport + SysProp.legacyTestReport, + streams.value.log ) ) } diff --git a/sbt/src/sbt-test/tests/junit-xml-report/build.sbt b/sbt/src/sbt-test/tests/junit-xml-report/build.sbt index 1d864c81b..a2f002d50 100644 --- a/sbt/src/sbt-test/tests/junit-xml-report/build.sbt +++ b/sbt/src/sbt-test/tests/junit-xml-report/build.sbt @@ -5,11 +5,11 @@ import Defaults._ val checkReport = taskKey[Unit]("Check the test reports") val checkNoReport = taskKey[Unit]("Check that no reports are present") -val oneSecondReportFile = "target/test-reports/a.pkg.OneSecondTest.xml" -val failingReportFile = "target/test-reports/another.pkg.FailingTest.xml" +val oneSecondReportFile = "target/test-reports/TEST-a.pkg.OneSecondTest.xml" +val failingReportFile = "target/test-reports/TEST-another.pkg.FailingTest.xml" -val flatSuiteReportFile = "target/test-reports/my.scalatest.MyFlatSuite.xml" -val nestedSuitesReportFile = "target/test-reports/my.scalatest.MyNestedSuites.xml" +val flatSuiteReportFile = "target/test-reports/TEST-my.scalatest.MyFlatSuite.xml" +val nestedSuitesReportFile = "target/test-reports/TEST-my.scalatest.MyNestedSuites.xml" val scalatest = "org.scalatest" %% "scalatest" % "3.0.5" val junitinterface = "com.novocode" % "junit-interface" % "0.11" diff --git a/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala b/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala index c92765186..d8e0bfa2b 100644 --- a/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala +++ b/testing/src/main/scala/sbt/JUnitXmlTestsListener.scala @@ -33,10 +33,11 @@ import sbt.protocol.testing.TestResult * report format. * @param outputDir path to the dir in which a folder with results is generated */ -class JUnitXmlTestsListener(val outputDir: String, logger: Logger, legacyTestReport: Boolean) +class JUnitXmlTestsListener(val outputDir: String, legacyTestReport: Boolean, logger: Logger) extends TestsListener { - // This constructor is for binary compatibility with older versions of sbt. - def this(outputDir: String) = this(outputDir, null, false) + // These constructors are for binary compatibility with older versions of sbt. + def this(outputDir: String, logger: Logger) = this(outputDir, false, logger) + def this(outputDir: String) = this(outputDir, false, null) /**Current hostname so we know which machine executed the tests*/ val hostname: String = {