aboutsummaryrefslogtreecommitdiff
path: root/plugins/scalatest/ScalaTest.scala
blob: 9335982e5ce8a59a92eee43cf98c6f2a53a79ccf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import cbt._
import org.scalatest._
import org.scalatest

/* FIXME:
 - Separate out SbtLayout
 - Allow depending on this via a git dependency.
   Probably by adding support for subfolders to "GitDependency"
*/

trait SbtLayout extends BaseBuild{
  outer =>
  override def sources = Seq( projectDirectory ++ "/src/main/scala" )
  def testSources = projectDirectory ++ "/src/test/scala"
  def testDependencies: Seq[Dependency] = Nil
  lazy val testBuild =
    new BasicBuild(context) with ScalaTest{
      override def sources = Seq(testSources)
      override def target = outer.target
      override def compileTarget = outer.scalaTarget ++ "/test-classes"
      override def dependencies = (outer +: testDependencies) ++ super.dependencies 
    }
  override def test: Option[ExitCode] =
    if(testSources.exists) Some( testBuild.run )
    else None
}

trait ScalaTest extends BaseBuild{
  override def run: ExitCode = {
    import ScalaTestLib._
    val _classLoader = classLoader(context.classLoaderCache)
    val suiteNames = compile.map( d => discoverSuites(d, _classLoader) ).toVector.flatten
    runSuites( suiteNames.map( loadSuite( _, _classLoader ) ) )
    ExitCode.Success
  }
}

object ScalaTestLib{
  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]]
      .to
  }
  def loadSuite(name: String, _classLoader: ClassLoader) = {
    _classLoader.loadClass(name).getConstructor().newInstance().asInstanceOf[Suite]
  }  
}