Skip to content

Commit 60d5362

Browse files
committed
Add tests for the zio-test test framework
1 parent 931b753 commit 60d5362

File tree

2 files changed

+126
-52
lines changed

2 files changed

+126
-52
lines changed

modules/cli/src/main/scala/scala/cli/commands/test/Test.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ object Test extends ScalaCommand[TestOptions] {
248248
val testOnly = build.options.testOptions.testOnly
249249

250250
val extraArgs =
251-
(if (requireTests) Seq("--require-tests") else Nil) ++
251+
(if requireTests then Seq("--require-tests") else Nil) ++
252252
build.options.internal.verbosity.map(v => s"--verbosity=$v") ++
253253
testFrameworkOpt0.map(fw => s"--test-framework=$fw").toSeq ++
254254
testOnly.map(to => s"--test-only=$to").toSeq ++

modules/integration/src/test/scala/scala/cli/integration/TestTestDefinitions.scala

Lines changed: 125 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ abstract class TestTestDefinitions extends ScalaCliSuite with TestScalaVersionAr
99
_: TestScalaVersion =>
1010
protected lazy val extraOptions: Seq[String] = scalaVersionArgs ++ TestUtil.extraOptions
1111
private val utestVersion = "0.8.3"
12+
private val zioTestVersion = "2.1.17"
1213

1314
def successfulTestInputs(directivesString: String =
1415
s"//> using dep org.scalameta::munit::$munitVersion"): TestInputs = TestInputs(
@@ -811,66 +812,139 @@ abstract class TestTestDefinitions extends ScalaCliSuite with TestScalaVersionAr
811812
}
812813
}
813814

815+
test(s"zio-test warning when zio-test-sbt was not passed") {
816+
TestUtil.retryOnCi() {
817+
val expectedMessage = "Hello from zio"
818+
TestInputs(os.rel / "Zio.test.scala" ->
819+
s"""//> using test.dep dev.zio::zio-test::$zioTestVersion
820+
|import zio._
821+
|import zio.test._
822+
|
823+
|object SimpleSpec extends ZIOSpecDefault {
824+
| override def spec: Spec[TestEnvironment with Scope, Any] =
825+
| suite("SimpleSpec")(
826+
| test("print hello and assert true") {
827+
| for {
828+
| _ <- Console.printLine("$expectedMessage")
829+
| } yield assertTrue(true)
830+
| }
831+
| )
832+
|}
833+
|""".stripMargin).fromRoot { root =>
834+
val r = os.proc(TestUtil.cli, "test", ".", extraOptions)
835+
.call(cwd = root, check = false, stderr = os.Pipe)
836+
expect(r.exitCode == 1)
837+
val expectedWarning =
838+
"zio-test found in the class path, zio-test-sbt should be added to run zio tests"
839+
expect(r.err.trim().contains(expectedWarning))
840+
}
841+
}
842+
}
843+
814844
for {
815845
platformOptions <- Seq(
816846
Nil, // JVM
817847
Seq("--native"),
818848
Seq("--js")
819849
)
820850
platformDescription = platformOptions.headOption.map(o => s" ($o)").getOrElse(" (JVM)")
821-
}
822-
test(s"multiple test frameworks$platformDescription") {
823-
val scalatestMessage = "Hello from ScalaTest"
824-
val munitMessage = "Hello from Munit"
825-
val utestMessage = "Hello from utest"
826-
TestInputs(
827-
os.rel / "project.scala" ->
828-
s"""//> using test.dep org.scalatest::scalatest::3.2.19
829-
|//> using test.dep org.scalameta::munit::$munitVersion
830-
|//> using dep com.lihaoyi::utest::$utestVersion
831-
|""".stripMargin,
832-
os.rel / "scalatest.test.scala" ->
833-
s"""import org.scalatest.flatspec.AnyFlatSpec
834-
|
835-
|class ScalaTestSpec extends AnyFlatSpec {
836-
| "example" should "work" in {
837-
| assertResult(1)(1)
838-
| println("$scalatestMessage")
839-
| }
840-
|}
841-
|""".stripMargin,
842-
os.rel / "munit.test.scala" ->
843-
s"""import munit.FunSuite
851+
} {
852+
test(s"zio-test$platformDescription") {
853+
TestUtil.retryOnCi() {
854+
val expectedMessage = "Hello from zio"
855+
TestInputs(os.rel / "Zio.test.scala" ->
856+
s"""//> using test.dep dev.zio::zio-test::$zioTestVersion
857+
|//> using test.dep dev.zio::zio-test-sbt::$zioTestVersion
858+
|import zio._
859+
|import zio.test._
844860
|
845-
|class Munit extends FunSuite {
846-
| test("foo") {
847-
| assert(2 + 2 == 4)
848-
| println("$munitMessage")
849-
| }
861+
|object SimpleSpec extends ZIOSpecDefault {
862+
| override def spec: Spec[TestEnvironment with Scope, Any] =
863+
| suite("SimpleSpec")(
864+
| test("print hello and assert true") {
865+
| for {
866+
| _ <- Console.printLine("$expectedMessage")
867+
| } yield assertTrue(true)
868+
| }
869+
| )
850870
|}
851-
|""".stripMargin,
852-
os.rel / "utest.test.scala" ->
853-
s"""import utest._
854-
|
855-
|object MyTests extends TestSuite {
856-
| val tests = Tests {
857-
| test("foo") {
858-
| assert(2 + 2 == 4)
859-
| println("$utestMessage")
860-
| }
861-
| }
862-
|}""".stripMargin
863-
).fromRoot { root =>
864-
val r = os.proc(TestUtil.cli, "test", extraOptions, ".", platformOptions).call(cwd = root)
865-
val output = r.out.trim()
866-
expect(output.nonEmpty)
867-
expect(output.contains(scalatestMessage))
868-
expect(countSubStrings(output, scalatestMessage) == 1)
869-
expect(output.contains(munitMessage))
870-
expect(countSubStrings(output, munitMessage) == 1)
871-
expect(output.contains(utestMessage))
872-
expect(countSubStrings(output, utestMessage) == 1)
873-
println(output)
871+
|""".stripMargin).fromRoot { root =>
872+
val r = os.proc(TestUtil.cli, "test", ".", extraOptions, platformOptions).call(cwd = root)
873+
val output = r.out.trim()
874+
expect(output.contains(expectedMessage))
875+
expect(countSubStrings(output, expectedMessage) == 1)
876+
}
874877
}
875878
}
879+
880+
test(s"multiple test frameworks$platformDescription") {
881+
TestUtil.retryOnCi() {
882+
val expectedMessages @ Seq(scalatestMessage, munitMessage, utestMessage, zioMessage) =
883+
Seq("Hello from ScalaTest", "Hello from Munit", "Hello from utest", "Hello from zio")
884+
TestInputs(
885+
os.rel / "project.scala" ->
886+
s"""//> using test.dep org.scalatest::scalatest::3.2.19
887+
|//> using test.dep org.scalameta::munit::$munitVersion
888+
|//> using dep com.lihaoyi::utest::$utestVersion
889+
|//> using test.dep dev.zio::zio-test::$zioTestVersion
890+
|//> using test.dep dev.zio::zio-test-sbt::$zioTestVersion
891+
|""".stripMargin,
892+
os.rel / "scalatest.test.scala" ->
893+
s"""import org.scalatest.flatspec.AnyFlatSpec
894+
|
895+
|class ScalaTestSpec extends AnyFlatSpec {
896+
| "example" should "work" in {
897+
| assertResult(1)(1)
898+
| println("$scalatestMessage")
899+
| }
900+
|}
901+
|""".stripMargin,
902+
os.rel / "munit.test.scala" ->
903+
s"""import munit.FunSuite
904+
|
905+
|class Munit extends FunSuite {
906+
| test("foo") {
907+
| assert(2 + 2 == 4)
908+
| println("$munitMessage")
909+
| }
910+
|}
911+
|""".stripMargin,
912+
os.rel / "utest.test.scala" ->
913+
s"""import utest._
914+
|
915+
|object MyTests extends TestSuite {
916+
| val tests = Tests {
917+
| test("foo") {
918+
| assert(2 + 2 == 4)
919+
| println("$utestMessage")
920+
| }
921+
| }
922+
|}""".stripMargin,
923+
os.rel / "Zio.test.scala" ->
924+
s"""import zio._
925+
|import zio.test._
926+
|
927+
|object SimpleSpec extends ZIOSpecDefault {
928+
| override def spec: Spec[TestEnvironment with Scope, Any] =
929+
| suite("SimpleSpec")(
930+
| test("print hello and assert true") {
931+
| for {
932+
| _ <- Console.printLine("$zioMessage")
933+
| } yield assertTrue(true)
934+
| }
935+
| )
936+
|}
937+
|""".stripMargin
938+
).fromRoot { root =>
939+
val r = os.proc(TestUtil.cli, "test", extraOptions, ".", platformOptions).call(cwd = root)
940+
val output = r.out.trim()
941+
expect(output.nonEmpty)
942+
expectedMessages.foreach { expectedMessage =>
943+
expect(output.contains(expectedMessage))
944+
expect(countSubStrings(output, expectedMessage) == 1)
945+
}
946+
}
947+
}
948+
}
949+
}
876950
}

0 commit comments

Comments
 (0)