sbt: set specific scalacOptions options when compiling tests

不问归期 提交于 2021-01-02 05:04:50

问题


Normally I use this set of options for compiling Scala code:

scalacOptions ++= Seq(
    "-deprecation",
    "-encoding", "UTF-8",
    "-feature",
    "-unchecked",
    "-language:higherKinds",
    "-language:implicitConversions",
    "-Xfatal-warnings",
    "-Xlint",
    "-Yinline-warnings",
    "-Yno-adapted-args",
    "-Ywarn-dead-code",
    "-Ywarn-numeric-widen",
    "-Ywarn-value-discard",
    "-Xfuture",
    "-Ywarn-unused-import"
)

But some of them don't play well with ScalaTest, so I would like to disable -Ywarn-dead-code and -Ywarn-value-discard when compiling tests.

I tried adding scope like this

scalacOptions in Compile ++= Seq(...)

or

scalacOptions in (Compile, compile) ++= Seq(...)

or even

val ignoredInTestScalacOptions = Set(
    "-Ywarn-dead-code",
    "-Ywarn-value-discard"
)

scalacOptions in Test ~= { defaultOptions =>
  defaultOptions filterNot ignoredInTestScalacOptions
}

but the first two disable options for normal compile scope as well while the latter doesn't affect tests compilation options.

How could I have a separate list of options when compiling tests?


回答1:


Had the same issue, @Mike Slinn answer didn't work for me. I believe the test options extend the compile options? What eventually did the trick was explicitly removing ignored options from test

scalacOptions in Test --= Seq( "-Ywarn-dead-code", "-Ywarn-value-discard")




回答2:


Why not define common options in a variable (which I called sopts), and other options in another variable (which I called soptsNoTest)?

val sopts = Seq(
  "-deprecation",
  "-encoding", "UTF-8",
  "-feature",
  "-target:jvm-1.8",
  "-unchecked",
  "-Ywarn-adapted-args",
  "-Ywarn-numeric-widen",
  "-Ywarn-unused",
  "-Xfuture",
  "-Xlint"
)
val soptsNoTest = Seq(
  "-Ywarn-dead-code",
  "-Ywarn-value-discard"
)

scalacOptions in (Compile, doc) ++= sopts ++ soptsNoTest
scalacOptions in Test ++= sopts

Tested with SBT 0.13.13.

Because this question has been unanswered for so long, and Scala 2.12 and 2.12.1 were released in the interim, I modified the common options to suit.

BTW, I do not experience any problem with ScalaTest using the switches you mention. I only answered this question because it was interesting.



来源:https://stackoverflow.com/questions/37630465/sbt-set-specific-scalacoptions-options-when-compiling-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!