Skip to content

Commit

Permalink
add bleep list-tests <...projects> command (#387)
Browse files Browse the repository at this point in the history
also continue with warning if BSP wanted to test/compile a non-existing project
  • Loading branch information
oyvindberg authored Jun 16, 2024
1 parent 37bb98e commit 37b3069
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 10 deletions.
5 changes: 5 additions & 0 deletions bleep-cli/src/scala/bleep/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ object Main {
commands.Test(watch, projectNames)
}
),
Opts.subcommand("list-tests", "list tests in projects")(
testProjectNames.map { projectNames =>
commands.ListTests(projectNames)
}
),
Opts.subcommand("run", "run project")(
(
projectName,
Expand Down
17 changes: 8 additions & 9 deletions bleep-core/src/scala/bleep/BleepCommandRemote.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package bleep

import bleep.bsp.{BleepRifleLogger, BspCommandFailed, SetupBloopRifle}
import bleep.internal.{BspClientDisplayProgress, Throwables, TransitiveProjects}
import bloop.rifle.internal.Operations
import bloop.rifle.*
import ch.epfl.scala.bsp4j

import java.nio.file.Files
import java.util
import bloop.rifle.{BloopServer, BloopThreads, BuildServer}
import bloop.rifle.internal.Operations
import bloop.rifle.{BloopRifleConfig, FailedToStartServerException}
import scala.util.Try

abstract class BleepCommandRemote(watch: Boolean) extends BleepBuildCommand {
Expand Down Expand Up @@ -102,7 +101,7 @@ abstract class BleepCommandRemote(watch: Boolean) extends BleepBuildCommand {
res <- buildClient.failed match {
case empty if empty.isEmpty => Right(())
case failed =>
val projects = failed.map(BleepCommandRemote.projectFromBuildTarget(started)).toArray
val projects = failed.flatMap(BleepCommandRemote.projectFromBuildTarget(started)).toArray
Left(new BspCommandFailed("Failed", projects, BspCommandFailed.NoDetails))
}
} yield res
Expand All @@ -111,7 +110,9 @@ abstract class BleepCommandRemote(watch: Boolean) extends BleepBuildCommand {
started.config.compileServerModeOrDefault match {
case model.CompileServerMode.NewEachInvocation =>
server.shutdown()
Operations.exit(bloopRifleConfig.address, started.buildPaths.dotBleepDir, System.out, System.err, bleepRifleLogger)
if (Operations.exit(bloopRifleConfig.address, started.buildPaths.dotBleepDir, System.out, System.err, bleepRifleLogger) != 0) {
started.logger.warn("Failed to shutdown the compile server")
}
()
case model.CompileServerMode.Shared =>
()
Expand All @@ -138,10 +139,8 @@ object BleepCommandRemote {
new bsp4j.BuildTargetIdentifier(amended)
}

def projectFromBuildTarget(started: Started)(name: bsp4j.BuildTargetIdentifier): model.CrossProjectName = {
val id = name.getUri.split("=").last
started.build.explodedProjects.keys.find(_.value == id).getOrElse(sys.error(s"Couldn't find project for $name"))
}
def projectFromBuildTarget(started: Started)(name: bsp4j.BuildTargetIdentifier): Option[model.CrossProjectName] =
started.build.explodedProjects.keys.find(_.value == name.getUri.split("=").last)

def buildTargets(buildPaths: BuildPaths, projects: Array[model.CrossProjectName]): util.List[bsp4j.BuildTargetIdentifier] =
util.List.of(projects.map(p => buildTarget(buildPaths, p)): _*)
Expand Down
7 changes: 6 additions & 1 deletion bleep-core/src/scala/bleep/bsp/BleepBspServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,12 @@ class BleepBspServer(
warn(s"bleep was not able to refresh the build", bleepException)
CompletableFuture.completedFuture[CompileResult](new CompileResult(bsp4j.StatusCode.ERROR))
case Right(started) =>
val projects = params.getTargets.asScala.toArray.map(BleepCommandRemote.projectFromBuildTarget(started))
val projects = params.getTargets.asScala.toArray.flatMap { target =>
BleepCommandRemote.projectFromBuildTarget(started)(target).orElse {
logger.warn(s"Couldn't find project for target ${target.getUri}. Bleep may have picked up a change you IDE hasn't. Try to reload the build.")
None
}
}

DoSourceGen(started, bloopServer, TransitiveProjects(started.build, projects)) match {
case Left(bleepException) =>
Expand Down
37 changes: 37 additions & 0 deletions bleep-core/src/scala/bleep/commands/ListTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package bleep
package commands

import bleep.internal.TransitiveProjects
import bloop.rifle.BuildServer
import ch.epfl.scala.bsp4j
import ch.epfl.scala.bsp4j.ScalaTestClassesParams

import scala.jdk.CollectionConverters.*

case class ListTests(projects: Array[model.CrossProjectName]) extends BleepCommandRemote(watch = false) {

override def watchableProjects(started: Started): TransitiveProjects =
TransitiveProjects(started.build, projects)

override def runWithServer(started: Started, bloop: BuildServer): Either[BleepException, Unit] = {
val all: Iterator[(model.CrossProjectName, String)] = testsByCrossProject(started, bloop)

all.toList.groupBy { case (pn, cls) => pn.name }.foreach { case (pn, tuples) =>
started.logger.info(s"${pn.value}:")
tuples.foreach { case (_, cls) => started.logger.info(s" $cls") }
}

Right(())
}

private def testsByCrossProject(started: Started, bloop: BuildServer): Iterator[(model.CrossProjectName, String)] = {
val targets = BleepCommandRemote.buildTargets(started.buildPaths, projects)
val result = bloop.buildTargetScalaTestClasses(new ScalaTestClassesParams(targets)).get()

for {
item <- result.getItems.iterator().asScala
projectName <- BleepCommandRemote.projectFromBuildTarget(started)(item.getTarget).iterator
cls <- item.getClasses.iterator.asScala
} yield (projectName, cls)
}
}

0 comments on commit 37b3069

Please sign in to comment.