[Gradle] KotlinPluginLifecycle: requiredStage: Wait for stage if necessary

^KT-58676 In Progress
This commit is contained in:
Sebastian Sellmair
2023-06-21 10:56:07 +02:00
committed by Space Team
parent 1a143ec1a2
commit b9a8f6dce2
2 changed files with 12 additions and 0 deletions
@@ -191,8 +191,10 @@ internal suspend fun <T : Any> Property<T>.awaitFinalValue(): T? {
* See also [withRestrictedStages]
*
* Will ensure that the given [block] can only execute in the given [stage]
* Will wait for the given [stage] if not arrived yet
*/
internal suspend fun <T> requiredStage(stage: Stage, block: suspend () -> T): T {
if (currentKotlinPluginLifecycle().stage < stage) stage.await()
return withRestrictedStages(hashSetOf(stage), block)
}
@@ -458,6 +458,16 @@ class KotlinPluginLifecycleTest {
}
}
@Test
fun `test - requireStage`() = project.runLifecycleAwareTest {
launch {
requiredStage(AfterFinaliseDsl) {
assertEquals(AfterFinaliseDsl, stage)
assertFailsWith<IllegalLifecycleException> { AfterFinaliseDsl.nextOrThrow.await() }
}
}
}
@Test
fun `test - withRestrictedStages`() = project.runLifecycleAwareTest {
launch {