Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting to get SBT to fail fast (stop) on an error

Tags:

scala

sbt

I am running a multi-project SBT (v0.13) build and would like it to fail fast (stop) on the first error (compile) encountered in a sub-project.

The current behavior is that when something fails to compile in a sub-project, the build continues (to compile all the other sub-projects).

Is there a way/setting to make SBT stop and exit as soon as the first sub-project with a compile error is encountered?

like image 635
TimT Avatar asked Jul 30 '15 05:07

TimT


People also ask

What are sbt settings?

A setting is something which can take the values stored at other Keys in the build state, and generates a new value for a particular build key. sbt converts all registered Setting[_] objects into a giant linear sequence and compiles them into a task graph. This task graph is then used to execute your build.

Does sbt run tests in parallel?

By default, sbt executes tasks in parallel (subject to the ordering constraints already described) in an effort to utilize all available processors. Also by default, each test class is mapped to its own task to enable executing tests in parallel.


1 Answers

In short, to my knowledge, no, SBT can not "fail fast" on a compiler or test error.

To my knowledge SBT doesn't control this. SBT is just going to invoke the appropriate test framework when it inspects your unit tests. The test framework can then decide what order to run the tests in, to run them concurrently, how to report issues, etc. Of course each test framework has its own features, configuration, and conventions. The two most widely used test frameworks for Scala are ScalaTest and Specs2.

Fortunately you can get the behavior you've requested in either Specs2 or ScalaTest. I've provided simple unit test examples below that fail early.

ScalaTest

You can get fail-fast behavior in ScalaTest for single test Suites by mixing in the CancelAfterFailure trait. For example, this test would execute the first test, fail the second, and show the third as cancelled.

class SomeSuite extends FunSuite with CancelAfterFailure with Assertions {    test("first") {     println("first")   }    test("second") {     assert(false)   }    test("third") {     println("third")   } } 

Specs2

Similar to ScalaTest you can control behavior in Specs2 on a per-Specification basis. To get fail-fast-like behavior you need to add two Arguments to your Specification: sequential and stopOnFail. See the docs for a full list of arguments you can set. (You do need both if you want an obvious linear ordering since Specs2 by default will execute your tests concurrently!)

class SomeSpec extends Specification {    sequential   stopOnFail    "specs2" should {     "first" in {       println("first")       ok     }      "second" in {       "foo" must equalTo ("bar")     }      "third" in {       println("third")     }   } } 
like image 196
Mark Kegel Avatar answered Sep 28 '22 04:09

Mark Kegel