diff --git a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtModuleFactory.kt b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtModuleFactory.kt index cc7978e4536..025472c5541 100644 --- a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtModuleFactory.kt +++ b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtModuleFactory.kt @@ -25,6 +25,11 @@ private val TestServices.ktModuleFactory: KtModuleFactory by TestServices.testSe * * By default, the [KtModuleFactory] registered with these [TestServices] is returned. It may be overruled by the * [MODULE_KIND][org.jetbrains.kotlin.analysis.test.framework.AnalysisApiTestDirectives.MODULE_KIND] directive for a specific test module. + * + * [DependencyKindModuleStructureTransformer][org.jetbrains.kotlin.analysis.test.framework.services.DependencyKindModuleStructureTransformer] + * should be used to properly set up [DependencyKind][org.jetbrains.kotlin.test.model.DependencyKind] for module dependencies + * + * @see org.jetbrains.kotlin.analysis.test.framework.services.DependencyKindModuleStructureTransformer */ fun TestServices.getKtModuleFactoryForTestModule(testModule: TestModule): KtModuleFactory = when (testModule.moduleKind) { TestModuleKind.Source -> KtSourceModuleFactory diff --git a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/services/DependencyKindModuleStructureTransformer.kt b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/services/DependencyKindModuleStructureTransformer.kt new file mode 100644 index 00000000000..4f0a13995b4 --- /dev/null +++ b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/services/DependencyKindModuleStructureTransformer.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2010-2023 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.analysis.test.framework.services + +import org.jetbrains.kotlin.analysis.test.framework.AnalysisApiTestDirectives +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.moduleKind +import org.jetbrains.kotlin.test.TestInfrastructureInternals +import org.jetbrains.kotlin.test.model.DependencyDescription +import org.jetbrains.kotlin.test.model.DependencyKind +import org.jetbrains.kotlin.test.model.TestModule +import org.jetbrains.kotlin.test.services.DefaultsProvider +import org.jetbrains.kotlin.test.services.ModuleStructureTransformer +import org.jetbrains.kotlin.test.services.TestModuleStructure +import org.jetbrains.kotlin.test.services.impl.TestModuleStructureImpl + +/** + * This transformer is required for correct work of [getKtModuleFactoryForTestModule][org.jetbrains.kotlin.analysis.test.framework.project.structure.getKtModuleFactoryForTestModule] + * to provide correct [DependencyKind] for dependencies + * + * @see org.jetbrains.kotlin.analysis.test.framework.project.structure.getKtModuleFactoryForTestModule + */ +@OptIn(TestInfrastructureInternals::class) +object DependencyKindModuleStructureTransformer : ModuleStructureTransformer() { + override fun transformModuleStructure(moduleStructure: TestModuleStructure, defaultsProvider: DefaultsProvider): TestModuleStructure { + if (AnalysisApiTestDirectives.MODULE_KIND !in moduleStructure.allDirectives) return moduleStructure + + val moduleMapping = moduleStructure.modules.associateBy(TestModule::name) + return TestModuleStructureImpl( + moduleStructure.modules.map { module -> + module.copy( + allDependencies = module.allDependencies.map { dependency -> + transformDependency(dependency, moduleMapping) + } + ) + }, + moduleStructure.originalTestDataFiles, + ) + } + + private fun transformDependency( + dependency: DependencyDescription, + moduleMapping: Map, + ): DependencyDescription { + val dependencyModule = moduleMapping.getValue(dependency.moduleName) + val newKind = when (dependencyModule.moduleKind) { + TestModuleKind.Source, TestModuleKind.LibrarySource, TestModuleKind.ScriptSource -> DependencyKind.Source + TestModuleKind.LibraryBinary -> DependencyKind.Binary + // There is no explicit module kind, so the dependency already has the right kind + null -> return dependency + } + + return dependency.copy(kind = newKind) + } +} diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirLibraryBinaryTestConfigurator.kt b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirLibraryBinaryTestConfigurator.kt index b3092718a74..3e7db043686 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirLibraryBinaryTestConfigurator.kt +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirLibraryBinaryTestConfigurator.kt @@ -30,6 +30,12 @@ object AnalysisApiFirLibraryBinaryTestConfigurator : AnalysisApiTestConfigurator override fun configureTest(builder: TestConfigurationBuilder, disposable: Disposable) { builder.apply { useAdditionalService { KtLibraryBinaryModuleFactory } + configureLibraryCompilationSupport(this) + } + } + + fun configureLibraryCompilationSupport(builder: TestConfigurationBuilder) { + builder.apply { useAdditionalService { DispatchingTestModuleCompiler() } useAdditionalService { TestModuleDecompilerJar() } } diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirSourceTestConfigurator.kt b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirSourceTestConfigurator.kt index 8be36951100..608303dfac5 100644 --- a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirSourceTestConfigurator.kt +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirSourceTestConfigurator.kt @@ -1,13 +1,17 @@ /* - * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Copyright 2010-2023 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.analysis.low.level.api.fir.test.configurators import com.intellij.openapi.Disposable +import org.jetbrains.kotlin.analysis.api.impl.base.test.configurators.AnalysisApiLibraryBaseTestServiceRegistrar import org.jetbrains.kotlin.analysis.test.framework.project.structure.KtModuleFactory import org.jetbrains.kotlin.analysis.test.framework.project.structure.KtSourceModuleFactory +import org.jetbrains.kotlin.analysis.test.framework.services.DependencyKindModuleStructureTransformer +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestServiceRegistrar +import org.jetbrains.kotlin.test.TestInfrastructureInternals import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder open class AnalysisApiFirSourceTestConfigurator( @@ -18,6 +22,14 @@ open class AnalysisApiFirSourceTestConfigurator( builder.apply { useAdditionalService { KtSourceModuleFactory } + + @OptIn(TestInfrastructureInternals::class) + useModuleStructureTransformers(DependencyKindModuleStructureTransformer) } + + AnalysisApiFirLibraryBinaryTestConfigurator.configureLibraryCompilationSupport(builder) } -} \ No newline at end of file + + override val serviceRegistrars: List + get() = super.serviceRegistrars + AnalysisApiLibraryBaseTestServiceRegistrar +}