From 974242fd832a4f1e938011ca3119e3174ee87bbd Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sat, 18 Jun 2022 23:40:00 +0000 Subject: [PATCH 01/12] Simplify the build --- .github/workflows/ci.yml | 18 ++--- build.sbt | 69 +++---------------- .../munit/CatsEffectFixturesPlatform.scala | 0 .../scala/munit/CatsEffectSuitePlatform.scala | 0 .../munit/CatsEffectFixturesPlatform.scala | 0 .../scala/munit/CatsEffectSuitePlatform.scala | 0 .../CatsEffectFixturesPlatformSpec.scala | 0 .../scala/munit/CatsEffectAssertions.scala | 0 .../main/scala/munit/CatsEffectFixtures.scala | 0 .../scala/munit/CatsEffectFunFixtures.scala | 0 .../main/scala/munit/CatsEffectSuite.scala | 0 .../src/main/scala/munit/internal.scala | 0 .../munit/CatsEffectAssertionsSpec.scala | 0 .../CatsEffectAssertionsSyntaxSpec.scala | 0 .../scala/munit/CatsEffectFixturesSpec.scala | 0 .../munit/CatsEffectFunFixturesSpec.scala | 0 .../scala/munit/CatsEffectSuiteSpec.scala | 0 .../src/test/scala/munit/ExampleSuite.scala | 0 18 files changed, 18 insertions(+), 69 deletions(-) rename {common => core}/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala (100%) rename {ce3 => core}/js/src/main/scala/munit/CatsEffectSuitePlatform.scala (100%) rename {common => core}/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala (100%) rename {ce3 => core}/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala (100%) rename {common => core}/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala (100%) rename {common => core}/shared/src/main/scala/munit/CatsEffectAssertions.scala (100%) rename {common => core}/shared/src/main/scala/munit/CatsEffectFixtures.scala (100%) rename {common => core}/shared/src/main/scala/munit/CatsEffectFunFixtures.scala (100%) rename {ce3 => core}/shared/src/main/scala/munit/CatsEffectSuite.scala (100%) rename {common => core}/shared/src/main/scala/munit/internal.scala (100%) rename {common => core}/shared/src/test/scala/munit/CatsEffectAssertionsSpec.scala (100%) rename {common => core}/shared/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala (100%) rename {common => core}/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala (100%) rename {common => core}/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala (100%) rename {common => core}/shared/src/test/scala/munit/CatsEffectSuiteSpec.scala (100%) rename {ce3 => core}/shared/src/test/scala/munit/ExampleSuite.scala (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index aea42cd..35102ff 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - scala: [3.0.2, 2.12.16, 2.13.8] + scala: [3.1.2, 2.12.16, 2.13.8] java: [temurin@8] project: [rootJS, rootJVM] runs-on: ${{ matrix.os }} @@ -90,11 +90,11 @@ jobs: - name: Make target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') - run: mkdir -p target ce2/jvm/target .js/target site/target ce3/js/target .jvm/target .native/target ce2/js/target ce3/jvm/target project/target + run: mkdir -p target .js/target site/target core/js/target core/jvm/target .jvm/target .native/target project/target - name: Compress target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') - run: tar cf targets.tar target ce2/jvm/target .js/target site/target ce3/js/target .jvm/target .native/target ce2/js/target ce3/jvm/target project/target + run: tar cf targets.tar target .js/target site/target core/js/target core/jvm/target .jvm/target .native/target project/target - name: Upload target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') @@ -147,22 +147,22 @@ jobs: ~/Library/Caches/Coursier/v1 key: ${{ runner.os }}-sbt-cache-v2-${{ hashFiles('**/*.sbt') }}-${{ hashFiles('project/build.properties') }} - - name: Download target directories (3.0.2, rootJS) + - name: Download target directories (3.1.2, rootJS) uses: actions/download-artifact@v2 with: - name: target-${{ matrix.os }}-${{ matrix.java }}-3.0.2-rootJS + name: target-${{ matrix.os }}-${{ matrix.java }}-3.1.2-rootJS - - name: Inflate target directories (3.0.2, rootJS) + - name: Inflate target directories (3.1.2, rootJS) run: | tar xf targets.tar rm targets.tar - - name: Download target directories (3.0.2, rootJVM) + - name: Download target directories (3.1.2, rootJVM) uses: actions/download-artifact@v2 with: - name: target-${{ matrix.os }}-${{ matrix.java }}-3.0.2-rootJVM + name: target-${{ matrix.os }}-${{ matrix.java }}-3.1.2-rootJVM - - name: Inflate target directories (3.0.2, rootJVM) + - name: Inflate target directories (3.1.2, rootJVM) run: | tar xf targets.tar rm targets.tar diff --git a/build.sbt b/build.sbt index 5642203..b08a0e7 100644 --- a/build.sbt +++ b/build.sbt @@ -1,77 +1,26 @@ -ThisBuild / tlBaseVersion := "1.0" +ThisBuild / tlBaseVersion := "2.0" ThisBuild / developers += tlGitHubDev("milanvdm", "Milan van der Meer") ThisBuild / startYear := Some(2021) -ThisBuild / crossScalaVersions := List("3.0.2", "2.12.16", "2.13.8") +ThisBuild / crossScalaVersions := List("3.1.2", "2.12.16", "2.13.8") ThisBuild / tlFatalWarningsInCi := false lazy val docs = project .in(file("site")) - .dependsOn(ce3.jvm) + .dependsOn(core.jvm) .enablePlugins(TypelevelSitePlugin) -lazy val root = tlCrossRootProject.aggregate(ce3, ce2) +lazy val root = tlCrossRootProject.aggregate(core) -lazy val ce3 = crossProject(JSPlatform, JVMPlatform) +lazy val core = crossProject(JSPlatform, JVMPlatform) .crossType(CrossType.Full) + .in(file("core")) .settings( - name := "munit-cats-effect-3", - Compile / unmanagedSourceDirectories += baseDirectory.value / "../../common/shared/src/main/scala", - Test / unmanagedSourceDirectories += baseDirectory.value / "../../common/shared/src/test/scala" - ) - .settings( + name := "munit-cats-effect", libraryDependencies ++= Seq( - "org.scalameta" %%% "munit" % "0.7.29", + "org.scalameta" %%% "munit" % "1.0.0-M5", "org.typelevel" %%% "cats-effect" % "3.3.12" - ), - // we are checking binary compatibility from the 1.0.6 version - mimaPreviousArtifacts ~= { - _.filter { m => - VersionNumber(m.revision).matchesSemVer(SemanticSelector(">=1.0.6")) - } - } - ) - .jvmSettings( - Compile / unmanagedSourceDirectories += baseDirectory.value / "../../common/jvm/src/main/scala", - Test / unmanagedSourceDirectories += baseDirectory.value / "../../common/jvm/src/test/scala" - ) - .jsSettings( - Compile / unmanagedSourceDirectories += baseDirectory.value / "../../common/js/src/main/scala", - Test / unmanagedSourceDirectories += baseDirectory.value / "../../common/js/src/test/scala", - scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)) - ) - -lazy val ce2 = crossProject(JSPlatform, JVMPlatform) - .crossType(CrossType.Full) - .settings( - name := "munit-cats-effect-2", - Compile / unmanagedSourceDirectories += baseDirectory.value / "../../common/shared/src/main/scala", - Test / unmanagedSourceDirectories += baseDirectory.value / "../../common/shared/src/test/scala" - ) - .settings( - libraryDependencies ++= Seq( - "org.scalameta" %%% "munit" % "0.7.29", - "org.typelevel" %%% "cats-effect" % "2.5.5" - ), - // we are checking binary compatibility from the 1.0.6 version - mimaPreviousArtifacts ~= { - _.filter { m => - VersionNumber(m.revision).matchesSemVer(SemanticSelector(">=1.0.6")) - } - } + ) ) - .jvmSettings( - Compile / unmanagedSourceDirectories += baseDirectory.value / "../../common/jvm/src/main/scala", - Test / unmanagedSourceDirectories += baseDirectory.value / "../../common/jvm/src/test/scala" - ) - .jsSettings( - libraryDependencies += "org.scala-js" %%% "scala-js-macrotask-executor" % "1.0.0", - Compile / unmanagedSourceDirectories += baseDirectory.value / "../../common/js/src/main/scala", - Test / unmanagedSourceDirectories += baseDirectory.value / "../../common/js/src/test/scala", - scalaJSLinkerConfig ~= (_.withModuleKind(ModuleKind.CommonJSModule)) - ) - -addCommandAlias("fmt", """scalafmtSbt;scalafmtAll""") -addCommandAlias("fmtCheck", """scalafmtSbtCheck;scalafmtCheckAll""") diff --git a/common/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala b/core/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala similarity index 100% rename from common/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala rename to core/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala diff --git a/ce3/js/src/main/scala/munit/CatsEffectSuitePlatform.scala b/core/js/src/main/scala/munit/CatsEffectSuitePlatform.scala similarity index 100% rename from ce3/js/src/main/scala/munit/CatsEffectSuitePlatform.scala rename to core/js/src/main/scala/munit/CatsEffectSuitePlatform.scala diff --git a/common/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala b/core/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala similarity index 100% rename from common/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala rename to core/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala diff --git a/ce3/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala b/core/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala similarity index 100% rename from ce3/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala rename to core/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala diff --git a/common/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala b/core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala similarity index 100% rename from common/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala rename to core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala diff --git a/common/shared/src/main/scala/munit/CatsEffectAssertions.scala b/core/shared/src/main/scala/munit/CatsEffectAssertions.scala similarity index 100% rename from common/shared/src/main/scala/munit/CatsEffectAssertions.scala rename to core/shared/src/main/scala/munit/CatsEffectAssertions.scala diff --git a/common/shared/src/main/scala/munit/CatsEffectFixtures.scala b/core/shared/src/main/scala/munit/CatsEffectFixtures.scala similarity index 100% rename from common/shared/src/main/scala/munit/CatsEffectFixtures.scala rename to core/shared/src/main/scala/munit/CatsEffectFixtures.scala diff --git a/common/shared/src/main/scala/munit/CatsEffectFunFixtures.scala b/core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala similarity index 100% rename from common/shared/src/main/scala/munit/CatsEffectFunFixtures.scala rename to core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala diff --git a/ce3/shared/src/main/scala/munit/CatsEffectSuite.scala b/core/shared/src/main/scala/munit/CatsEffectSuite.scala similarity index 100% rename from ce3/shared/src/main/scala/munit/CatsEffectSuite.scala rename to core/shared/src/main/scala/munit/CatsEffectSuite.scala diff --git a/common/shared/src/main/scala/munit/internal.scala b/core/shared/src/main/scala/munit/internal.scala similarity index 100% rename from common/shared/src/main/scala/munit/internal.scala rename to core/shared/src/main/scala/munit/internal.scala diff --git a/common/shared/src/test/scala/munit/CatsEffectAssertionsSpec.scala b/core/shared/src/test/scala/munit/CatsEffectAssertionsSpec.scala similarity index 100% rename from common/shared/src/test/scala/munit/CatsEffectAssertionsSpec.scala rename to core/shared/src/test/scala/munit/CatsEffectAssertionsSpec.scala diff --git a/common/shared/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala b/core/shared/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala similarity index 100% rename from common/shared/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala rename to core/shared/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala diff --git a/common/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala b/core/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala similarity index 100% rename from common/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala rename to core/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala diff --git a/common/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala b/core/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala similarity index 100% rename from common/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala rename to core/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala diff --git a/common/shared/src/test/scala/munit/CatsEffectSuiteSpec.scala b/core/shared/src/test/scala/munit/CatsEffectSuiteSpec.scala similarity index 100% rename from common/shared/src/test/scala/munit/CatsEffectSuiteSpec.scala rename to core/shared/src/test/scala/munit/CatsEffectSuiteSpec.scala diff --git a/ce3/shared/src/test/scala/munit/ExampleSuite.scala b/core/shared/src/test/scala/munit/ExampleSuite.scala similarity index 100% rename from ce3/shared/src/test/scala/munit/ExampleSuite.scala rename to core/shared/src/test/scala/munit/ExampleSuite.scala From df9cd7d8943459077dbd6d0614c2e99bfdd90c5e Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sat, 18 Jun 2022 23:41:04 +0000 Subject: [PATCH 02/12] Enable fatal warnings --- build.sbt | 2 -- 1 file changed, 2 deletions(-) diff --git a/build.sbt b/build.sbt index b08a0e7..cc30b0c 100644 --- a/build.sbt +++ b/build.sbt @@ -5,8 +5,6 @@ ThisBuild / startYear := Some(2021) ThisBuild / crossScalaVersions := List("3.1.2", "2.12.16", "2.13.8") -ThisBuild / tlFatalWarningsInCi := false - lazy val docs = project .in(file("site")) .dependsOn(core.jvm) From ba30103f368e081140756e473ff6a9f748808e8c Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 02:18:55 +0000 Subject: [PATCH 03/12] Upgrade to munit 1.0.0-M5 --- .../munit/CatsEffectFixturesPlatform.scala | 51 ------------- .../scala/munit/CatsEffectSuitePlatform.scala | 28 ------- .../CatsEffectFixturesPlatformSpec.scala | 4 +- .../main/scala/munit/CatsEffectFixtures.scala | 44 +++++------ .../scala/munit/CatsEffectFunFixtures.scala | 66 ++++++++-------- .../main/scala/munit/CatsEffectSuite.scala | 7 -- .../munit/catseffect/ResourceFixture.scala | 75 +++++++++++++++++++ 7 files changed, 127 insertions(+), 148 deletions(-) delete mode 100644 core/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala delete mode 100644 core/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala create mode 100644 core/shared/src/main/scala/munit/catseffect/ResourceFixture.scala diff --git a/core/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala b/core/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala deleted file mode 100644 index f3f7330..0000000 --- a/core/jvm/src/main/scala/munit/CatsEffectFixturesPlatform.scala +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -import cats.effect.{IO, Resource} - -trait CatsEffectFixturesPlatform { self: CatsEffectSuite => - - object ResourceSuiteLocalFixture { - - final class FixtureNotInstantiatedException(name: String) - extends Exception( - s"The fixture `$name` was not instantiated. Override `munitFixtures` and include a reference to this fixture." - ) - - def apply[T](name: String, resource: Resource[IO, T]): Fixture[T] = - new Fixture[T](name) { - var value: Option[(T, IO[Unit])] = None - - def apply(): T = value match { - case Some(v) => v._1 - case None => throw new FixtureNotInstantiatedException(name) - } - - override def beforeAll(): Unit = { - val resourceEffect = resource.allocated - val (t, cleanup) = resourceEffect.unsafeRunSync() - value = Some(t -> cleanup) - } - - override def afterAll(): Unit = { - value.get._2.unsafeRunSync() - } - } - } - -} diff --git a/core/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala b/core/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala deleted file mode 100644 index f680555..0000000 --- a/core/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -import cats.effect.IO - -private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite => - - private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = { - ioa.unsafeRunSync() - () - } - -} diff --git a/core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala b/core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala index c66188b..a689411 100644 --- a/core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala +++ b/core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala @@ -17,6 +17,8 @@ package munit import cats.effect.{IO, Resource} +import munit.catseffect.ResourceFixture.FixtureNotInstantiatedException + import scala.concurrent.duration._ class CatsEffectFixturesPlatformSpec extends CatsEffectSuite with CatsEffectAssertions { @@ -66,7 +68,7 @@ class CatsEffectFixturesPlatformSpec extends CatsEffectSuite with CatsEffectAsse test("throws exception") { IO(uninitializedFixture()) - .interceptMessage[ResourceSuiteLocalFixture.FixtureNotInstantiatedException]( + .interceptMessage[FixtureNotInstantiatedException]( "The fixture `uninitialized-fixture` was not instantiated. Override `munitFixtures` and include a reference to this fixture." ) } diff --git a/core/shared/src/main/scala/munit/CatsEffectFixtures.scala b/core/shared/src/main/scala/munit/CatsEffectFixtures.scala index e81a9e2..49508db 100644 --- a/core/shared/src/main/scala/munit/CatsEffectFixtures.scala +++ b/core/shared/src/main/scala/munit/CatsEffectFixtures.scala @@ -16,37 +16,27 @@ package munit -import cats.effect.{IO, Resource} +import cats.effect.IO +import cats.effect.Resource +import munit.catseffect.IOFixture +import munit.catseffect.ResourceFixture -trait CatsEffectFixtures extends CatsEffectFixturesPlatform { self: CatsEffectSuite => +trait CatsEffectFixtures { - import CatsEffectSuite.Deferred - - /** Similar to `ResourceSuiteLocalFixture`, but supported on both JVM and JS via several caveats. - * Instead of directly providing `T` provides a (memoized) `IO[T]` that is backed by a - * `Deferred[T]`. It is unsafe because on JS the resource is closed concurrently without - * backpressure, - * i.e. the suite will complete even while the resource has not closed yet. On JVM it is - * semantically equivalent to `ResourceSuiteLocalFixture`. Note also that constructing this - * fixture is impure because it unsafely allocates a `Deferred`. - */ - object UnsafeResourceSuiteLocalDeferredFixture { - - def apply[T](name: String, resource: Resource[IO, T]): Fixture[IO[T]] = - new Fixture[IO[T]](name) { - val value: Deferred[IO, (T, IO[Unit])] = Deferred.unsafe - - def apply(): IO[T] = value.get.map(_._1) + object ResourceTestLocalFixture { + def apply[A](name: String, resource: Resource[IO, A]): IOFixture[A] = + ResourceFixture.testLocal(name, resource) + } - override def beforeAll(): Unit = { - val resourceEffect = resource.allocated.flatMap(value.complete) - unsafeRunSyncOrForget(resourceEffect) - } + object ResourceSuiteLocalFixture { + def apply[A](name: String, resource: Resource[IO, A]): IOFixture[A] = + ResourceFixture.suiteLocal(name, resource) + } - override def afterAll(): Unit = { - unsafeRunSyncOrForget(value.get.flatMap(_._2)) - } - } + @deprecated("Use ResourceSuiteLocalFixture", "2.0.0") + object UnsafeResourceSuiteLocalDeferredFixture { + def apply[A](name: String, resource: Resource[IO, A]): IOFixture[IO[A]] = + ResourceSuiteLocalFixture(name, resource.map(IO.pure)) } } diff --git a/core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala b/core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala index cc18e56..5d616c2 100644 --- a/core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala +++ b/core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala @@ -16,51 +16,49 @@ package munit -import cats.effect.{IO, SyncIO, Resource} -import cats.syntax.flatMap._ - -import scala.concurrent.Promise +import cats.effect.IO +import cats.effect.Resource +import cats.effect.SyncIO +import cats.effect.kernel.Deferred +import cats.syntax.all._ trait CatsEffectFunFixtures extends FunFixtures { self: CatsEffectSuite => + @deprecated("Use ResourceFunFixture", "2.0.0") object ResourceFixture { + def apply[A]( + resource: Resource[IO, A] + ): SyncIO[FunFixture[A]] = ResourceFunFixture(_ => resource) - def apply[T]( - resource: Resource[IO, T] - ): SyncIO[FunFixture[T]] = - apply( - resource, - (_, _) => IO.unit, - _ => IO.unit - ) - + @deprecated("ResourceFunFixture(TestOptions => Resource[IO, T])", "2.0.0") def apply[T]( resource: Resource[IO, T], setup: (TestOptions, T) => IO[Unit], teardown: T => IO[Unit] - ): SyncIO[FunFixture[T]] = SyncIO { - val promise = Promise[IO[Unit]]() + ): SyncIO[FunFixture[T]] = ResourceFunFixture { options => + resource.flatTap(t => Resource.make(setup(options, t))(_ => teardown(t))) + } + } - FunFixture.async( - setup = { testOptions => - val resourceEffect = resource.allocated - val setupEffect = - resourceEffect - .map { case (t, release) => - promise.success(release) - t - } - .flatTap(t => setup(testOptions, t)) + object ResourceFunFixture { - setupEffect.unsafeToFuture() - }, - teardown = { (argument: T) => - teardown(argument) - .flatMap(_ => IO.fromFuture(IO(promise.future)).flatten) - .unsafeToFuture() - } - ) - } + def apply[A]( + resource: Resource[IO, A] + ): SyncIO[FunFixture[A]] = apply(_ => resource) + + def apply[A](resource: TestOptions => Resource[IO, A]): SyncIO[FunFixture[A]] = + Deferred.in[SyncIO, IO, IO[Unit]].map { deferred => + FunFixture.async( + setup = { testOptions => + resource(testOptions).allocated + .flatMap { case (a, release) => + deferred.complete(release).as(a) + } + .unsafeToFuture() + }, + teardown = { _ => deferred.get.flatten.unsafeToFuture() } + ) + } } diff --git a/core/shared/src/main/scala/munit/CatsEffectSuite.scala b/core/shared/src/main/scala/munit/CatsEffectSuite.scala index 0b5cf2e..32ff1f2 100644 --- a/core/shared/src/main/scala/munit/CatsEffectSuite.scala +++ b/core/shared/src/main/scala/munit/CatsEffectSuite.scala @@ -24,7 +24,6 @@ import munit.internal.NestingChecks.{checkNestingIO, checkNestingSyncIO} abstract class CatsEffectSuite extends FunSuite - with CatsEffectSuitePlatform with CatsEffectAssertions with CatsEffectFixtures with CatsEffectFunFixtures { @@ -48,9 +47,3 @@ abstract class CatsEffectSuite { case e: SyncIO[_] => Future(checkNestingSyncIO(e).unsafeRunSync())(munitExecutionContext) } ) } - -object CatsEffectSuite { - private[munit] type Deferred[F[_], A] = cats.effect.kernel.Deferred[F, A] - private[munit] val Deferred = cats.effect.kernel.Deferred - -} diff --git a/core/shared/src/main/scala/munit/catseffect/ResourceFixture.scala b/core/shared/src/main/scala/munit/catseffect/ResourceFixture.scala new file mode 100644 index 0000000..7358231 --- /dev/null +++ b/core/shared/src/main/scala/munit/catseffect/ResourceFixture.scala @@ -0,0 +1,75 @@ +/* + * Copyright 2021 Typelevel + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package munit +package catseffect + +import cats.effect.IO +import cats.effect.Resource + +object ResourceFixture { + + final class FixtureNotInstantiatedException(name: String) + extends Exception( + s"The fixture `$name` was not instantiated. Override `munitFixtures` and include a reference to this fixture." + ) + + def testLocal[A](name: String, resource: Resource[IO, A]): IOFixture[A] = + new IOFixture[A](name) { + @volatile var value: Option[(A, IO[Unit])] = None + + def apply() = value match { + case Some(v) => v._1 + case None => throw new FixtureNotInstantiatedException(name) + } + + override def beforeEach(context: BeforeEach) = resource.allocated.flatMap { value => + IO(this.value = Some(value)) + } + + override def afterEach(context: AfterEach) = value.fold(IO.unit)(_._2) + } + + def suiteLocal[A](name: String, resource: Resource[IO, A]): IOFixture[A] = + new IOFixture[A](name) { + @volatile var value: A = _ + @volatile var release: IO[Unit] = IO.unit + + def apply() = value + + override def beforeAll() = resource.allocated.flatMap { case (a, release) => + IO { + value = a + this.release = release + } + } + + override def afterAll() = release + } + +} + +abstract class IOFixture[A](name: String) extends AnyFixture[A](name: String) { + + override def beforeAll(): IO[Unit] = IO.unit + + override def beforeEach(context: BeforeEach): IO[Unit] = IO.unit + + override def afterEach(context: AfterEach): IO[Unit] = IO.unit + + override def afterAll(): IO[Unit] = IO.unit + +} From b58baef38eab75dec5aa47a86513c78ca2acca2c Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 02:51:53 +0000 Subject: [PATCH 04/12] Update the tests --- build.sbt | 2 +- .../scala/munit/CatsEffectFixturesSpec.scala | 62 ------------------- .../scala/munit/CatsEffectAssertions.scala | 0 .../main/scala/munit/CatsEffectFixtures.scala | 0 .../scala/munit/CatsEffectFunFixtures.scala | 0 .../main/scala/munit/CatsEffectSuite.scala | 0 .../munit/catseffect/ResourceFixture.scala | 0 .../src/main/scala/munit/internal.scala | 0 .../munit/CatsEffectAssertionsSpec.scala | 0 .../CatsEffectAssertionsSyntaxSpec.scala | 0 .../scala/munit/CatsEffectFixturesSpec.scala} | 6 +- .../munit/CatsEffectFunFixturesSpec.scala | 49 ++++++--------- .../scala/munit/CatsEffectSuiteSpec.scala | 0 .../src/test/scala/munit/ExampleSuite.scala | 2 +- 14 files changed, 24 insertions(+), 97 deletions(-) delete mode 100644 core/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala rename core/{shared => }/src/main/scala/munit/CatsEffectAssertions.scala (100%) rename core/{shared => }/src/main/scala/munit/CatsEffectFixtures.scala (100%) rename core/{shared => }/src/main/scala/munit/CatsEffectFunFixtures.scala (100%) rename core/{shared => }/src/main/scala/munit/CatsEffectSuite.scala (100%) rename core/{shared => }/src/main/scala/munit/catseffect/ResourceFixture.scala (100%) rename core/{shared => }/src/main/scala/munit/internal.scala (100%) rename core/{shared => }/src/test/scala/munit/CatsEffectAssertionsSpec.scala (100%) rename core/{shared => }/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala (100%) rename core/{jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala => src/test/scala/munit/CatsEffectFixturesSpec.scala} (92%) rename core/{shared => }/src/test/scala/munit/CatsEffectFunFixturesSpec.scala (62%) rename core/{shared => }/src/test/scala/munit/CatsEffectSuiteSpec.scala (100%) rename core/{shared => }/src/test/scala/munit/ExampleSuite.scala (96%) diff --git a/build.sbt b/build.sbt index cc30b0c..8d193ed 100644 --- a/build.sbt +++ b/build.sbt @@ -13,7 +13,7 @@ lazy val docs = project lazy val root = tlCrossRootProject.aggregate(core) lazy val core = crossProject(JSPlatform, JVMPlatform) - .crossType(CrossType.Full) + .crossType(CrossType.Pure) .in(file("core")) .settings( name := "munit-cats-effect", diff --git a/core/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala b/core/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala deleted file mode 100644 index a07e9c1..0000000 --- a/core/shared/src/test/scala/munit/CatsEffectFixturesSpec.scala +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -import cats.effect.{IO, Resource} -import scala.concurrent.duration._ - -class CatsEffectFixturesSpec extends CatsEffectSuite with CatsEffectAssertions { - - @volatile var acquired: Int = 0 - @volatile var released: Int = 0 - - val fixture = UnsafeResourceSuiteLocalDeferredFixture( - "fixture", - Resource.make( - IO.sleep(1.millis) *> IO { - acquired += 1 - () - } - )(_ => - IO.sleep(1.millis) *> IO { - released += 1 - () - } - ) - ) - - override def munitFixtures = List(fixture) - - override def beforeAll(): Unit = { - assertEquals(acquired, 0) - assertEquals(released, 0) - } - - override def afterAll(): Unit = { - assertEquals(acquired, 1) - // assertEquals(released, 1) // Release is async, no way to check - } - - test("first test") { - fixture().assertEquals(()) - } - - test("second test") { - fixture().assertEquals(()) - } - -} diff --git a/core/shared/src/main/scala/munit/CatsEffectAssertions.scala b/core/src/main/scala/munit/CatsEffectAssertions.scala similarity index 100% rename from core/shared/src/main/scala/munit/CatsEffectAssertions.scala rename to core/src/main/scala/munit/CatsEffectAssertions.scala diff --git a/core/shared/src/main/scala/munit/CatsEffectFixtures.scala b/core/src/main/scala/munit/CatsEffectFixtures.scala similarity index 100% rename from core/shared/src/main/scala/munit/CatsEffectFixtures.scala rename to core/src/main/scala/munit/CatsEffectFixtures.scala diff --git a/core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala b/core/src/main/scala/munit/CatsEffectFunFixtures.scala similarity index 100% rename from core/shared/src/main/scala/munit/CatsEffectFunFixtures.scala rename to core/src/main/scala/munit/CatsEffectFunFixtures.scala diff --git a/core/shared/src/main/scala/munit/CatsEffectSuite.scala b/core/src/main/scala/munit/CatsEffectSuite.scala similarity index 100% rename from core/shared/src/main/scala/munit/CatsEffectSuite.scala rename to core/src/main/scala/munit/CatsEffectSuite.scala diff --git a/core/shared/src/main/scala/munit/catseffect/ResourceFixture.scala b/core/src/main/scala/munit/catseffect/ResourceFixture.scala similarity index 100% rename from core/shared/src/main/scala/munit/catseffect/ResourceFixture.scala rename to core/src/main/scala/munit/catseffect/ResourceFixture.scala diff --git a/core/shared/src/main/scala/munit/internal.scala b/core/src/main/scala/munit/internal.scala similarity index 100% rename from core/shared/src/main/scala/munit/internal.scala rename to core/src/main/scala/munit/internal.scala diff --git a/core/shared/src/test/scala/munit/CatsEffectAssertionsSpec.scala b/core/src/test/scala/munit/CatsEffectAssertionsSpec.scala similarity index 100% rename from core/shared/src/test/scala/munit/CatsEffectAssertionsSpec.scala rename to core/src/test/scala/munit/CatsEffectAssertionsSpec.scala diff --git a/core/shared/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala b/core/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala similarity index 100% rename from core/shared/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala rename to core/src/test/scala/munit/CatsEffectAssertionsSyntaxSpec.scala diff --git a/core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala b/core/src/test/scala/munit/CatsEffectFixturesSpec.scala similarity index 92% rename from core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala rename to core/src/test/scala/munit/CatsEffectFixturesSpec.scala index a689411..30dbaeb 100644 --- a/core/jvm/src/test/scala/munit/CatsEffectFixturesPlatformSpec.scala +++ b/core/src/test/scala/munit/CatsEffectFixturesSpec.scala @@ -21,10 +21,10 @@ import munit.catseffect.ResourceFixture.FixtureNotInstantiatedException import scala.concurrent.duration._ -class CatsEffectFixturesPlatformSpec extends CatsEffectSuite with CatsEffectAssertions { +class CatsEffectFixturesSpec extends CatsEffectSuite with CatsEffectAssertions { - var acquired: Int = 0 - var released: Int = 0 + @volatile var acquired: Int = 0 + @volatile var released: Int = 0 val fixture = ResourceSuiteLocalFixture( "fixture", diff --git a/core/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala b/core/src/test/scala/munit/CatsEffectFunFixturesSpec.scala similarity index 62% rename from core/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala rename to core/src/test/scala/munit/CatsEffectFunFixturesSpec.scala index 948425a..ed7708b 100644 --- a/core/shared/src/test/scala/munit/CatsEffectFunFixturesSpec.scala +++ b/core/src/test/scala/munit/CatsEffectFunFixturesSpec.scala @@ -16,43 +16,34 @@ package munit -import cats.effect.{IO, SyncIO, Resource} -import cats.syntax.flatMap._ +import cats.effect.IO +import cats.effect.Resource +import cats.effect.SyncIO import scala.concurrent.Promise import scala.concurrent.duration._ class CatsEffectFunFixturesSpec extends CatsEffectSuite with CatsEffectFunFixtures { val latch: Promise[Unit] = Promise[Unit]() - var completedFromTest: Option[Boolean] = None - var completedFromTeardown: Option[Boolean] = None - var completedFromResourceAcquire: Option[Boolean] = None - var completedFromResourceRelease: Option[Boolean] = None + @volatile var completedFromTest: Option[Boolean] = None + + @volatile var completedFromResourceAcquire: Option[Boolean] = None + @volatile var completedFromResourceRelease: Option[Boolean] = None val latchOnTeardown: SyncIO[FunFixture[String]] = - ResourceFixture[String]( - resource = Resource.make[IO, String]( - IO { - completedFromResourceAcquire = Some(true) - "test" - } - )(_ => - IO { - completedFromResourceRelease = Some(true) - } - ), - setup = { (_: TestOptions, _: String) => - IO { - completedFromResourceAcquire = Some(false) - } - }, - teardown = { (_: String) => - IO { - completedFromResourceRelease = Some(false) - completedFromTeardown = Some(latch.trySuccess(())); - } - } + ResourceFunFixture[String]( + Resource + .make[IO, String]( + IO { + completedFromResourceAcquire = Some(true) + "test" + } + )(_ => + IO { + completedFromResourceRelease = Some(true) + } + ) ) override def afterAll(): Unit = { @@ -62,8 +53,6 @@ class CatsEffectFunFixturesSpec extends CatsEffectSuite with CatsEffectFunFixtur assertEquals(completedFromResourceRelease, Some(true)) // promise was completed first by the test assertEquals(completedFromTest, Some(true)) - // and then there was a completion attempt by the teardown - assertEquals(completedFromTeardown, Some(false)) } latchOnTeardown.test("teardown runs only after test completes") { _ => diff --git a/core/shared/src/test/scala/munit/CatsEffectSuiteSpec.scala b/core/src/test/scala/munit/CatsEffectSuiteSpec.scala similarity index 100% rename from core/shared/src/test/scala/munit/CatsEffectSuiteSpec.scala rename to core/src/test/scala/munit/CatsEffectSuiteSpec.scala diff --git a/core/shared/src/test/scala/munit/ExampleSuite.scala b/core/src/test/scala/munit/ExampleSuite.scala similarity index 96% rename from core/shared/src/test/scala/munit/ExampleSuite.scala rename to core/src/test/scala/munit/ExampleSuite.scala index 6f2a316..6317650 100644 --- a/core/shared/src/test/scala/munit/ExampleSuite.scala +++ b/core/src/test/scala/munit/ExampleSuite.scala @@ -41,7 +41,7 @@ class ExampleSuite extends CatsEffectSuite { import cats.effect.std.Dispatcher - val dispatcher = ResourceFixture(Dispatcher[IO]) + val dispatcher = ResourceFunFixture(Dispatcher[IO]) dispatcher.test("resources can be lifted to munit fixtures") { dsp => dsp.unsafeRunAndForget(IO(42)) From 4ab51259f21573ed202cbf16c1206e2c41778980 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 03:05:35 +0000 Subject: [PATCH 05/12] Fix CatsEffectFunFixturesSpec --- .../munit/CatsEffectFunFixturesSpec.scala | 48 +++++++------------ 1 file changed, 18 insertions(+), 30 deletions(-) diff --git a/core/src/test/scala/munit/CatsEffectFunFixturesSpec.scala b/core/src/test/scala/munit/CatsEffectFunFixturesSpec.scala index ed7708b..aac5fad 100644 --- a/core/src/test/scala/munit/CatsEffectFunFixturesSpec.scala +++ b/core/src/test/scala/munit/CatsEffectFunFixturesSpec.scala @@ -17,52 +17,40 @@ package munit import cats.effect.IO +import cats.effect.Ref import cats.effect.Resource import cats.effect.SyncIO -import scala.concurrent.Promise import scala.concurrent.duration._ class CatsEffectFunFixturesSpec extends CatsEffectSuite with CatsEffectFunFixtures { - val latch: Promise[Unit] = Promise[Unit]() + val counter: Ref[IO, Int] = Ref.unsafe[IO, Int](0) - @volatile var completedFromTest: Option[Boolean] = None + @volatile var acquired: Option[Int] = None + @volatile var tested: Option[Int] = None + @volatile var released: Option[Int] = None - @volatile var completedFromResourceAcquire: Option[Boolean] = None - @volatile var completedFromResourceRelease: Option[Boolean] = None - - val latchOnTeardown: SyncIO[FunFixture[String]] = - ResourceFunFixture[String]( + val countingFixture: SyncIO[FunFixture[Unit]] = + ResourceFunFixture[Unit]( Resource - .make[IO, String]( - IO { - completedFromResourceAcquire = Some(true) - "test" - } - )(_ => - IO { - completedFromResourceRelease = Some(true) - } - ) + .make[IO, Unit]( + counter.getAndUpdate(_ + 1).flatMap(i => IO(this.acquired = Some(i))) + )(_ => counter.getAndUpdate(_ + 1).flatMap(i => IO(this.released = Some(i)))) ) override def afterAll(): Unit = { - // resource was created before setup - assertEquals(completedFromResourceAcquire, Some(false)) - // resource was released after teardown - assertEquals(completedFromResourceRelease, Some(true)) - // promise was completed first by the test - assertEquals(completedFromTest, Some(true)) + // resource was acquired first + assertEquals(acquired, Some(0)) + // then the test was run + assertEquals(tested, Some(1)) + // then it was released + assertEquals(released, Some(2)) } - latchOnTeardown.test("teardown runs only after test completes") { _ => + countingFixture.test("fixture runs before/after test") { _ => // Simulate some work here, which increases the certainty that this test // will fail by design and not by lucky scheduling if the happens-before // relationship between the test and teardown is removed. - IO.sleep(50.millis).flatTap { _ => - IO { - completedFromTest = Some(latch.trySuccess(())) - } - } + IO.sleep(50.millis) *> counter.getAndUpdate(_ + 1).flatMap(i => IO(this.tested = Some(i))) } } From 88db7d75701e9c9b36fb11da9d70048f835336f7 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 03:13:12 +0000 Subject: [PATCH 06/12] Bug fix --- .../munit/catseffect/ResourceFixture.scala | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/core/src/main/scala/munit/catseffect/ResourceFixture.scala b/core/src/main/scala/munit/catseffect/ResourceFixture.scala index 7358231..fef37e6 100644 --- a/core/src/main/scala/munit/catseffect/ResourceFixture.scala +++ b/core/src/main/scala/munit/catseffect/ResourceFixture.scala @@ -45,19 +45,18 @@ object ResourceFixture { def suiteLocal[A](name: String, resource: Resource[IO, A]): IOFixture[A] = new IOFixture[A](name) { - @volatile var value: A = _ - @volatile var release: IO[Unit] = IO.unit + @volatile var value: Option[(A, IO[Unit])] = None - def apply() = value + def apply() = value match { + case Some(v) => v._1 + case None => throw new FixtureNotInstantiatedException(name) + } - override def beforeAll() = resource.allocated.flatMap { case (a, release) => - IO { - value = a - this.release = release - } + override def beforeAll() = resource.allocated.flatMap { value => + IO(this.value = Some(value)) } - override def afterAll() = release + override def afterAll() = value.fold(IO.unit)(_._2) } } From 824f0917d9f94626909db732419dbdcbc15c69d3 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 17:21:56 +0000 Subject: [PATCH 07/12] Actually delete ce2 srcs --- .../scala/munit/CatsEffectSuitePlatform.scala | 30 ---------- .../scala/munit/CatsEffectSuitePlatform.scala | 32 ----------- .../main/scala/munit/CatsEffectSuite.scala | 57 ------------------- 3 files changed, 119 deletions(-) delete mode 100644 ce2/js/src/main/scala/munit/CatsEffectSuitePlatform.scala delete mode 100644 ce2/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala delete mode 100644 ce2/shared/src/main/scala/munit/CatsEffectSuite.scala diff --git a/ce2/js/src/main/scala/munit/CatsEffectSuitePlatform.scala b/ce2/js/src/main/scala/munit/CatsEffectSuitePlatform.scala deleted file mode 100644 index 063003d..0000000 --- a/ce2/js/src/main/scala/munit/CatsEffectSuitePlatform.scala +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -import cats.effect.IO -import org.scalajs.macrotaskexecutor.MacrotaskExecutor - -import scala.concurrent.ExecutionContext - -private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite => - - private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = ioa.unsafeRunAsyncAndForget() - - private[munit] def suiteExecutionContext: ExecutionContext = MacrotaskExecutor - -} diff --git a/ce2/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala b/ce2/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala deleted file mode 100644 index 4b48adf..0000000 --- a/ce2/jvm/src/main/scala/munit/CatsEffectSuitePlatform.scala +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -import cats.effect.IO - -import scala.concurrent.ExecutionContext - -private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite => - - private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = { - ioa.unsafeRunSync() - () - } - - private[munit] def suiteExecutionContext: ExecutionContext = ExecutionContext.global - -} diff --git a/ce2/shared/src/main/scala/munit/CatsEffectSuite.scala b/ce2/shared/src/main/scala/munit/CatsEffectSuite.scala deleted file mode 100644 index 85fd19b..0000000 --- a/ce2/shared/src/main/scala/munit/CatsEffectSuite.scala +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -import cats.effect.{ContextShift, IO, SyncIO, Timer} -import scala.concurrent.{Future, ExecutionContext} -import munit.internal.NestingChecks.{checkNestingIO, checkNestingSyncIO} - -abstract class CatsEffectSuite - extends FunSuite - with CatsEffectSuitePlatform - with CatsEffectAssertions - with CatsEffectFixtures - with CatsEffectFunFixtures { - - private val ec: ExecutionContext = suiteExecutionContext - - implicit def munitContextShift: ContextShift[IO] = - IO.contextShift(ec) - - implicit def munitTimer: Timer[IO] = - IO.timer(ec) - - override def munitValueTransforms: List[ValueTransform] = - super.munitValueTransforms ++ List(munitIOTransform, munitSyncIOTransform) - - private val munitIOTransform: ValueTransform = - new ValueTransform( - "IO", - { case e: IO[_] => checkNestingIO(e).unsafeToFuture() } - ) - - private val munitSyncIOTransform: ValueTransform = - new ValueTransform( - "SyncIO", - { case e: SyncIO[_] => Future(checkNestingSyncIO(e).unsafeRunSync())(munitExecutionContext) } - ) -} - -object CatsEffectSuite { - private[munit] type Deferred[F[_], A] = cats.effect.concurrent.Deferred[F, A] - private[munit] val Deferred = cats.effect.concurrent.Deferred -} From 98ecf0648a1e7406057ee129a9ad11a965be1e52 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 17:54:25 +0000 Subject: [PATCH 08/12] Fixup CatsEffectFixturesSpec --- .../scala/munit/CatsEffectFixturesSpec.scala | 32 +++++++++++++++---- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/core/src/test/scala/munit/CatsEffectFixturesSpec.scala b/core/src/test/scala/munit/CatsEffectFixturesSpec.scala index 30dbaeb..6634d89 100644 --- a/core/src/test/scala/munit/CatsEffectFixturesSpec.scala +++ b/core/src/test/scala/munit/CatsEffectFixturesSpec.scala @@ -46,18 +46,36 @@ class CatsEffectFixturesSpec extends CatsEffectSuite with CatsEffectAssertions { Resource.make(IO.unit)(_ => IO.unit) ) - override def munitFixtures = List(fixture) + object AssertBeforeFixture extends Fixture[Unit]("assertBefore") { + def apply() = () - override def beforeAll(): Unit = { - assertEquals(acquired, 0) - assertEquals(released, 0) + override def beforeAll(): Unit = { + assertEquals(acquired, 0) + assertEquals(released, 0) + } + + override def afterAll(): Unit = { + assertEquals(acquired, 1) + assertEquals(released, 0) + } } - override def afterAll(): Unit = { - assertEquals(acquired, 1) - assertEquals(released, 1) + object AssertAfterFixture extends Fixture[Unit]("assertAfter") { + def apply() = () + + override def beforeAll(): Unit = { + assertEquals(acquired, 1) + assertEquals(released, 0) + } + + override def afterAll(): Unit = { + assertEquals(acquired, 1) + assertEquals(released, 1) + } } + override def munitFixtures = List[AnyFixture[_]](AssertBeforeFixture, fixture, AssertAfterFixture) + test("first test") { IO(fixture()).assertEquals(()) } From b54943ad6192cd0081cdfcc3da491961e8a1c305 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 17:57:36 +0000 Subject: [PATCH 09/12] Regenerate workflow --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 35102ff..954044a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -90,11 +90,11 @@ jobs: - name: Make target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') - run: mkdir -p target .js/target site/target core/js/target core/jvm/target .jvm/target .native/target project/target + run: mkdir -p target .js/target site/target core/.js/target core/.jvm/target .jvm/target .native/target project/target - name: Compress target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') - run: tar cf targets.tar target .js/target site/target core/js/target core/jvm/target .jvm/target .native/target project/target + run: tar cf targets.tar target .js/target site/target core/.js/target core/.jvm/target .jvm/target .native/target project/target - name: Upload target directories if: github.event_name != 'pull_request' && (startsWith(github.ref, 'refs/tags/v') || github.ref == 'refs/heads/main') From cd8357d6aa48bdf9b413f201ce02b551d423b0b8 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 19 Jun 2022 18:09:24 +0000 Subject: [PATCH 10/12] Fix the docs --- docs/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/index.md b/docs/index.md index 25e50da..241b1ca 100644 --- a/docs/index.md +++ b/docs/index.md @@ -50,7 +50,7 @@ class ExampleSuite extends CatsEffectSuite { import cats.effect.std.Dispatcher - val dispatcher = ResourceFixture(Dispatcher[IO]) + val dispatcher = ResourceFunFixture(Dispatcher[IO]) dispatcher.test("resources can be lifted to munit fixtures") { dsp => dsp.unsafeRunAndForget(IO(42)) From 2af08f7638f6fefc13095b22a03d46e8d98cce69 Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Tue, 28 Jun 2022 10:11:52 -0700 Subject: [PATCH 11/12] munit 1.0.0-M6 --- build.sbt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sbt b/build.sbt index 8d193ed..0d82710 100644 --- a/build.sbt +++ b/build.sbt @@ -18,7 +18,7 @@ lazy val core = crossProject(JSPlatform, JVMPlatform) .settings( name := "munit-cats-effect", libraryDependencies ++= Seq( - "org.scalameta" %%% "munit" % "1.0.0-M5", + "org.scalameta" %%% "munit" % "1.0.0-M6", "org.typelevel" %%% "cats-effect" % "3.3.12" ) ) From 146426a32177968677cb11816e520c273a4a4cea Mon Sep 17 00:00:00 2001 From: Arman Bilge Date: Sun, 17 Jul 2022 09:24:57 +0000 Subject: [PATCH 12/12] Delete more dead code --- .../munit/CatsEffectFixturesPlatform.scala | 19 -------------- .../scala/munit/CatsEffectSuitePlatform.scala | 25 ------------------- 2 files changed, 44 deletions(-) delete mode 100644 core/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala delete mode 100644 core/js/src/main/scala/munit/CatsEffectSuitePlatform.scala diff --git a/core/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala b/core/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala deleted file mode 100644 index 9a21cf3..0000000 --- a/core/js/src/main/scala/munit/CatsEffectFixturesPlatform.scala +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -trait CatsEffectFixturesPlatform { self: CatsEffectSuite => } diff --git a/core/js/src/main/scala/munit/CatsEffectSuitePlatform.scala b/core/js/src/main/scala/munit/CatsEffectSuitePlatform.scala deleted file mode 100644 index 06a7a60..0000000 --- a/core/js/src/main/scala/munit/CatsEffectSuitePlatform.scala +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright 2021 Typelevel - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package munit - -import cats.effect.IO - -private[munit] trait CatsEffectSuitePlatform { self: CatsEffectSuite => - - private[munit] def unsafeRunSyncOrForget[A](ioa: IO[A]): Unit = ioa.unsafeRunAndForget() - -}