From ce44a2a7e4706a8c04171b9e3e48a9bfd7214cae Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 12 Jan 2021 11:46:14 +0300 Subject: [PATCH] [Test] Inject info about test (class and method names, tags) to TestServices --- .../kotlin/test/services/KotlinTestInfo.kt | 14 ++++++++++++ .../test/builders/TestConfigurationBuilder.kt | 12 ++++++++++ .../handlers/FirTestDataConsistencyHandler.kt | 2 ++ .../kotlin/test/impl/TestConfigurationImpl.kt | 12 ++++++++-- .../runners/AbstractKotlinCompilerTest.kt | 22 +++++++++++++++++++ 5 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/KotlinTestInfo.kt diff --git a/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/KotlinTestInfo.kt b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/KotlinTestInfo.kt new file mode 100644 index 00000000000..48261c752e1 --- /dev/null +++ b/compiler/test-infrastructure/tests/org/jetbrains/kotlin/test/services/KotlinTestInfo.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.test.services + +data class KotlinTestInfo( + val className: String, + val methodName: String, + val tags: Set +) : TestService + +val TestServices.testInfo: KotlinTestInfo by TestServices.testServiceAccessor() diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt index a8b47bf5ea8..c1b0c0001a8 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/builders/TestConfigurationBuilder.kt @@ -36,6 +36,17 @@ class TestConfigurationBuilder { val defaultRegisteredDirectivesBuilder: RegisteredDirectivesBuilder = RegisteredDirectivesBuilder() private val configurationsByTestDataCondition: MutableList Unit>> = mutableListOf() + private val additionalServices: MutableList = mutableListOf() + + lateinit var testInfo: KotlinTestInfo + + inline fun useAdditionalService(noinline serviceConstructor: (TestServices) -> T) { + useAdditionalService(service(serviceConstructor)) + } + + fun useAdditionalService(serviceRegistrationData: ServiceRegistrationData) { + additionalServices += serviceRegistrationData + } fun forTestsMatching(pattern: String, configuration: TestConfigurationBuilder.() -> Unit) { val regex = pattern.toMatchingRegexString().toRegex() @@ -127,6 +138,7 @@ class TestConfigurationBuilder { } } return TestConfigurationImpl( + testInfo, defaultsProviderBuilder.build(), assertions, facades, diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt index f98051b3ab3..fc1628bf56a 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/frontend/classic/handlers/FirTestDataConsistencyHandler.kt @@ -13,6 +13,7 @@ import org.jetbrains.kotlin.test.runners.AbstractFirDiagnosticTest import org.jetbrains.kotlin.test.services.TestServices import org.jetbrains.kotlin.test.services.assertions import org.jetbrains.kotlin.test.services.moduleStructure +import org.jetbrains.kotlin.test.services.testInfo import org.jetbrains.kotlin.test.utils.firTestDataFile import java.io.File @@ -41,6 +42,7 @@ class FirTestDataConsistencyHandler(testServices: TestServices) : AfterAnalysisC private fun runFirTestAndGeneratedTestData(testData: File, firTestData: File) { firTestData.writeText(clearTextFromDiagnosticMarkup(testData.readText())) val test = object : AbstractFirDiagnosticTest() {} + test.initTestInfo(testServices.testInfo.copy(className = "${testServices.testInfo.className}_fir_anonymous")) test.runTest(firTestData.absolutePath) } } 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 fa8f5cb9c65..1af0b7c00c3 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 @@ -17,6 +17,8 @@ import org.jetbrains.kotlin.test.services.impl.ModuleStructureExtractorImpl import org.jetbrains.kotlin.test.utils.TestDisposable class TestConfigurationImpl( + testInfo: KotlinTestInfo, + defaultsProvider: DefaultsProvider, assertions: AssertionsService, @@ -35,11 +37,17 @@ class TestConfigurationImpl( override val metaInfoHandlerEnabled: Boolean, directives: List, - override val defaultRegisteredDirectives: RegisteredDirectives + override val defaultRegisteredDirectives: RegisteredDirectives, + additionalServices: List ) : TestConfiguration() { override val rootDisposable: Disposable = TestDisposable() override val testServices: TestServices = TestServices() + init { + testServices.register(KotlinTestInfo::class, testInfo) + additionalServices.forEach { testServices.register(it) } + } + private val allDirectives = directives.toMutableSet() override val directives: DirectivesContainer by lazy { when (allDirectives.size) { @@ -81,7 +89,7 @@ class TestConfigurationImpl( testServices.apply { @OptIn(ExperimentalStdlibApi::class) val sourceFilePreprocessors = sourcePreprocessors.map { it.invoke(this@apply) } - val sourceFileProvider = SourceFileProviderImpl(sourceFilePreprocessors) + val sourceFileProvider = SourceFileProviderImpl(this, sourceFilePreprocessors) register(SourceFileProvider::class, sourceFileProvider) val environmentProvider = CompilerConfigurationProviderImpl(rootDisposable, this@TestConfigurationImpl.environmentConfigurators) diff --git a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt index 32ceb70a10d..fb04a27b64e 100644 --- a/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt +++ b/compiler/tests-common-new/tests/org/jetbrains/kotlin/test/runners/AbstractKotlinCompilerTest.kt @@ -14,8 +14,11 @@ import org.jetbrains.kotlin.test.directives.LanguageSettingsDirectives import org.jetbrains.kotlin.test.preprocessors.MetaInfosCleanupPreprocessor import org.jetbrains.kotlin.test.services.JUnit5Assertions import org.jetbrains.kotlin.test.services.SourceFilePreprocessor +import org.jetbrains.kotlin.test.services.KotlinTestInfo import org.jetbrains.kotlin.types.AbstractTypeChecker import org.jetbrains.kotlin.types.FlexibleTypeImpl +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.TestInfo abstract class AbstractKotlinCompilerTest { companion object { @@ -43,8 +46,27 @@ abstract class AbstractKotlinCompilerTest { } abstract fun TestConfigurationBuilder.configuration() + private lateinit var testInfo: KotlinTestInfo + + @BeforeEach + fun initTestInfo(testInfo: TestInfo) { + initTestInfo( + KotlinTestInfo( + className = testInfo.testClass.orElseGet(null)?.name ?: "_undefined_", + methodName = testInfo.testMethod.orElseGet(null)?.name ?: "_testUndefined_", + tags = testInfo.tags + ) + ) + } + + fun initTestInfo(testInfo: KotlinTestInfo) { + this.testInfo = testInfo + } open fun configure(builder: TestConfigurationBuilder) { + with(builder) { + testInfo = this@AbstractKotlinCompilerTest.testInfo + } builder.configuration() }