From 3446af59a17bdef8730008a61eade7937b9cda56 Mon Sep 17 00:00:00 2001 From: Christopher Vogt Date: Thu, 30 Mar 2017 10:47:28 -0400 Subject: scalatest plugin for 2.12 (and move most code from plugin into library) --- examples/scalatest-example/build/build.scala | 2 +- libraries/scalatest-runner/Runner.scala | 37 +++++++++++++++++++++++++ libraries/scalatest-runner/build/build.scala | 8 ++++++ plugins/scalatest/ScalaTest.scala | 41 ++++------------------------ plugins/scalatest/build/build.scala | 2 -- stage2/BasicBuild.scala | 2 +- stage2/DirectoryDependency.scala | 8 ++++-- stage2/libraries.scala | 11 ++++++-- 8 files changed, 67 insertions(+), 44 deletions(-) create mode 100644 libraries/scalatest-runner/Runner.scala create mode 100644 libraries/scalatest-runner/build/build.scala diff --git a/examples/scalatest-example/build/build.scala b/examples/scalatest-example/build/build.scala index 2c0ec21..8f8ed5f 100644 --- a/examples/scalatest-example/build/build.scala +++ b/examples/scalatest-example/build/build.scala @@ -8,7 +8,7 @@ java.lang.Exception: This should never happend. Could not find (org.scala-lang,s */ override def test: Dependency = { new BasicBuild(context) with ScalaTest with SbtLayoutTest{ - override def dependencies = outer +: super.dependencies + override def dependencies = outer +: super.dependencies } } } diff --git a/libraries/scalatest-runner/Runner.scala b/libraries/scalatest-runner/Runner.scala new file mode 100644 index 0000000..e2eaf87 --- /dev/null +++ b/libraries/scalatest-runner/Runner.scala @@ -0,0 +1,37 @@ +package cbt.scalatest + +import org.scalatest._ + +import java.io.File + +object Runner{ + def run( classpath: Array[File], classLoader: ClassLoader ): Unit = { + val suiteNames = classpath.map( d => discoverSuites(d, classLoader) ).flatten + runSuites( suiteNames.map( loadSuite( _, classLoader ) ) ) + } + + def runSuites( suites: Seq[Suite] ) = { + def color: Boolean = true + def durations: Boolean = true + def shortstacks: Boolean = true + def fullstacks: Boolean = true + def stats: Boolean = true + def testName: String = null + def configMap: ConfigMap = ConfigMap.empty + suites.foreach{ + _.execute(testName, configMap, color, durations, shortstacks, fullstacks, stats) + } + } + + def discoverSuites( discoveryPath: File, classLoader: ClassLoader ): Seq[String] = { + classLoader + .loadClass("org.scalatest.tools.SuiteDiscoveryHelper") + .getMethod("discoverSuiteNames", classOf[List[_]], classOf[ClassLoader], classOf[Option[_]]) + .invoke(null, List(discoveryPath.toString ++ "/"), classLoader, None) + .asInstanceOf[Set[String]] + .toVector + } + def loadSuite(name: String, classLoader: ClassLoader) = { + classLoader.loadClass(name).getConstructor().newInstance().asInstanceOf[Suite] + } +} diff --git a/libraries/scalatest-runner/build/build.scala b/libraries/scalatest-runner/build/build.scala new file mode 100644 index 0000000..781e314 --- /dev/null +++ b/libraries/scalatest-runner/build/build.scala @@ -0,0 +1,8 @@ +import cbt._ + +class Build(val context: Context) extends BaseBuild{ + override def dependencies = super.dependencies ++ + Resolver( mavenCentral ).bind( + ScalaDependency("org.scalatest","scalatest", if(scalaMajorVersion == "2.12") "3.0.1" else "2.2.6") + ) +} diff --git a/plugins/scalatest/ScalaTest.scala b/plugins/scalatest/ScalaTest.scala index e2f44e3..87ab16d 100644 --- a/plugins/scalatest/ScalaTest.scala +++ b/plugins/scalatest/ScalaTest.scala @@ -1,42 +1,11 @@ package cbt -import org.scalatest._ - - +import java.io.File trait ScalaTest extends BaseBuild{ + override def dependencies = super.dependencies :+ libraries.scalatestRunner override def run: ExitCode = { - import ScalaTestLib._ - val suiteNames = exportedClasspath.files.map( d => discoverSuites(d, classLoader) ).flatten - runSuites( suiteNames.map( loadSuite( _, classLoader ) ) ) + classLoader.loadClass( "cbt.scalatest.Runner" ).method( + "run", classOf[Array[File]], classOf[ClassLoader] + ).invoke( null, exportedClasspath.files.toArray, classLoader ) ExitCode.Success } - override def dependencies = super.dependencies ++ Resolver( mavenCentral ).bind( ScalaDependency("org.scalatest","scalatest","2.2.4") ) } - -object ScalaTestLib{ - import java.io.File - def runSuites(suites: Seq[Suite]) = { - def color: Boolean = true - def durations: Boolean = true - def shortstacks: Boolean = true - def fullstacks: Boolean = true - def stats: Boolean = true - def testName: String = null - def configMap: ConfigMap = ConfigMap.empty - suites.foreach{ - _.execute(testName, configMap, color, durations, shortstacks, fullstacks, stats) - } - } - - def discoverSuites(discoveryPath: File, classLoader: ClassLoader): Seq[String] = { - classLoader - .loadClass("org.scalatest.tools.SuiteDiscoveryHelper") - .getMethod("discoverSuiteNames", classOf[List[_]], classOf[ClassLoader], classOf[Option[_]]) - .invoke(null, List(discoveryPath.string ++ "/"), classLoader, None) - .asInstanceOf[Set[String]] - .toVector - } - def loadSuite(name: String, classLoader: ClassLoader) = { - classLoader.loadClass(name).getConstructor().newInstance().asInstanceOf[Suite] - } -} - diff --git a/plugins/scalatest/build/build.scala b/plugins/scalatest/build/build.scala index ce07b36..4e94cd7 100644 --- a/plugins/scalatest/build/build.scala +++ b/plugins/scalatest/build/build.scala @@ -4,7 +4,5 @@ class Build(val context: Context) extends BaseBuild{ override def dependencies = ( super.dependencies :+ context.cbtDependency - ) ++ Resolver( mavenCentral ).bind( - ScalaDependency("org.scalatest","scalatest","2.2.4") ) } diff --git a/stage2/BasicBuild.scala b/stage2/BasicBuild.scala index 067d1b7..2cf97e5 100644 --- a/stage2/BasicBuild.scala +++ b/stage2/BasicBuild.scala @@ -20,7 +20,7 @@ trait BaseBuild extends BuildInterface with DependencyImplementation with SbtDep lazy val moduleKey: String = "BaseBuild("+target.string+")" implicit def transientCache: java.util.Map[AnyRef,AnyRef] = context.transientCache - implicit def libraries(implicit context: Context): libraries = new libraries(context) + implicit def libraries(implicit context: Context): libraries = new libraries(context, scalaVersion) // library available to builds implicit protected final val logger: Logger = context.logger diff --git a/stage2/DirectoryDependency.scala b/stage2/DirectoryDependency.scala index 6ebb988..f2b5557 100644 --- a/stage2/DirectoryDependency.scala +++ b/stage2/DirectoryDependency.scala @@ -57,7 +57,9 @@ object DirectoryDependency { // the beginning. Instead CBT always needs to build the pure Java // Launcher in the checkout with itself and then run it via reflection. val ( checkoutDirectory, dependency ) = - GitDependency.withCheckoutDirectory( base, hash, Some( "nailgun_launcher" ) )( context ) + GitDependency.withCheckoutDirectory( base, hash, Some( "nailgun_launcher" ) )( + context.copy( scalaVersion = None ) + ) dependency .dependency .asInstanceOf[BaseBuild] // should work because nailgun_launcher/ has no cbt build of it's own @@ -71,7 +73,9 @@ object DirectoryDependency { def loadCustomBuild: AnyRef = { lib.logger.composition( "Loading build at " ++ buildDirectory.string ) - val buildBuild = apply( buildDirectory, None )( context ).dependency.asInstanceOf[BuildInterface] + val buildBuild = apply( + context.copy( workingDirectory = buildDirectory, scalaVersion = None ), None + ).dependency.asInstanceOf[BuildInterface] import buildBuild._ val managedContext = context.copy( parentBuild = Some( buildBuild ) ) diff --git a/stage2/libraries.scala b/stage2/libraries.scala index 3d2951c..f0e4ecc 100644 --- a/stage2/libraries.scala +++ b/stage2/libraries.scala @@ -1,6 +1,12 @@ package cbt -class libraries( context: Context ) { - private def dep( name: String ) = DirectoryDependency( context.cbtHome / "libraries" / name )( context ) +class libraries( context: Context, scalaVersion: String ) { + private def dep( name: String ) = DirectoryDependency( + context.copy( + scalaVersion = Some( scalaVersion ), + workingDirectory = context.cbtHome / "libraries" / name + ), + None + ) def captureArgs = dep( "capture_args" ) def eval = dep( "eval" ) def file = dep( "file" ) @@ -9,4 +15,5 @@ class libraries( context: Context ) { def common_0 = dep( "common-0" ) def common_1 = dep( "common-1" ) def interfaces = dep( "interfaces" ) + def scalatestRunner = dep( "scalatest-runner" ) } -- cgit v1.2.3