[kpm] kgp-idea: Implement CompatibilityTestUtil.kt

^KT-52568 In Progress
This commit is contained in:
sebastian.sellmair
2022-06-13 14:32:49 +02:00
committed by Space
parent faaec4689a
commit f52b4550d8
2 changed files with 77 additions and 1 deletions
@@ -0,0 +1,62 @@
/*
* Copyright 2010-2022 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.gradle.kpm.idea.testUtils
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmProject
import org.jetbrains.kotlin.gradle.kpm.idea.serialize.IdeaKpmSerializationLogger
import org.jetbrains.kotlin.gradle.kpm.idea.testFixtures.TestIdeaKpmClassLoaderProjectSerializer
import java.io.File
import java.net.URLClassLoader
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertSame
/*
* Copyright 2010-2022 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.
*/
fun classLoaderForBackwardsCompatibleClasses(): ClassLoader {
val uris = classpathForBackwardsCompatibleClasses().map { file -> file.toURI().toURL() }.toTypedArray()
return URLClassLoader.newInstance(uris, null)
}
fun classpathForBackwardsCompatibleClasses(): List<File> {
val compatibilityTestClasspath = System.getProperty("compatibilityTestClasspath")
?: error("Missing compatibilityTestClasspath system property")
return compatibilityTestClasspath.split(";").map { path -> File(path) }
.onEach { file -> if (!file.exists()) println("[WARNING] Missing $file") }
.flatMap { file -> if (file.isDirectory) file.listFiles().orEmpty().toList() else listOf(file) }
}
fun deserializeIdeaKpmProjectWithBackwardsCompatibleClasses(project: IdeaKpmProject): Any {
return deserializeIdeaKpmProjectWithBackwardsCompatibleClasses(
TestIdeaKpmClassLoaderProjectSerializer().serialize(project)
)
}
fun deserializeIdeaKpmProjectWithBackwardsCompatibleClasses(project: ByteArray): Any {
val classLoader = classLoaderForBackwardsCompatibleClasses()
val serializer = TestIdeaKpmClassLoaderProjectSerializer(classLoader)
val deserialized = assertNotNull(
serializer.deserialize(project),
"Failed to deserialize project: ${serializer.reports}"
)
assertEquals(
0, serializer.reports.count { it.severity > IdeaKpmSerializationLogger.Severity.WARNING },
"Expected no severe deserialization reports. Found ${serializer.reports}"
)
assertSame(
classLoader, deserialized::class.java.classLoader,
"Expected model do be deserialized in with old classes"
)
return deserialized
}
@@ -1,3 +1,10 @@
/*
* Copyright 2010-2022 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.gradle.kpm.idea.testUtils
import org.gradle.api.Project
import org.gradle.api.artifacts.verification.DependencyVerificationMode
import org.gradle.api.internal.project.ProjectInternal
@@ -5,6 +12,8 @@ import org.gradle.configurationcache.extensions.serviceOf
import org.gradle.testfixtures.ProjectBuilder
import org.gradle.tooling.provider.model.ToolingModelBuilderRegistry
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmProject
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmProjectBinaryContainer
import org.jetbrains.kotlin.gradle.kpm.idea.IdeaKpmProjectContainer
import org.jetbrains.kotlin.gradle.plugin.KotlinPm20PluginWrapper
import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinPm20ProjectExtension
@@ -13,11 +22,16 @@ import org.jetbrains.kotlin.gradle.plugin.mpp.pm20.KotlinPm20ProjectExtension
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
fun Project.buildIdeaKpmProjectModel(): IdeaKpmProject {
fun Project.buildIdeaKpmProject(): IdeaKpmProject {
return serviceOf<ToolingModelBuilderRegistry>().getBuilder(IdeaKpmProject::class.java.name)
.buildAll(IdeaKpmProject::class.java.name, this) as IdeaKpmProject
}
fun Project.buildIdeaKpmProjectBinary(): IdeaKpmProjectBinaryContainer {
return serviceOf<ToolingModelBuilderRegistry>().getBuilder(IdeaKpmProjectContainer::class.java.name)
.buildAll(IdeaKpmProjectContainer::class.java.name, this) as IdeaKpmProjectBinaryContainer
}
fun createKpmProject(): Pair<ProjectInternal, KotlinPm20ProjectExtension> {
val project = ProjectBuilder.builder().build() as ProjectInternal
project.plugins.apply(KotlinPm20PluginWrapper::class.java)