From 516bea3f68854f39da96cadeaa6e79f6b44e4e44 Mon Sep 17 00:00:00 2001 From: Olafur Pall Geirsson Date: Sun, 8 Mar 2020 16:00:46 +0000 Subject: [PATCH] Fix buggy filtering examples from blog post. Previously, the "rich filtering capabilities" examples from the initial MUnit release announcement for v0.3 did not work as expected because of the async changes in v0.5. This commit fixes the example and improves the handling of tests with the Ignore tag. --- .../src/main/scala/munit/FunSuite.scala | 2 -- .../src/main/scala/munit/MUnitRunner.scala | 22 ++++++++++++------- website/blog/2020-02-01-hello-world.md | 6 ++--- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/munit/shared/src/main/scala/munit/FunSuite.scala b/munit/shared/src/main/scala/munit/FunSuite.scala index 35f9f344..04f374d4 100644 --- a/munit/shared/src/main/scala/munit/FunSuite.scala +++ b/munit/shared/src/main/scala/munit/FunSuite.scala @@ -106,8 +106,6 @@ abstract class FunSuite munitExpectFailure(options, body) } else if (options.tags(Flaky)) { munitFlaky(options, body) - } else if (options.tags(Ignore)) { - Future.successful(Ignore) } else { body() } diff --git a/munit/shared/src/main/scala/munit/MUnitRunner.scala b/munit/shared/src/main/scala/munit/MUnitRunner.scala index 95d9c9c2..a2890040 100644 --- a/munit/shared/src/main/scala/munit/MUnitRunner.scala +++ b/munit/shared/src/main/scala/munit/MUnitRunner.scala @@ -201,6 +201,10 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite) return Future.successful(false) } notifier.fireTestStarted(description) + if (test.tags(Ignore)) { + notifier.fireTestIgnored(description) + return Future.successful(false) + } val onError: PartialFunction[Throwable, Future[Unit]] = { case ex: AssumptionViolatedException => StackTraces.trimStackTrace(ex) @@ -225,14 +229,19 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite) } } + private def futureFromAny(any: Any): Future[Any] = any match { + case f: Future[_] => f + case _ => Future.successful(any) + } + private def runTestBody( notifier: RunNotifier, description: Description, test: suite.Test ): Future[Unit] = { - val result = StackTraces.dropOutside { + val result: Future[Any] = StackTraces.dropOutside { val beforeEachResult = runBeforeEach(test) - beforeEachResult.error match { + val any = beforeEachResult.error match { case None => try test.body() finally runAfterEach(test, beforeEachResult.loadedFixtures) @@ -240,19 +249,16 @@ class MUnitRunner(val cls: Class[_ <: Suite], newInstance: () => Suite) try runAfterEach(test, beforeEachResult.loadedFixtures) finally throw error } + futureFromAny(any) } - result match { + result.map { case f: TestValues.FlakyFailure => StackTraces.trimStackTrace(f) notifier.fireTestAssumptionFailed(new Failure(description, f)) - Future.successful(()) case TestValues.Ignore => notifier.fireTestIgnored(description) - Future.successful(()) - case f: Future[_] => - f.map(_ => ()) case _ => - Future.successful(()) + () } } diff --git a/website/blog/2020-02-01-hello-world.md b/website/blog/2020-02-01-hello-world.md index 18178438..fd72201c 100644 --- a/website/blog/2020-02-01-hello-world.md +++ b/website/blog/2020-02-01-hello-world.md @@ -127,14 +127,14 @@ import scala.util.Properties import munit._ object Windows213 extends Tag("Windows213") class MySuite extends FunSuite { - // reminder: type Test = GenericTest[Any] + // reminder: type Test = GenericTest[Future[Any]] override def munitNewTest(test: Test): Test = { val isIgnored = - options.tags(Windows213) && !( + test.tags(Windows213) && !( Properties.isWin && Properties.versionNumberString.startsWith("2.13") ) - if (isIgnored) test.withBody(() => Ignore) + if (isIgnored) test.tag(Ignore) else test }