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

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
)

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

Related

Why won't sbt run ScalaTest's example test?

Please assume the normal "newbie" appologies.
I'm using scala 2.12.10; In build.sbt I added ScalaTest:
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.8" % "test"
I'm adding some scala.js to an existing java project so my scala source paths are normal but I made some empty directories for java and resources and use a different target so there would be no collision with existing code:
javaSource in Compile := baseDirectory.value / "src/main/scalajs_java"
javaSource in Test := baseDirectory.value / "src/test/scalajs_java"
resourceDirectory in Compile := baseDirectory.value / "src/main/scalajs_resources"
resourceDirectory in Test := baseDirectory.value / "src/test/scalajs_resources"
target := baseDirectory.value / "scalajs_target"
I put the ScalaTest example file ExampleSpec.scala in src/test/scala/ExampleSpec.scala. The example ran fine using org.scalatest.run as described in http://www.scalatest.org/.
The test classpath looks reasonable, I thought:
sbt:RedsPro-ScalaJS> show test:fullClasspath
[info] * Attributed(/Users/tballard/git/redspro/reds/scalajs_target/scala-2.12/test-classes)
[info] * Attributed(/Users/tballard/git/redspro/reds/scalajs_target/scala-2.12/classes)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.12.10.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-library_2.12/jars/scalajs-library_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-dom_sjs0.6_2.12/jars/scalajs-dom_sjs0.6_2.12-0.9.7.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-test-bridge_2.12/jars/scalajs-test-bridge_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-js/scalajs-test-interface_2.12/jars/scalajs-test-interface_2.12-0.6.31.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scalatest/scalatest_2.12/bundles/scalatest_2.12-3.0.8.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scalactic/scalactic_2.12/bundles/scalactic_2.12-3.0.8.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.12.10.jar)
[info] * Attributed(/Users/tballard/.ivy2/cache/org.scala-lang.modules/scala-xml_2.12/bundles/scala-xml_2.12-1.2.0.jar)
After compiling, the test is where I would expect it:
> ls scalajs_target/scala-2.12/test-classes/
ExampleSpec.class ExampleSpec.sjsir JS_DEPENDENCIES org/
However, sbt apparently isn't convinced there are any tests:
sbt:RedsPro-ScalaJS> show definedTestNames
[info] *
[success] Total time: 1 s, completed Dec 1, 2019, 11:37:51 AM
sbt:RedsPro-ScalaJS> testOnly ExampleSpec
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for Test / testOnly
[success] Total time: 1 s, completed Dec 1, 2019, 12:44:35 PM
A point in the right direction would be greatly appreciated.
You need to use %%% instead of %% when depending on ScalaTest. In general, you always need to use %%% in Scala.js.

Scalatest GeneratorDrivenPropertyChecks init seed

I'm using Scalatest 3.1.0-SNAP13 and cannot find how to specify the init seed option from this PR.
I'm using SBT to run the test so if there is a way to specify this option in build.sbt would be ideal.
The -S flag seems to be disabled in 3.1.x:
parseLongArgument(seedArgs, "-S") match {
case Some(seed) => // Randomizer.defaultSeed.getAndSet(Some(seed))
println("Note: -S for setting the Randomizer seed is not yet supported.")
case None => // do nothing
}
However it is seems to be enabled in 3.2.x, so try
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.0-M1" % Test
and
testOptions in Test += Tests.Argument(TestFrameworks.ScalaTest, "-S", "1568769615146")
I managed to get it working with:
// specify an initial seed 0
Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-S", "0")
Result:
[info] SummaryTest:
[info] - hello *** FAILED *** (49 milliseconds)
[info] GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation. (SummaryTest.scala:8)
[info] Falsified after 0 successful property evaluations.
[info] Message: 0 was not greater than 1
[info] Location: (SummaryTest.scala:10)
[info] Occurred when passed generated values (
[info] ""
[info] )
[info] Init Seed: 0

How do I specify specific test from the build.sbt

How do I can specify the test from the build.sbt file , I wanted to run one test only and I used the filter as in the sbt docs, but it doesn't work with me, this is my code I have two test classes and in my sbt I specify test1 to be rub but it seems that the two test are running at the same time any one know what I should do ?
Test1Demo.scala
import org.scalatest.{FlatSpec, Matchers}
class Test1Demo extends FlatSpec with Matchers{
"value of x " should " be 9 " in { assert(my.App.x == 9) }
}
Test2Demo.scala
import org.scalatest.{FlatSpec, Matchers}
class Test2Demo extends FlatSpec with Matchers{
"value of y " should " be 8 " in { assert(my.App2.y == 8) }
}
build.sbt
version := "0.1"
scalaVersion := "2.12.8"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test
testOptions in Test := Seq(Tests.Filter(s => s.startsWith("Test1")))
the output :
[info] Done updating.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/classes ...
[info] Done compiling.
[info] Compiling 2 Scala sources to /home/****/target/scala-2.12/test-classes ...
[info] Done compiling.
[info] Test2Demo:
[info] value of y
[info] - should be 8
[info] Test1Demo:
[info] value of x
[info] - should be 9
[info] Run completed in 6 seconds, 365 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 2, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 264 s, completed Apr 15, 2019 2:47:10 PM
If you want to run value of x test from Test1Demo:
testOnly *Test1Demo -- -z value
This sbt command will run only the tests whose name includes the substring "value".
For exact match rather than substring, use -t instead of -z.
Pay attention to -- (two -, not one)

Creating File using `resourceGenerators`

Given:
$cat build.sbt
scalaVersion := "2.12.1"
resourceGenerators in Compile += Def.task {
val file = (resourceManaged in Compile).value / "demo" / "myapp.properties"
val contents = "name=%s\nversion=%s".format(name.value,version.value)
IO.write(file, contents)
Seq(file)
}.taskValue
$sbt compile
[info] Set current project to sbt_testing (in build file:/home/kmeredith/src/sbt_testing/)
[info] Updating {file:/home/kmeredith/src/sbt_testing/}sbt_testing...
[info] Resolving jline#jline;2.14.1 ...
[info] Done updating.
[success] Total time: 1 s, completed Dec 23, 2016 9:14:50 AM
My understanding is that a myapp.properties file should been created during the compile task.
But, I saw no such file created:
$find . -name *.properties
./target/resolution-cache/default/sbt_testing_2.12/0.1-SNAPSHOT/resolved.xml.properties
Please explain to me how to generate the myapp.properties.
You have the wrong expectation regarding the triggering of the resource generation - running compile does not trigger it (compiles existing sources but not generated ones!), but if you execute test or run, it will be generated:
$sbt clean compile
[success] Total time: 0 s, completed Dec 23, 2016 5:39:28 PM
[info] Updating {file:/home/tzachz/dev/sbt_testing/}sbt_testing...
[info] Done updating.
[info] Compiling 25 Scala sources to /home/tzachz/dev/sbt_testing/target/scala-2.11/classes...
[success] Total time: 24 s, completed Dec 23, 2016 5:39:52 PM
$find . -name "*.properties"
zsh: no matches found: *.properties
$sbt run
// ...
$find . -name "*.properties"
./target/scala-2.11/resource_managed/main/demo/myapp.properties
./target/scala-2.11/classes/demo/myapp.properties

Play 2.0 - Compilation of twitter bootstrap in test

I have a project where I want to compile twitter bootstrap as well as to prevent tests to be executed in parallel
def twitterBootstrapEntryPoints(base:File):PathFinder = {
(base / "app" / "assets" / "stylesheets" / "bootstrap" * "bootstrap.less") +++
(base / "app" / "assets" / "stylesheets" / "bootstrap" * "responsive.less") +++
(base / "app" / "assets" / "stylesheets" * "*.less")
}
val common = PlayProject(appName, appVersion,appDependencies, mainLang = SCALA).settings(
organization := appOrganization,
lessEntryPoints <<= baseDirectory(twitterBootstrapEntryPoints),
resolvers ++= commonResolvers).settings( inConfig(Test)(parallelExecution := false) : _* )
Edit:
To build twitter bootstrap, one should only compile bootstrap.less and responsive.less which import all the other files. in Compile everything works fine, when running in Test this does not work anymore, the compiler tries to compile all the .less files
This is what I see on the play console
[GottwareWeb] $ clean
[success] Total time: 0 s, completed 26 sept. 2012 17:44:07
[GottwareWeb] $ compile
[info] Updating {file:/G:/GottwareWeb/}GottwareWeb...
[info] Resolving org.hibernate.javax.persistence#hibernate-jpa-2.0-api;1.0.1.Fin
[info] Done updating.
[info] Compiling 34 Scala sources and 1 Java source to G:\GottwareWeb\target\sca
la-2.9.1\classes...
[success] Total time: 9 s, completed 26 sept. 2012 17:44:17
[GottwareWeb] $ test
[error] {file:/G:/GottwareWeb/}GottwareWeb/*:play-copy-assets: in G:\GottwareWeb
\app\assets\stylesheets\accordion.less - PlayException: Compilation error [varia
ble #baseLineHeight is undefined]
[error] {file:/G:/GottwareWeb/}GottwareWeb/compile:resources: in G:\GottwareWeb\
app\assets\stylesheets\accordion.less - PlayException: Compilation error [variab
le #baseLineHeight is undefined]
[error] Total time: 0 s, completed 26 sept. 2012 17:44:32
[GottwareWeb] $
Parallel execution of tests is set to false by default. You shouldn't have to include it in the configuration.
Documentation here: https://github.com/playframework/Play20/wiki/SBTSettings
In your setup, Play must be compiling all the less files in app/assets/stylesheets in production as well. Is this what you don't want? Or is it compiling everything in app/assets/stylesheets/bootstrap/ ?