|
| 1 | +package eu.neverblink.jelly.cli.command |
| 2 | + |
| 3 | +import caseapp.core.RemainingArgs |
| 4 | +import eu.neverblink.jelly.cli.{CriticalException, ExitException, JellyCommand} |
| 5 | +import org.scalatest.matchers.should.Matchers |
| 6 | +import org.scalatest.wordspec.AnyWordSpec |
| 7 | + |
| 8 | +class ErrorHandlerSpec extends AnyWordSpec, Matchers: |
| 9 | + def makeMockCommand(ex: Throwable): JellyCommand[VersionOptions] = |
| 10 | + val command = new JellyCommand[VersionOptions] { |
| 11 | + override def names: List[List[String]] = List(List("test")) |
| 12 | + override def doRun(options: VersionOptions, remainingArgs: RemainingArgs): Unit = |
| 13 | + throw ex |
| 14 | + } |
| 15 | + command.testMode(true) |
| 16 | + command |
| 17 | + |
| 18 | + "ErrorHandler" should { |
| 19 | + "not print stack trace for known exceptions if not in debug mode" in { |
| 20 | + val command = makeMockCommand(new CriticalException("Known error!")) |
| 21 | + intercept[ExitException] { |
| 22 | + command.main("test", Array()) |
| 23 | + } |
| 24 | + val err = command.getErrString |
| 25 | + err should include("Known error!") |
| 26 | + err should include("Run with --debug to see the complete stack trace.") |
| 27 | + } |
| 28 | + |
| 29 | + "print stack trace for known exceptions in debug mode" in { |
| 30 | + val command = makeMockCommand(new CriticalException("Known error!")) |
| 31 | + intercept[ExitException] { |
| 32 | + command.main("test", Array("--debug")) |
| 33 | + } |
| 34 | + val err = command.getErrString |
| 35 | + err should include("Known error!") |
| 36 | + err should include("CriticalException") |
| 37 | + err should include("at eu.neverblink.jelly.cli.command.ErrorHandlerSpec") |
| 38 | + } |
| 39 | + |
| 40 | + "print stack trace for unknown exceptions if not in debug mode" in { |
| 41 | + val command = makeMockCommand(new RuntimeException("Unknown error!")) |
| 42 | + intercept[ExitException] { |
| 43 | + command.main("test", Array()) |
| 44 | + } |
| 45 | + val err = command.getErrString |
| 46 | + err should include("Unknown error") |
| 47 | + err should include("RuntimeException") |
| 48 | + err should include("at eu.neverblink.jelly.cli.command.ErrorHandlerSpec") |
| 49 | + } |
| 50 | + |
| 51 | + "print stack trace for unknown exceptions in debug mode" in { |
| 52 | + val command = makeMockCommand(new RuntimeException("Unknown error!")) |
| 53 | + intercept[ExitException] { |
| 54 | + command.main("test", Array("--debug")) |
| 55 | + } |
| 56 | + val err = command.getErrString |
| 57 | + err should include("Unknown error") |
| 58 | + err should include("RuntimeException") |
| 59 | + err should include("at eu.neverblink.jelly.cli.command.ErrorHandlerSpec") |
| 60 | + } |
| 61 | + } |
0 commit comments