diff --git a/main-actions/src/main/scala/sbt/Tests.scala b/main-actions/src/main/scala/sbt/Tests.scala index 61dda1875..c5b7c82d7 100644 --- a/main-actions/src/main/scala/sbt/Tests.scala +++ b/main-actions/src/main/scala/sbt/Tests.scala @@ -281,8 +281,23 @@ object Tests { if (orderedFilters.isEmpty) filtered0 else orderedFilters.flatMap(f => filtered0.filter(d => f(d.name))).toList.distinct val uniqueTests = distinctBy(tests)(_.name) + // When user ran testOnly with explicit args and the filter restricted the set, mark matching + // tests as explicitlySpecified so frameworks (e.g. ScalaTest) can run @DoNotDiscover suites. + // Do not mark when orderedFilters is "run all" (e.g. test with no args -> selectedFilter(Nil) -> match all). + val filteredToSubset = orderedFilters.nonEmpty && uniqueTests.size < discovered.size + val testsToUse = + if (!filteredToSubset) uniqueTests + else + uniqueTests.map(t => + new TestDefinition( + t.name, + t.fingerprint, + explicitlySpecified = true, + Array(new SuiteSelector: Selector) + ) + ) new ProcessedOptions( - uniqueTests.toVector, + testsToUse.toVector, setup.toVector, cleanup.toVector, testListeners.toVector @@ -555,7 +570,6 @@ object Tests { c.topLevel case _ => false }) - // TODO: To pass in correct explicitlySpecified and selectors val tests = for { (df, di) <- discovered diff --git a/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/build.sbt b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/build.sbt new file mode 100644 index 000000000..8cd9690fa --- /dev/null +++ b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/build.sbt @@ -0,0 +1,9 @@ +val scalatest = "org.scalatest" %% "scalatest" % "3.0.5" + +ThisBuild / scalaVersion := "2.12.21" + +lazy val root = (project in file(".")) + .settings( + libraryDependencies += scalatest, + Test / testOptions += Tests.Argument("-C", "custom.CustomReporter") + ) diff --git a/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/main/scala/custom/CustomReporter.scala b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/main/scala/custom/CustomReporter.scala new file mode 100644 index 000000000..d3f20a21b --- /dev/null +++ b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/main/scala/custom/CustomReporter.scala @@ -0,0 +1,30 @@ +package custom + +import java.io._ +import org.scalatest._ +import events._ + +class CustomReporter extends Reporter { + + private def writeFile(filePath: String, content: String): Unit = { + val file = new File(filePath) + val writer = + if (!file.exists) + new FileWriter(new File(filePath)) + else + new FileWriter(new File(filePath + "-2")) + writer.write(content) + writer.flush() + writer.close() + } + + def apply(event: Event): Unit = { + event match { + case SuiteStarting(_, suiteName, _, _, _, _, _, _, _, _) => writeFile("target/SuiteStarting-" + suiteName, suiteName) + case SuiteCompleted(_, suiteName, _, _, _, _, _, _, _, _, _) => writeFile("target/SuiteCompleted-" + suiteName, suiteName) + case TestStarting(_, _, _, _, testName, _, _, _, _, _, _, _) => writeFile("target/TestStarting-" + testName, testName) + case TestSucceeded(_, _, _, _, testName, _, _, _, _, _, _, _, _, _) => writeFile("target/TestSucceeded-" + testName, testName) + case _ => + } + } +} diff --git a/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/test/scala/com/test/TestSpec.scala b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/test/scala/com/test/TestSpec.scala new file mode 100644 index 000000000..05c155b63 --- /dev/null +++ b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/test/scala/com/test/TestSpec.scala @@ -0,0 +1,12 @@ +package com.test + +import org.scalatest.Spec + +class TestSpec extends Spec { + + def `TestSpec-test-1 ` {} + + def `TestSpec-test-2 ` {} + + def `TestSpec-test-3 ` {} +} diff --git a/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/test/scala/com/test/TestSpec2.scala b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/test/scala/com/test/TestSpec2.scala new file mode 100644 index 000000000..1ff28dffc --- /dev/null +++ b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/src/test/scala/com/test/TestSpec2.scala @@ -0,0 +1,13 @@ +package com.test + +import org.scalatest._ + +@DoNotDiscover +class TestSpec2 extends Spec { + + def `TestSpec2-test-1 ` {} + + def `TestSpec2-test-2 ` {} + + def `TestSpec2-test-3 ` {} +} diff --git a/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/test b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/test new file mode 100644 index 000000000..2391b2260 --- /dev/null +++ b/sbt-app/src/sbt-test/tests/i5609-do-not-discover-testonly/test @@ -0,0 +1,5 @@ +# #5609: When explicitly requested via testOnly, @DoNotDiscover suite should run +> clean +> testOnly com.test.TestSpec2 +$ exists target/SuiteStarting-TestSpec2 +$ exists target/SuiteCompleted-TestSpec2