Add test and allow the java options to be picked for a default test group created.

This commit is contained in:
Eugene Vigdorchik 2012-04-05 12:02:53 +04:00
parent 05c4fbdc3d
commit e8798ddca8
5 changed files with 67 additions and 7 deletions

View File

@ -318,9 +318,7 @@ object Defaults extends BuildCommon
},
testOptions <<= (testOptions in TaskGlobal, testListeners) map { (options, ls) => Tests.Listeners(ls) +: options },
testExecution <<= testExecutionTask(key),
testGrouping <<= testGrouping in TaskGlobal or ((definedTests, fork) map {
(tests, fork) => Seq(new Tests.Group("<default>", tests, if (fork) Tests.SubProcess(Seq()) else Tests.InProcess))
})
testGrouping <<= testGrouping or singleTestGroup(key)
) )
def testLogger(manager: Streams, baseKey: Scoped)(tdef: TestDefinition): Logger =
{
@ -335,6 +333,10 @@ object Defaults extends BuildCommon
val mod = tdef.fingerprint match { case f: SubclassFingerprint => f.isModule; case f: AnnotatedFingerprint => f.isModule; case _ => false }
extra.put(name.key, tdef.name).put(isModule, mod)
}
def singleTestGroup(key: Scoped): Initialize[Task[Seq[Tests.Group]]] =
((definedTests in key, fork in key, javaOptions in key) map {
(tests, fork, javaOpts) => Seq(new Tests.Group("<default>", tests, if (fork) Tests.SubProcess(javaOpts) else Tests.InProcess))
})
def testExecutionTask(task: Scoped): Initialize[Task[Tests.Execution]] =
(testOptions in task, parallelExecution in task, fork in task, tags in task) map {

View File

@ -182,10 +182,10 @@ object Tests
}
}
sealed trait RunPolicy
case object InProcess extends RunPolicy
final case class SubProcess(javaOptions: Seq[String]) extends RunPolicy
sealed trait TestRunPolicy
case object InProcess extends TestRunPolicy
final case class SubProcess(javaOptions: Seq[String]) extends TestRunPolicy
final case class Group(name: String, tests: Seq[TestDefinition], runPolicy: RunPolicy)
final case class Group(name: String, tests: Seq[TestDefinition], runPolicy: TestRunPolicy)
}

View File

@ -0,0 +1,36 @@
import sbt._
import Keys._
import Tests._
import Defaults._
object ForkTestsTest extends Build {
val totalFiles = 9
val groupSize = 3
val check = TaskKey[Unit]("check", "Check all files were created and remove them.")
def groupId(idx: Int) = "group_" + (idx + 1)
def groupPrefix(idx: Int) = groupId(idx) + "_file_"
lazy val root = Project("root", file("."), settings = defaultSettings ++ Seq(
testGrouping <<= definedTests in Test map { tests =>
assert(tests.size == 1)
val groups = Stream const tests(0) take totalFiles grouped groupSize
for ((ts, idx) <- groups.toSeq.zipWithIndex) yield {
new Group(groupId(idx), ts, SubProcess(Seq("-Dgroup.prefix=" + groupPrefix(idx), "-Dgroup.size=" + ts.size)))
}
},
check := {
for (i <- 0 until totalFiles/groupSize)
for (j <- 1 to groupSize) {
val f = file(groupPrefix(i) + j)
if (!f.exists)
error("File " + f.getName + " was not created.")
else
f.delete()
}
},
concurrentRestrictions := Tags.limit(Tags.ForkedTestGroup, 2) :: Nil,
libraryDependencies += "org.scalatest" % "scalatest_2.9.0" % "1.6.1" % "test"
))
}

View File

@ -0,0 +1,20 @@
import org.scalatest.FlatSpec
import org.scalatest.matchers.MustMatchers
import java.io.File
class Ensemble extends FlatSpec with MustMatchers {
val prefix = System.getProperty("group.prefix")
val countTo = System.getProperty("group.size").toInt
"an ensemble" must "create all files" in {
@annotation.tailrec
def step(i: Int): Unit = {
val f = new File(prefix + i)
if (!f.createNewFile)
step(if (f.exists) i + 1 else i)
else
i must be <= (countTo)
}
step(1)
}
}

View File

@ -0,0 +1,2 @@
> test
> check