Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test initialCommands in SBT

Tags:

scala

sbt

I have a subproject in my build.sbt with a rather long setting for initialCommands, comprising a list of imports and some definitions. I'd like to test this as part of regular CI, because otherwise I won't notice breaking changes after refactoring code. It is not clear to me how to do so.

  1. Just running sbt console doesn't seem to cut it, because there is always a "successful" exit code even when code doesn't compile.
  2. Moving the code out into an object defined in a special source file won't help because I need the list of imports to be present (and I don't want to cakeify my whole code base).
  3. Moving the code out into a source file and then loading that with :load also always gives a successful exit code.
  4. I found out about scala -e but that does strange things on my machine (see the error log below).

This is Scala 2.12.

$ scala -e '1'  
cat: /release: No such file or directory
Exception in thread "main" java.net.UnknownHostException: <my-host-name-here>: <my-host-name-here>: Name or service not known
like image 414
larsrh Avatar asked Aug 03 '26 11:08

larsrh


1 Answers

You could generate a file and run it like any other test file:

(sourceGenerators in Test) += Def.task {
  val contents = """object TestRepl {
{{}}
}""".replace("{{}}", (initialCommands in console).value)
  val file = (sourceManaged in Test).value / "repltest.scala"
  IO.write(file, contents)
  Seq(file)
}.taskValue
like image 133
Reactormonk Avatar answered Aug 06 '26 09:08

Reactormonk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!