Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose rootCause util #542

Merged
merged 8 commits into from
Jun 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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