如何在sbt中所有模块的所有测试之前运行任务

时间:2014-03-11 09:29:52

标签: scala sbt

我有一个多模块游戏!使用sbt构建的应用程序我在CI服务器上有并行测试和演进的问题:当sbt开始测试并首次启动evolution脚本时,其他因为“数据库'默认'处于不一致状态”一段时间失败(直到进化脚本停止)。

我知道如何手动执行evolutions以及如何从sbt运行它们,但我不知道如何将此配置插入到sbt以确保应用在所有模块的所有测试之前

作为参考我正在使用以下来进行演化:

object ApplyEvolutions extends App {
  OfflineEvolutions.applyScript(
    new File("."), 
    this.getClass.getClassLoader, 
    args.headOption.getOrElse("default"))
}

我的sbt设置:

val runEvolution = taskKey[Unit]("Applies evolutions")

lazy val runEvolutionTask = runEvolution := {
  val log = streams.value.log
  val dbName = "default"
  Run.executeTrapExit( {
    log.info(s"Applying evolutions for database $dbName")
    Run.run("controllers.ApplyEvolutions",
      (fullClasspath in Compile).value.map { _.data },
      Seq(dbName),
      log
    )(runner.value)
  }, log )
}

lazy val runEvolutionsBeforeTests = Seq(
  Tasks.runEvolutionTask,
  (test in Test) <<= (test in Test) dependsOn Tasks.runEvolution
)

1 个答案:

答案 0 :(得分:1)

tl; dr 使用alias

我使用SBT 0.13.2-M3

[task-dependson]> about
[info] This is sbt 0.13.2-M3
[info] The current project is {file:/Users/jacek/sandbox/so/task-dependsOn/}task-dependson 0.1-SNAPSHOT
[info] The current project is built against Scala 2.10.3
[info] Available Plugins: sbt.plugins.IvyModule, sbt.plugins.JvmModule, sbt.plugins.GlobalModule, com.typesafe.sbt.SbtGit, com.typesafe.sbt.SbtProguard, growl.GrowlingTests, org.sbtidea.SbtIdeaPlugin, sbtman.Plugin, np.Plugin, com.timushev.sbt.updates.UpdatesPlugin
[info] sbt, sbt plugins, and build definitions are using Scala 2.10.3

以下是任务的示例 - sayHelloT - 在所有子项目中test之前执行一次。我使用Specs2作为测试框架。

import java.util.concurrent._

lazy val a = project

lazy val b = project

lazy val sayHelloT = taskKey[Unit]("Say Hello")

sayHelloT := {
  val log = streams.value.log
  log.info("Sleeping for 5 secs...")
  TimeUnit.SECONDS.sleep(5)
  log.info("Hello, my friend")
}

libraryDependencies in ThisBuild += "org.specs2" %% "specs2" % "2.3.10" % "test"

scalacOptions in Test ++= Seq("-Yrangepos")

addCommandAlias("sht", ";sayHelloT; test") 

正如您所看到的,有两个子项目 - ab - 以及一个将执行sht的别名sayHelloT,只有在成功完成后才会执行test秒,[task-dependson]> sht [info] Sleeping for 5 secs... [info] Hello, my friend [success] Total time: 5 s, completed Mar 12, 2014 10:19:39 PM [info] ASpec [info] [info] A project should [info] + contain 11 characters [info] [info] Total for specification ASpec [info] Finished in 205 ms [info] 1 example, 0 failure, 0 error [info] Passed: Total 0, Failed 0, Errors 0, Passed 0 [info] No tests to run for task-dependson/test:test [info] Passed: Total 1, Failed 0, Errors 0, Passed 1 [info] BSpec [info] [info] B project should [info] + contain 11 characters [info] [info] Total for specification BSpec [info] Finished in 240 ms [info] 1 example, 0 failure, 0 error [info] Passed: Total 1, Failed 0, Errors 0, Passed 1 [success] Total time: 3 s, completed Mar 12, 2014 10:19:42 PM 开始。

{{1}}