From 5f3d9c1f0985b14e204f366d8a589f6c983e5372 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Mon, 28 Mar 2022 11:40:26 -0600 Subject: [PATCH 01/22] Start on some tests & notes --- .../zio/test/sbt/ZTestFrameworkSpec.scala | 5 +-- .../test/scala/zio/test/TestOutputSpec.scala | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala diff --git a/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala b/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala index bd5d3fad5d27..e988c3ce2c17 100644 --- a/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala +++ b/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala @@ -26,8 +26,9 @@ object ZTestFrameworkSpec { // test("should correctly display colorized output for multi-line strings")(testColored()), // test("should test only selected test")(testTestSelection()), // test("should return summary when done")(testSummary()), - test("should use a shared layer without re-initializing it")(testSharedLayer()) -// test("should warn when no tests are executed")(testNoTestsExecutedWarning()) + test("should use a shared layer without re-initializing it")(testSharedLayer()), +// test("should warn when no tests are executed")(testNoTestsExecutedWarning()), +// test("should ensure that output is not interleaved")(???) ) def testFingerprints(): Unit = { diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala new file mode 100644 index 000000000000..9095ac3af0df --- /dev/null +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -0,0 +1,31 @@ +package zio.test +import zio.Scope + +import zio._ + +object TestOutputSpec extends ZIOSpecDefault { + override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( + test("TestOutput.run") { + for { + testConsole <- ZIO.service[TestConsole] + testOutput <- ZIO.service[TestOutput] + _ <- testOutput.printOrQueue(SuiteId(2), List(SuiteId(1)), ExecutionEvent.Test( + labelsReversed = List("a", "b"), + test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), + annotations = TestAnnotationMap.empty, + ancestors = List.empty, + duration = 0L, + id = SuiteId(1) + )) + output <- testConsole.output + _ <- ZIO.debug(output) + _ <- ZIO.succeed("blah") + } yield assertTrue(1 == 1) + } + ).provideSome[ + TestConsole + with TestOutput + ](ExecutionEventSink.live, TestLogger.fromConsole) + + +} From 1913a03c7b468dda6cdbd74eb60e9ae824e34abe Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Mon, 28 Mar 2022 14:55:41 -0600 Subject: [PATCH 02/22] Give more structure to Output tests --- .../test/scala/zio/test/TestOutputSpec.scala | 99 ++++++++++++++++--- 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index 9095ac3af0df..85bd2b5f4f5a 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -3,29 +3,106 @@ import zio.Scope import zio._ +case class TestEntity( + id: SuiteId, + ancestors: List[SuiteId], + ) + object TestOutputSpec extends ZIOSpecDefault { + private val parent = TestEntity( + id = SuiteId(1), + ancestors = List.empty, + ) + + private val child1 = + TestEntity( + SuiteId(2), + List(parent.id) + ) + + private val child2 = + TestEntity( + SuiteId(3), + List(parent.id) + ) + + override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( test("TestOutput.run") { for { testConsole <- ZIO.service[TestConsole] - testOutput <- ZIO.service[TestOutput] - _ <- testOutput.printOrQueue(SuiteId(2), List(SuiteId(1)), ExecutionEvent.Test( - labelsReversed = List("a", "b"), - test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), - annotations = TestAnnotationMap.empty, - ancestors = List.empty, - duration = 0L, - id = SuiteId(1) - )) + _ <- printOrQueue(child1, Success, List("success")) + _ <- printOrQueue(child1, Failure, List("failure")) + _ <- printOrQueue(child2, Failure, List("queuedMessage")) output <- testConsole.output _ <- ZIO.debug(output) - _ <- ZIO.succeed("blah") - } yield assertTrue(1 == 1) + } yield outputContainsAllOf(output, "success", "failure") && + outputContainsNoneOf(output, "queuedMessage") } ).provideSome[ TestConsole with TestOutput ](ExecutionEventSink.live, TestLogger.fromConsole) + def outputContainsAllOf(output: Seq[String], expected: String*) = { + expected.map( + expectedValue => + assertTrue(output.exists(_.contains(expectedValue))) + ).reduce(_ && _) + } + + def outputContainsNoneOf(output: Seq[String], expected: String*) = { + expected.map( + expectedValue => + assertTrue(!output.exists(_.contains(expectedValue))) + ).reduce(_ && _) + } + + + + + sealed trait TestStatus + case object Success extends TestStatus + case object Failure extends TestStatus + + def printOrQueue(testEntity: TestEntity, testStatus: TestStatus, labels: List[String]) = + + for { + testOutput <- ZIO.service[TestOutput] + _ <- testOutput.printOrQueue( + testEntity.id, + testEntity.ancestors, + testStatus match { + case Success => successfulTest(testEntity.id, "TestOutputSpec" :: labels) + case Failure => failedTest(testEntity.id, "TestOutputSpec" :: labels) + } + ) + } yield () + + private def successfulTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = + ExecutionEvent.Test( + labelsReversed = labels.reverse , + test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), + annotations = TestAnnotationMap.empty, + ancestors = ancestors, + duration = 0L, + id = suiteId + ) + + private def failedTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = + ExecutionEvent.Test( + labelsReversed = labels.reverse , + test = Left(arbitraryFailure), + annotations = TestAnnotationMap.empty, + ancestors, + duration = 0L, + id = suiteId + ) + private val arbitraryFailure = + TestFailure.Assertion(BoolAlgebra.failure[AssertionResult](AssertionResult.FailureDetailsResult( + FailureDetails( + ::(AssertionValue(Assertion.anything, (), Assertion.anything.run(())), Nil) + ) + ))) } From 0b4c326e445b790d9ae20eb34fc9539d574b2d97 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Mon, 28 Mar 2022 20:59:49 -0600 Subject: [PATCH 03/22] rm redundant fields from methods that were already taking an ExecutionEvent with that data --- .../test/ParallelSuitesInterleavedResultsSpec.scala | 12 ++++++------ .../src/test/scala/zio/test/TestOutputSpec.scala | 2 -- .../src/main/scala/zio/test/ExecutionEventSink.scala | 1 + 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index 2f0e9d3d9be7..6706ba5387bf 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -14,7 +14,7 @@ object AMinimalSpec extends ZIOSpecDefault { test("test after big delay") { Live.live(ZIO.sleep(5.second)).map(_ => assertTrue(true)) } - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } @@ -26,7 +26,7 @@ object BMinimalSpec extends ZIOSpecDefault { test("B 2") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object MultiCMinimalSpec extends ZIOSpecDefault { @@ -47,7 +47,7 @@ object MultiCMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object SmallMinimalSpec extends ZIOSpecDefault { @@ -62,7 +62,7 @@ object SmallMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } ) - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object SlowMinimalSpec extends ZIOSpecDefault { @@ -91,13 +91,13 @@ object SlowMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object SingleMinimalSpec extends ZIOSpecDefault { override def spec = test("Single spec not in a suite") { Live.live(ZIO.sleep(2.second)).map(_ => assertTrue(true)) - } @@ TestAspect.ignore + }// @@ TestAspect.ignore } diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index 85bd2b5f4f5a..2137a0a764ff 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -70,8 +70,6 @@ object TestOutputSpec extends ZIOSpecDefault { for { testOutput <- ZIO.service[TestOutput] _ <- testOutput.printOrQueue( - testEntity.id, - testEntity.ancestors, testStatus match { case Success => successfulTest(testEntity.id, "TestOutputSpec" :: labels) case Failure => failedTest(testEntity.id, "TestOutputSpec" :: labels) diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala index 2246dc3a40c8..3e1f406edab5 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala @@ -1,5 +1,6 @@ package zio.test +import zio.test.ExecutionEvent.RuntimeFailure import zio.{Ref, UIO, ZIO, ZLayer} trait ExecutionEventSink { From aad8fd06e0e3659a9bd82dbec62ff7b28c32a9e8 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Mon, 28 Mar 2022 21:27:43 -0600 Subject: [PATCH 04/22] More TestOutput API improvements. --- .../zio/test/sbt/ZTestFrameworkSpec.scala | 2 +- ...ParallelSuitesInterleavedResultsSpec.scala | 12 +- .../test/scala/zio/test/TestOutputSpec.scala | 104 +++++++++++------- .../scala/zio/test/ExecutionEventSink.scala | 1 - 4 files changed, 71 insertions(+), 48 deletions(-) diff --git a/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala b/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala index e988c3ce2c17..237ed1ec1830 100644 --- a/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala +++ b/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala @@ -26,7 +26,7 @@ object ZTestFrameworkSpec { // test("should correctly display colorized output for multi-line strings")(testColored()), // test("should test only selected test")(testTestSelection()), // test("should return summary when done")(testSummary()), - test("should use a shared layer without re-initializing it")(testSharedLayer()), + test("should use a shared layer without re-initializing it")(testSharedLayer()) // test("should warn when no tests are executed")(testNoTestsExecutedWarning()), // test("should ensure that output is not interleaved")(???) ) diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index 6706ba5387bf..ce6cc2c9a536 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -14,7 +14,7 @@ object AMinimalSpec extends ZIOSpecDefault { test("test after big delay") { Live.live(ZIO.sleep(5.second)).map(_ => assertTrue(true)) } - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } @@ -26,7 +26,7 @@ object BMinimalSpec extends ZIOSpecDefault { test("B 2") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object MultiCMinimalSpec extends ZIOSpecDefault { @@ -47,7 +47,7 @@ object MultiCMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object SmallMinimalSpec extends ZIOSpecDefault { @@ -62,7 +62,7 @@ object SmallMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } ) - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object SlowMinimalSpec extends ZIOSpecDefault { @@ -91,13 +91,13 @@ object SlowMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object SingleMinimalSpec extends ZIOSpecDefault { override def spec = test("Single spec not in a suite") { Live.live(ZIO.sleep(2.second)).map(_ => assertTrue(true)) - }// @@ TestAspect.ignore + } // @@ TestAspect.ignore } diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index 2137a0a764ff..1244ab24b684 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -4,14 +4,21 @@ import zio.Scope import zio._ case class TestEntity( - id: SuiteId, - ancestors: List[SuiteId], - ) + id: SuiteId, + ancestors: List[SuiteId] +) object TestOutputSpec extends ZIOSpecDefault { + /* + 1 -> 2 -> 4 + -> 5 + + -> 3 -> 6 + -> 7 + */ private val parent = TestEntity( id = SuiteId(1), - ancestors = List.empty, + ancestors = List.empty ) private val child1 = @@ -26,60 +33,73 @@ object TestOutputSpec extends ZIOSpecDefault { List(parent.id) ) + private val grandchild4 = + TestEntity( + SuiteId(4), + List(child1.id, parent.id) + ) + + private val grandchild5 = + TestEntity( + SuiteId(5), + List(child1.id, parent.id) + ) + + private val grandChild6 = + TestEntity( + SuiteId(6), + List(child2.id, parent.id) + ) + + private val grandChild7 = + TestEntity( + SuiteId(7), + List(child2.id, parent.id) + ) + + val allEntities = List(parent, child1, child2, grandchild4, grandchild5, grandChild6, grandChild7) override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( test("TestOutput.run") { for { + _ <- ZIO.debug("Family tree: " + allEntities) testConsole <- ZIO.service[TestConsole] - _ <- printOrQueue(child1, Success, List("success")) - _ <- printOrQueue(child1, Failure, List("failure")) - _ <- printOrQueue(child2, Failure, List("queuedMessage")) - output <- testConsole.output - _ <- ZIO.debug(output) + _ <- printOrQueue(child1, Success, List("success")) + _ <- printOrQueue(child1, Failure, List("failure")) + _ <- printOrQueue(child2, Failure, List("queuedMessage")) + output <- testConsole.output + _ <- ZIO.debug(output) } yield outputContainsAllOf(output, "success", "failure") && outputContainsNoneOf(output, "queuedMessage") } ).provideSome[ - TestConsole - with TestOutput + TestConsole with TestOutput ](ExecutionEventSink.live, TestLogger.fromConsole) - def outputContainsAllOf(output: Seq[String], expected: String*) = { - expected.map( - expectedValue => - assertTrue(output.exists(_.contains(expectedValue))) - ).reduce(_ && _) - } - - def outputContainsNoneOf(output: Seq[String], expected: String*) = { - expected.map( - expectedValue => - assertTrue(!output.exists(_.contains(expectedValue))) - ).reduce(_ && _) - } - - + def outputContainsAllOf(output: Seq[String], expected: String*) = + expected.map(expectedValue => assertTrue(output.exists(_.contains(expectedValue)))).reduce(_ && _) + def outputContainsNoneOf(output: Seq[String], expected: String*) = + expected.map(expectedValue => assertTrue(!output.exists(_.contains(expectedValue)))).reduce(_ && _) sealed trait TestStatus case object Success extends TestStatus case object Failure extends TestStatus def printOrQueue(testEntity: TestEntity, testStatus: TestStatus, labels: List[String]) = - for { - testOutput <- ZIO.service[TestOutput] - _ <- testOutput.printOrQueue( - testStatus match { - case Success => successfulTest(testEntity.id, "TestOutputSpec" :: labels) - case Failure => failedTest(testEntity.id, "TestOutputSpec" :: labels) - } - ) + testOutput <- ZIO.service[TestOutput] + _ <- testOutput.print( + testStatus match { + case Success => successfulTest(testEntity.id, "TestOutputSpec" :: labels) + case Failure => failedTest(testEntity.id, "TestOutputSpec" :: labels) + } + ) } yield () private def successfulTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = ExecutionEvent.Test( - labelsReversed = labels.reverse , + labelsReversed = labels.reverse, test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), annotations = TestAnnotationMap.empty, ancestors = ancestors, @@ -89,7 +109,7 @@ object TestOutputSpec extends ZIOSpecDefault { private def failedTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = ExecutionEvent.Test( - labelsReversed = labels.reverse , + labelsReversed = labels.reverse, test = Left(arbitraryFailure), annotations = TestAnnotationMap.empty, ancestors, @@ -98,9 +118,13 @@ object TestOutputSpec extends ZIOSpecDefault { ) private val arbitraryFailure = - TestFailure.Assertion(BoolAlgebra.failure[AssertionResult](AssertionResult.FailureDetailsResult( - FailureDetails( - ::(AssertionValue(Assertion.anything, (), Assertion.anything.run(())), Nil) + TestFailure.Assertion( + BoolAlgebra.failure[AssertionResult]( + AssertionResult.FailureDetailsResult( + FailureDetails( + ::(AssertionValue(Assertion.anything, (), Assertion.anything.run(())), Nil) + ) + ) ) - ))) + ) } diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala index 3e1f406edab5..2246dc3a40c8 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala @@ -1,6 +1,5 @@ package zio.test -import zio.test.ExecutionEvent.RuntimeFailure import zio.{Ref, UIO, ZIO, ZLayer} trait ExecutionEventSink { From db762a0432ea2414b65bba0433d525f3f833c197 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Mon, 28 Mar 2022 21:28:04 -0600 Subject: [PATCH 05/22] rm half-baked test for now --- .../test/scala/zio/test/TestOutputSpec.scala | 130 ------------------ 1 file changed, 130 deletions(-) delete mode 100644 test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala deleted file mode 100644 index 1244ab24b684..000000000000 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ /dev/null @@ -1,130 +0,0 @@ -package zio.test -import zio.Scope - -import zio._ - -case class TestEntity( - id: SuiteId, - ancestors: List[SuiteId] -) - -object TestOutputSpec extends ZIOSpecDefault { - /* - 1 -> 2 -> 4 - -> 5 - - -> 3 -> 6 - -> 7 - */ - private val parent = TestEntity( - id = SuiteId(1), - ancestors = List.empty - ) - - private val child1 = - TestEntity( - SuiteId(2), - List(parent.id) - ) - - private val child2 = - TestEntity( - SuiteId(3), - List(parent.id) - ) - - private val grandchild4 = - TestEntity( - SuiteId(4), - List(child1.id, parent.id) - ) - - private val grandchild5 = - TestEntity( - SuiteId(5), - List(child1.id, parent.id) - ) - - private val grandChild6 = - TestEntity( - SuiteId(6), - List(child2.id, parent.id) - ) - - private val grandChild7 = - TestEntity( - SuiteId(7), - List(child2.id, parent.id) - ) - - val allEntities = List(parent, child1, child2, grandchild4, grandchild5, grandChild6, grandChild7) - - override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( - test("TestOutput.run") { - for { - _ <- ZIO.debug("Family tree: " + allEntities) - testConsole <- ZIO.service[TestConsole] - _ <- printOrQueue(child1, Success, List("success")) - _ <- printOrQueue(child1, Failure, List("failure")) - _ <- printOrQueue(child2, Failure, List("queuedMessage")) - output <- testConsole.output - _ <- ZIO.debug(output) - } yield outputContainsAllOf(output, "success", "failure") && - outputContainsNoneOf(output, "queuedMessage") - } - ).provideSome[ - TestConsole with TestOutput - ](ExecutionEventSink.live, TestLogger.fromConsole) - - def outputContainsAllOf(output: Seq[String], expected: String*) = - expected.map(expectedValue => assertTrue(output.exists(_.contains(expectedValue)))).reduce(_ && _) - - def outputContainsNoneOf(output: Seq[String], expected: String*) = - expected.map(expectedValue => assertTrue(!output.exists(_.contains(expectedValue)))).reduce(_ && _) - - sealed trait TestStatus - case object Success extends TestStatus - case object Failure extends TestStatus - - def printOrQueue(testEntity: TestEntity, testStatus: TestStatus, labels: List[String]) = - for { - testOutput <- ZIO.service[TestOutput] - _ <- testOutput.print( - testStatus match { - case Success => successfulTest(testEntity.id, "TestOutputSpec" :: labels) - case Failure => failedTest(testEntity.id, "TestOutputSpec" :: labels) - } - ) - } yield () - - private def successfulTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = - ExecutionEvent.Test( - labelsReversed = labels.reverse, - test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), - annotations = TestAnnotationMap.empty, - ancestors = ancestors, - duration = 0L, - id = suiteId - ) - - private def failedTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = - ExecutionEvent.Test( - labelsReversed = labels.reverse, - test = Left(arbitraryFailure), - annotations = TestAnnotationMap.empty, - ancestors, - duration = 0L, - id = suiteId - ) - - private val arbitraryFailure = - TestFailure.Assertion( - BoolAlgebra.failure[AssertionResult]( - AssertionResult.FailureDetailsResult( - FailureDetails( - ::(AssertionValue(Assertion.anything, (), Assertion.anything.run(())), Nil) - ) - ) - ) - ) -} From 5e0c87977e04ee98a74ffedc1081b9e9131f2390 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Tue, 29 Mar 2022 07:33:31 -0600 Subject: [PATCH 06/22] Cleanup incidental file changes --- .../test/scala/zio/test/sbt/ZTestFrameworkSpec.scala | 3 +-- .../test/ParallelSuitesInterleavedResultsSpec.scala | 12 ++++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala b/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala index 237ed1ec1830..bd5d3fad5d27 100644 --- a/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala +++ b/test-sbt/jvm/src/test/scala/zio/test/sbt/ZTestFrameworkSpec.scala @@ -27,8 +27,7 @@ object ZTestFrameworkSpec { // test("should test only selected test")(testTestSelection()), // test("should return summary when done")(testSummary()), test("should use a shared layer without re-initializing it")(testSharedLayer()) -// test("should warn when no tests are executed")(testNoTestsExecutedWarning()), -// test("should ensure that output is not interleaved")(???) +// test("should warn when no tests are executed")(testNoTestsExecutedWarning()) ) def testFingerprints(): Unit = { diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index ce6cc2c9a536..2f0e9d3d9be7 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -14,7 +14,7 @@ object AMinimalSpec extends ZIOSpecDefault { test("test after big delay") { Live.live(ZIO.sleep(5.second)).map(_ => assertTrue(true)) } - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } @@ -26,7 +26,7 @@ object BMinimalSpec extends ZIOSpecDefault { test("B 2") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } object MultiCMinimalSpec extends ZIOSpecDefault { @@ -47,7 +47,7 @@ object MultiCMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } object SmallMinimalSpec extends ZIOSpecDefault { @@ -62,7 +62,7 @@ object SmallMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } ) - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } object SlowMinimalSpec extends ZIOSpecDefault { @@ -91,13 +91,13 @@ object SlowMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } object SingleMinimalSpec extends ZIOSpecDefault { override def spec = test("Single spec not in a suite") { Live.live(ZIO.sleep(2.second)).map(_ => assertTrue(true)) - } // @@ TestAspect.ignore + } @@ TestAspect.ignore } From f1fe615800e41516e2758fadafa8915a0dde4ec9 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Tue, 29 Mar 2022 11:09:58 -0600 Subject: [PATCH 07/22] Revert "rm half-baked test for now" This reverts commit 2b5b27a6feedcbb759f78866dec18530bbf814d1. --- .../test/scala/zio/test/TestOutputSpec.scala | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala new file mode 100644 index 000000000000..1244ab24b684 --- /dev/null +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -0,0 +1,130 @@ +package zio.test +import zio.Scope + +import zio._ + +case class TestEntity( + id: SuiteId, + ancestors: List[SuiteId] +) + +object TestOutputSpec extends ZIOSpecDefault { + /* + 1 -> 2 -> 4 + -> 5 + + -> 3 -> 6 + -> 7 + */ + private val parent = TestEntity( + id = SuiteId(1), + ancestors = List.empty + ) + + private val child1 = + TestEntity( + SuiteId(2), + List(parent.id) + ) + + private val child2 = + TestEntity( + SuiteId(3), + List(parent.id) + ) + + private val grandchild4 = + TestEntity( + SuiteId(4), + List(child1.id, parent.id) + ) + + private val grandchild5 = + TestEntity( + SuiteId(5), + List(child1.id, parent.id) + ) + + private val grandChild6 = + TestEntity( + SuiteId(6), + List(child2.id, parent.id) + ) + + private val grandChild7 = + TestEntity( + SuiteId(7), + List(child2.id, parent.id) + ) + + val allEntities = List(parent, child1, child2, grandchild4, grandchild5, grandChild6, grandChild7) + + override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( + test("TestOutput.run") { + for { + _ <- ZIO.debug("Family tree: " + allEntities) + testConsole <- ZIO.service[TestConsole] + _ <- printOrQueue(child1, Success, List("success")) + _ <- printOrQueue(child1, Failure, List("failure")) + _ <- printOrQueue(child2, Failure, List("queuedMessage")) + output <- testConsole.output + _ <- ZIO.debug(output) + } yield outputContainsAllOf(output, "success", "failure") && + outputContainsNoneOf(output, "queuedMessage") + } + ).provideSome[ + TestConsole with TestOutput + ](ExecutionEventSink.live, TestLogger.fromConsole) + + def outputContainsAllOf(output: Seq[String], expected: String*) = + expected.map(expectedValue => assertTrue(output.exists(_.contains(expectedValue)))).reduce(_ && _) + + def outputContainsNoneOf(output: Seq[String], expected: String*) = + expected.map(expectedValue => assertTrue(!output.exists(_.contains(expectedValue)))).reduce(_ && _) + + sealed trait TestStatus + case object Success extends TestStatus + case object Failure extends TestStatus + + def printOrQueue(testEntity: TestEntity, testStatus: TestStatus, labels: List[String]) = + for { + testOutput <- ZIO.service[TestOutput] + _ <- testOutput.print( + testStatus match { + case Success => successfulTest(testEntity.id, "TestOutputSpec" :: labels) + case Failure => failedTest(testEntity.id, "TestOutputSpec" :: labels) + } + ) + } yield () + + private def successfulTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = + ExecutionEvent.Test( + labelsReversed = labels.reverse, + test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), + annotations = TestAnnotationMap.empty, + ancestors = ancestors, + duration = 0L, + id = suiteId + ) + + private def failedTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = + ExecutionEvent.Test( + labelsReversed = labels.reverse, + test = Left(arbitraryFailure), + annotations = TestAnnotationMap.empty, + ancestors, + duration = 0L, + id = suiteId + ) + + private val arbitraryFailure = + TestFailure.Assertion( + BoolAlgebra.failure[AssertionResult]( + AssertionResult.FailureDetailsResult( + FailureDetails( + ::(AssertionValue(Assertion.anything, (), Assertion.anything.run(())), Nil) + ) + ) + ) + ) +} From b03ca83ae2851073e30b91e77884ffecda108606 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Tue, 29 Mar 2022 12:46:11 -0600 Subject: [PATCH 08/22] Work on more meaningful tests for TestOutput --- .../test/scala/zio/test/TestOutputSpec.scala | 55 +++++++++++++++---- .../src/main/scala/zio/test/TestOutput.scala | 1 + .../main/scala/zio/test/TestReporters.scala | 2 + 3 files changed, 46 insertions(+), 12 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index 1244ab24b684..c553e261aaf7 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -1,7 +1,7 @@ package zio.test import zio.Scope - import zio._ +import zio.test.ExecutionEvent.{SectionEnd, SectionStart} case class TestEntity( id: SuiteId, @@ -60,13 +60,30 @@ object TestOutputSpec extends ZIOSpecDefault { val allEntities = List(parent, child1, child2, grandchild4, grandchild5, grandChild6, grandChild7) override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( - test("TestOutput.run") { +// test("TestOutput.run") { +// for { +// _ <- ZIO.debug("Family tree: " + allEntities) +// testConsole <- ZIO.service[TestConsole] +// _ <- printOrQueue(child1, Success, List("success")) +// _ <- printOrQueue(child1, Failure, List("failure")) +// _ <- printOrQueue(child2, Failure, List("queuedMessage")) +// output <- testConsole.output +// _ <- ZIO.debug(output) +// } yield outputContainsAllOf(output, "success", "failure") && +// outputContainsNoneOf(output, "queuedMessage") +// }, + + test("nested events") { for { - _ <- ZIO.debug("Family tree: " + allEntities) testConsole <- ZIO.service[TestConsole] - _ <- printOrQueue(child1, Success, List("success")) - _ <- printOrQueue(child1, Failure, List("failure")) - _ <- printOrQueue(child2, Failure, List("queuedMessage")) + testOutput <- ZIO.service[TestOutput] + _ <- sectionStart(parent) + _ <- sectionStart(child1) + _ <- testOutput.print(successfulTest(child1.id, List("success"))) + _ <- testOutput.print(failedTest(child1.id, List("failure"))) + _ <- testOutput.print(failedTest(child2.id, List("queuedMessage"))) + _ <- sectionEnd(child1, 0) + _ <- sectionEnd(parent, 0) output <- testConsole.output _ <- ZIO.debug(output) } yield outputContainsAllOf(output, "success", "failure") && @@ -86,17 +103,31 @@ object TestOutputSpec extends ZIOSpecDefault { case object Success extends TestStatus case object Failure extends TestStatus - def printOrQueue(testEntity: TestEntity, testStatus: TestStatus, labels: List[String]) = + private def sectionStart(testEntity: TestEntity) = for { testOutput <- ZIO.service[TestOutput] _ <- testOutput.print( - testStatus match { - case Success => successfulTest(testEntity.id, "TestOutputSpec" :: labels) - case Failure => failedTest(testEntity.id, "TestOutputSpec" :: labels) - } - ) + SectionStart( + List("section: " + testEntity.id.id), + testEntity.id, + testEntity.ancestors, + ) + ) } yield () + private def sectionEnd(testEntity: TestEntity, depth: Int) = + for { + testOutput <- ZIO.service[TestOutput] + _ <- testOutput.print( + SectionEnd( + List("section: " + testEntity.id.id), + testEntity.id, + testEntity.ancestors, + ) + ) + } yield () + + private def successfulTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = ExecutionEvent.Test( labelsReversed = labels.reverse, diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index d5246841e672..be46571f61bf 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -69,6 +69,7 @@ object TestOutput { reporterEvent: ExecutionEvent ): ZIO[ExecutionEventSink with TestLogger, Nothing, Unit] = for { + _ <- ZIO.debug("printOrQueue.reporterEvent: " + reporterEvent) _ <- appendToSectionContents(reporterEvent.id, Chunk(reporterEvent)) suiteIsPrinting <- reporters.attemptToGetPrintingControl(reporterEvent.id, reporterEvent.ancestors) _ <- ZIO.when(suiteIsPrinting)( diff --git a/test/shared/src/main/scala/zio/test/TestReporters.scala b/test/shared/src/main/scala/zio/test/TestReporters.scala index 7e8b4b733334..80b032e187c9 100644 --- a/test/shared/src/main/scala/zio/test/TestReporters.scala +++ b/test/shared/src/main/scala/zio/test/TestReporters.scala @@ -12,9 +12,11 @@ case class TestReporters(reportersStack: Ref[List[SuiteId]]) { def attemptToGetPrintingControl(id: SuiteId, ancestors: List[SuiteId]): ZIO[Any, Nothing, Boolean] = reportersStack.updateSomeAndGet { case Nil => + println("No reporters yet. Selecting: " + id) List(id) case reporters if ancestors.nonEmpty && reporters.head == ancestors.head => + println("Getting control from parent. Child id: " + id) id :: reporters }.map(_.head == id) From 74b58a0f19a67530b0b5367d5182aeb1c9dc2a42 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Tue, 29 Mar 2022 12:50:22 -0600 Subject: [PATCH 09/22] Confirm some unwanted Console output w/ test --- .../shared/src/test/scala/zio/test/TestOutputSpec.scala | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index c553e261aaf7..d42a5880fc6f 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -79,11 +79,13 @@ object TestOutputSpec extends ZIOSpecDefault { testOutput <- ZIO.service[TestOutput] _ <- sectionStart(parent) _ <- sectionStart(child1) + _ <- sectionStart(child2) _ <- testOutput.print(successfulTest(child1.id, List("success"))) _ <- testOutput.print(failedTest(child1.id, List("failure"))) _ <- testOutput.print(failedTest(child2.id, List("queuedMessage"))) - _ <- sectionEnd(child1, 0) - _ <- sectionEnd(parent, 0) + _ <- sectionEnd(child2) + _ <- sectionEnd(child1) +// _ <- sectionEnd(parent) output <- testConsole.output _ <- ZIO.debug(output) } yield outputContainsAllOf(output, "success", "failure") && @@ -115,7 +117,7 @@ object TestOutputSpec extends ZIOSpecDefault { ) } yield () - private def sectionEnd(testEntity: TestEntity, depth: Int) = + private def sectionEnd(testEntity: TestEntity) = for { testOutput <- ZIO.service[TestOutput] _ <- testOutput.print( From b27db9f6a1e94dee00b213b428ab9e4152b13d82 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Tue, 29 Mar 2022 17:18:58 -0600 Subject: [PATCH 10/22] Separate TestOutput and Rendering. Much more meaningful TestOutput tests. Might want to rename it TestOutputSorter or something. --- .../scala/zio/test/sbt/BaseTestTask.scala | 2 +- .../zio/test/ExecutionEventSinkSpec.scala | 3 +- .../scala/zio/test/IntellijRendererSpec.scala | 2 +- ...ParallelSuitesInterleavedResultsSpec.scala | 5 +- .../scala/zio/test/ReportingTestUtils.scala | 4 +- .../test/scala/zio/test/TestOutputSpec.scala | 164 ++++++++++-------- .../scala/zio/test/AbstractRunnableSpec.scala | 2 +- .../scala/zio/test/DefaultRunnableSpec.scala | 2 +- .../scala/zio/test/DefaultTestReporter.scala | 3 +- .../main/scala/zio/test/ExecutionEvent.scala | 9 +- .../scala/zio/test/ExecutionEventSink.scala | 6 +- .../scala/zio/test/MutableRunnableSpec.scala | 2 +- .../main/scala/zio/test/RunnableSpec.scala | 2 +- .../main/scala/zio/test/TestExecutor.scala | 6 +- .../src/main/scala/zio/test/TestOutput.scala | 47 ++++- .../main/scala/zio/test/TestReporters.scala | 2 - .../src/main/scala/zio/test/TestRunner.scala | 6 +- .../main/scala/zio/test/ZIOSpecAbstract.scala | 12 +- .../src/main/scala/zio/test/package.scala | 4 +- 19 files changed, 165 insertions(+), 118 deletions(-) diff --git a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala index 663fad45cad5..d3bd25af24a9 100644 --- a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala +++ b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala @@ -25,7 +25,7 @@ abstract class BaseTestTask( protected def run( eventHandler: EventHandler, spec: AbstractRunnableSpec - ): ZIO[TestLogger with Clock with TestOutput with ExecutionEventSink with Random, Throwable, Unit] = { + ): ZIO[TestLogger with Clock with TestOutput with ExecutionEventSink with ExecutionEventPrinter with Random, Throwable, Unit] = { assert(eventHandler != null) for { summary <- spec.runSpec(FilteredSpec(spec.spec, args)) diff --git a/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala b/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala index dca22fa2baa6..e16ed8c8e5a5 100644 --- a/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala @@ -52,7 +52,8 @@ object ExecutionEventSinkSpec extends ZIOSpecDefault { Console.live, TestLogger.fromConsole, ExecutionEventSink.live, - TestOutput.live + TestOutput.live, + ExecutionEventPrinter.live // TODO test version that just accumulates ) } diff --git a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala index 4f16a6e6ea5e..6b9f49a9dd87 100644 --- a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala @@ -194,7 +194,7 @@ object IntelliJRenderUtils { IntelliJTestRunner(testEnvironment) .run(spec) .provideLayer[Nothing, TestEnvironment with Scope]( - TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live + TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ++ ExecutionEventPrinter.live ) output <- TestConsole.output } yield output.mkString diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index 2f0e9d3d9be7..b5323e5efb34 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -46,7 +46,10 @@ object MultiCMinimalSpec extends ZIOSpecDefault { test("slow 2") { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } - ) + ), + test("standalone 1") { + Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) + }, ) @@ TestAspect.ignore } diff --git a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala index cc1a33319729..b04fb019e8e9 100644 --- a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala +++ b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala @@ -50,7 +50,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live + TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ++ ExecutionEventPrinter.live ) output <- TestConsole.output } yield output.mkString @@ -61,7 +61,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - Scope.default >>> (TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live) + Scope.default >>> (TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live) ++ ExecutionEventPrinter.live ) } yield summary.summary diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index d42a5880fc6f..ac3b88eca32c 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -59,105 +59,117 @@ object TestOutputSpec extends ZIOSpecDefault { val allEntities = List(parent, child1, child2, grandchild4, grandchild5, grandChild6, grandChild7) + class ExecutionEventHolder(events: Ref[List[ExecutionEvent]]) extends ExecutionEventPrinter { + override def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] = + events.update(_ :+ event) + + def getEvents: ZIO[TestLogger, Nothing, List[ExecutionEvent]] = + events.get + } + + val makeFakePrinter: ZIO[Any, Nothing, ExecutionEventHolder] = + for { + output <- Ref.make[List[ExecutionEvent]](List.empty) + } yield + new ExecutionEventHolder(output) + + val fakePrinterLayer = ZLayer.fromZIO(makeFakePrinter) + override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( -// test("TestOutput.run") { -// for { -// _ <- ZIO.debug("Family tree: " + allEntities) -// testConsole <- ZIO.service[TestConsole] -// _ <- printOrQueue(child1, Success, List("success")) -// _ <- printOrQueue(child1, Failure, List("failure")) -// _ <- printOrQueue(child2, Failure, List("queuedMessage")) -// output <- testConsole.output -// _ <- ZIO.debug(output) -// } yield outputContainsAllOf(output, "success", "failure") && -// outputContainsNoneOf(output, "queuedMessage") -// }, - - test("nested events") { + test("nested events without flushing") { for { - testConsole <- ZIO.service[TestConsole] - testOutput <- ZIO.service[TestOutput] - _ <- sectionStart(parent) - _ <- sectionStart(child1) + parentSectionStart <- sectionStart(parent) + child1SectionStart <- sectionStart(child1) _ <- sectionStart(child2) - _ <- testOutput.print(successfulTest(child1.id, List("success"))) - _ <- testOutput.print(failedTest(child1.id, List("failure"))) - _ <- testOutput.print(failedTest(child2.id, List("queuedMessage"))) - _ <- sectionEnd(child2) - _ <- sectionEnd(child1) -// _ <- sectionEnd(parent) - output <- testConsole.output - _ <- ZIO.debug(output) - } yield outputContainsAllOf(output, "success", "failure") && - outputContainsNoneOf(output, "queuedMessage") + child1test1 <- submitSuccessfulTest(child1) + child1test2 <- submitSuccessfulTest(child1) + _ <- submitSuccessfulTest(child2) + _ <- sectionEnd(child2) // This output is sent to the parent, so if we don't close the parent section, we will not see this event + child1end <- sectionEnd(child1) + outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) + } yield assertTrue(outputEvents == + List( + parentSectionStart, + child1SectionStart, + child1test1, + child1test2, + child1end + ) + ) + }, + test("nested events with flushing") { + for { + parentSectionStart <- sectionStart(parent) + child1SectionStart <- sectionStart(child1) + child2SectionStart <- sectionStart(child2) + child1test1 <- submitSuccessfulTest(child1) + child1test2 <- submitSuccessfulTest(child1) + child2test1 <- submitSuccessfulTest(child2) + child2end <- sectionEnd(child2) // This output is sent to the parent, so if we don't close the parent section, we will not see this event + child1end <- sectionEnd(child1) + parentEnd <- sectionEnd(parent) + outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) + } yield assertTrue(outputEvents == + List( + parentSectionStart, + child1SectionStart, + child1test1, + child1test2, + child1end, + child2SectionStart, + child2test1, + child2end, + parentEnd + ) + ) } ).provideSome[ TestConsole with TestOutput - ](ExecutionEventSink.live, TestLogger.fromConsole) - - def outputContainsAllOf(output: Seq[String], expected: String*) = - expected.map(expectedValue => assertTrue(output.exists(_.contains(expectedValue)))).reduce(_ && _) - - def outputContainsNoneOf(output: Seq[String], expected: String*) = - expected.map(expectedValue => assertTrue(!output.exists(_.contains(expectedValue)))).reduce(_ && _) - - sealed trait TestStatus - case object Success extends TestStatus - case object Failure extends TestStatus + ](ExecutionEventSink.live, TestLogger.fromConsole, fakePrinterLayer) private def sectionStart(testEntity: TestEntity) = for { testOutput <- ZIO.service[TestOutput] - _ <- testOutput.print( + sectionStart = SectionStart( - List("section: " + testEntity.id.id), + List("label"), testEntity.id, testEntity.ancestors, ) + _ <- testOutput.print( + sectionStart ) - } yield () + } yield sectionStart private def sectionEnd(testEntity: TestEntity) = for { testOutput <- ZIO.service[TestOutput] + end = SectionEnd( + List("section: " + testEntity.id.id), + testEntity.id, + testEntity.ancestors, + ) _ <- testOutput.print( - SectionEnd( - List("section: " + testEntity.id.id), - testEntity.id, - testEntity.ancestors, - ) + end ) - } yield () + } yield end + private def submitSuccessfulTest(testEntity: TestEntity) = { + for { + testOutput <- ZIO.service[TestOutput] + test = + ExecutionEvent.Test( + labelsReversed = List("label"), + test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), + annotations = TestAnnotationMap.empty, + ancestors = testEntity.ancestors, + duration = 0L, + id = testEntity.id + ) + _ <- testOutput.print(test) - private def successfulTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = - ExecutionEvent.Test( - labelsReversed = labels.reverse, - test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), - annotations = TestAnnotationMap.empty, - ancestors = ancestors, - duration = 0L, - id = suiteId - ) + } yield test + } - private def failedTest(suiteId: SuiteId, labels: List[String], ancestors: List[SuiteId] = List.empty) = - ExecutionEvent.Test( - labelsReversed = labels.reverse, - test = Left(arbitraryFailure), - annotations = TestAnnotationMap.empty, - ancestors, - duration = 0L, - id = suiteId - ) - private val arbitraryFailure = - TestFailure.Assertion( - BoolAlgebra.failure[AssertionResult]( - AssertionResult.FailureDetailsResult( - FailureDetails( - ::(AssertionValue(Assertion.anything, (), Assertion.anything.run(())), Nil) - ) - ) - ) - ) } diff --git a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala index 7e0c7414f57f..309e297f7ab9 100644 --- a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala @@ -52,7 +52,7 @@ abstract class AbstractRunnableSpec { spec: ZSpec[Environment, Failure] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary] = + ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = runner.run(aspects.foldLeft(spec)(_ @@ _)) /** diff --git a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala index 0d7e53b09f76..3978dbbd63a9 100644 --- a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala @@ -40,7 +40,7 @@ abstract class DefaultRunnableSpec extends RunnableSpec[TestEnvironment, Any] { spec: ZSpec[Environment, Failure] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary] = + ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) /** diff --git a/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala b/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala index 5249e5e60457..231ed0aebe14 100644 --- a/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala +++ b/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala @@ -46,7 +46,8 @@ object DefaultTestReporter { )(implicit trace: ZTraceElement): Seq[ExecutionResult] = { reporterEvent match { case SectionStart(labelsReversed, _, ancestors) => - val depth = labelsReversed.length - 1 + val depth = ancestors.length + println("Ancestors of start event: " + ancestors.mkString("-")) labelsReversed.reverse match { case Nil => Seq.empty case nonEmptyList => diff --git a/test/shared/src/main/scala/zio/test/ExecutionEvent.scala b/test/shared/src/main/scala/zio/test/ExecutionEvent.scala index cc15a3aaeb7d..0529b37834f6 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEvent.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEvent.scala @@ -10,7 +10,7 @@ object ExecutionEvent { duration: Long = 0L, id: SuiteId ) extends ExecutionEvent { - def labels: List[String] = labelsReversed.reverse + val labels: List[String] = labelsReversed.reverse } final case class SectionStart( @@ -18,7 +18,7 @@ object ExecutionEvent { id: SuiteId, ancestors: List[SuiteId] ) extends ExecutionEvent { - def labels: List[String] = labelsReversed.reverse + val labels: List[String] = labelsReversed.reverse } final case class SectionEnd( @@ -26,7 +26,7 @@ object ExecutionEvent { id: SuiteId, ancestors: List[SuiteId] ) extends ExecutionEvent { - def labels: List[String] = labelsReversed.reverse + val labels: List[String] = labelsReversed.reverse } final case class RuntimeFailure[+E]( @@ -35,7 +35,7 @@ object ExecutionEvent { failure: TestFailure[E], ancestors: List[SuiteId] ) extends ExecutionEvent { - def labels: List[String] = labelsReversed.reverse + val labels: List[String] = labelsReversed.reverse } } @@ -43,4 +43,5 @@ object ExecutionEvent { sealed trait ExecutionEvent { val id: SuiteId val ancestors: List[SuiteId] + val labels: List[String] } diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala index 2246dc3a40c8..d83e22a5e21f 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala @@ -5,14 +5,14 @@ import zio.{Ref, UIO, ZIO, ZLayer} trait ExecutionEventSink { def getSummary: UIO[Summary] - def process(event: ExecutionEvent): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] + def process(event: ExecutionEvent): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] } object ExecutionEventSink { def getSummary: ZIO[ExecutionEventSink, Nothing, Summary] = ZIO.serviceWithZIO[ExecutionEventSink](_.getSummary) - def process(event: ExecutionEvent): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] = + def process(event: ExecutionEvent): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = ZIO.serviceWithZIO[ExecutionEventSink](_.process(event)) val ExecutionEventSinkLive: ZIO[Any, Nothing, ExecutionEventSink] = @@ -22,7 +22,7 @@ object ExecutionEventSink { override def process( event: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] = + ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = event match { case testEvent: ExecutionEvent.Test[_] => summary.update( diff --git a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala index abe9657248bc..460ac02b918c 100644 --- a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala @@ -167,6 +167,6 @@ class MutableRunnableSpec[R: Tag]( spec: ZSpec[Environment, Failure] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary] = + ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) } diff --git a/test/shared/src/main/scala/zio/test/RunnableSpec.scala b/test/shared/src/main/scala/zio/test/RunnableSpec.scala index c0a272c82936..86c22abd47b7 100644 --- a/test/shared/src/main/scala/zio/test/RunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/RunnableSpec.scala @@ -28,7 +28,7 @@ abstract class RunnableSpec[R, E] extends AbstractRunnableSpec { private def run(spec: ZSpec[Environment, Failure], testArgs: TestArgs)(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Int] = { + ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Int] = { val filteredSpec = FilteredSpec(spec, testArgs) val testReporter = testArgs.testRenderer.fold(runner.reporter)(createTestReporter) for { diff --git a/test/shared/src/main/scala/zio/test/TestExecutor.scala b/test/shared/src/main/scala/zio/test/TestExecutor.scala index f06fae2a2057..d73df6bfa383 100644 --- a/test/shared/src/main/scala/zio/test/TestExecutor.scala +++ b/test/shared/src/main/scala/zio/test/TestExecutor.scala @@ -27,7 +27,7 @@ import zio.{ExecutionStrategy, Random, ZIO, ZTraceElement} abstract class TestExecutor[+R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement - ): ZIO[TestOutput with TestLogger with ExecutionEventSink with Random, Nothing, Summary] + ): ZIO[TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random, Nothing, Summary] def environment: ZLayer[Scope, Nothing, R] } @@ -39,7 +39,7 @@ object TestExecutor { ): TestExecutor[R, E] = new TestExecutor[R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement - ): ZIO[TestOutput with TestLogger with ExecutionEventSink with Random, Nothing, Summary] = + ): ZIO[TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random, Nothing, Summary] = for { sink <- ZIO.service[ExecutionEventSink] topParent <- SuiteId.newRandom @@ -51,7 +51,7 @@ object TestExecutor { exec: ExecutionStrategy, ancestors: List[SuiteId], sectionId: SuiteId - ): ZIO[TestOutput with TestLogger with ExecutionEventSink with Random with Scope, Nothing, Unit] = + ): ZIO[TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random with Scope, Nothing, Unit] = (spec.caseValue match { case Spec.ExecCase(exec, spec) => loop(labels, spec, exec, ancestors, sectionId) diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index be46571f61bf..aa04331ec838 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -2,6 +2,27 @@ package zio.test import zio.{Chunk, Ref, ZIO, ZLayer} +trait ExecutionEventPrinter { + def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] +} + +object ExecutionEventPrinter { + def live: ZLayer[Any, Nothing, ExecutionEventPrinter] = + ZLayer.succeed(Live) + + def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter with TestLogger, Nothing, Unit] = { + ZIO.serviceWithZIO[ExecutionEventPrinter](_.print(event)) + } + + object Live extends ExecutionEventPrinter { + override def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] = { + TestLogger.logLine( + ReporterEventRenderer.render(event).mkString("\n") + ) + } + } +} + trait TestOutput { /** @@ -10,7 +31,7 @@ trait TestOutput { */ def print( executionEvent: ExecutionEvent - ): ZIO[ExecutionEventSink with TestLogger, Nothing, Unit] + ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] } object TestOutput { @@ -19,9 +40,18 @@ object TestOutput { TestOutputLive.make ) + /** + * Guarantees: + * - Everything at or below a specific suite level will be printed contiguously + * - Everything will be printed, as long as required SectionEnd events have been passed in + * + * Not guaranteed: + * - Ordering within a suite + * + */ def print( executionEvent: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] = + ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = ZIO.serviceWithZIO[TestOutput](_.print(executionEvent)) case class TestOutputLive( @@ -36,7 +66,7 @@ object TestOutput { def print( executionEvent: ExecutionEvent - ): ZIO[ExecutionEventSink with TestLogger, Nothing, Unit] = + ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = executionEvent match { case end: ExecutionEvent.SectionEnd => printOrFlush(end) @@ -46,9 +76,10 @@ object TestOutput { private def printOrFlush( end: ExecutionEvent.SectionEnd - ): ZIO[ExecutionEventSink with TestLogger, Nothing, Unit] = + ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = for { suiteIsPrinting <- reporters.attemptToGetPrintingControl(end.id, end.ancestors) + _ <- appendToSectionContents(end.id, Chunk(end)) sectionOutput <- getAndRemoveSectionOutput(end.id) _ <- if (suiteIsPrinting) @@ -67,9 +98,9 @@ object TestOutput { private def printOrQueue( reporterEvent: ExecutionEvent - ): ZIO[ExecutionEventSink with TestLogger, Nothing, Unit] = + ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = for { - _ <- ZIO.debug("printOrQueue.reporterEvent: " + reporterEvent) +// _ <- ZIO.debug("printOrQueue.reporterEvent: " + reporterEvent) _ <- appendToSectionContents(reporterEvent.id, Chunk(reporterEvent)) suiteIsPrinting <- reporters.attemptToGetPrintingControl(reporterEvent.id, reporterEvent.ancestors) _ <- ZIO.when(suiteIsPrinting)( @@ -82,9 +113,7 @@ object TestOutput { private def printToConsole(events: Chunk[ExecutionEvent]) = ZIO.foreachDiscard(events) { event => - TestLogger.logLine( - ReporterEventRenderer.render(event).mkString("\n") - ) + ExecutionEventPrinter.print(event) } private def appendToSectionContents(id: SuiteId, content: Chunk[ExecutionEvent]) = diff --git a/test/shared/src/main/scala/zio/test/TestReporters.scala b/test/shared/src/main/scala/zio/test/TestReporters.scala index 80b032e187c9..7e8b4b733334 100644 --- a/test/shared/src/main/scala/zio/test/TestReporters.scala +++ b/test/shared/src/main/scala/zio/test/TestReporters.scala @@ -12,11 +12,9 @@ case class TestReporters(reportersStack: Ref[List[SuiteId]]) { def attemptToGetPrintingControl(id: SuiteId, ancestors: List[SuiteId]): ZIO[Any, Nothing, Boolean] = reportersStack.updateSomeAndGet { case Nil => - println("No reporters yet. Selecting: " + id) List(id) case reporters if ancestors.nonEmpty && reporters.head == ancestors.head => - println("Getting control from parent. Child id: " + id) id :: reporters }.map(_.head == id) diff --git a/test/shared/src/main/scala/zio/test/TestRunner.scala b/test/shared/src/main/scala/zio/test/TestRunner.scala index 6b85e38ce66f..e24bf041cf23 100644 --- a/test/shared/src/main/scala/zio/test/TestRunner.scala +++ b/test/shared/src/main/scala/zio/test/TestRunner.scala @@ -32,13 +32,13 @@ final case class TestRunner[R, E]( runtimeConfig: RuntimeConfig = RuntimeConfig.makeDefault(), reporter: TestReporter[E] = DefaultTestReporter(TestRenderer.default, TestAnnotationRenderer.default)(ZTraceElement.empty), - bootstrap: Layer[Nothing, TestOutput with TestLogger with Clock with ExecutionEventSink with Random] = { + bootstrap: Layer[Nothing, TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random] = { (Console.live.to(TestLogger.fromConsole(ZTraceElement.empty))( ZTraceElement.empty )) ++ Clock.live ++ TestOutput.live ++ (TestOutput.live >>> ExecutionEventSink.live)( ZTraceElement.empty - ) ++ Random.live + ) ++ Random.live ++ ExecutionEventPrinter.live } ) { self => @@ -51,7 +51,7 @@ final case class TestRunner[R, E]( spec: ZSpec[R, E] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary] = + ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = executor.run(spec, ExecutionStrategy.ParallelN(4)).timed.flatMap { case (duration, summary) => // TODO Why is duration 0 here? Resolve for #6482 ZIO.succeed(summary) diff --git a/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala b/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala index d28de28d7361..1ab986623b7b 100644 --- a/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala +++ b/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala @@ -55,7 +55,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { self.layer +!+ that.layer override def runSpec: ZIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventPrinter, Any, Any ] = @@ -73,7 +73,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { } protected def runSpec: ZIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with Scope, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Scope, Any, Any ] = { @@ -112,14 +112,14 @@ abstract class ZIOSpecAbstract extends ZIOApp { private[zio] def runSpec( spec: ZSpec[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Clock with Scope with ExecutionEventSink, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Clock with Scope with ExecutionEventSink with ExecutionEventPrinter, Any ], testArgs: TestArgs )(implicit trace: ZTraceElement ): URIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink with ExecutionEventPrinter, Summary ] = { val filteredSpec = FilteredSpec(spec, testArgs) @@ -127,7 +127,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { for { runtime <- ZIO.runtime[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with Scope + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Scope ] environment = runtime.environment runtimeConfig = hook(runtime.runtimeConfig) @@ -135,7 +135,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { TestRunner( TestExecutor .default[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink with ExecutionEventPrinter, Any ]( ZLayer.succeedEnvironment(environment) +!+ (Scope.default >>> testEnvironment) diff --git a/test/shared/src/main/scala/zio/test/package.scala b/test/shared/src/main/scala/zio/test/package.scala index dd9c4b22db6e..7ed8c37dbbe8 100644 --- a/test/shared/src/main/scala/zio/test/package.scala +++ b/test/shared/src/main/scala/zio/test/package.scala @@ -66,6 +66,7 @@ package object test extends CompileVariants { with TestRandom with TestSystem with ExecutionEventSink + with ExecutionEventPrinter with TestOutput object TestEnvironment { @@ -82,7 +83,8 @@ package object test extends CompileVariants { TestRandom.deterministic ++ TestSystem.default ++ (TestOutput.live >>> ExecutionEventSink.live) ++ - TestOutput.live + TestOutput.live ++ + ExecutionEventPrinter.live } } From dc3f166608394ff680ab9f938d8eaf135dd859ef Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Tue, 29 Mar 2022 19:45:56 -0600 Subject: [PATCH 11/22] Additional test scenarios --- .../scala/zio/test/sbt/BaseTestTask.scala | 6 +- ...ParallelSuitesInterleavedResultsSpec.scala | 2 +- .../test/scala/zio/test/TestOutputSpec.scala | 235 +++++++++++------- .../scala/zio/test/AbstractRunnableSpec.scala | 5 +- .../scala/zio/test/DefaultRunnableSpec.scala | 5 +- .../scala/zio/test/ExecutionEventSink.scala | 8 +- .../scala/zio/test/MutableRunnableSpec.scala | 5 +- .../main/scala/zio/test/TestExecutor.scala | 12 +- .../src/main/scala/zio/test/TestOutput.scala | 15 +- .../src/main/scala/zio/test/TestRunner.scala | 10 +- 10 files changed, 201 insertions(+), 102 deletions(-) diff --git a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala index d3bd25af24a9..e651f463b890 100644 --- a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala +++ b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala @@ -25,7 +25,11 @@ abstract class BaseTestTask( protected def run( eventHandler: EventHandler, spec: AbstractRunnableSpec - ): ZIO[TestLogger with Clock with TestOutput with ExecutionEventSink with ExecutionEventPrinter with Random, Throwable, Unit] = { + ): ZIO[ + TestLogger with Clock with TestOutput with ExecutionEventSink with ExecutionEventPrinter with Random, + Throwable, + Unit + ] = { assert(eventHandler != null) for { summary <- spec.runSpec(FilteredSpec(spec.spec, args)) diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index b5323e5efb34..704216ab1bb1 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -49,7 +49,7 @@ object MultiCMinimalSpec extends ZIOSpecDefault { ), test("standalone 1") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) - }, + } ) @@ TestAspect.ignore } diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index ac3b88eca32c..a8e1c0438cc2 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -33,31 +33,31 @@ object TestOutputSpec extends ZIOSpecDefault { List(parent.id) ) - private val grandchild4 = + private val child1child1 = TestEntity( SuiteId(4), List(child1.id, parent.id) ) - private val grandchild5 = + private val child1child2 = TestEntity( SuiteId(5), List(child1.id, parent.id) ) - private val grandChild6 = + private val child2child1 = TestEntity( SuiteId(6), List(child2.id, parent.id) ) - private val grandChild7 = + private val child2child2 = TestEntity( SuiteId(7), List(child2.id, parent.id) ) - val allEntities = List(parent, child1, child2, grandchild4, grandchild5, grandChild6, grandChild7) + val allEntities = List(parent, child1, child2, child1child1, child1child2, child2child1, child2child2) class ExecutionEventHolder(events: Ref[List[ExecutionEvent]]) extends ExecutionEventPrinter { override def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] = @@ -70,106 +70,175 @@ object TestOutputSpec extends ZIOSpecDefault { val makeFakePrinter: ZIO[Any, Nothing, ExecutionEventHolder] = for { output <- Ref.make[List[ExecutionEvent]](List.empty) - } yield - new ExecutionEventHolder(output) + } yield new ExecutionEventHolder(output) val fakePrinterLayer = ZLayer.fromZIO(makeFakePrinter) override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( test("nested events without flushing") { + val events = + List( + Start(parent), + Start(child1), + Start(child2), + Test(child1), + Test(child2), + Test(child1), + End(child2), + End(child1) + ) for { - parentSectionStart <- sectionStart(parent) - child1SectionStart <- sectionStart(child1) - _ <- sectionStart(child2) - child1test1 <- submitSuccessfulTest(child1) - child1test2 <- submitSuccessfulTest(child1) - _ <- submitSuccessfulTest(child2) - _ <- sectionEnd(child2) // This output is sent to the parent, so if we don't close the parent section, we will not see this event - child1end <- sectionEnd(child1) + _ <- ZIO.foreach(events)(event => TestOutput.print(event)) outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) - } yield assertTrue(outputEvents == + } yield assertTrue( + outputEvents == List( - parentSectionStart, - child1SectionStart, - child1test1, - child1test2, - child1end + Start(parent), + Start(child1), + Test(child1), + Test(child1), + End(child1) ) - ) + ) }, test("nested events with flushing") { + val events = + List( + Start(parent), + Start(child1), + Start(child2), + Test(child1), + Test(child2), + Test(child1), + End(child2), + End(child1), + End(parent) + ) for { - parentSectionStart <- sectionStart(parent) - child1SectionStart <- sectionStart(child1) - child2SectionStart <- sectionStart(child2) - child1test1 <- submitSuccessfulTest(child1) - child1test2 <- submitSuccessfulTest(child1) - child2test1 <- submitSuccessfulTest(child2) - child2end <- sectionEnd(child2) // This output is sent to the parent, so if we don't close the parent section, we will not see this event - child1end <- sectionEnd(child1) - parentEnd <- sectionEnd(parent) + _ <- ZIO.foreach(events)(event => TestOutput.print(event)) outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) - } yield assertTrue(outputEvents == + } yield assertTrue( + outputEvents == + List( + Start(parent), + Start(child1), + Test(child1), + Test(child1), + End(child1), + Start(child2), + Test(child2), + End(child2), + End(parent) + ) + ) + }, + test("mix of suites and individual tests") { + val events = List( - parentSectionStart, - child1SectionStart, - child1test1, - child1test2, - child1end, - child2SectionStart, - child2test1, - child2end, - parentEnd + Start(parent), + Start(child1), + Test(child1, "a"), + Start(child1child1), + Test(child1, "b"), + Test(child1child1), + Test(child1, "c"), + End(child1child1), + End(child1), + End(parent) ) + for { + _ <- ZIO.foreach(events)(event => TestOutput.print(event)) + outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) + } yield assertTrue( + outputEvents == + List( + Start(parent), + Start(child1), + Test(child1, "a"), // This child1 test came before the child1child1 section, so it was printed immediately + Start(child1child1), + Test(child1child1), + End(child1child1), + Test(child1, "b"), // These other child1 tests were queued until child1child1 ended + Test(child1, "c"), + End(child1), + End(parent) + ) + ) + }, + + test("more complex mix of suites and individual tests") { + val events = + List( + Start(parent), + Start(child1), + Start(child2), + Test(child1, "a"), + Start(child2child1), + Test(child2, "j"), + Test(child2child1, "k"), + End(child2child1), + Start(child1child1), + Test(child1, "b"), + Test(child2, "m"), + Test(child1child1), + End(child2), + Test(child1, "c"), + End(child1child1), + End(child1), + End(parent) + ) + for { + _ <- ZIO.foreach(events)(event => TestOutput.print(event)) + outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) + } yield assertTrue( + outputEvents == + List( + Start(parent), + Start(child1), + Test(child1, "a"), // This child1 test came before the child1child1 section, so it was printed immediately + Start(child1child1), + Test(child1child1), + End(child1child1), + Test(child1, "b"), // These other child1 tests were queued until child1child1 ended + Test(child1, "c"), + End(child1), + Start(child2), + Test(child2, "j"), + Start(child2child1), + Test(child2child1, "k"), + End(child2child1), + Test(child2, "m"), + End(child2), + End(parent) + ) ) } ).provideSome[ TestConsole with TestOutput ](ExecutionEventSink.live, TestLogger.fromConsole, fakePrinterLayer) - private def sectionStart(testEntity: TestEntity) = - for { - testOutput <- ZIO.service[TestOutput] - sectionStart = - SectionStart( - List("label"), - testEntity.id, - testEntity.ancestors, - ) - _ <- testOutput.print( - sectionStart - ) - } yield sectionStart - - private def sectionEnd(testEntity: TestEntity) = - for { - testOutput <- ZIO.service[TestOutput] - end = SectionEnd( - List("section: " + testEntity.id.id), - testEntity.id, - testEntity.ancestors, - ) - _ <- testOutput.print( - end - ) - } yield end - - private def submitSuccessfulTest(testEntity: TestEntity) = { - for { - testOutput <- ZIO.service[TestOutput] - test = - ExecutionEvent.Test( - labelsReversed = List("label"), - test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), - annotations = TestAnnotationMap.empty, - ancestors = testEntity.ancestors, - duration = 0L, - id = testEntity.id - ) - _ <- testOutput.print(test) + private def Start(testEntity: TestEntity) = + SectionStart( + List("label"), + testEntity.id, + testEntity.ancestors + ) - } yield test - } + private def End(testEntity: TestEntity) = + SectionEnd( + List("section: " + testEntity.id.id), + testEntity.id, + testEntity.ancestors + ) + private def Test(testEntity: TestEntity, label: String = "label") = + ExecutionEvent.Test( + labelsReversed = List(label), + test = Right(TestSuccess.Succeeded(BoolAlgebra.unit)), + annotations = TestAnnotationMap.empty, + ancestors = testEntity.ancestors, + duration = 0L, + id = testEntity.id + ) } diff --git a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala index 309e297f7ab9..5c45d80a4d5e 100644 --- a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala @@ -52,7 +52,10 @@ abstract class AbstractRunnableSpec { spec: ZSpec[Environment, Failure] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = + ): URIO[ + TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + Summary + ] = runner.run(aspects.foldLeft(spec)(_ @@ _)) /** diff --git a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala index 3978dbbd63a9..bce37ba3327e 100644 --- a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala @@ -40,7 +40,10 @@ abstract class DefaultRunnableSpec extends RunnableSpec[TestEnvironment, Any] { spec: ZSpec[Environment, Failure] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = + ): URIO[ + TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + Summary + ] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) /** diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala index d83e22a5e21f..12cec89e636a 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala @@ -5,14 +5,18 @@ import zio.{Ref, UIO, ZIO, ZLayer} trait ExecutionEventSink { def getSummary: UIO[Summary] - def process(event: ExecutionEvent): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] + def process( + event: ExecutionEvent + ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] } object ExecutionEventSink { def getSummary: ZIO[ExecutionEventSink, Nothing, Summary] = ZIO.serviceWithZIO[ExecutionEventSink](_.getSummary) - def process(event: ExecutionEvent): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = + def process( + event: ExecutionEvent + ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = ZIO.serviceWithZIO[ExecutionEventSink](_.process(event)) val ExecutionEventSinkLive: ZIO[Any, Nothing, ExecutionEventSink] = diff --git a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala index 460ac02b918c..4dd43f4c9a4d 100644 --- a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala @@ -167,6 +167,9 @@ class MutableRunnableSpec[R: Tag]( spec: ZSpec[Environment, Failure] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = + ): URIO[ + TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + Summary + ] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) } diff --git a/test/shared/src/main/scala/zio/test/TestExecutor.scala b/test/shared/src/main/scala/zio/test/TestExecutor.scala index d73df6bfa383..89c234d0e991 100644 --- a/test/shared/src/main/scala/zio/test/TestExecutor.scala +++ b/test/shared/src/main/scala/zio/test/TestExecutor.scala @@ -39,7 +39,11 @@ object TestExecutor { ): TestExecutor[R, E] = new TestExecutor[R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement - ): ZIO[TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random, Nothing, Summary] = + ): ZIO[ + TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random, + Nothing, + Summary + ] = for { sink <- ZIO.service[ExecutionEventSink] topParent <- SuiteId.newRandom @@ -51,7 +55,11 @@ object TestExecutor { exec: ExecutionStrategy, ancestors: List[SuiteId], sectionId: SuiteId - ): ZIO[TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random with Scope, Nothing, Unit] = + ): ZIO[ + TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random with Scope, + Nothing, + Unit + ] = (spec.caseValue match { case Spec.ExecCase(exec, spec) => loop(labels, spec, exec, ancestors, sectionId) diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index aa04331ec838..33db36e2a88e 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -10,16 +10,14 @@ object ExecutionEventPrinter { def live: ZLayer[Any, Nothing, ExecutionEventPrinter] = ZLayer.succeed(Live) - def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter with TestLogger, Nothing, Unit] = { + def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter with TestLogger, Nothing, Unit] = ZIO.serviceWithZIO[ExecutionEventPrinter](_.print(event)) - } object Live extends ExecutionEventPrinter { - override def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] = { + override def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] = TestLogger.logLine( ReporterEventRenderer.render(event).mkString("\n") ) - } } } @@ -42,12 +40,13 @@ object TestOutput { /** * Guarantees: - * - Everything at or below a specific suite level will be printed contiguously - * - Everything will be printed, as long as required SectionEnd events have been passed in + * - Everything at or below a specific suite level will be printed + * contiguously + * - Everything will be printed, as long as required SectionEnd events have + * been passed in * * Not guaranteed: - * - Ordering within a suite - * + * - Ordering within a suite */ def print( executionEvent: ExecutionEvent diff --git a/test/shared/src/main/scala/zio/test/TestRunner.scala b/test/shared/src/main/scala/zio/test/TestRunner.scala index e24bf041cf23..cb0cab45f18e 100644 --- a/test/shared/src/main/scala/zio/test/TestRunner.scala +++ b/test/shared/src/main/scala/zio/test/TestRunner.scala @@ -32,7 +32,10 @@ final case class TestRunner[R, E]( runtimeConfig: RuntimeConfig = RuntimeConfig.makeDefault(), reporter: TestReporter[E] = DefaultTestReporter(TestRenderer.default, TestAnnotationRenderer.default)(ZTraceElement.empty), - bootstrap: Layer[Nothing, TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random] = { + bootstrap: Layer[ + Nothing, + TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random + ] = { (Console.live.to(TestLogger.fromConsole(ZTraceElement.empty))( ZTraceElement.empty @@ -51,7 +54,10 @@ final case class TestRunner[R, E]( spec: ZSpec[R, E] )(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Summary] = + ): URIO[ + TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + Summary + ] = executor.run(spec, ExecutionStrategy.ParallelN(4)).timed.flatMap { case (duration, summary) => // TODO Why is duration 0 here? Resolve for #6482 ZIO.succeed(summary) From 181cb31e1da4b6bbcbb138206cafe83d3d9d68ea Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 10:03:42 -0600 Subject: [PATCH 12/22] Simplify child creation --- .../test/scala/zio/test/TestOutputSpec.scala | 36 ++++++------------- 1 file changed, 10 insertions(+), 26 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index a8e1c0438cc2..3430f71d061e 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -6,7 +6,10 @@ import zio.test.ExecutionEvent.{SectionEnd, SectionStart} case class TestEntity( id: SuiteId, ancestors: List[SuiteId] -) +) { + def child(newId: Int): TestEntity = + TestEntity(SuiteId(newId), id :: ancestors) +} object TestOutputSpec extends ZIOSpecDefault { /* @@ -22,40 +25,22 @@ object TestOutputSpec extends ZIOSpecDefault { ) private val child1 = - TestEntity( - SuiteId(2), - List(parent.id) - ) + parent.child(2) private val child2 = - TestEntity( - SuiteId(3), - List(parent.id) - ) + parent.child(3) private val child1child1 = - TestEntity( - SuiteId(4), - List(child1.id, parent.id) - ) + child1.child(4) private val child1child2 = - TestEntity( - SuiteId(5), - List(child1.id, parent.id) - ) + child1.child(5) private val child2child1 = - TestEntity( - SuiteId(6), - List(child2.id, parent.id) - ) + child2.child(6) private val child2child2 = - TestEntity( - SuiteId(7), - List(child2.id, parent.id) - ) + child2.child(7) val allEntities = List(parent, child1, child2, child1child1, child1child2, child2child1, child2child2) @@ -165,7 +150,6 @@ object TestOutputSpec extends ZIOSpecDefault { ) ) }, - test("more complex mix of suites and individual tests") { val events = List( From 5215599aa0e07dd80dfc1483df1490806910a584 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 10:13:01 -0600 Subject: [PATCH 13/22] Fix import after rebase --- .../src/main/scala/zio/test/sbt/BaseTestTask.scala | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala index e651f463b890..eec04b11298c 100644 --- a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala +++ b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala @@ -2,16 +2,7 @@ package zio.test.sbt import sbt.testing.{EventHandler, Logger, Task, TaskDef} import zio.test.render.ConsoleRenderer -import zio.test.{ - AbstractRunnableSpec, - ExecutionEventSink, - FilteredSpec, - TestOutput, - TestArgs, - TestEnvironment, - TestLogger, - ZIOSpecAbstract -} +import zio.test.{AbstractRunnableSpec, ExecutionEventPrinter, ExecutionEventSink, FilteredSpec, TestArgs, TestEnvironment, TestLogger, TestOutput, ZIOSpecAbstract} import zio.{Clock, Random, Runtime, Scope, ZEnvironment, ZIO, ZIOAppArgs, ZLayer, ZTraceElement} abstract class BaseTestTask( From 5e8b23626c05badb4e728699f045298dd9b5cd49 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 12:52:06 -0600 Subject: [PATCH 14/22] Refactoring TestOutput --- ...ParallelSuitesInterleavedResultsSpec.scala | 12 ++-- .../scala/zio/test/DefaultTestReporter.scala | 3 +- .../src/main/scala/zio/test/TestOutput.scala | 66 ++++++++----------- 3 files changed, 33 insertions(+), 48 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index 704216ab1bb1..afc1a96650d8 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -14,7 +14,7 @@ object AMinimalSpec extends ZIOSpecDefault { test("test after big delay") { Live.live(ZIO.sleep(5.second)).map(_ => assertTrue(true)) } - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } @@ -26,7 +26,7 @@ object BMinimalSpec extends ZIOSpecDefault { test("B 2") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object MultiCMinimalSpec extends ZIOSpecDefault { @@ -50,7 +50,7 @@ object MultiCMinimalSpec extends ZIOSpecDefault { test("standalone 1") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object SmallMinimalSpec extends ZIOSpecDefault { @@ -65,7 +65,7 @@ object SmallMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } ) - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object SlowMinimalSpec extends ZIOSpecDefault { @@ -94,13 +94,13 @@ object SlowMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - ) @@ TestAspect.ignore + )// @@ TestAspect.ignore } object SingleMinimalSpec extends ZIOSpecDefault { override def spec = test("Single spec not in a suite") { Live.live(ZIO.sleep(2.second)).map(_ => assertTrue(true)) - } @@ TestAspect.ignore + }// @@ TestAspect.ignore } diff --git a/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala b/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala index 231ed0aebe14..5249e5e60457 100644 --- a/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala +++ b/test/shared/src/main/scala/zio/test/DefaultTestReporter.scala @@ -46,8 +46,7 @@ object DefaultTestReporter { )(implicit trace: ZTraceElement): Seq[ExecutionResult] = { reporterEvent match { case SectionStart(labelsReversed, _, ancestors) => - val depth = ancestors.length - println("Ancestors of start event: " + ancestors.mkString("-")) + val depth = labelsReversed.length - 1 labelsReversed.reverse match { case Nil => Seq.empty case nonEmptyList => diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index 33db36e2a88e..9b181e599326 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -65,50 +65,36 @@ object TestOutput { def print( executionEvent: ExecutionEvent - ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = - executionEvent match { - case end: ExecutionEvent.SectionEnd => - printOrFlush(end) - case other => - printOrQueue(other) - } - - private def printOrFlush( - end: ExecutionEvent.SectionEnd - ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = + ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = { for { - suiteIsPrinting <- reporters.attemptToGetPrintingControl(end.id, end.ancestors) - _ <- appendToSectionContents(end.id, Chunk(end)) - sectionOutput <- getAndRemoveSectionOutput(end.id) + suiteIsPrinting <- reporters.attemptToGetPrintingControl(executionEvent.id, executionEvent.ancestors) + _ <- appendToSectionContents(executionEvent.id, Chunk(executionEvent)) + sectionOutput <- getAndRemoveSectionOutput(executionEvent.id) _ <- - if (suiteIsPrinting) - printToConsole(sectionOutput) - else { - end.ancestors.headOption match { - case Some(parentId) => - appendToSectionContents(parentId, sectionOutput) - case None => - ZIO.dieMessage("Suite tried to send its output to a nonexistent parent") - } + executionEvent match { + case _: ExecutionEvent.SectionEnd => + for { + _ <- + if (suiteIsPrinting) + printToConsole(sectionOutput) + else { + executionEvent.ancestors.headOption match { + case Some(parentId) => + appendToSectionContents(parentId, sectionOutput) + case None => + ZIO.dieMessage("Suite tried to send its output to a nonexistent parent") + } + } + _ <- reporters.relinquishPrintingControl(executionEvent.id) + } yield () + case _ => + if(suiteIsPrinting) + printToConsole(sectionOutput) + else + appendToSectionContents(executionEvent.id, sectionOutput) // TODO More } - - _ <- reporters.relinquishPrintingControl(end.id) - } yield () - - private def printOrQueue( - reporterEvent: ExecutionEvent - ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = - for { -// _ <- ZIO.debug("printOrQueue.reporterEvent: " + reporterEvent) - _ <- appendToSectionContents(reporterEvent.id, Chunk(reporterEvent)) - suiteIsPrinting <- reporters.attemptToGetPrintingControl(reporterEvent.id, reporterEvent.ancestors) - _ <- ZIO.when(suiteIsPrinting)( - for { - currentOutput <- getAndRemoveSectionOutput(reporterEvent.id) - _ <- printToConsole(currentOutput) - } yield () - ) } yield () + } private def printToConsole(events: Chunk[ExecutionEvent]) = ZIO.foreachDiscard(events) { event => From 0bc79d81454f1e30c12a1e83b33c8208ce3f6677 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 13:58:13 -0600 Subject: [PATCH 15/22] Start shuffling construction of layers --- .../scala/zio/test/IntellijRendererSpec.scala | 2 +- .../scala/zio/test/ReportingTestUtils.scala | 4 ++-- .../test/scala/zio/test/TestOutputSpec.scala | 3 ++- .../src/main/scala/zio/test/TestOutput.scala | 17 ++++++++++------- .../src/main/scala/zio/test/TestRunner.scala | 4 +++- .../src/main/scala/zio/test/package.scala | 2 +- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala index 6b9f49a9dd87..6d8fe532c6b3 100644 --- a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala @@ -194,7 +194,7 @@ object IntelliJRenderUtils { IntelliJTestRunner(testEnvironment) .run(spec) .provideLayer[Nothing, TestEnvironment with Scope]( - TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ++ ExecutionEventPrinter.live + TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ++ (TestLogger.fromConsole >>> ExecutionEventPrinter.live) ) output <- TestConsole.output } yield output.mkString diff --git a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala index b04fb019e8e9..eff0191f78e6 100644 --- a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala +++ b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala @@ -50,7 +50,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ++ ExecutionEventPrinter.live + (TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ) output <- TestConsole.output } yield output.mkString @@ -61,7 +61,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - Scope.default >>> (TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live) ++ ExecutionEventPrinter.live + Scope.default >>> ((TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live) ) } yield summary.summary diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index 3430f71d061e..71457823e42e 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -45,7 +45,7 @@ object TestOutputSpec extends ZIOSpecDefault { val allEntities = List(parent, child1, child2, child1child1, child1child2, child2child1, child2child2) class ExecutionEventHolder(events: Ref[List[ExecutionEvent]]) extends ExecutionEventPrinter { - override def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] = + override def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] = events.update(_ :+ event) def getEvents: ZIO[TestLogger, Nothing, List[ExecutionEvent]] = @@ -102,6 +102,7 @@ object TestOutputSpec extends ZIOSpecDefault { for { _ <- ZIO.foreach(events)(event => TestOutput.print(event)) outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) + _ <- ZIO.debug(outputEvents.mkString("\n")) } yield assertTrue( outputEvents == List( diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index 9b181e599326..870d809537ec 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -3,19 +3,22 @@ package zio.test import zio.{Chunk, Ref, ZIO, ZLayer} trait ExecutionEventPrinter { - def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] + def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] } object ExecutionEventPrinter { - def live: ZLayer[Any, Nothing, ExecutionEventPrinter] = - ZLayer.succeed(Live) + val live: ZLayer[TestLogger, Nothing, ExecutionEventPrinter] = { + ZLayer.fromZIO(for { + testLogger <- ZIO.service[TestLogger] + } yield new Live(testLogger)) + } - def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter with TestLogger, Nothing, Unit] = + def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter, Nothing, Unit] = ZIO.serviceWithZIO[ExecutionEventPrinter](_.print(event)) - object Live extends ExecutionEventPrinter { - override def print(event: ExecutionEvent): ZIO[TestLogger, Nothing, Unit] = - TestLogger.logLine( + class Live(logger: TestLogger) extends ExecutionEventPrinter { + override def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] = + logger.logLine( ReporterEventRenderer.render(event).mkString("\n") ) } diff --git a/test/shared/src/main/scala/zio/test/TestRunner.scala b/test/shared/src/main/scala/zio/test/TestRunner.scala index cb0cab45f18e..46c411d53d2d 100644 --- a/test/shared/src/main/scala/zio/test/TestRunner.scala +++ b/test/shared/src/main/scala/zio/test/TestRunner.scala @@ -41,7 +41,9 @@ final case class TestRunner[R, E]( ZTraceElement.empty )) ++ Clock.live ++ TestOutput.live ++ (TestOutput.live >>> ExecutionEventSink.live)( ZTraceElement.empty - ) ++ Random.live ++ ExecutionEventPrinter.live + ) ++ Random.live ++ (((Console.live.to(TestLogger.fromConsole(ZTraceElement.empty))( + ZTraceElement.empty + )) >>> ExecutionEventPrinter.live)(ZTraceElement.empty) ) } ) { self => diff --git a/test/shared/src/main/scala/zio/test/package.scala b/test/shared/src/main/scala/zio/test/package.scala index 7ed8c37dbbe8..be331e0b5055 100644 --- a/test/shared/src/main/scala/zio/test/package.scala +++ b/test/shared/src/main/scala/zio/test/package.scala @@ -84,7 +84,7 @@ package object test extends CompileVariants { TestSystem.default ++ (TestOutput.live >>> ExecutionEventSink.live) ++ TestOutput.live ++ - ExecutionEventPrinter.live + TestLogger.fromConsole.map(testLogger => ZEnvironment(new ExecutionEventPrinter.Live(testLogger.get))) } } From 40b08690c06712d766e7b88c3d1574d42beb37d8 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 14:25:54 -0600 Subject: [PATCH 16/22] Significantly reduce Environment types --- .../scala/zio/test/sbt/BaseTestTask.scala | 13 ++++++-- .../test/scala/zio/test/TestOutputSpec.scala | 12 +++---- .../scala/zio/test/AbstractRunnableSpec.scala | 2 +- .../scala/zio/test/DefaultRunnableSpec.scala | 2 +- .../scala/zio/test/ExecutionEventSink.scala | 6 ++-- .../scala/zio/test/MutableRunnableSpec.scala | 2 +- .../main/scala/zio/test/RunnableSpec.scala | 2 +- .../main/scala/zio/test/TestExecutor.scala | 6 ++-- .../src/main/scala/zio/test/TestOutput.scala | 31 ++++++++++--------- .../src/main/scala/zio/test/TestRunner.scala | 15 ++++----- .../main/scala/zio/test/ZIOSpecAbstract.scala | 12 +++---- .../src/main/scala/zio/test/package.scala | 6 ++-- 12 files changed, 61 insertions(+), 48 deletions(-) diff --git a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala index eec04b11298c..344d1bb73ebd 100644 --- a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala +++ b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala @@ -2,7 +2,16 @@ package zio.test.sbt import sbt.testing.{EventHandler, Logger, Task, TaskDef} import zio.test.render.ConsoleRenderer -import zio.test.{AbstractRunnableSpec, ExecutionEventPrinter, ExecutionEventSink, FilteredSpec, TestArgs, TestEnvironment, TestLogger, TestOutput, ZIOSpecAbstract} +import zio.test.{ + AbstractRunnableSpec, + ExecutionEventSink, + FilteredSpec, + TestArgs, + TestEnvironment, + TestLogger, + TestOutput, + ZIOSpecAbstract +} import zio.{Clock, Random, Runtime, Scope, ZEnvironment, ZIO, ZIOAppArgs, ZLayer, ZTraceElement} abstract class BaseTestTask( @@ -17,7 +26,7 @@ abstract class BaseTestTask( eventHandler: EventHandler, spec: AbstractRunnableSpec ): ZIO[ - TestLogger with Clock with TestOutput with ExecutionEventSink with ExecutionEventPrinter with Random, + TestLogger with Clock with TestOutput with ExecutionEventSink with Random, Throwable, Unit ] = { diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index 71457823e42e..cdbd90e8fd86 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -48,7 +48,7 @@ object TestOutputSpec extends ZIOSpecDefault { override def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] = events.update(_ :+ event) - def getEvents: ZIO[TestLogger, Nothing, List[ExecutionEvent]] = + def getEvents: ZIO[Any, Nothing, List[ExecutionEvent]] = events.get } @@ -57,7 +57,7 @@ object TestOutputSpec extends ZIOSpecDefault { output <- Ref.make[List[ExecutionEvent]](List.empty) } yield new ExecutionEventHolder(output) - val fakePrinterLayer = ZLayer.fromZIO(makeFakePrinter) + val fakePrinterLayer: ZLayer[Any, Nothing, ExecutionEventHolder] = ZLayer.fromZIO(makeFakePrinter) override def spec: ZSpec[TestEnvironment with Scope, Any] = suite("TestOutputSpec")( test("nested events without flushing") { @@ -73,6 +73,8 @@ object TestOutputSpec extends ZIOSpecDefault { End(child1) ) for { + fakePrinter <- ZIO.service[ExecutionEventPrinter] + _ <- ZIO.debug("Printer: " + fakePrinter) _ <- ZIO.foreach(events)(event => TestOutput.print(event)) outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) } yield assertTrue( @@ -102,7 +104,7 @@ object TestOutputSpec extends ZIOSpecDefault { for { _ <- ZIO.foreach(events)(event => TestOutput.print(event)) outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) - _ <- ZIO.debug(outputEvents.mkString("\n")) + _ <- ZIO.debug(outputEvents.mkString("\n")) } yield assertTrue( outputEvents == List( @@ -198,9 +200,7 @@ object TestOutputSpec extends ZIOSpecDefault { ) ) } - ).provideSome[ - TestConsole with TestOutput - ](ExecutionEventSink.live, TestLogger.fromConsole, fakePrinterLayer) + ).provide(fakePrinterLayer >+> TestOutput.live) private def Start(testEntity: TestEntity) = SectionStart( diff --git a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala index 5c45d80a4d5e..0e109919bfc7 100644 --- a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala @@ -53,7 +53,7 @@ abstract class AbstractRunnableSpec { )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary ] = runner.run(aspects.foldLeft(spec)(_ @@ _)) diff --git a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala index bce37ba3327e..cda2aeac3163 100644 --- a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala @@ -41,7 +41,7 @@ abstract class DefaultRunnableSpec extends RunnableSpec[TestEnvironment, Any] { )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary ] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala index 12cec89e636a..795467bf79d3 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala @@ -7,7 +7,7 @@ trait ExecutionEventSink { def process( event: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] + ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] } object ExecutionEventSink { @@ -16,7 +16,7 @@ object ExecutionEventSink { def process( event: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = + ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] = ZIO.serviceWithZIO[ExecutionEventSink](_.process(event)) val ExecutionEventSinkLive: ZIO[Any, Nothing, ExecutionEventSink] = @@ -26,7 +26,7 @@ object ExecutionEventSink { override def process( event: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = + ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] = event match { case testEvent: ExecutionEvent.Test[_] => summary.update( diff --git a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala index 4dd43f4c9a4d..c2d7f7c5b6ec 100644 --- a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala @@ -168,7 +168,7 @@ class MutableRunnableSpec[R: Tag]( )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary ] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) diff --git a/test/shared/src/main/scala/zio/test/RunnableSpec.scala b/test/shared/src/main/scala/zio/test/RunnableSpec.scala index 86c22abd47b7..c0a272c82936 100644 --- a/test/shared/src/main/scala/zio/test/RunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/RunnableSpec.scala @@ -28,7 +28,7 @@ abstract class RunnableSpec[R, E] extends AbstractRunnableSpec { private def run(spec: ZSpec[Environment, Failure], testArgs: TestArgs)(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, Int] = { + ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Int] = { val filteredSpec = FilteredSpec(spec, testArgs) val testReporter = testArgs.testRenderer.fold(runner.reporter)(createTestReporter) for { diff --git a/test/shared/src/main/scala/zio/test/TestExecutor.scala b/test/shared/src/main/scala/zio/test/TestExecutor.scala index 89c234d0e991..67c5be6c895d 100644 --- a/test/shared/src/main/scala/zio/test/TestExecutor.scala +++ b/test/shared/src/main/scala/zio/test/TestExecutor.scala @@ -27,7 +27,7 @@ import zio.{ExecutionStrategy, Random, ZIO, ZTraceElement} abstract class TestExecutor[+R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement - ): ZIO[TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random, Nothing, Summary] + ): ZIO[TestOutput with TestLogger with ExecutionEventSink with Random, Nothing, Summary] def environment: ZLayer[Scope, Nothing, R] } @@ -40,7 +40,7 @@ object TestExecutor { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement ): ZIO[ - TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random, + TestOutput with TestLogger with ExecutionEventSink with Random, Nothing, Summary ] = @@ -56,7 +56,7 @@ object TestExecutor { ancestors: List[SuiteId], sectionId: SuiteId ): ZIO[ - TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Random with Scope, + TestOutput with TestLogger with ExecutionEventSink with Random with Scope, Nothing, Unit ] = diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index 870d809537ec..b4c98d3f62c7 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -32,13 +32,16 @@ trait TestOutput { */ def print( executionEvent: ExecutionEvent - ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] + ): ZIO[Any, Nothing, Unit] } object TestOutput { - val live: ZLayer[Any, Nothing, TestOutput] = + val live: ZLayer[ExecutionEventPrinter, Nothing, TestOutput] = ZLayer.fromZIO( - TestOutputLive.make + for { + executionEventPrinter <- ZIO.service[ExecutionEventPrinter] + outputLive <- TestOutputLive.make(executionEventPrinter) + } yield outputLive ) /** @@ -53,12 +56,13 @@ object TestOutput { */ def print( executionEvent: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = + ): ZIO[TestOutput, Nothing, Unit] = ZIO.serviceWithZIO[TestOutput](_.print(executionEvent)) case class TestOutputLive( output: Ref[Map[SuiteId, Chunk[ExecutionEvent]]], - reporters: TestReporters + reporters: TestReporters, + executionEventPrinter: ExecutionEventPrinter ) extends TestOutput { private def getAndRemoveSectionOutput(id: SuiteId) = @@ -68,7 +72,7 @@ object TestOutput { def print( executionEvent: ExecutionEvent - ): ZIO[ExecutionEventSink with ExecutionEventPrinter with TestLogger, Nothing, Unit] = { + ): ZIO[Any, Nothing, Unit] = for { suiteIsPrinting <- reporters.attemptToGetPrintingControl(executionEvent.id, executionEvent.ancestors) _ <- appendToSectionContents(executionEvent.id, Chunk(executionEvent)) @@ -91,17 +95,16 @@ object TestOutput { _ <- reporters.relinquishPrintingControl(executionEvent.id) } yield () case _ => - if(suiteIsPrinting) - printToConsole(sectionOutput) - else - appendToSectionContents(executionEvent.id, sectionOutput) // TODO More + if (suiteIsPrinting) + printToConsole(sectionOutput) + else + appendToSectionContents(executionEvent.id, sectionOutput) // TODO More } } yield () - } private def printToConsole(events: Chunk[ExecutionEvent]) = ZIO.foreachDiscard(events) { event => - ExecutionEventPrinter.print(event) + executionEventPrinter.print(event) } private def appendToSectionContents(id: SuiteId, content: Chunk[ExecutionEvent]) = @@ -128,10 +131,10 @@ object TestOutput { object TestOutputLive { - def make: ZIO[Any, Nothing, TestOutput] = for { + def make(executionEventPrinter: ExecutionEventPrinter): ZIO[Any, Nothing, TestOutput] = for { talkers <- TestReporters.make output <- Ref.make[Map[SuiteId, Chunk[ExecutionEvent]]](Map.empty) - } yield TestOutputLive(output, talkers) + } yield TestOutputLive(output, talkers, executionEventPrinter) } } diff --git a/test/shared/src/main/scala/zio/test/TestRunner.scala b/test/shared/src/main/scala/zio/test/TestRunner.scala index 46c411d53d2d..bdd9857a6463 100644 --- a/test/shared/src/main/scala/zio/test/TestRunner.scala +++ b/test/shared/src/main/scala/zio/test/TestRunner.scala @@ -34,16 +34,17 @@ final case class TestRunner[R, E]( DefaultTestReporter(TestRenderer.default, TestAnnotationRenderer.default)(ZTraceElement.empty), bootstrap: Layer[ Nothing, - TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random + TestOutput with TestLogger with Clock with ExecutionEventSink with Random ] = { + implicit val emptyTracer = ZTraceElement.empty + val printerLayer: ZLayer[Any, Nothing, ExecutionEventPrinter] = + (Console.live.to(TestLogger.fromConsole) >>> ExecutionEventPrinter.live) + + val sinkLayer = ((printerLayer >>> TestOutput.live)(ZTraceElement.empty) >+> ExecutionEventSink.live) (Console.live.to(TestLogger.fromConsole(ZTraceElement.empty))( ZTraceElement.empty - )) ++ Clock.live ++ TestOutput.live ++ (TestOutput.live >>> ExecutionEventSink.live)( - ZTraceElement.empty - ) ++ Random.live ++ (((Console.live.to(TestLogger.fromConsole(ZTraceElement.empty))( - ZTraceElement.empty - )) >>> ExecutionEventPrinter.live)(ZTraceElement.empty) ) + )) ++ Clock.live ++ sinkLayer ++ Random.live } ) { self => @@ -57,7 +58,7 @@ final case class TestRunner[R, E]( )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with ExecutionEventPrinter with Random, + TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Summary ] = executor.run(spec, ExecutionStrategy.ParallelN(4)).timed.flatMap { case (duration, summary) => diff --git a/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala b/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala index 1ab986623b7b..d28de28d7361 100644 --- a/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala +++ b/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala @@ -55,7 +55,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { self.layer +!+ that.layer override def runSpec: ZIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventPrinter, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope, Any, Any ] = @@ -73,7 +73,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { } protected def runSpec: ZIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Scope, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with Scope, Any, Any ] = { @@ -112,14 +112,14 @@ abstract class ZIOSpecAbstract extends ZIOApp { private[zio] def runSpec( spec: ZSpec[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Clock with Scope with ExecutionEventSink with ExecutionEventPrinter, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Clock with Scope with ExecutionEventSink, Any ], testArgs: TestArgs )(implicit trace: ZTraceElement ): URIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink with ExecutionEventPrinter, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink, Summary ] = { val filteredSpec = FilteredSpec(spec, testArgs) @@ -127,7 +127,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { for { runtime <- ZIO.runtime[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with ExecutionEventPrinter with Scope + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with Scope ] environment = runtime.environment runtimeConfig = hook(runtime.runtimeConfig) @@ -135,7 +135,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { TestRunner( TestExecutor .default[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink with ExecutionEventPrinter, + Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink, Any ]( ZLayer.succeedEnvironment(environment) +!+ (Scope.default >>> testEnvironment) diff --git a/test/shared/src/main/scala/zio/test/package.scala b/test/shared/src/main/scala/zio/test/package.scala index be331e0b5055..6ed5444e20cc 100644 --- a/test/shared/src/main/scala/zio/test/package.scala +++ b/test/shared/src/main/scala/zio/test/package.scala @@ -82,9 +82,9 @@ package object test extends CompileVariants { (Live.default >>> TestConsole.debug) ++ TestRandom.deterministic ++ TestSystem.default ++ - (TestOutput.live >>> ExecutionEventSink.live) ++ - TestOutput.live ++ - TestLogger.fromConsole.map(testLogger => ZEnvironment(new ExecutionEventPrinter.Live(testLogger.get))) + (TestLogger.fromConsole.map(testLogger => + ZEnvironment(new ExecutionEventPrinter.Live(testLogger.get)) + ) >+> TestOutput.live >+> ExecutionEventSink.live) } } From d6c2e50f92d5ab875b7e4e1825443ed4fc5824f1 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 15:02:55 -0600 Subject: [PATCH 17/22] Test Environment types-- --- .../ParallelSuitesInterleavedResultsSpec.scala | 12 ++++++------ .../scala/zio/test/ReportingTestUtils.scala | 4 ++-- .../src/test/scala/zio/test/TestUtils.scala | 2 +- .../scala/zio/test/ExecutionEventSink.scala | 17 ++++++++++------- .../src/main/scala/zio/test/TestExecutor.scala | 6 +++--- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index afc1a96650d8..bf7346c42cf3 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -14,7 +14,7 @@ object AMinimalSpec extends ZIOSpecDefault { test("test after big delay") { Live.live(ZIO.sleep(5.second)).map(_ => assertTrue(true)) } - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } @@ -26,7 +26,7 @@ object BMinimalSpec extends ZIOSpecDefault { test("B 2") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object MultiCMinimalSpec extends ZIOSpecDefault { @@ -50,7 +50,7 @@ object MultiCMinimalSpec extends ZIOSpecDefault { test("standalone 1") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object SmallMinimalSpec extends ZIOSpecDefault { @@ -65,7 +65,7 @@ object SmallMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } ) - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object SlowMinimalSpec extends ZIOSpecDefault { @@ -94,13 +94,13 @@ object SlowMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - )// @@ TestAspect.ignore + ) // @@ TestAspect.ignore } object SingleMinimalSpec extends ZIOSpecDefault { override def spec = test("Single spec not in a suite") { Live.live(ZIO.sleep(2.second)).map(_ => assertTrue(true)) - }// @@ TestAspect.ignore + } // @@ TestAspect.ignore } diff --git a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala index eff0191f78e6..4fcdda16ef3f 100644 --- a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala +++ b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala @@ -50,7 +50,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - (TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live + (TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ) output <- TestConsole.output } yield output.mkString @@ -61,7 +61,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - Scope.default >>> ((TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live) + Scope.default >>> ((TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live) ) } yield summary.summary diff --git a/test-tests/shared/src/test/scala/zio/test/TestUtils.scala b/test-tests/shared/src/test/scala/zio/test/TestUtils.scala index cc6e65945013..feead2e39d95 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestUtils.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestUtils.scala @@ -8,7 +8,7 @@ object TestUtils { TestExecutor .default(testEnvironment) .run(spec, ExecutionStrategy.Sequential) - .provide(testEnvironment, TestLogger.fromConsole, Scope.default) + .provide(testEnvironment, Scope.default) def isIgnored[E](spec: ZSpec[TestEnvironment, E]): UIO[Boolean] = execute(spec) diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala index 795467bf79d3..b3faaa2e4b07 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala @@ -7,7 +7,7 @@ trait ExecutionEventSink { def process( event: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] + ): ZIO[Any, Nothing, Unit] } object ExecutionEventSink { @@ -16,28 +16,28 @@ object ExecutionEventSink { def process( event: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] = + ): ZIO[ExecutionEventSink, Nothing, Unit] = ZIO.serviceWithZIO[ExecutionEventSink](_.process(event)) - val ExecutionEventSinkLive: ZIO[Any, Nothing, ExecutionEventSink] = + def ExecutionEventSinkLive(testOutput: TestOutput): ZIO[Any, Nothing, ExecutionEventSink] = for { summary <- Ref.make[Summary](Summary(0, 0, 0, "")) } yield new ExecutionEventSink { override def process( event: ExecutionEvent - ): ZIO[TestOutput with ExecutionEventSink with TestLogger, Nothing, Unit] = + ): ZIO[Any, Nothing, Unit] = event match { case testEvent: ExecutionEvent.Test[_] => summary.update( _.add(testEvent) ) *> - TestOutput.print( + testOutput.print( testEvent ) case otherEvents => - TestOutput.print( + testOutput.print( otherEvents ) } @@ -48,6 +48,9 @@ object ExecutionEventSink { val live: ZLayer[TestOutput, Nothing, ExecutionEventSink] = ZLayer.fromZIO( - ExecutionEventSinkLive + for { + testOutput <- ZIO.service[TestOutput] + sink <- ExecutionEventSinkLive(testOutput) + } yield sink ) } diff --git a/test/shared/src/main/scala/zio/test/TestExecutor.scala b/test/shared/src/main/scala/zio/test/TestExecutor.scala index 67c5be6c895d..8a5d11500455 100644 --- a/test/shared/src/main/scala/zio/test/TestExecutor.scala +++ b/test/shared/src/main/scala/zio/test/TestExecutor.scala @@ -27,7 +27,7 @@ import zio.{ExecutionStrategy, Random, ZIO, ZTraceElement} abstract class TestExecutor[+R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement - ): ZIO[TestOutput with TestLogger with ExecutionEventSink with Random, Nothing, Summary] + ): ZIO[TestOutput with ExecutionEventSink with Random, Nothing, Summary] def environment: ZLayer[Scope, Nothing, R] } @@ -40,7 +40,7 @@ object TestExecutor { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement ): ZIO[ - TestOutput with TestLogger with ExecutionEventSink with Random, + TestOutput with ExecutionEventSink with Random, Nothing, Summary ] = @@ -56,7 +56,7 @@ object TestExecutor { ancestors: List[SuiteId], sectionId: SuiteId ): ZIO[ - TestOutput with TestLogger with ExecutionEventSink with Random with Scope, + TestOutput with ExecutionEventSink with Random with Scope, Nothing, Unit ] = From 591a342f17fbfead7efac06226ef36c81fe63a14 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 15:28:39 -0600 Subject: [PATCH 18/22] Test Environment types-- --- .../src/main/scala/zio/test/sbt/BaseTestTask.scala | 3 +-- .../test/scala/zio/test/IntellijRendererSpec.scala | 2 +- .../src/test/scala/zio/test/ReportingTestUtils.scala | 4 ++-- .../main/scala/zio/test/AbstractRunnableSpec.scala | 2 +- .../main/scala/zio/test/DefaultRunnableSpec.scala | 2 +- .../main/scala/zio/test/MutableRunnableSpec.scala | 2 +- .../src/main/scala/zio/test/TestExecutor.scala | 8 ++++---- test/shared/src/main/scala/zio/test/TestRunner.scala | 2 +- .../src/main/scala/zio/test/ZIOSpecAbstract.scala | 12 ++++++------ test/shared/src/main/scala/zio/test/package.scala | 2 -- 10 files changed, 18 insertions(+), 21 deletions(-) diff --git a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala index 344d1bb73ebd..fbaa2f28939a 100644 --- a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala +++ b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala @@ -9,7 +9,6 @@ import zio.test.{ TestArgs, TestEnvironment, TestLogger, - TestOutput, ZIOSpecAbstract } import zio.{Clock, Random, Runtime, Scope, ZEnvironment, ZIO, ZIOAppArgs, ZLayer, ZTraceElement} @@ -26,7 +25,7 @@ abstract class BaseTestTask( eventHandler: EventHandler, spec: AbstractRunnableSpec ): ZIO[ - TestLogger with Clock with TestOutput with ExecutionEventSink with Random, + TestLogger with Clock with ExecutionEventSink with Random, Throwable, Unit ] = { diff --git a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala index 6d8fe532c6b3..de995ad853d4 100644 --- a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala @@ -194,7 +194,7 @@ object IntelliJRenderUtils { IntelliJTestRunner(testEnvironment) .run(spec) .provideLayer[Nothing, TestEnvironment with Scope]( - TestLogger.fromConsole ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ++ (TestLogger.fromConsole >>> ExecutionEventPrinter.live) + TestLogger.fromConsole ++ TestClock.default ++ ((TestLogger.fromConsole >>> ExecutionEventPrinter.live) >>> TestOutput.live >+> ExecutionEventSink.live) ++ Random.live ) output <- TestConsole.output } yield output.mkString diff --git a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala index 4fcdda16ef3f..a8dd0a192f98 100644 --- a/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala +++ b/test-tests/shared/src/test/scala/zio/test/ReportingTestUtils.scala @@ -50,7 +50,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - (TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live + (TestLogger.fromConsole >>> ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live) ++ TestClock.default ++ Random.live ) output <- TestConsole.output } yield output.mkString @@ -61,7 +61,7 @@ object ReportingTestUtils { TestTestRunner(testEnvironment) .run(spec) .provideLayer( - Scope.default >>> ((TestLogger.fromConsole >+> ExecutionEventPrinter.live) ++ TestClock.default ++ (TestOutput.live >+> ExecutionEventSink.live) ++ Random.live) + Scope.default >>> ((TestLogger.fromConsole >>> ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live) ++ TestClock.default ++ Random.live) ) } yield summary.summary diff --git a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala index 0e109919bfc7..fe18100cbd24 100644 --- a/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/AbstractRunnableSpec.scala @@ -53,7 +53,7 @@ abstract class AbstractRunnableSpec { )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with Random, + Clock with ExecutionEventSink with Random, Summary ] = runner.run(aspects.foldLeft(spec)(_ @@ _)) diff --git a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala index cda2aeac3163..8c3ff293baba 100644 --- a/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/DefaultRunnableSpec.scala @@ -41,7 +41,7 @@ abstract class DefaultRunnableSpec extends RunnableSpec[TestEnvironment, Any] { )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with Random, + Clock with ExecutionEventSink with Random, Summary ] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) diff --git a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala index c2d7f7c5b6ec..e0f3ac4480a1 100644 --- a/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/MutableRunnableSpec.scala @@ -168,7 +168,7 @@ class MutableRunnableSpec[R: Tag]( )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with Random, + Clock with ExecutionEventSink with Random, Summary ] = runner.run(aspects.foldLeft(spec)(_ @@ _) @@ TestAspect.fibers) diff --git a/test/shared/src/main/scala/zio/test/TestExecutor.scala b/test/shared/src/main/scala/zio/test/TestExecutor.scala index 8a5d11500455..b4e648e90d06 100644 --- a/test/shared/src/main/scala/zio/test/TestExecutor.scala +++ b/test/shared/src/main/scala/zio/test/TestExecutor.scala @@ -27,20 +27,20 @@ import zio.{ExecutionStrategy, Random, ZIO, ZTraceElement} abstract class TestExecutor[+R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement - ): ZIO[TestOutput with ExecutionEventSink with Random, Nothing, Summary] + ): ZIO[ExecutionEventSink with Random, Nothing, Summary] def environment: ZLayer[Scope, Nothing, R] } object TestExecutor { - def default[R <: Annotations, E]( + def default[R <: Annotations with ExecutionEventSink, E]( env: ZLayer[Scope, Nothing, R] ): TestExecutor[R, E] = new TestExecutor[R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit trace: ZTraceElement ): ZIO[ - TestOutput with ExecutionEventSink with Random, + ExecutionEventSink with Random, Nothing, Summary ] = @@ -56,7 +56,7 @@ object TestExecutor { ancestors: List[SuiteId], sectionId: SuiteId ): ZIO[ - TestOutput with ExecutionEventSink with Random with Scope, + Random with Scope, Nothing, Unit ] = diff --git a/test/shared/src/main/scala/zio/test/TestRunner.scala b/test/shared/src/main/scala/zio/test/TestRunner.scala index bdd9857a6463..623c83bb54e7 100644 --- a/test/shared/src/main/scala/zio/test/TestRunner.scala +++ b/test/shared/src/main/scala/zio/test/TestRunner.scala @@ -58,7 +58,7 @@ final case class TestRunner[R, E]( )(implicit trace: ZTraceElement ): URIO[ - TestOutput with TestLogger with Clock with ExecutionEventSink with Random, + Clock with ExecutionEventSink with Random, Summary ] = executor.run(spec, ExecutionStrategy.ParallelN(4)).timed.flatMap { case (duration, summary) => diff --git a/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala b/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala index d28de28d7361..a4ac9cd81d39 100644 --- a/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala +++ b/test/shared/src/main/scala/zio/test/ZIOSpecAbstract.scala @@ -55,7 +55,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { self.layer +!+ that.layer override def runSpec: ZIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope, + Environment with TestEnvironment with ZIOAppArgs with Scope, Any, Any ] = @@ -73,7 +73,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { } protected def runSpec: ZIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with Scope, + Environment with TestEnvironment with ZIOAppArgs with Scope, Any, Any ] = { @@ -112,14 +112,14 @@ abstract class ZIOSpecAbstract extends ZIOApp { private[zio] def runSpec( spec: ZSpec[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Clock with Scope with ExecutionEventSink, + Environment with TestEnvironment with ZIOAppArgs with Clock with Scope, Any ], testArgs: TestArgs )(implicit trace: ZTraceElement ): URIO[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink, + Environment with TestEnvironment with ZIOAppArgs with Scope, Summary ] = { val filteredSpec = FilteredSpec(spec, testArgs) @@ -127,7 +127,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { for { runtime <- ZIO.runtime[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with ExecutionEventSink with Scope + Environment with TestEnvironment with ZIOAppArgs with Scope ] environment = runtime.environment runtimeConfig = hook(runtime.runtimeConfig) @@ -135,7 +135,7 @@ abstract class ZIOSpecAbstract extends ZIOApp { TestRunner( TestExecutor .default[ - Environment with TestEnvironment with ZIOAppArgs with TestOutput with TestLogger with Scope with ExecutionEventSink, + Environment with TestEnvironment with ZIOAppArgs with Scope, Any ]( ZLayer.succeedEnvironment(environment) +!+ (Scope.default >>> testEnvironment) diff --git a/test/shared/src/main/scala/zio/test/package.scala b/test/shared/src/main/scala/zio/test/package.scala index 6ed5444e20cc..50c01edb6beb 100644 --- a/test/shared/src/main/scala/zio/test/package.scala +++ b/test/shared/src/main/scala/zio/test/package.scala @@ -66,8 +66,6 @@ package object test extends CompileVariants { with TestRandom with TestSystem with ExecutionEventSink - with ExecutionEventPrinter - with TestOutput object TestEnvironment { val any: ZLayer[TestEnvironment, Nothing, TestEnvironment] = From 5d6aa2c36b27c9977abc09dcb96dae4c407ccc7e Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 16:01:14 -0600 Subject: [PATCH 19/22] Test Environment types-- --- .../src/main/scala/zio/test/sbt/BaseTestTask.scala | 2 +- .../test/scala/zio/test/IntellijRendererSpec.scala | 2 +- .../src/main/scala/zio/test/RunnableSpec.scala | 2 +- .../src/main/scala/zio/test/TestExecutor.scala | 2 +- test/shared/src/main/scala/zio/test/TestRunner.scala | 12 +++++------- test/shared/src/main/scala/zio/test/package.scala | 4 +--- 6 files changed, 10 insertions(+), 14 deletions(-) diff --git a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala index fbaa2f28939a..2b70b67a43cf 100644 --- a/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala +++ b/test-sbt/shared/src/main/scala/zio/test/sbt/BaseTestTask.scala @@ -25,7 +25,7 @@ abstract class BaseTestTask( eventHandler: EventHandler, spec: AbstractRunnableSpec ): ZIO[ - TestLogger with Clock with ExecutionEventSink with Random, + Clock with ExecutionEventSink with Random, Throwable, Unit ] = { diff --git a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala index de995ad853d4..e1fd2bb1dbd1 100644 --- a/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/IntellijRendererSpec.scala @@ -194,7 +194,7 @@ object IntelliJRenderUtils { IntelliJTestRunner(testEnvironment) .run(spec) .provideLayer[Nothing, TestEnvironment with Scope]( - TestLogger.fromConsole ++ TestClock.default ++ ((TestLogger.fromConsole >>> ExecutionEventPrinter.live) >>> TestOutput.live >+> ExecutionEventSink.live) ++ Random.live + TestClock.default ++ (TestLogger.fromConsole >>> ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live) ++ Random.live ) output <- TestConsole.output } yield output.mkString diff --git a/test/shared/src/main/scala/zio/test/RunnableSpec.scala b/test/shared/src/main/scala/zio/test/RunnableSpec.scala index c0a272c82936..9ca734ef8031 100644 --- a/test/shared/src/main/scala/zio/test/RunnableSpec.scala +++ b/test/shared/src/main/scala/zio/test/RunnableSpec.scala @@ -28,7 +28,7 @@ abstract class RunnableSpec[R, E] extends AbstractRunnableSpec { private def run(spec: ZSpec[Environment, Failure], testArgs: TestArgs)(implicit trace: ZTraceElement - ): URIO[TestOutput with TestLogger with Clock with ExecutionEventSink with Random, Int] = { + ): URIO[TestLogger with Clock with ExecutionEventSink with Random, Int] = { val filteredSpec = FilteredSpec(spec, testArgs) val testReporter = testArgs.testRenderer.fold(runner.reporter)(createTestReporter) for { diff --git a/test/shared/src/main/scala/zio/test/TestExecutor.scala b/test/shared/src/main/scala/zio/test/TestExecutor.scala index b4e648e90d06..d449cf9711e7 100644 --- a/test/shared/src/main/scala/zio/test/TestExecutor.scala +++ b/test/shared/src/main/scala/zio/test/TestExecutor.scala @@ -34,7 +34,7 @@ abstract class TestExecutor[+R, E] { object TestExecutor { - def default[R <: Annotations with ExecutionEventSink, E]( + def default[R <: Annotations, E]( env: ZLayer[Scope, Nothing, R] ): TestExecutor[R, E] = new TestExecutor[R, E] { def run(spec: ZSpec[R, E], defExec: ExecutionStrategy)(implicit diff --git a/test/shared/src/main/scala/zio/test/TestRunner.scala b/test/shared/src/main/scala/zio/test/TestRunner.scala index 623c83bb54e7..4fd4c138f045 100644 --- a/test/shared/src/main/scala/zio/test/TestRunner.scala +++ b/test/shared/src/main/scala/zio/test/TestRunner.scala @@ -34,17 +34,15 @@ final case class TestRunner[R, E]( DefaultTestReporter(TestRenderer.default, TestAnnotationRenderer.default)(ZTraceElement.empty), bootstrap: Layer[ Nothing, - TestOutput with TestLogger with Clock with ExecutionEventSink with Random + TestLogger with Clock with ExecutionEventSink with Random ] = { implicit val emptyTracer = ZTraceElement.empty - val printerLayer: ZLayer[Any, Nothing, ExecutionEventPrinter] = - (Console.live.to(TestLogger.fromConsole) >>> ExecutionEventPrinter.live) + val printerLayer = + Console.live.to(TestLogger.fromConsole) - val sinkLayer = ((printerLayer >>> TestOutput.live)(ZTraceElement.empty) >+> ExecutionEventSink.live) + val sinkLayer = ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live - (Console.live.to(TestLogger.fromConsole(ZTraceElement.empty))( - ZTraceElement.empty - )) ++ Clock.live ++ sinkLayer ++ Random.live + Clock.live ++ (printerLayer >+> sinkLayer) ++ Random.live } ) { self => diff --git a/test/shared/src/main/scala/zio/test/package.scala b/test/shared/src/main/scala/zio/test/package.scala index 50c01edb6beb..b7290ecd0ed2 100644 --- a/test/shared/src/main/scala/zio/test/package.scala +++ b/test/shared/src/main/scala/zio/test/package.scala @@ -80,9 +80,7 @@ package object test extends CompileVariants { (Live.default >>> TestConsole.debug) ++ TestRandom.deterministic ++ TestSystem.default ++ - (TestLogger.fromConsole.map(testLogger => - ZEnvironment(new ExecutionEventPrinter.Live(testLogger.get)) - ) >+> TestOutput.live >+> ExecutionEventSink.live) + (TestLogger.fromConsole >>> ExecutionEventPrinter.live >>> TestOutput.live >>> ExecutionEventSink.live) } } From a05577736ee3ca248fda9eff4202b1ae196db634 Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 16:15:08 -0600 Subject: [PATCH 20/22] Cleanup and back-out one of the TestOutput experiments --- .../zio/test/junit/ZTestJUnitRunner.scala | 3 +- .../test/scala/zio/test/TestOutputSpec.scala | 3 - .../scala/zio/test/ExecutionEventSink.scala | 2 +- .../src/main/scala/zio/test/TestOutput.scala | 61 +++++++++++-------- 4 files changed, 39 insertions(+), 30 deletions(-) diff --git a/test-junit/jvm/src/main/scala/zio/test/junit/ZTestJUnitRunner.scala b/test-junit/jvm/src/main/scala/zio/test/junit/ZTestJUnitRunner.scala index baa28bb188bf..0adfb5f57696 100644 --- a/test-junit/jvm/src/main/scala/zio/test/junit/ZTestJUnitRunner.scala +++ b/test-junit/jvm/src/main/scala/zio/test/junit/ZTestJUnitRunner.scala @@ -103,8 +103,7 @@ class ZTestJUnitRunner(klass: Class[_]) extends Runner with Filterable { .provide( Scope.default >>> (ZEnv.live >>> TestEnvironment.live ++ ZLayer.environment[Scope]), ZIOAppArgs.empty, - spec.layer, - TestLogger.fromConsole + spec.layer ) } } diff --git a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala index cdbd90e8fd86..2efabdcf324b 100644 --- a/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/TestOutputSpec.scala @@ -73,8 +73,6 @@ object TestOutputSpec extends ZIOSpecDefault { End(child1) ) for { - fakePrinter <- ZIO.service[ExecutionEventPrinter] - _ <- ZIO.debug("Printer: " + fakePrinter) _ <- ZIO.foreach(events)(event => TestOutput.print(event)) outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) } yield assertTrue( @@ -104,7 +102,6 @@ object TestOutputSpec extends ZIOSpecDefault { for { _ <- ZIO.foreach(events)(event => TestOutput.print(event)) outputEvents <- ZIO.serviceWithZIO[ExecutionEventHolder](_.getEvents) - _ <- ZIO.debug(outputEvents.mkString("\n")) } yield assertTrue( outputEvents == List( diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala index b3faaa2e4b07..068bfc8eba1c 100644 --- a/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala +++ b/test/shared/src/main/scala/zio/test/ExecutionEventSink.scala @@ -50,7 +50,7 @@ object ExecutionEventSink { ZLayer.fromZIO( for { testOutput <- ZIO.service[TestOutput] - sink <- ExecutionEventSinkLive(testOutput) + sink <- ExecutionEventSinkLive(testOutput) } yield sink ) } diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index b4c98d3f62c7..dc1ee7bf2524 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -72,34 +72,47 @@ object TestOutput { def print( executionEvent: ExecutionEvent + ): ZIO[Any, Nothing, Unit] = + executionEvent match { + case end: ExecutionEvent.SectionEnd => + printOrFlush(end) + case other => + printOrQueue(other) + } + + private def printOrFlush( + end: ExecutionEvent.SectionEnd ): ZIO[Any, Nothing, Unit] = for { - suiteIsPrinting <- reporters.attemptToGetPrintingControl(executionEvent.id, executionEvent.ancestors) - _ <- appendToSectionContents(executionEvent.id, Chunk(executionEvent)) - sectionOutput <- getAndRemoveSectionOutput(executionEvent.id) + suiteIsPrinting <- reporters.attemptToGetPrintingControl(end.id, end.ancestors) + sectionOutput <- getAndRemoveSectionOutput(end.id).map(_ :+ end) _ <- - executionEvent match { - case _: ExecutionEvent.SectionEnd => - for { - _ <- - if (suiteIsPrinting) - printToConsole(sectionOutput) - else { - executionEvent.ancestors.headOption match { - case Some(parentId) => - appendToSectionContents(parentId, sectionOutput) - case None => - ZIO.dieMessage("Suite tried to send its output to a nonexistent parent") - } - } - _ <- reporters.relinquishPrintingControl(executionEvent.id) - } yield () - case _ => - if (suiteIsPrinting) - printToConsole(sectionOutput) - else - appendToSectionContents(executionEvent.id, sectionOutput) // TODO More + if (suiteIsPrinting) + printToConsole(sectionOutput) + else { + end.ancestors.headOption match { + case Some(parentId) => + appendToSectionContents(parentId, sectionOutput) + case None => + ZIO.dieMessage("Suite tried to send its output to a nonexistent parent") + } } + + _ <- reporters.relinquishPrintingControl(end.id) + } yield () + + private def printOrQueue( + reporterEvent: ExecutionEvent + ): ZIO[Any, Nothing, Unit] = + for { + _ <- appendToSectionContents(reporterEvent.id, Chunk(reporterEvent)) + suiteIsPrinting <- reporters.attemptToGetPrintingControl(reporterEvent.id, reporterEvent.ancestors) + _ <- ZIO.when(suiteIsPrinting)( + for { + currentOutput <- getAndRemoveSectionOutput(reporterEvent.id) + _ <- printToConsole(currentOutput) + } yield () + ) } yield () private def printToConsole(events: Chunk[ExecutionEvent]) = From 285940d33c2a7c509224fdecf2ca227e14670b0e Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 16:17:56 -0600 Subject: [PATCH 21/22] More cleanup --- .../scala/zio/test/ExecutionEventSinkSpec.scala | 2 +- .../ParallelSuitesInterleavedResultsSpec.scala | 17 +++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala b/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala index e16ed8c8e5a5..a2b004788d58 100644 --- a/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ExecutionEventSinkSpec.scala @@ -53,7 +53,7 @@ object ExecutionEventSinkSpec extends ZIOSpecDefault { TestLogger.fromConsole, ExecutionEventSink.live, TestOutput.live, - ExecutionEventPrinter.live // TODO test version that just accumulates + ExecutionEventPrinter.live ) } diff --git a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala index bf7346c42cf3..2f0e9d3d9be7 100644 --- a/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala +++ b/test-tests/shared/src/test/scala/zio/test/ParallelSuitesInterleavedResultsSpec.scala @@ -14,7 +14,7 @@ object AMinimalSpec extends ZIOSpecDefault { test("test after big delay") { Live.live(ZIO.sleep(5.second)).map(_ => assertTrue(true)) } - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } @@ -26,7 +26,7 @@ object BMinimalSpec extends ZIOSpecDefault { test("B 2") { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } object MultiCMinimalSpec extends ZIOSpecDefault { @@ -46,11 +46,8 @@ object MultiCMinimalSpec extends ZIOSpecDefault { test("slow 2") { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } - ), - test("standalone 1") { - Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) - } - ) // @@ TestAspect.ignore + ) + ) @@ TestAspect.ignore } object SmallMinimalSpec extends ZIOSpecDefault { @@ -65,7 +62,7 @@ object SmallMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(1.second)).map(_ => assertTrue(true)) } ) - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } object SlowMinimalSpec extends ZIOSpecDefault { @@ -94,13 +91,13 @@ object SlowMinimalSpec extends ZIOSpecDefault { Live.live(ZIO.sleep(3.second)).map(_ => assertTrue(true)) } ) - ) // @@ TestAspect.ignore + ) @@ TestAspect.ignore } object SingleMinimalSpec extends ZIOSpecDefault { override def spec = test("Single spec not in a suite") { Live.live(ZIO.sleep(2.second)).map(_ => assertTrue(true)) - } // @@ TestAspect.ignore + } @@ TestAspect.ignore } From a0977af9c04c6a819b463ccbaa54e9d333e8f15d Mon Sep 17 00:00:00 2001 From: Bill Frasure Date: Wed, 30 Mar 2022 16:20:41 -0600 Subject: [PATCH 22/22] Pull Printer into separate file --- .../zio/test/ExecutionEventPrinter.scala | 25 +++++++++++++++++++ .../src/main/scala/zio/test/TestOutput.scala | 22 ---------------- 2 files changed, 25 insertions(+), 22 deletions(-) create mode 100644 test/shared/src/main/scala/zio/test/ExecutionEventPrinter.scala diff --git a/test/shared/src/main/scala/zio/test/ExecutionEventPrinter.scala b/test/shared/src/main/scala/zio/test/ExecutionEventPrinter.scala new file mode 100644 index 000000000000..fdc2f5e20fda --- /dev/null +++ b/test/shared/src/main/scala/zio/test/ExecutionEventPrinter.scala @@ -0,0 +1,25 @@ +package zio.test + +import zio.{ZIO, ZLayer} + +trait ExecutionEventPrinter { + def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] +} + +object ExecutionEventPrinter { + val live: ZLayer[TestLogger, Nothing, ExecutionEventPrinter] = { + ZLayer.fromZIO(for { + testLogger <- ZIO.service[TestLogger] + } yield new Live(testLogger)) + } + + def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter, Nothing, Unit] = + ZIO.serviceWithZIO[ExecutionEventPrinter](_.print(event)) + + class Live(logger: TestLogger) extends ExecutionEventPrinter { + override def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] = + logger.logLine( + ReporterEventRenderer.render(event).mkString("\n") + ) + } +} diff --git a/test/shared/src/main/scala/zio/test/TestOutput.scala b/test/shared/src/main/scala/zio/test/TestOutput.scala index dc1ee7bf2524..1cb32705acee 100644 --- a/test/shared/src/main/scala/zio/test/TestOutput.scala +++ b/test/shared/src/main/scala/zio/test/TestOutput.scala @@ -2,28 +2,6 @@ package zio.test import zio.{Chunk, Ref, ZIO, ZLayer} -trait ExecutionEventPrinter { - def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] -} - -object ExecutionEventPrinter { - val live: ZLayer[TestLogger, Nothing, ExecutionEventPrinter] = { - ZLayer.fromZIO(for { - testLogger <- ZIO.service[TestLogger] - } yield new Live(testLogger)) - } - - def print(event: ExecutionEvent): ZIO[ExecutionEventPrinter, Nothing, Unit] = - ZIO.serviceWithZIO[ExecutionEventPrinter](_.print(event)) - - class Live(logger: TestLogger) extends ExecutionEventPrinter { - override def print(event: ExecutionEvent): ZIO[Any, Nothing, Unit] = - logger.logLine( - ReporterEventRenderer.render(event).mkString("\n") - ) - } -} - trait TestOutput { /**