From a4310d4da88cbbccf52c03b52ec8a34c77624507 Mon Sep 17 00:00:00 2001 From: Adam Evans Date: Fri, 4 Mar 2022 17:28:48 +1100 Subject: [PATCH] Allow loading GlobalResources from libraries on the classpath. This is useful if you want to package GlobalResource up as a subproject and re-use in a multi-module build. --- modules/framework/src-jvm/RunnerCompat.scala | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/modules/framework/src-jvm/RunnerCompat.scala b/modules/framework/src-jvm/RunnerCompat.scala index f6facbbc..35dbc6a5 100644 --- a/modules/framework/src-jvm/RunnerCompat.scala +++ b/modules/framework/src-jvm/RunnerCompat.scala @@ -17,6 +17,9 @@ import sbt.testing.{ Task, TaskDef } import CECompat.Ref import CECompat.Semaphore +import java.util.ServiceLoader +import scala.collection.mutable.ListBuffer + trait RunnerCompat[F[_]] { self: sbt.testing.Runner => protected val suiteLoader: SuiteLoader[F] @@ -106,9 +109,9 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner => makeTasks(taskDef, mkSuite) }.unzip - val globalResources = tasksAndSuites.collect { + val globalResources = (tasksAndSuites.collect { case (_, suiteLoader.GlobalResourcesRef(init)) => init - }.toList + }.toSet ++ spiGlobalResources).toList // Passing a promise to the FP side that needs to be fulfilled // when the global resources have been allocated. @@ -184,6 +187,13 @@ trait RunnerCompat[F[_]] { self: sbt.testing.Runner => globalResources.traverse(_.sharedResources(map)).void } + private def spiGlobalResources: List[GlobalResourceF[F]] = { + val result = ListBuffer.empty[GlobalResourceF[F]] + val loader: ServiceLoader[GlobalResourceF[F]] = ServiceLoader.load(classOf[GlobalResourceF[F]]) + loader.iterator().forEachRemaining(result += _) + result.toList + } + private case class IOTask( fqn: String, mkSuite: MakeSuite,