[AA] Tests: Remove decompiled files from LibraryBinary and add LibraryBinaryDecompiled

- `LibraryBinary` should not contain any decompiled files, as we want
  FIR symbols in tests to be provided from indexed stubs or class files,
  but definitely not from decompiled PSI. This brings `LibraryBinary`
  much closer to the behavior of binary libraries in the IDE.
- Some tests may still require access to a decompiled file, for example
  when trying to test `getOrBuildFir` for some `KtElement` coming from a
  library. This commit introduces `LibraryBinaryDecompiled`, which
  does contain decompiled files.
- We don't really need `LibraryBinary` as a main test module kind
  anymore, since tests generally want to access some main `KtFile`.
  Hence, test configurators for `LibraryBinary` have been turned into
  configurators for `LibraryBinaryDecompiled`.
- An alternative would be decompiling files on demand, but this is not
  currently feasible because the Standalone API doesn't reconcile stubs
  with decompiled PSI, like the IDE does automatically. (For the same
  declaration, the stub and the PSI will have a different identity.) As
  long as there is no support for this, we'll have to rely on a separate
  test module kind.

^KT-65960
This commit is contained in:
Marco Pennekamp
2024-02-20 22:41:10 +01:00
committed by Space Team
parent 7baaa38b8a
commit 78ef58bef4
21 changed files with 126 additions and 180 deletions
@@ -0,0 +1,24 @@
/*
* Copyright 2010-2024 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.PsiFile
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.testModuleDecompiler
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind
import org.jetbrains.kotlin.test.services.TestServices
import java.nio.file.Path
/**
* @see TestModuleKind.LibraryBinaryDecompiled
*/
object KtLibraryBinaryDecompiledModuleFactory : KtLibraryBinaryModuleFactoryBase() {
override val testModuleKind: TestModuleKind
get() = TestModuleKind.LibraryBinaryDecompiled
override fun decompileToPsiFiles(binaryRoot: Path, testServices: TestServices, project: Project): List<PsiFile> =
testServices.testModuleDecompiler.getAllPsiFilesFromLibrary(binaryRoot, project)
}
@@ -6,44 +6,17 @@
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.StandaloneProjectFactory
import org.jetbrains.kotlin.analysis.test.framework.services.environmentManager
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.compiledLibraryProvider
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.testModuleDecompiler
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import java.nio.file.Path
/**
* @see TestModuleKind.LibraryBinary
*/
object KtLibraryBinaryModuleFactory : KtModuleFactory {
override fun createModule(
testModule: TestModule,
contextModule: KtTestModule?,
dependencyPaths: Collection<Path>,
testServices: TestServices,
project: Project,
): KtTestModule {
val library = testServices.compiledLibraryProvider.compileToLibrary(testModule, dependencyPaths).artifact
val decompiledFiles = testServices.testModuleDecompiler.getAllPsiFilesFromLibrary(library, project)
object KtLibraryBinaryModuleFactory : KtLibraryBinaryModuleFactoryBase() {
override val testModuleKind: TestModuleKind
get() = TestModuleKind.LibraryBinary
return KtTestModule(
TestModuleKind.LibraryBinary,
testModule,
KtLibraryModuleImpl(
testModule.name,
testModule.targetPlatform,
StandaloneProjectFactory.createSearchScopeByLibraryRoots(
listOf(library),
testServices.environmentManager.getProjectEnvironment(),
),
project,
binaryRoots = listOf(library),
librarySources = null,
),
decompiledFiles,
)
}
override fun decompileToPsiFiles(binaryRoot: Path, testServices: TestServices, project: Project): List<PsiFile> = emptyList()
}
@@ -0,0 +1,50 @@
/*
* Copyright 2010-2024 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.PsiFile
import org.jetbrains.kotlin.analysis.api.standalone.base.project.structure.StandaloneProjectFactory
import org.jetbrains.kotlin.analysis.test.framework.services.environmentManager
import org.jetbrains.kotlin.analysis.test.framework.services.libraries.compiledLibraryProvider
import org.jetbrains.kotlin.analysis.test.framework.test.configurators.TestModuleKind
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestServices
import java.nio.file.Path
abstract class KtLibraryBinaryModuleFactoryBase : KtModuleFactory {
protected abstract val testModuleKind: TestModuleKind
protected abstract fun decompileToPsiFiles(binaryRoot: Path, testServices: TestServices, project: Project): List<PsiFile>
override fun createModule(
testModule: TestModule,
contextModule: KtTestModule?,
testServices: TestServices,
project: Project,
dependencyPaths: Collection<Path>,
): KtTestModule {
val binaryRoot = testServices.compiledLibraryProvider.compileToLibrary(testModule).artifact
val decompiledFiles = decompileToPsiFiles(binaryRoot, testServices, project)
return KtTestModule(
testModuleKind,
testModule,
KtLibraryModuleImpl(
testModule.name,
testModule.targetPlatform,
StandaloneProjectFactory.createSearchScopeByLibraryRoots(
listOf(binaryRoot),
testServices.environmentManager.getProjectEnvironment(),
),
project,
binaryRoots = listOf(binaryRoot),
librarySources = null,
),
decompiledFiles,
)
}
}
@@ -49,6 +49,7 @@ private val TestServices.ktModuleFactory: KtModuleFactory by TestServices.testSe
fun TestServices.getKtModuleFactoryForTestModule(testModule: TestModule): KtModuleFactory = when (testModule.explicitTestModuleKind) {
TestModuleKind.Source -> KtSourceModuleFactory
TestModuleKind.LibraryBinary -> KtLibraryBinaryModuleFactory
TestModuleKind.LibraryBinaryDecompiled -> KtLibraryBinaryDecompiledModuleFactory
TestModuleKind.LibrarySource -> KtLibrarySourceModuleFactory
TestModuleKind.ScriptSource -> KtScriptModuleFactory
TestModuleKind.CodeFragment -> KtCodeFragmentModuleFactory
@@ -54,7 +54,8 @@ object DependencyKindModuleStructureTransformer : ModuleStructureTransformer() {
DependencyKind.Source
}
TestModuleKind.LibraryBinary -> {
TestModuleKind.LibraryBinary,
TestModuleKind.LibraryBinaryDecompiled -> {
DependencyKind.Binary
}
@@ -29,6 +29,18 @@ enum class TestModuleKind(val suffix: String) {
*/
LibraryBinary("LibraryBinary"),
/**
* A binary library with PSI files decompiled from the library's class files. Instead of building and indexing stubs (if applicable),
* the test's declaration provider will instead index the decompiled PSI files directly.
*
* [LibraryBinaryDecompiled] should be specified when tests access the library's files as test files, usually as a main file in a main
* module. See [AbstractAnalysisApiBasedTest][org.jetbrains.kotlin.analysis.test.framework.base.AbstractAnalysisApiBasedTest] for an
* overview of "main module" and "main file".
*
* @see org.jetbrains.kotlin.analysis.test.framework.project.structure.KtLibraryBinaryDecompiledModuleFactory
*/
LibraryBinaryDecompiled("LibraryBinaryDecompiled"),
/**
* @see org.jetbrains.kotlin.analysis.test.framework.project.structure.KtLibrarySourceModuleFactory
*/