Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@guizmaii
Copy link
Member

@guizmaii guizmaii commented Nov 14, 2025

I need to be able to ignore some tests by tagging them and use -ignore-tags <tag>

But I noticed that if my tests use .provideShared then the layers inside are launched even if my entire suite (including the .provideShared) is flagged to not be run.

Example:

suite("should be filtered out by the -ignore-tags option")(
   ...
).provideShared(
   TestContainersPG.layer, // should not be started. Will fail if the Docker socket is not available
) @@ TestAspect.tag("dontRun")

Before the changes in this PR, the TestContainersPG.layer was always started, even if I launched my tests with the -ignore-tags dontRun option
With these changes, the TestContainersPG.layer is not started when I launch my tests with with the -ignore-tags dontRun option 🎉

My test launched in an env without Docker before these changes:

+ migration
16:22:21.180 [zio-default-blocking-1] [] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
	UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)
	DockerDesktopClientProviderStrategy: failed with exception NullPointerException (Cannot invoke "java.nio.file.Path.toString()" because the return value of "org.testcontainers.dockerclient.DockerDesktopClientProviderStrategy.getSocketPath()" is null)As no valid configuration was found, execution cannot continue.
See https://java.testcontainers.org/on_failure.html for more details.
    - does migrate
      java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
      	at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$7(DockerClientProviderStrategy.java:274)
      	at java.base/java.util.Optional.orElseThrow(Optional.java:403)

Same test with these changes:

+ migration
No tests were executed
[info] Completed tests

@guizmaii guizmaii marked this pull request as draft November 14, 2025 08:03
@guizmaii guizmaii force-pushed the zio_test_better_tags branch 3 times, most recently from fde50d0 to caa3e4a Compare November 15, 2025 04:10
@guizmaii guizmaii marked this pull request as ready for review November 15, 2025 04:10
@guizmaii guizmaii marked this pull request as draft November 15, 2025 04:21
@guizmaii guizmaii force-pushed the zio_test_better_tags branch from 851a773 to 86dd7be Compare November 15, 2025 05:01
@guizmaii guizmaii marked this pull request as ready for review November 15, 2025 05:32
@guizmaii guizmaii changed the title zio-test: Ignored annotated tests/suites should not trigger layer creation zio-test: Ignored annotated tests/suites should not trigger shared layer creation Nov 15, 2025
@guizmaii guizmaii changed the title zio-test: Ignored annotated tests/suites should not trigger shared layer creation zio-test: Ignored annotated suites should not trigger shared layer creation Nov 15, 2025
@guizmaii guizmaii force-pushed the zio_test_better_tags branch from bb4d5ff to 44f562c Compare November 17, 2025 03:40
@guizmaii guizmaii force-pushed the zio_test_better_tags branch from 44f562c to 6755420 Compare November 17, 2025 03:42
@kyri-petrou
Copy link
Contributor

kyri-petrou commented Nov 17, 2025

Could you provide an explanation on why / how this works? It's not obvious to me how this change affects the behaviour w.r.t. tags

@guizmaii guizmaii self-assigned this Nov 18, 2025
Comment on lines 350 to 364
Spec.scoped[R0] {
ZIO.scopeWith { scope =>
import Spec.RichLayer

val scopedLayer: ZLayer[R0, E1, R] =
ZLayer.makeSome[R0, R](
ZLayer.succeed(scope),
layer.extendScope
)

scopedLayer.memoizeWithinScope(scope).map { memoizedLayer =>
Spec.multiple(specs.map(_.provideLayer(memoizedLayer)))
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to reuse memoize to avoid code duplication. Also let's chain the methods together, creating a separate val for the scoped layer feels like it should be used more than once:

        Spec.scoped[R0] {
          ZIO.scopeWith { scope =>
            ZLayer
              .makeSome[R0, R](
                ZLayer.succeed(scope),
                layer.extendScope
              )
              .memoize
              .map { memoizedLayer =>
                Spec.multiple(specs.map(_.provideLayer(memoizedLayer)))
              }
          }
        }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 🙂

Comment on lines 551 to 563
Spec.scoped[R0] {
ZIO.scopeWith { scope =>
val scopedLayer: ZLayer[R0, E1, R1] =
ZLayer.makeSome[R0, R1](
ZLayer.succeed(scope),
layer.extendScope
)

scopedLayer.memoizeWithinScope(scope).map { memoizedLayer =>
Spec.multiple(specs.map(_.provideSomeLayer[R0](memoizedLayer)))
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 🙂

Comment on lines 588 to 595
private implicit final class RichLayer[RIn, E0, ROut](private val layer: ZLayer[RIn, E0, ROut]) extends AnyVal {

/**
* Version of `ZLayer::memoize` that uses the provided scope.
*/
def memoizeWithinScope(scope: Scope)(implicit trace: Trace): ZIO[Any, Nothing, ZLayer[RIn, E0, ROut]] =
layer.build(scope).memoize.map(ZLayer.fromZIOEnvironment(_))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@guizmaii guizmaii merged commit 15e7227 into series/2.x Nov 18, 2025
19 checks passed
@guizmaii guizmaii deleted the zio_test_better_tags branch November 18, 2025 04:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants