8000 Simplify Runnable Spec by adamgfraser · Pull Request #5545 · zio/zio · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Simplify Runnable Spec #5545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions core/js/src/main/scala/zio/internal/PlatformSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ private[zio] trait PlatformSpecific {
val _ = action
}

/**
* Exits the application with the specified exit code.
*/
def exit(code: Int): Unit = {
val _ = code
}

/**
* Returns the name of the thread group to which this thread belongs. This
* is a side-effecting method.
Expand Down
6 changes: 6 additions & 0 deletions core/jvm/src/main/scala/zio/internal/PlatformSpecific.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ private[zio] trait PlatformSpecific {
}
}

/**
* Exits the application with the specified exit code.
*/
def exit(code: Int): Unit =
java.lang.System.exit(code)

/**
* Returns the name of the thread group to which this thread belongs. This
* is a side-effecting method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ private[zio] trait PlatformSpecific {
val _ = action
}

/**
* Exits the application with the specified exit code.
*/
def exit(code: Int): Unit = {
val _ = code
}

/**
* Returns the name of the thread group to which this thread belongs. This
* is a side-effecting method.
Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/main/scala/zio/ZIOApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ trait ZIOApp { self =>
UIO {
if (!shuttingDown) {
shuttingDown = true
try java.lang.System.exit(code.code)
try Platform.exit(code.code)
catch { case _: SecurityException => }
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
package zio.test

import org.portablescala.reflect.annotation.EnableReflectiveInstantiation
import zio.{Clock, Has, RuntimeConfig, URIO}
import zio._

@EnableReflectiveInstantiation
abstract class AbstractRunnableSpec {
abstract class AbstractRunnableSpec extends ZIOApp {

type Environment
type Failure
Expand All @@ -39,8 +39,8 @@ abstract class AbstractRunnableSpec {
/**
* Returns an effect that executes the spec, producing the results of the execution.
*/
final def run: URIO[Has[TestLogger] with Has[Clock], ExecutedSpec[Failure]] =
runSpec(spec)
def run: ZIO[ZEnv with Has[ZIOAppArgs], Any, Any] =
runSpec(spec).provideCustomLayer(runner.bootstrap)

/**
* Returns an effect that executes a given spec, producing the results of the execution.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ import zio.{Has, ZLayer}
* }
* }}}
*/
@deprecated("use DefaultRunnableSpec", "2.0.0")
class DefaultMutableRunnableSpec extends MutableRunnableSpec[Has[Any]](ZLayer.succeed[Any](()), TestAspect.identity)
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import scala.util.control.NoStackTrace
* }
* }}}
*/
@deprecated("use RunnableSpec", "2.0.0")
class MutableRunnableSpec[R <: Has[_]: Tag](
layer: ZLayer[TestEnvironment, Throwable, R],
aspect: TestAspect[R with TestEnvironment, R with TestEnvironment, Any, Any] = TestAspect.identity
Expand Down
25 changes: 9 additions & 16 deletions test/shared/src/main/scala/zio/test/RunnableSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,16 @@ abstract class RunnableSpec[R, E] extends AbstractRunnableSpec {
/**
* A simple main function that can be used to run the spec.
*/
final def main(args: Array[String]): Unit = {
val testArgs = TestArgs.parse(args)
val runtime = runner.runtime
if (TestPlatform.isJVM) {
val exitCode = runtime.unsafeRun(run(spec, testArgs).provideLayer(runner.bootstrap))
doExit(exitCode)
} else if (TestPlatform.isJS) {
runtime.unsafeRunAsyncWith[Nothing, Int](run(spec, testArgs).provideLayer(runner.bootstrap)) { exit =>
val exitCode = exit.getOrElse(_ => 1)
doExit(exitCode)
}
}
}
override final def run: ZIO[ZEnv with Has[ZIOAppArgs], Any, Any] =
for {
args <- ZIO.service[ZIOAppArgs]
testArgs = TestArgs.parse(args.args.toArray)
exitCode <- run(spec, testArgs).provideLayer(runner.bootstrap)
_ <- doExit(exitCode)
} yield ()

private def doExit(exitCode: Int): Unit =
try if (!isAmmonite) sys.exit(exitCode)
catch { case _: SecurityException => }
private def doExit(exitCode: Int): UIO[Unit] =
exit(ExitCode(exitCode)).when(!isAmmonite).unit

private def isAmmonite: Boolean =
sys.env.exists { case (k, v) =>
Expand Down
93F5
7 changes: 4 additions & 3 deletions test/shared/src/main/scala/zio/test/TestAspect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,10 @@ object TestAspect extends TimeoutVariants {
}

/**
* An aspect that records the state of fibers spawned by the current test in [[TestAnnotation.fibers]].
* Applied by default in [[DefaultRunnableSpec]] and [[MutableRunnableSpec]] but not in [[RunnableSpec]].
* This aspect is required for the proper functioning of `TestClock.adjust`.
* An aspect that records the state of fibers spawned by the current test in
* [[TestAnnotation.fibers]]. Applied by default in [[DefaultRunnableSpec]]
* but not in [[RunnableSpec]]. This aspect is required for the proper
* functioning of `TestClock.adjust`.
*/
lazy val fibers: TestAspect[Nothing, Has[Annotations], Nothing, Any] =
new TestAspect.PerTest[Nothing, Has[Annotations], Nothing, Any] {
Expand Down
0