Skip to content

Commit 6c96906

Browse files
author
Thilo Schuchort
committed
Run ResourceIO tests
1 parent 4f2762b commit 6c96906

File tree

3 files changed

+74
-6
lines changed

3 files changed

+74
-6
lines changed

core/src/main/scala/munit/CatsEffectSuite.scala

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@
1717
package munit
1818

1919
import cats.effect.unsafe.IORuntime
20-
import cats.effect.{IO, SyncIO}
20+
import cats.effect.{IO, ResourceIO, SyncIO}
2121

2222
import scala.annotation.nowarn
2323
import scala.concurrent.{ExecutionContext, Future, TimeoutException}
24-
import scala.concurrent.duration._
25-
import munit.internal.NestingChecks.{checkNestingIO, checkNestingSyncIO}
24+
import scala.concurrent.duration.*
25+
import munit.internal.NestingChecks.{checkNestingIO, checkNestingSyncIO, checkNestingResourceIO}
2626

2727
abstract class CatsEffectSuite
2828
extends FunSuite
@@ -62,7 +62,11 @@ abstract class CatsEffectSuite
6262
override def munitTimeout: Duration = munitIOTimeout + 1.second
6363

6464
override def munitValueTransforms: List[ValueTransform] =
65-
super.munitValueTransforms ++ List(munitIOTransform, munitSyncIOTransform)
65+
super.munitValueTransforms ++ List(
66+
munitIOTransform,
67+
munitResourceIOTransform,
68+
munitSyncIOTransform
69+
)
6670

6771
private val munitIOTransform: ValueTransform =
6872
new ValueTransform(
@@ -79,6 +83,21 @@ abstract class CatsEffectSuite
7983
}
8084
)
8185

86+
private val munitResourceIOTransform: ValueTransform =
87+
new ValueTransform(
88+
"ResourceIO",
89+
{ case e: ResourceIO[_] =>
90+
val unnestedResourceIO = checkNestingResourceIO(e)
91+
92+
val timedIO = unnestedResourceIO.use_.timeoutTo(
93+
munitIOTimeout,
94+
IO.raiseError(new TimeoutException(s"test timed out after $munitIOTimeout"))
95+
)
96+
97+
timedIO.unsafeToFuture()
98+
}
99+
)
100+
82101
private val munitSyncIOTransform: ValueTransform =
83102
new ValueTransform(
84103
"SyncIO",

core/src/main/scala/munit/internal.scala

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
package munit.internal
1818

19-
import cats.effect.{IO, SyncIO}
20-
import cats.syntax.all._
19+
import cats.effect.{IO, ResourceIO, SyncIO}
20+
import cats.syntax.all.*
21+
2122
import scala.concurrent.Future
2223

2324
private[munit] object NestingChecks {
@@ -41,6 +42,11 @@ private[munit] object NestingChecks {
4142
"your test returns an `IO[IO[_]]`, which means the inner `IO` will not execute." ++
4243
" Call `.flatten` if you want it to execute, or `.void` if you want to discard it"
4344
)
45+
case _: ResourceIO[_] =>
46+
err(
47+
"your test returns an `IO[ResourceIO[_]]`, which means the inner `ResourceIO` will not execute." ++
48+
" Call `.flatMap(_.use_)` if you want it to execute, or `.void` if you want to discard it"
49+
)
4450
case _: SyncIO[_] =>
4551
err(
4652
"your test returns an `IO[SyncIO[_]]`, which means the inner `SyncIO` will not execute." ++
@@ -55,6 +61,35 @@ private[munit] object NestingChecks {
5561
}
5662
}
5763

64+
// same as above, but for ResourceIO
65+
def checkNestingResourceIO(fa: ResourceIO[_]): ResourceIO[Any] = {
66+
def err(msg: String) = IO.raiseError[Any](new Exception(msg)).toResource
67+
68+
fa.flatMap {
69+
case _: IO[_] =>
70+
err(
71+
"your test returns a `ResourceIO[IO[_]]`, which means the inner `IO` will not execute." ++
72+
" Call `.flatMap(_.toResource)` if you want it to execute, or `.void` if you want to discard it"
73+
)
74+
case _: ResourceIO[_] =>
75+
err(
76+
"your test returns a `ResourceIO[ResourceIO[_]]`, which means the inner `ResourceIO` will not execute." ++
77+
" Call `.flatten` if you want it to execute, or `.void` if you want to discard it"
78+
)
79+
case _: SyncIO[_] =>
80+
err(
81+
"your test returns a `ResourceIO[SyncIO[_]]`, which means the inner `SyncIO` will not execute." ++
82+
" Call `.flatMap(_.to[IO].toResource)` if you want it to execute, or `.void` if you want to discard it"
83+
)
84+
case _: Future[_] =>
85+
err(
86+
"your test returns a `ResourceIO[Future[_]]`, which means the inner `Future` might not execute." ++
87+
" Change it to `_.flatMap(x => IO.fromFuture(IO.pure(x)).toResource)` if you want it to execute, or call `.void` if you want to discard it"
88+
)
89+
case v => v.pure[ResourceIO]
90+
}
91+
}
92+
5893
// same as above, but for SyncIO
5994
def checkNestingSyncIO(fa: SyncIO[_]): SyncIO[Any] = {
6095
def err(msg: String) = SyncIO.raiseError[Any](new Exception(msg))
@@ -65,6 +100,11 @@ private[munit] object NestingChecks {
65100
"your test returns a `SyncIO[IO[_]]`, which means the inner `IO` will not execute." ++
66101
" Call `.to[IO].flatten` if you want it to execute, or `.void` if you want to discard it"
67102
)
103+
case _: ResourceIO[_] =>
104+
err(
105+
"your test returns an `SyncIO[ResourceIO[_]]`, which means the inner `ResourceIO` will not execute." ++
106+
" Call `.to[IO].flatMap(_.use_)` if you want it to execute, or `.void` if you want to discard it"
107+
)
68108
case _: SyncIO[_] =>
69109
err(
70110
"your test returns a `SyncIO[SyncIO[_]]`, which means the inner `SyncIO` will not execute." ++

core/src/test/scala/munit/CatsEffectSuiteSpec.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,18 @@ class CatsEffectSuiteSpec extends CatsEffectSuite {
3131
test("times out".fail) { IO.sleep(1.second) }
3232

3333
test("nested IO fail".fail) { IO(IO(1)) }
34+
test("nested IO and ResourceIO fail".fail) { IO(IO(1).toResource) }
3435
test("nested IO and SyncIO fail".fail) { IO(SyncIO(1)) }
3536
test("nested IO and Future fail".fail) { IO(Future.successful(1)) }
37+
38+
test("evaluates ResourceIO".fail) { IO.raiseError(new RuntimeException("hello")).toResource }
39+
test("nested ResourceIO fail".fail) { IO(IO(1).toResource).toResource }
40+
test("nested ResourceIO and IO".fail) { IO(IO(1)).toResource }
41+
test("nested ResourceIO and SyncIO fail".fail) { IO(SyncIO(1)).toResource }
42+
test("nested ResourceIO and Future fail".fail) { IO(Future.successful(1)).toResource }
43+
3644
test("nested SyncIO fail".fail) { SyncIO(SyncIO(1)) }
3745
test("nested SyncIO and IO fail".fail) { SyncIO(IO(1)) }
46+
test("nested SyncIO and ResourceIO fail".fail) { SyncIO(IO(1).toResource) }
3847
test("nested SyncIO and Future fail".fail) { SyncIO(Future.successful(1)) }
3948
}

0 commit comments

Comments
 (0)