Skip to content

Commit

Permalink
Handle a StackOverflowError in addition to NonFatal errors
Browse files Browse the repository at this point in the history
if they are happening inside a test

I only execute the test on the JVM, because
- on JS the error message is different
- on Native the runner just terminates
  • Loading branch information
mzuehlke committed Apr 18, 2023
1 parent 29eeea4 commit 91cb6db
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
32 changes: 19 additions & 13 deletions munit/shared/src/main/scala/munit/MUnitRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -285,25 +285,31 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite)
}

notifier.fireTestStarted(description)
def handleNonFatalOrStackOverflow(ex: Throwable): Future[Unit] = {
trimStackTrace(ex)
val cause = Exceptions.rootCause(ex)
val failure = new Failure(description, cause)
cause match {
case _: AssumptionViolatedException =>
notifier.fireTestAssumptionFailed(failure)
case _: FailSuiteException =>
suiteAborted = true
notifier.fireTestFailure(failure)
case _ =>
notifier.fireTestFailure(failure)
}
Future.successful(())
}

val onError: PartialFunction[Throwable, Future[Unit]] = {
case ex: AssumptionViolatedException =>
trimStackTrace(ex)
notifier.fireTestAssumptionFailed(new Failure(description, ex))
Future.successful(())
case NonFatal(ex) =>
trimStackTrace(ex)
val cause = Exceptions.rootCause(ex)
val failure = new Failure(description, cause)
cause match {
case _: AssumptionViolatedException =>
notifier.fireTestAssumptionFailed(failure)
case _: FailSuiteException =>
suiteAborted = true
notifier.fireTestFailure(failure)
case _ =>
notifier.fireTestFailure(failure)
}
Future.successful(())
handleNonFatalOrStackOverflow(ex)
case ex: StackOverflowError =>
handleNonFatalOrStackOverflow(ex)
}
val result: Future[Unit] =
try runTestBody(notifier, description, test).recoverWith(onError)
Expand Down
25 changes: 25 additions & 0 deletions tests/shared/src/main/scala/munit/Issue583FrameworkSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package munit

class Issue583FrameworkSuite extends FunSuite {
test("simple test") {
()
}
test("infinite loop test") {
def loop(x: Int): Int = loop(x) + loop(x)

loop(0)
}
test("another test") {
()
}
}

object Issue583FrameworkSuite
extends FrameworkTest(
classOf[Issue583FrameworkSuite],
"""|==> success munit.Issue583FrameworkSuite.simple test
|==> failure munit.Issue583FrameworkSuite.infinite loop test - null
|==> success munit.Issue583FrameworkSuite.another test
|""".stripMargin,
//tags = Set(OnlyJVM)
)
1 change: 1 addition & 0 deletions tests/shared/src/test/scala/munit/FrameworkSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class FrameworkSuite extends BaseFrameworkSuite {
Issue179FrameworkSuite,
Issue285FrameworkSuite,
Issue497FrameworkSuite,
Issue583FrameworkSuite,
ScalaCheckExceptionFrameworkSuite,
BoxedFrameworkSuite,
SkippedFrameworkSuite
Expand Down

0 comments on commit 91cb6db

Please sign in to comment.