Skip to content

Commit

Permalink
Merge pull request #2360 from KristianAN/bsp-testparamsdata-kinds
Browse files Browse the repository at this point in the history
Add support for BSP TestParamsData kinds
  • Loading branch information
tgodzik committed Jul 1, 2024
2 parents 79f9e6e + 20e7c71 commit 7154823
Show file tree
Hide file tree
Showing 3 changed files with 357 additions and 16 deletions.
45 changes: 43 additions & 2 deletions frontend/src/main/scala/bloop/bsp/BloopBspServices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ import bloop.util.JavaRuntime

import com.github.plokhotnyuk.jsoniter_scala.core._
import com.github.plokhotnyuk.jsoniter_scala.macros.JsonCodecMaker
import com.github.plokhotnyuk.jsoniter_scala.core.JsonValueCodec
import jsonrpc4s._
import com.github.plokhotnyuk.jsoniter_scala.core.readFromArray
import monix.execution.Cancelable
import monix.execution.CancelablePromise
import monix.execution.Scheduler
Expand Down Expand Up @@ -711,15 +713,54 @@ final class BloopBspServices(
}

def test(params: bsp.TestParams): BspEndpointResponse[bsp.TestResult] = {
def scalaTestSuitesByKind(kind: String): bsp.ScalaTestSuites =
kind match {
case bsp.TestParamsDataKind.ScalaTest =>
val scalaTestParams: Option[bsp.ScalaTestParams] =
params.data.map(raw => readFromArray[bsp.ScalaTestParams](raw.value))

val suites: List[bsp.ScalaTestSuiteSelection] =
scalaTestParams
.flatMap(
_.testClasses
.map(_.flatMap(item => item.classes))
.map(_.map(cls => bsp.ScalaTestSuiteSelection.apply(cls, Nil)))
)
.getOrElse(Nil)
bsp.ScalaTestSuites(suites, Nil, Nil)

case bsp.TestParamsDataKind.ScalaTestSuites =>
val scalaTestSuites: Option[List[bsp.ScalaTestSuiteSelection]] = params.data
.map { raw =>
readFromArray[List[String]](raw.value)(JsonCodecMaker.make[List[String]])
}
.map(_.map(className => bsp.ScalaTestSuiteSelection(className, Nil)))
bsp.ScalaTestSuites(scalaTestSuites.getOrElse(Nil), Nil, Nil)

case bsp.TestParamsDataKind.ScalaTestSuitesSelection =>
params.data
.map(raw => readFromArray[bsp.ScalaTestSuites](raw.value))
.getOrElse(bsp.ScalaTestSuites(Nil, Nil, Nil))

case _ => bsp.ScalaTestSuites(Nil, Nil, Nil)
}

def test(project: Project, state: State): Task[Tasks.TestRuns] = {
val testFilter = TestInternals.parseFilters(Nil) // Don't support test only for now
val scalaTestSuites: bsp.ScalaTestSuites = params.dataKind match {
case None => bsp.ScalaTestSuites(Nil, Nil, Nil)
case Some(kind) => scalaTestSuitesByKind(kind)
}

val testFilter =
TestInternals.parseFilters(Nil) // Does not handle filtering of tests, yet

val handler = new LoggingEventHandler(state.logger)
Tasks.test(
state,
List(project),
Nil,
testFilter,
bsp.ScalaTestSuites(Nil, Nil, Nil),
scalaTestSuites,
handler,
mode = RunMode.Normal
)
Expand Down
41 changes: 27 additions & 14 deletions frontend/src/main/scala/bloop/engine/tasks/TestTask.scala
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ object TestTask {
val fqn = taskDef.fullyQualifiedName()
!excluded(fqn) && testFilter(fqn)
}

if (logger.isVerbose) {
val allNames = ungroupedTests.map(_.taskDef.fullyQualifiedName).mkString(", ")
val includedNames = includedTests.map(_.taskDef.fullyQualifiedName).mkString(", ")
Expand All @@ -321,21 +322,33 @@ object TestTask {
// usually it is a Array(new SuiteSelector). However, if only subset of test are supposed to
// be run, then it can be altered to Array[TestSelector]
val selectedTests = testClasses.suites.map(entry => (entry.className, entry.tests)).toMap
includedTests.groupBy(_.framework).mapValues { taskDefs =>
taskDefs.map {
case TaskDefWithFramework(taskDef, _) =>
selectedTests.get(taskDef.fullyQualifiedName()).getOrElse(Nil) match {
case Nil => taskDef
case selectedTests =>
new TaskDef(
taskDef.fullyQualifiedName(),
taskDef.fingerprint(),
false,
selectedTests.map(test => new TestSelector(test)).toList.toArray
)
}

val testsToRun =
if (testClasses.suites.nonEmpty) {
includedTests
.filter(t => testClasses.suites.map(_.className).contains(t.taskDef.fullyQualifiedName))
} else {
includedTests
}

testsToRun
.groupBy(_.framework)
.mapValues { taskDefs =>
taskDefs.map {
case TaskDefWithFramework(taskDef, _) =>
selectedTests.get(taskDef.fullyQualifiedName()) match {
case None =>
taskDef
case Some(value) =>
new TaskDef(
taskDef.fullyQualifiedName(),
taskDef.fingerprint(),
false,
value.map(test => new TestSelector(test)).toList.toArray
)
}
}
}
}
}

private[bloop] def discoverTests(
Expand Down
Loading

0 comments on commit 7154823

Please sign in to comment.