Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run task before all tests from all modules in sbt

Tags:

scala

sbt

I have a multi-module Play! application built with sbt and I have a problem on CI server with parallel tests and evolutions: when sbt starts tests and first starts evolution script, other fails because of "Database 'default' is in inconsistent state" for a while (until evolution script stops).

I know how to execute evolutions manually and how to run them from sbt, but I don't know how can I plug this configuration to sbt to ensure evolutions are applied only once and before all tests from all modules.

For reference I'm using following for running evolutions:

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

And my sbt settings:

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
)
like image 523
Keros Avatar asked Nov 02 '22 03:11

Keros


1 Answers

tl;dr Use alias

I use 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

Here's an example of a task - sayHelloT - that's executed once before test in all subprojects. I use Specs2 as the testing framework.

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") 

As you can see there are two subprojects - a and b - and a single alias sht that will execute sayHelloT and only after it finishes successfully which takes 5 secs, test kicks in.

[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
like image 173
Jacek Laskowski Avatar answered Nov 15 '22 05:11

Jacek Laskowski