Skip to content

Instantly share code, notes, and snippets.

@danicheg
Created September 15, 2021 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danicheg/be9ed79cd6f5764899e7cc16fe23e48c to your computer and use it in GitHub Desktop.
Save danicheg/be9ed79cd6f5764899e7cc16fe23e48c to your computer and use it in GitHub Desktop.
package munit
import cats.effect.{IO, Resource}
// ce3/js/src/main/scala/munit/ResourceSuiteLocalFixturePlatform
private[munit] trait ResourceSuiteLocalFixturePlatform { self: CatsEffectSuite =>
def createResourceSuiteLocalFixturePlatform[T](
name: String,
resource: Resource[IO, T]
): Fixture[T] =
new Fixture[T](name) {
var value: Option[Either[Throwable, (T, IO[Unit])]] = None
def apply(): T = value match {
case Some(Right(v)) => v._1
case Some(Left(ex)) => throw ex
case None => throw new FixtureNotInstantiatedException(name)
}
override def beforeAll(): Unit = {
val resourceEffect = resource.allocated
resourceEffect.unsafeRunAsync { result =>
value = Some(result)
}
}
override def afterAll(): Unit = {
value match {
case None =>
throw new FixtureNotInstantiatedException(name)
case Some(Right((_, cleanup))) =>
cleanup.unsafeRunAsync(_ => ())
case Some(Left(ex)) =>
throw ex
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment