From 72a9b8fe6a204065eef9b4c94b6b3e9081d61f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Thu, 21 Dec 2023 21:15:17 +0100 Subject: [PATCH 1/3] upgrade: Kotest 5.8.1 --- gradle/libs.versions.toml | 2 +- runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 3ccbabf..1174395 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ # List of dependencies of the project [versions] -kotest = "5.8.0" # https://github.com/kotest/kotest/releases +kotest = "5.8.1" # https://github.com/kotest/kotest/releases arrow = "1.2.3" # https://github.com/arrow-kt/arrow/releases kotlinx-coroutines = "1.8.0" # https://github.com/Kotlin/kotlinx.coroutines/releases kotlinx-datetime = "0.5.0" # https://github.com/Kotlin/kotlinx-datetime/releases diff --git a/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt b/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt index 6983a7e..d136b40 100644 --- a/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt +++ b/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt @@ -6,7 +6,6 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.core.spec.style.scopes.RootScope import io.kotest.core.spec.style.scopes.addTest import io.kotest.core.test.TestType -import io.kotest.core.test.config.UnresolvedTestConfig import opensavvy.prepared.suite.SuiteDsl import opensavvy.prepared.suite.TestDsl import opensavvy.prepared.suite.config.* @@ -57,7 +56,7 @@ private class NonNestedSuite(private val root: RootScope, private val parentConf override fun test(name: String, context: CoroutineContext, config: TestConfig, block: suspend TestDsl.() -> Unit) { val thisConfig = parentConfig + config - val kotestConfig = UnresolvedTestConfig( + val kotestConfig = io.kotest.core.test.config.TestConfig( enabled = thisConfig[Ignored] == null, tags = config[Tag] .mapTo(HashSet()) { io.kotest.core.Tag(it.name) } -- 2.51.2 From 18b2ad6f877103b7cc08c9fc0758b32f5f5c1ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Thu, 21 Dec 2023 21:16:21 +0100 Subject: [PATCH 2/3] fix(runner-kotest): Execute in the same test scope as Kotest itself --- .../src/commonMain/kotlin/PreparedSuite.kt | 12 ++++++++++-- suite/src/commonMain/kotlin/RunTest.kt | 18 ++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt b/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt index d136b40..4b2be35 100644 --- a/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt +++ b/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt @@ -6,10 +6,12 @@ import io.kotest.core.spec.style.StringSpec import io.kotest.core.spec.style.scopes.RootScope import io.kotest.core.spec.style.scopes.addTest import io.kotest.core.test.TestType +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.withContext import opensavvy.prepared.suite.SuiteDsl import opensavvy.prepared.suite.TestDsl import opensavvy.prepared.suite.config.* -import opensavvy.prepared.suite.runTestDsl +import opensavvy.prepared.suite.runTestDslSuspend import kotlin.coroutines.CoroutineContext /** @@ -61,10 +63,16 @@ private class NonNestedSuite(private val root: RootScope, private val parentConf tags = config[Tag] .mapTo(HashSet()) { io.kotest.core.Tag(it.name) } .takeIf { it.isNotEmpty() }, + testCoroutineDispatcher = true, + coroutineTestScope = true, + coroutineDebugProbes = true, ) root.addTest(testName = TestName(name = prefix child name), disabled = false, type = TestType.Test, config = kotestConfig) { - runTestDsl(name, context, thisConfig, block) + val scope = this as TestScope // TODO: access the TestScope + withContext(context) { + scope.runTestDslSuspend(name, context, config, block) + } } } } diff --git a/suite/src/commonMain/kotlin/RunTest.kt b/suite/src/commonMain/kotlin/RunTest.kt index 242baa3..5daac19 100644 --- a/suite/src/commonMain/kotlin/RunTest.kt +++ b/suite/src/commonMain/kotlin/RunTest.kt @@ -2,7 +2,9 @@ package opensavvy.prepared.suite import kotlinx.coroutines.CoroutineName import kotlinx.coroutines.test.TestResult +import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.withContext import opensavvy.prepared.suite.config.TestConfig import opensavvy.prepared.suite.config.effectiveTimeout import kotlin.coroutines.CoroutineContext @@ -18,9 +20,21 @@ private class TestDslImpl( * It is only provided because it is required for people who implement their own test runner. */ fun runTestDsl(name: String, context: CoroutineContext, config: TestConfig, block: suspend TestDsl.() -> Unit): TestResult { - return runTest(CoroutineName("Test ‘$name’") + context, timeout = config.effectiveTimeout()) { + return runTest(timeout = config.effectiveTimeout()) { + runTestDslSuspend(name, context, config, block) + } +} + +/** + * Low-level primitive to execute a test declared as a [TestDsl], when already inside a [TestScope]. + * + * Regular users of the library should never need to call this function. It is only provided because it is required + * for people who implement their own test runner. + */ +suspend fun TestScope.runTestDslSuspend(name: String, context: CoroutineContext, config: TestConfig, block: suspend TestDsl.() -> Unit) { + withContext(context + CoroutineName("Test ‘$name’")) { val test = TestDslImpl( - environment = TestEnvironment(name, this), + environment = TestEnvironment(name, this@runTestDslSuspend) ) var successful = false -- 2.51.2 From be8085b9a6bd4040a59bdea5d5e5a1f94d09e6cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20=E2=80=9CCLOVIS=E2=80=9D=20Canet?= Date: Mon, 11 Mar 2024 19:12:25 +0100 Subject: [PATCH 3/3] fix(runner-kotest): Execute in the same test scope as Kotest itself --- .../runner-kotest/src/commonMain/kotlin/PreparedSuite.kt | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt b/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt index 4b2be35..fd6847e 100644 --- a/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt +++ b/runners/runner-kotest/src/commonMain/kotlin/PreparedSuite.kt @@ -1,13 +1,12 @@ package opensavvy.prepared.runner.kotest +import io.kotest.core.coroutines.coroutineTestScope import io.kotest.core.names.TestName import io.kotest.core.spec.KotestTestScope import io.kotest.core.spec.style.StringSpec import io.kotest.core.spec.style.scopes.RootScope import io.kotest.core.spec.style.scopes.addTest import io.kotest.core.test.TestType -import kotlinx.coroutines.test.TestScope -import kotlinx.coroutines.withContext import opensavvy.prepared.suite.SuiteDsl import opensavvy.prepared.suite.TestDsl import opensavvy.prepared.suite.config.* @@ -69,10 +68,7 @@ private class NonNestedSuite(private val root: RootScope, private val parentConf ) root.addTest(testName = TestName(name = prefix child name), disabled = false, type = TestType.Test, config = kotestConfig) { - val scope = this as TestScope // TODO: access the TestScope - withContext(context) { - scope.runTestDslSuspend(name, context, config, block) - } + coroutineTestScope.runTestDslSuspend(name, context, config, block) } } } -- 2.51.2