From e8798ddca8688172b175cac81fccd06bc95df2b7 Mon Sep 17 00:00:00 2001 From: Eugene Vigdorchik Date: Thu, 5 Apr 2012 12:02:53 +0400 Subject: [PATCH] Add test and allow the java options to be picked for a default test group created. --- main/Defaults.scala | 8 +++-- main/actions/Tests.scala | 8 ++--- .../tests/fork/project/ForkTestsTest.scala | 36 +++++++++++++++++++ .../tests/fork/src/test/scala/Ensemble.scala | 20 +++++++++++ sbt/src/sbt-test/tests/fork/test | 2 ++ 5 files changed, 67 insertions(+), 7 deletions(-) create mode 100755 sbt/src/sbt-test/tests/fork/project/ForkTestsTest.scala create mode 100755 sbt/src/sbt-test/tests/fork/src/test/scala/Ensemble.scala create mode 100755 sbt/src/sbt-test/tests/fork/test diff --git a/main/Defaults.scala b/main/Defaults.scala index 255fb9561..e968955e3 100755 --- a/main/Defaults.scala +++ b/main/Defaults.scala @@ -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("", 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("", 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 { diff --git a/main/actions/Tests.scala b/main/actions/Tests.scala index e72cc8c3e..4487f7139 100644 --- a/main/actions/Tests.scala +++ b/main/actions/Tests.scala @@ -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) } diff --git a/sbt/src/sbt-test/tests/fork/project/ForkTestsTest.scala b/sbt/src/sbt-test/tests/fork/project/ForkTestsTest.scala new file mode 100755 index 000000000..4cfa42fef --- /dev/null +++ b/sbt/src/sbt-test/tests/fork/project/ForkTestsTest.scala @@ -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" + )) +} diff --git a/sbt/src/sbt-test/tests/fork/src/test/scala/Ensemble.scala b/sbt/src/sbt-test/tests/fork/src/test/scala/Ensemble.scala new file mode 100755 index 000000000..fc009691f --- /dev/null +++ b/sbt/src/sbt-test/tests/fork/src/test/scala/Ensemble.scala @@ -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) + } +} diff --git a/sbt/src/sbt-test/tests/fork/test b/sbt/src/sbt-test/tests/fork/test new file mode 100755 index 000000000..de5041ba0 --- /dev/null +++ b/sbt/src/sbt-test/tests/fork/test @@ -0,0 +1,2 @@ +> test +> check \ No newline at end of file