From cb3f41f6697b8953abe5ad1c305ecdb9485b2b87 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 10 Jan 2024 10:52:07 +0200 Subject: [PATCH] [Test] Don't override manually registered services during test configuration There are two ways of test service registration: - manual call to `useAdditionalServices` in abstract test runner - using of `additionalServices` property in one of test entity (e.g. in handler) Services are registered in the same order (manual first, automatic second) This led to the situation that if there was registered more specific implementation of the service in the test configuration, it will be overridden with regular implementation from handler, which is fixed by this commit --- .../org/jetbrains/kotlin/test/services/TestServices.kt | 9 ++++++--- .../jetbrains/kotlin/test/impl/TestConfigurationImpl.kt | 6 ++++-- .../src/org/jetbrains/kotlin/util/ComponentArrayOwner.kt | 8 +++++++- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/TestServices.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/TestServices.kt index 38a90eb9506..3fe2f4677b3 100644 --- a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/TestServices.kt +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/TestServices.kt @@ -39,7 +39,10 @@ class TestServices : ComponentArrayOwner(){ } } - fun register(data: ServiceRegistrationData) { + fun register(data: ServiceRegistrationData, skipAlreadyRegistered: Boolean) { + if (skipAlreadyRegistered && getOrNull(data.kClass) != null) { + return + } registerComponent(data.kClass, data.serviceConstructor(this)) } @@ -47,8 +50,8 @@ class TestServices : ComponentArrayOwner(){ registerComponent(kClass, service) } - fun register(data: List) { - data.forEach { register(it) } + fun register(data: List, skipAlreadyRegistered: Boolean) { + data.forEach { register(it, skipAlreadyRegistered) } } } diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt index f281010ae7d..f599ab0db3c 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/impl/TestConfigurationImpl.kt @@ -58,7 +58,9 @@ class TestConfigurationImpl( testServices.register(KotlinTestInfo::class, testInfo) val runtimeClassPathProviders = runtimeClasspathProviders.map { it.invoke(testServices) } testServices.register(RuntimeClasspathProvidersContainer::class, RuntimeClasspathProvidersContainer(runtimeClassPathProviders)) - additionalServices.forEach { testServices.register(it) } + additionalServices.forEach { + testServices.register(it, skipAlreadyRegistered = false) + } } private val allDirectives = directives.toMutableSet() @@ -131,7 +133,7 @@ class TestConfigurationImpl( private fun ServicesAndDirectivesContainer.registerDirectivesAndServices() { allDirectives += directiveContainers - testServices.register(additionalServices) + testServices.register(additionalServices, skipAlreadyRegistered = true) } private fun List.registerDirectivesAndServices() { diff --git a/core/compiler.common/src/org/jetbrains/kotlin/util/ComponentArrayOwner.kt b/core/compiler.common/src/org/jetbrains/kotlin/util/ComponentArrayOwner.kt index 67cab26709e..cf5c28ef9eb 100644 --- a/core/compiler.common/src/org/jetbrains/kotlin/util/ComponentArrayOwner.kt +++ b/core/compiler.common/src/org/jetbrains/kotlin/util/ComponentArrayOwner.kt @@ -25,8 +25,14 @@ abstract class ComponentArrayOwner : AbstractArrayMapOwner): V { + getOrNull(key)?.let { return it } val id = typeRegistry.getId(key) - return arrayMap[id] ?: error("No '$key'($id) component in array: $this") + error("No '$key'($id) component in array: $this") + } + + protected fun getOrNull(key: KClass): V? { + val id = typeRegistry.getId(key) + return arrayMap[id] } private fun createDiagnosticMessage(id: Int, keyQualifiedName: String): String = buildString {