Skip to content

Commit

Permalink
Properly support test frameworks which spawn child tasks
Browse files Browse the repository at this point in the history
Needed to fix #286
  • Loading branch information
lihaoyi committed Apr 8, 2018
1 parent bcdb1aa commit d94bb5e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
9 changes: 5 additions & 4 deletions scalalib/src/mill/scalalib/Lib.scala
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,12 @@ object Lib{

val tasks = runner.tasks(
for ((cls, fingerprint) <- testClasses.toArray)
yield new TaskDef(cls.getName.stripSuffix("$"), fingerprint, true, Array(new SuiteSelector))
yield new TaskDef(cls.getName.stripSuffix("$"), fingerprint, true, Array(new SuiteSelector))
)

for (t <- tasks) {
t.execute(
val taskQueue = tasks.to[mutable.Queue]
while (taskQueue.nonEmpty){
val next = taskQueue.dequeue().execute(
new EventHandler {
def handle(event: Event) = events.append(event)
},
Expand All @@ -242,6 +243,7 @@ object Lib{
def info(msg: String) = ctx.log.outputStream.println(msg)
})
)
taskQueue.enqueue(next:_*)
}
ctx.log.outputStream.println(runner.done())
}
Expand Down Expand Up @@ -299,7 +301,6 @@ object Lib{
cls.isAnnotationPresent(annotationCls) ||
cls.getDeclaredMethods.exists(_.isAnnotationPresent(annotationCls))
)

}.map { f => (cls, f) }
}
}
Expand Down
3 changes: 3 additions & 0 deletions scalalib/test/resources/hello-scalacheck/foo/src/Main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object Main extends App {
println("Hello world!")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import org.scalacheck._

object MainProps extends Properties("String") {
import Prop.forAll

property("startsWith") = forAll { (a: String, b: String) =>
(a+b).startsWith(a)
}

property("endsWith") = forAll { (a: String, b: String) =>
(a+b).endsWith(b)
}

property("substring") = forAll { (a: String, b: String) =>
(a+b).substring(a.length) == b
}

property("substring") = forAll { (a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
}
}
36 changes: 36 additions & 0 deletions scalalib/test/src/mill/scalalib/HelloWorldTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,16 @@ object HelloWorldTests extends TestSuite {
}
}

object HelloScalacheck extends HelloBase{
object foo extends ScalaModule {
def scalaVersion = "2.12.4"
object test extends Tests {
def ivyDeps = Agg(ivy"org.scalacheck::scalacheck:1.13.5")
def testFrameworks = Seq("org.scalacheck.ScalaCheckFramework")
}
}
}

val resourcePath = pwd / 'scalalib / 'test / 'resources / "hello-world"

def jarMainClass(jar: JarFile): Option[String] = {
Expand Down Expand Up @@ -182,6 +192,7 @@ object HelloWorldTests extends TestSuite {
)
}
}

'scalacOptions - {
'emptyByDefault - workspaceTest(HelloWorld){eval =>
val Right((result, evalCount)) = eval.apply(HelloWorld.core.scalacOptions)
Expand All @@ -200,6 +211,7 @@ object HelloWorldTests extends TestSuite {
)
}
}

'compile - {
'fromScratch - workspaceTest(HelloWorld){eval =>
val Right((result, evalCount)) = eval.apply(HelloWorld.core.compile)
Expand Down Expand Up @@ -260,6 +272,7 @@ object HelloWorldTests extends TestSuite {

}
}

'runMain - {
'runMainObject - workspaceTest(HelloWorld){eval =>
val runResult = eval.outPath / 'core / 'runMain / 'dest / "hello-mill"
Expand Down Expand Up @@ -351,6 +364,7 @@ object HelloWorldTests extends TestSuite {
)
}
}

'run - {
'runIfMainClassProvided - workspaceTest(HelloWorldWithMain){eval =>
val runResult = eval.outPath / 'core / 'run / 'dest / "hello-mill"
Expand Down Expand Up @@ -388,6 +402,7 @@ object HelloWorldTests extends TestSuite {

}
}

'jar - {
'nonEmpty - workspaceTest(HelloWorldWithMain){eval =>
val Right((result, evalCount)) = eval.apply(HelloWorldWithMain.core.jar)
Expand All @@ -413,6 +428,7 @@ object HelloWorldTests extends TestSuite {
val mainClass = jarMainClass(jarFile)
assert(mainClass.contains("Main"))
}

'logOutputToFile - workspaceTest(HelloWorld){eval =>
val outPath = eval.outPath
eval.apply(HelloWorld.core.compile)
Expand All @@ -439,6 +455,7 @@ object HelloWorldTests extends TestSuite {
val mainClass = jarMainClass(jarFile)
assert(mainClass.contains("Main"))
}

'run - workspaceTest(HelloWorldWithMain){eval =>
val Right((result, evalCount)) = eval.apply(HelloWorldWithMain.core.assembly)

Expand Down Expand Up @@ -470,6 +487,7 @@ object HelloWorldTests extends TestSuite {
!result2.exists(_.path.last == "sourcecode_2.12-0.1.3.jar")
)
}

'typeLevel - workspaceTest(HelloWorldTypeLevel){ eval =>
val classPathsToCheck = Seq(
HelloWorldTypeLevel.foo.runClasspath,
Expand All @@ -489,6 +507,7 @@ object HelloWorldTests extends TestSuite {
)
}
}

'macros - {
// make sure macros are applied when compiling/running
'runMain - workspaceTest(
Expand All @@ -507,5 +526,22 @@ object HelloWorldTests extends TestSuite {
assert(evalCount > 0)
}
}

'scalacheck - workspaceTest(
HelloScalacheck,
resourcePath = pwd / 'scalalib / 'test / 'resources / "hello-scalacheck"
){ eval =>
val Right((res, evalCount)) = eval.apply(HelloScalacheck.foo.test.test())
assert(
evalCount > 0,
res._2.map(_.selector) == Seq(
"String.startsWith",
"String.endsWith",
"String.substring",
"String.substring"
)
)
}

}
}

0 comments on commit d94bb5e

Please sign in to comment.