Merge pull request #6418 from VlachJosef/develop

Instantiate only test runners needed by current TestDefinitions
This commit is contained in:
eugene yokota 2021-03-28 16:39:01 -04:00 committed by GitHub
commit 966dff9684
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 79 additions and 8 deletions

View File

@ -23,15 +23,13 @@ import sbt.internal.util.{ RunningProcesses, Terminal => UTerminal }
private[sbt] object ForkTests {
def apply(
runners: Map[TestFramework, Runner],
tests: Vector[TestDefinition],
opts: ProcessedOptions,
config: Execution,
classpath: Seq[File],
fork: ForkOptions,
log: Logger,
tags: (Tag, Int)*
): Task[TestOutput] = {
val opts = processOptions(config, tests, log)
import std.TaskExtra._
val dummyLoader = this.getClass.getClassLoader // can't provide the loader for test classes, which is in another jvm
def all(work: Seq[ClassLoader => Unit]) = work.fork(f => f(dummyLoader))
@ -46,6 +44,19 @@ private[sbt] object ForkTests {
}
}
def apply(
runners: Map[TestFramework, Runner],
tests: Vector[TestDefinition],
config: Execution,
classpath: Seq[File],
fork: ForkOptions,
log: Logger,
tags: (Tag, Int)*
): Task[TestOutput] = {
val opts = processOptions(config, tests, log)
apply(runners, opts, config, classpath, fork, log, tags: _*)
}
def apply(
runners: Map[TestFramework, Runner],
tests: Vector[TestDefinition],

View File

@ -308,11 +308,10 @@ object Tests {
frameworks: Map[TestFramework, Framework],
testLoader: ClassLoader,
runners: Map[TestFramework, Runner],
discovered: Vector[TestDefinition],
o: ProcessedOptions,
config: Execution,
log: ManagedLogger
): Task[Output] = {
val o = processOptions(config, discovered, log)
testTask(
testLoader,
frameworks,
@ -326,6 +325,18 @@ object Tests {
)
}
def apply(
frameworks: Map[TestFramework, Framework],
testLoader: ClassLoader,
runners: Map[TestFramework, Runner],
discovered: Vector[TestDefinition],
config: Execution,
log: ManagedLogger
): Task[Output] = {
val o = processOptions(config, discovered, log)
apply(frameworks, testLoader, runners, o, config, log)
}
def testTask(
loader: ClassLoader,
frameworks: Map[TestFramework, Framework],

View File

@ -1502,7 +1502,26 @@ object Defaults extends BuildCommon {
strategy: ClassLoaderLayeringStrategy,
projectId: String
): Initialize[Task[Tests.Output]] = {
val runners = createTestRunners(frameworks, loader, config)
val processedOptions: Map[Tests.Group, Tests.ProcessedOptions] =
groups
.map(
group => group -> Tests.processOptions(config, group.tests.toVector, s.log)
)
.toMap
val testDefinitions: Iterable[TestDefinition] = processedOptions.values.flatMap(_.tests)
val filteredFrameworks: Map[TestFramework, Framework] = frameworks.filter {
case (_, framework) =>
TestFramework.getFingerprints(framework).exists { t =>
testDefinitions.exists { test =>
TestFramework.matches(t, test.fingerprint)
}
}
}
val runners = createTestRunners(filteredFrameworks, loader, config)
val groupTasks = groups map { group =>
group.runPolicy match {
case Tests.SubProcess(opts) =>
@ -1511,7 +1530,7 @@ object Defaults extends BuildCommon {
s.log.debug(s"Forking tests - parallelism = ${forkedConfig.parallel}")
ForkTests(
runners,
group.tests.toVector,
processedOptions(group),
forkedConfig,
cp.files,
opts,
@ -1526,7 +1545,7 @@ object Defaults extends BuildCommon {
frameworks,
loader,
runners,
group.tests.toVector,
processedOptions(group),
config.copy(tags = config.tags ++ group.tags),
s.log
)

View File

@ -0,0 +1,9 @@
val scalatest = "org.scalatest" %% "scalatest" % "3.2.2"
val munit = "org.scalameta" %% "munit" % "0.7.22"
ThisBuild / scalaVersion := "2.12.12"
libraryDependencies += scalatest % Test
libraryDependencies += munit % Test
testFrameworks += new TestFramework("munit.Framework")

View File

@ -0,0 +1,9 @@
package example
import munit.FunSuite
class Spec extends FunSuite {
test("munit should work") {
assertEquals(1, 1)
}
}

View File

@ -0,0 +1,10 @@
package example
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
class ScalaTestSpec extends AnyFlatSpec with Matchers {
"ScalaTest" should "work" in {
1 shouldBe 1
}
}

View File

@ -0,0 +1,2 @@
> testOnly example.MunitSpec example.ScalaTestSpec
> testOnly example.MunitSpec -- "--tests=munit"