How to suppress deprecation warnings when testing deprecated Scala functions?
Asked Answered
T

4

6

Suppose I have a library, which contains both a deprecated function and a preferred function:

object MyLib {
  def preferredFunction() = ()
  @deprecated("Use preferredFunction instead", "1.0") def deprecatedFunction() = ()
}

I want to test both preferredFunction and deprecatedFunction in ScalaTest:

class MyLibSpec extends FreeSpec with Matchers {
  "preferred function" in {
    MyLib.preferredFunction() should be(())
  }
  "deprecated function" in {
    MyLib.deprecatedFunction() should be(())
  }
}

However, a deprecation warning is reported at MyLib.deprecatedFunction().

How to avoid the warning?

Trinomial answered 16/10, 2018 at 14:22 Comment(0)
C
2

Scala does not support that, see https://groups.google.com/forum/#!topic/scala-internals/LsycMcEkXiA

However there is a plugin mentioned:

https://github.com/ghik/silencer

I haven't used it - so I am not sure if this works for your case.

Crossed answered 16/10, 2018 at 14:33 Comment(0)
R
4

Just deprecate the class, which is instantiated reflectively by the test rig.

scala> @deprecated("","") def f() = ()
f: ()Unit

scala> @deprecated("","") class C { f() }
defined class C

scala> f()
<console>:13: warning: method f is deprecated:
       f()
       ^
Ruskin answered 16/10, 2018 at 15:39 Comment(0)
L
4

With time, the state-of-the-art has moved on and, with Scala 2.13, we have a lot of flexibility with compiler flags.

Yes you can use the nowarn annotation:

import scala.annotation.nowarn

@nowarn
@nowarn("cat=deprecation")
@nowarn("msg=method deprecatedFunction in object MyLib is deprecated")

This is good for use at the call-site, but if you have an API that is used broadly, it does not scale so well; here, compiler flags come to the fore:

Compile / scalacOptions := Seq(
  "-deprecation",
  """-Wconf:cat=deprecation&origin=MyLib\.deprecatedFunction:i""",
  "-Xfatal-warnings"
)

Here we:

  • Promote all warnings to compile errors, with -Xfatal-warnings i.e. they will fail the build
  • Emit warning and location for usages of deprecated APIs, with -deprecation; note that these will also now be promoted to errors
  • Demote any use of MyLib.deprecatedFunction from a warning to just an information, with -Wconf:cat=deprecation&origin=MyLib\.deprecatedFunction:i i.e. this specific case will not fail the build
Loginov answered 17/6, 2022 at 9:48 Comment(0)
B
3

Scala 2.13.2 and 2.12.13 have added support for local suppression of compiler warnings.

https://www.scala-lang.org/2021/01/12/configuring-and-suppressing-warnings.html

Now you can do @nowarn("cat=deprecation")

Bjork answered 1/3, 2022 at 13:54 Comment(0)
C
2

Scala does not support that, see https://groups.google.com/forum/#!topic/scala-internals/LsycMcEkXiA

However there is a plugin mentioned:

https://github.com/ghik/silencer

I haven't used it - so I am not sure if this works for your case.

Crossed answered 16/10, 2018 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.