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

Handle a StackOverflowError in addition to NonFatal errors #648

Merged
merged 1 commit into from
Apr 19, 2023
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
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