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 6 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

This file was deleted.

4 changes: 0 additions & 4 deletions munit/js/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,4 @@ object PlatformCompat {
private var myClassLoader: ClassLoader = _
def setThisClassLoader(loader: ClassLoader): Unit = myClassLoader = loader
def getThisClassLoader: ClassLoader = myClassLoader

type InvocationTargetException = munit.internal.InvocationTargetException
type UndeclaredThrowableException =
munit.internal.UndeclaredThrowableException
}

This file was deleted.

4 changes: 0 additions & 4 deletions munit/jvm/src/main/scala/munit/internal/PlatformCompat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,4 @@ object PlatformCompat {
def isJS: Boolean = false
def isNative: Boolean = false
def getThisClassLoader: ClassLoader = this.getClass().getClassLoader()

type InvocationTargetException = java.lang.reflect.InvocationTargetException
type UndeclaredThrowableException =
java.lang.reflect.UndeclaredThrowableException
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,4 @@ object PlatformCompat {
private var myClassLoader: ClassLoader = _
def setThisClassLoader(loader: ClassLoader): Unit = myClassLoader = loader
def getThisClassLoader: ClassLoader = myClassLoader

type InvocationTargetException = java.lang.reflect.InvocationTargetException
type UndeclaredThrowableException =
java.lang.reflect.UndeclaredThrowableException
}
31 changes: 17 additions & 14 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 @@ -18,6 +16,7 @@ import org.junit.runner.notification.RunNotifier

import java.lang.reflect.Modifier
import java.util.concurrent.ExecutionException
import scala.annotation.tailrec
import scala.collection.mutable
import scala.concurrent.Await
import scala.concurrent.ExecutionContext
Expand Down Expand Up @@ -293,7 +292,7 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
Future.successful(())
case NonFatal(ex) =>
trimStackTrace(ex)
val cause = rootCause(ex)
val cause = MUnitRunner.rootCause(ex)
val failure = new Failure(description, cause)
cause match {
case _: AssumptionViolatedException =>
Expand All @@ -315,16 +314,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 +417,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, MUnitRunner.rootCause(ex))
)
notifier.fireTestFinished(description)
}
private def trimStackTrace(ex: Throwable): Unit = {
Expand Down Expand Up @@ -465,4 +456,16 @@ object MUnitRunner {
case nsme: NoSuchMethodException => false
}
}

// NOTE(olafur): these exceptions appear when we await on futures. We unwrap
// these exception in order to provide more helpful error messages.
// NOTE(valencik): preserve binary compatibility as this util is used in
// downstream integration libraries.
@tailrec
private[munit] def rootCause(x: Throwable): Throwable = x match {
case _: ExceptionInInitializerError | _: ExecutionException
if x.getCause != null =>
rootCause(x.getCause)
case _ => x
}
}