[Analysis API] [Test Infra] move (de)compilation services

move (de)compilation services from static dependencies into DI
This is done for allowing different (de)compile strategies in the future


Merge-request: KT-MR-13213
Merged-by: Artem Olkov <artem.olkov@jetbrains.com>
This commit is contained in:
Artem Olkov
2023-11-24 10:58:49 +00:00
committed by Space Team
parent 88bbe8d92d
commit 54c2339dfb
5 changed files with 68 additions and 24 deletions
@@ -17,15 +17,17 @@ class CompiledLibraryProvider(private val testServices: TestServices) : TestServ
if (module.name in libraries) {
error("Library for module ${module.name} is already compiled")
}
val libraryJar = TestModuleCompiler.compileTestModuleToLibrary(module, testServices)
val librarySourcesJar = TestModuleCompiler.compileTestModuleToLibrarySources(module, testServices)
val libraryJar = testServices.testModuleCompiler.compileTestModuleToLibrary(module, testServices)
val librarySourcesJar = testServices.testModuleCompiler.compileTestModuleToLibrarySources(module, testServices)
return CompiledLibrary(libraryJar, librarySourcesJar).also { libraries[module.name] = it }
}
}
val TestServices.compiledLibraryProvider: CompiledLibraryProvider by TestServices.testServiceAccessor()
val TestServices.testModuleCompiler: TestModuleCompiler by TestServices.testServiceAccessor()
data class CompiledLibrary(
val jar: Path,
val sourcesJar: Path,
)
val artifact: Path,
val sources: Path?,
)
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.analysis.test.framework.services.libraries
import org.jetbrains.kotlin.test.model.TestModule
import org.jetbrains.kotlin.test.services.TestService
import org.jetbrains.kotlin.test.services.TestServices
import org.jetbrains.kotlin.test.services.sourceFileProvider
import org.jetbrains.kotlin.test.util.KtTestUtil
@@ -20,8 +21,7 @@ import kotlin.io.path.div
import kotlin.io.path.outputStream
import kotlin.io.path.writeText
internal object TestModuleCompiler {
abstract class TestModuleCompiler : TestService {
fun compileTestModuleToLibrary(module: TestModule, testServices: TestServices): Path {
val tmpDir = KtTestUtil.tmpDir("testSourcesToCompile").toPath()
for (testFile in module.files) {
@@ -29,14 +29,28 @@ internal object TestModuleCompiler {
val tmpSourceFile = (tmpDir / testFile.name).createFile()
tmpSourceFile.writeText(text)
}
return CompilerExecutor.compileLibrary(
tmpDir,
CompilerExecutor.parseCompilerOptionsFromTestdata(module),
compilationErrorExpected = CompilerExecutor.Directives.COMPILATION_ERRORS in module.directives
)
return compile(tmpDir, module)
}
fun compileTestModuleToLibrarySources(module: TestModule, testServices: TestServices): Path {
abstract fun compile(tmpDir: Path, module: TestModule): Path
abstract fun compileTestModuleToLibrarySources(module: TestModule, testServices: TestServices): Path?
}
class TestModuleCompilerJar : TestModuleCompiler() {
override fun compile(tmpDir: Path, module: TestModule): Path = CompilerExecutor.compileLibrary(
tmpDir,
CompilerExecutor.parseCompilerOptionsFromTestdata(module),
compilationErrorExpected = CompilerExecutor.Directives.COMPILATION_ERRORS in module.directives
)
override fun compileTestModuleToLibrarySources(module: TestModule, testServices: TestServices): Path {
fun addFileToJar(path: String, text: String, jarOutputStream: JarOutputStream) {
jarOutputStream.putNextEntry(JarEntry(path))
ByteArrayInputStream(text.toByteArray()).copyTo(jarOutputStream)
jarOutputStream.closeEntry()
}
val tmpDir = KtTestUtil.tmpDir("testSourcesToCompile").toPath()
val librarySourcesPath = tmpDir / "library-sources.jar"
val manifest = Manifest().apply { mainAttributes[Attributes.Name.MANIFEST_VERSION] = "1.0" }
@@ -48,10 +62,4 @@ internal object TestModuleCompiler {
}
return librarySourcesPath
}
private fun addFileToJar(path: String, text: String, jarOutputStream: JarOutputStream) {
jarOutputStream.putNextEntry(JarEntry(path))
ByteArrayInputStream(text.toByteArray()).copyTo(jarOutputStream)
jarOutputStream.closeEntry()
}
}
}
@@ -0,0 +1,24 @@
/*
* 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.libraries
import com.intellij.openapi.project.Project
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.analysis.api.impl.base.util.LibraryUtils
import org.jetbrains.kotlin.test.services.TestService
import org.jetbrains.kotlin.test.services.TestServices
import java.nio.file.Path
interface TestModuleDecompiler : TestService {
fun getAllPsiFilesFromLibrary(artifact: Path, project: Project): List<PsiFile>
}
val TestServices.testModuleDecompiler: TestModuleDecompiler by TestServices.testServiceAccessor()
class TestModuleDecompilerJar : TestModuleDecompiler {
override fun getAllPsiFilesFromLibrary(artifact: Path, project: Project): List<PsiFile> =
LibraryUtils.getAllPsiFilesFromJar(artifact, project)
}