diff --git a/analysis/analysis-api-impl-base/tests/org/jetbrains/kotlin/analysis/api/impl/base/test/configurators/AnalysisApiScriptTestServiceRegistrar.kt b/analysis/analysis-api-impl-base/tests/org/jetbrains/kotlin/analysis/api/impl/base/test/configurators/AnalysisApiScriptTestServiceRegistrar.kt new file mode 100644 index 00000000000..6302149ab8f --- /dev/null +++ b/analysis/analysis-api-impl-base/tests/org/jetbrains/kotlin/analysis/api/impl/base/test/configurators/AnalysisApiScriptTestServiceRegistrar.kt @@ -0,0 +1,28 @@ +/* + * 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.api.impl.base.test.configurators + +import com.intellij.mock.MockApplication +import com.intellij.mock.MockProject +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestServiceRegistrar +import org.jetbrains.kotlin.scripting.compiler.plugin.definitions.CliScriptDefinitionProvider +import org.jetbrains.kotlin.scripting.definitions.ScriptDefinitionProvider +import org.jetbrains.kotlin.test.services.TestServices + +object AnalysisApiScriptTestServiceRegistrar : AnalysisApiTestServiceRegistrar() { + override fun registerProjectExtensionPoints(project: MockProject, testServices: TestServices) { + } + + override fun registerProjectServices(project: MockProject, testServices: TestServices) { + } + + override fun registerProjectModelServices(project: MockProject, testServices: TestServices) { + project.registerService(ScriptDefinitionProvider::class.java, CliScriptDefinitionProvider()) + } + + override fun registerApplicationServices(application: MockApplication, testServices: TestServices) { + } +} diff --git a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/AnalysisApiKtModuleProviderImpl.kt b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/AnalysisApiKtModuleProviderImpl.kt index da61de08bf3..178ff9b4597 100644 --- a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/AnalysisApiKtModuleProviderImpl.kt +++ b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/AnalysisApiKtModuleProviderImpl.kt @@ -1,5 +1,5 @@ /* - * 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. */ @@ -34,7 +34,8 @@ class AnalysisApiKtModuleProviderImpl( return modulesByName.getValue(moduleName).ktModule } - override fun getModuleFiles(module: TestModule): List = modulesByName.getValue(module.name).files + override fun getModuleFiles(module: TestModule): List = + (modulesByName[module.name] ?: modulesByName.getValue(module.files.single().name)).files override fun registerProjectStructure(modules: KtModuleProjectStructure) { require(!this::modulesStructure.isInitialized) @@ -58,6 +59,7 @@ fun List.associateByName(): Map { is KtSdkModule -> ktModule.sdkName is KtBuiltinsModule -> "Builtins for ${ktModule.platform}" is KtNotUnderContentRootModule -> ktModule.name + is KtScriptModule -> ktModule.file.name else -> error("Unsupported module type: " + ktModule.javaClass.name) } } diff --git a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtScriptModuleFactory.kt b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtScriptModuleFactory.kt new file mode 100644 index 00000000000..5b964c93ace --- /dev/null +++ b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtScriptModuleFactory.kt @@ -0,0 +1,26 @@ +/* + * 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.project.structure + +import com.intellij.openapi.project.Project +import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.KtModuleWithFiles +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.test.model.TestModule +import org.jetbrains.kotlin.test.services.TestServices + +class KtScriptModuleFactory : KtModuleFactory { + override fun createModule(testModule: TestModule, testServices: TestServices, project: Project): KtModuleWithFiles { + val ktFile = TestModuleStructureFactory.createSourcePsiFiles(testModule, testServices, project).single() as KtFile + val module = KtScriptModuleImpl( + ktFile, + testModule.targetPlatform, + testModule.languageVersionSettings, + project, + ) + + return KtModuleWithFiles(module, listOf(ktFile)) + } +} diff --git a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtScriptModuleImpl.kt b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtScriptModuleImpl.kt new file mode 100644 index 00000000000..190bdd1fa1f --- /dev/null +++ b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/KtScriptModuleImpl.kt @@ -0,0 +1,28 @@ +/* + * 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.project.structure + +import com.intellij.openapi.project.Project +import com.intellij.psi.search.GlobalSearchScope +import org.jetbrains.kotlin.analysis.project.structure.* +import org.jetbrains.kotlin.config.LanguageVersionSettings +import org.jetbrains.kotlin.platform.TargetPlatform +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.PlatformDependentAnalyzerServices +import org.jetbrains.kotlin.test.getAnalyzerServices + +class KtScriptModuleImpl( + override val file: KtFile, + override val platform: TargetPlatform, + override val languageVersionSettings: LanguageVersionSettings, + override val project: Project, +) : KtModuleWithModifiableDependencies(), KtScriptModule { + override val contentScope: GlobalSearchScope get() = GlobalSearchScope.fileScope(file) + override val analyzerServices: PlatformDependentAnalyzerServices get() = platform.getAnalyzerServices() + override val directRegularDependencies: MutableList = mutableListOf() + override val directDependsOnDependencies: MutableList = mutableListOf() + override val directFriendDependencies: MutableList = mutableListOf() +} diff --git a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/TestModuleStructureFactory.kt b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/TestModuleStructureFactory.kt index 08cdb196842..3769289e61e 100644 --- a/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/TestModuleStructureFactory.kt +++ b/analysis/analysis-test-framework/tests/org/jetbrains/kotlin/analysis/test/framework/project/structure/TestModuleStructureFactory.kt @@ -50,7 +50,8 @@ object TestModuleStructureFactory { val binaryModulesBySourceRoots = mutableMapOf, KtBinaryModule>() for (testModule in moduleStructure.modules) { - when (val ktModule = moduleEntriesByName.getValue(testModule.name).ktModule) { + val moduleWithFiles = moduleEntriesByName[testModule.name] ?: moduleEntriesByName.getValue(testModule.files.single().name) + when (val ktModule = moduleWithFiles.ktModule) { is KtNotUnderContentRootModule -> { // Not-under-content-root modules have no external dependencies on purpose } diff --git a/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirScriptTestConfigurator.kt b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirScriptTestConfigurator.kt new file mode 100644 index 00000000000..40e232d7336 --- /dev/null +++ b/analysis/low-level-api-fir/tests/org/jetbrains/kotlin/analysis/low/level/api/fir/test/configurators/AnalysisApiFirScriptTestConfigurator.kt @@ -0,0 +1,25 @@ +/* + * 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.AnalysisApiScriptTestServiceRegistrar +import org.jetbrains.kotlin.analysis.test.framework.project.structure.KtModuleFactory +import org.jetbrains.kotlin.analysis.test.framework.project.structure.KtScriptModuleFactory +import org.jetbrains.kotlin.analysis.test.framework.test.configurators.AnalysisApiTestServiceRegistrar +import org.jetbrains.kotlin.test.builders.TestConfigurationBuilder + +object AnalysisApiFirScriptTestConfigurator : AnalysisApiFirSourceLikeTestConfigurator(analyseInDependentSession = false) { + override fun configureTest(builder: TestConfigurationBuilder, disposable: Disposable) { + super.configureTest(builder, disposable) + + builder.apply { + useAdditionalService { KtScriptModuleFactory() } + } + } + + override val serviceRegistrars: List = super.serviceRegistrars + AnalysisApiScriptTestServiceRegistrar +}