Skip to content

Commit

Permalink
Merge pull request #542 from scalameta/rootcause
Browse files Browse the repository at this point in the history
Expose rootCause util
  • Loading branch information
valencik authored Jun 28, 2022
2 parents 64fee65 + de2997f commit ab24df6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
21 changes: 21 additions & 0 deletions munit/shared/src/main/scala/munit/Exceptions.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package munit

import munit.internal.PlatformCompat.InvocationTargetException
import munit.internal.PlatformCompat.UndeclaredThrowableException

import java.util.concurrent.ExecutionException
import scala.annotation.tailrec

object Exceptions {

// NOTE(olafur): these exceptions appear when we await on futures. We unwrap
// these exception in order to provide more helpful error messages.
@tailrec
def rootCause(x: Throwable): Throwable = x match {
case _: InvocationTargetException | _: ExceptionInInitializerError |
_: UndeclaredThrowableException | _: ExecutionException
if x.getCause != null =>
rootCause(x.getCause)
case _ => x
}
}
19 changes: 4 additions & 15 deletions munit/shared/src/main/scala/munit/MUnitRunner.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package munit

import munit.internal.FutureCompat._
import munit.internal.PlatformCompat.InvocationTargetException
import munit.internal.PlatformCompat
import munit.internal.PlatformCompat.UndeclaredThrowableException
import munit.internal.console.Printers
import munit.internal.console.StackTraces
import munit.internal.junitinterface.Configurable
Expand All @@ -17,7 +15,6 @@ import org.junit.runner.notification.Failure
import org.junit.runner.notification.RunNotifier

import java.lang.reflect.Modifier
import java.util.concurrent.ExecutionException
import scala.collection.mutable
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
Expand Down Expand Up @@ -293,7 +290,7 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
Future.successful(())
case NonFatal(ex) =>
trimStackTrace(ex)
val cause = rootCause(ex)
val cause = Exceptions.rootCause(ex)
val failure = new Failure(description, cause)
cause match {
case _: AssumptionViolatedException =>
Expand All @@ -315,16 +312,6 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
}
}

// NOTE(olafur): these exceptions appear when we await on futures. We unwrap
// these exception in order to provide more helpful error messages.
private def rootCause(x: Throwable): Throwable = x match {
case _: InvocationTargetException | _: ExceptionInInitializerError |
_: UndeclaredThrowableException | _: ExecutionException
if x.getCause != null =>
rootCause(x.getCause)
case _ => x
}

private def futureFromAny(any: Any): Future[Any] = any match {
case f: Future[_] => f
case _ => Future.successful(any)
Expand Down Expand Up @@ -428,7 +415,9 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
val description = createTestDescription(test)
notifier.fireTestStarted(description)
trimStackTrace(ex)
notifier.fireTestFailure(new Failure(description, rootCause(ex)))
notifier.fireTestFailure(
new Failure(description, Exceptions.rootCause(ex))
)
notifier.fireTestFinished(description)
}
private def trimStackTrace(ex: Throwable): Unit = {
Expand Down

0 comments on commit ab24df6

Please sign in to comment.