[Gradle] Implement KotlinNativeCompileArgumentsTest for KT-57944

... covering K2 shared native compilations + sources
... covering K2 platform compilations + sources
This commit is contained in:
Sebastian Sellmair
2023-04-19 11:08:20 +02:00
committed by Space Team
parent 101c229335
commit 3b96ab195f
@@ -10,16 +10,17 @@ package org.jetbrains.kotlin.gradle.unitTests.compilerArgumetns
import org.jetbrains.kotlin.cli.common.arguments.K2NativeCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
import org.jetbrains.kotlin.compilerRunner.ArgumentUtils
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.dsl.multiplatformExtension
import org.jetbrains.kotlin.gradle.idea.tcs.extras.sourcesClasspathKey
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext.Companion.default
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilerArgumentsProducer.CreateCompilerArgumentsContext.Companion.lenient
import org.jetbrains.kotlin.gradle.tasks.KotlinNativeCompile
import org.jetbrains.kotlin.gradle.util.buildProjectWithMPP
import org.jetbrains.kotlin.gradle.util.main
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFails
import kotlin.test.assertNull
import java.io.File
import kotlin.test.*
class KotlinNativeCompileArgumentsTest {
@Test
@@ -94,4 +95,114 @@ class KotlinNativeCompileArgumentsTest {
listOf("my.OptIn", "my.other.OptIn"), arguments.optIn?.toList()
)
}
@Test
fun `test - k2 - shared native compilation - sources`() {
val project = buildProjectWithMPP()
val kotlin = project.multiplatformExtension
kotlin.linuxX64()
kotlin.linuxArm64()
/* Enable K2 if necessary */
if (KotlinVersion.DEFAULT < KotlinVersion.KOTLIN_2_0) {
kotlin.sourceSets.all {
it.languageSettings.languageVersion = "2.0"
}
}
val commonMainSourceFile = project.file("src/commonMain/kotlin/CommonMain.kt")
commonMainSourceFile.parentFile.mkdirs()
commonMainSourceFile.writeText("object CommonMain")
project.evaluate()
val sharedNativeCompilation = kotlin.metadata().compilations.getByName("commonMain")
val sharedNativeCompileTask = sharedNativeCompilation.compileTaskProvider.get() as KotlinNativeCompile
val arguments = sharedNativeCompileTask.createCompilerArguments(lenient)
assertTrue(
LanguageVersion.fromVersionString(arguments.languageVersion)!! >= LanguageVersion.KOTLIN_2_0,
"Expected 'languageVersion' 2.0 or higher"
)
assertNull(
arguments.fragments?.toList(),
"Expected 'fragments' to *not* be set: Metadata compilations shall use -Xcommon-sources and provide klib dependencies"
)
assertNull(
arguments.fragmentSources?.toList(),
"Expected 'fragmentSources' to *not* be set: Metadata compilations shall use -Xcommon-sources and provide klib dependencies"
)
assertNull(
arguments.fragmentRefines?.toList(),
"Expected 'fragmentRefines' to *not* be set: Metadata compilations shall use -Xcommon-sources and provide klib dependencies"
)
assertEquals(
listOf(commonMainSourceFile), arguments.commonSources?.toList().orEmpty().map(::File)
)
assertTrue(
commonMainSourceFile.absolutePath in arguments.freeArgs,
"Expected commonMain source file to be present in 'freeArgs'"
)
}
@Test
fun `test - k2 - platform native compilation - sources`() {
val project = buildProjectWithMPP()
val kotlin = project.multiplatformExtension
kotlin.linuxX64()
/* Enable K2 if necessary */
if (KotlinVersion.DEFAULT < KotlinVersion.KOTLIN_2_0) {
kotlin.sourceSets.all {
it.languageSettings.languageVersion = "2.0"
}
}
val linuxX64SourceFile = project.file("src/linuxX64Main/kotlin/CommonMain.kt")
linuxX64SourceFile.parentFile.mkdirs()
linuxX64SourceFile.writeText("object Linux")
project.evaluate()
val nativeCompilation = kotlin.linuxX64().compilations.main
val sharedNativeCompileTask = nativeCompilation.compileTaskProvider.get() as KotlinNativeCompile
val arguments = sharedNativeCompileTask.createCompilerArguments(lenient)
assertTrue(
LanguageVersion.fromVersionString(arguments.languageVersion)!! >= LanguageVersion.KOTLIN_2_0,
"Expected 'languageVersion' 2.0 or higher"
)
assertNull(
arguments.commonSources?.toList(),
"Expected 'commonSources' to not be set: Native Platform compilations shall use -Xfragment{x} arguments"
)
assertEquals(
setOf("commonMain", "linuxX64Main"),
arguments.fragments?.toSet(),
"Expected 'fragments' to *not* be set: Metadata compilations shall use -Xcommon-sources and provide klib dependencies"
)
assertEquals(
listOf("linuxX64Main:${linuxX64SourceFile.absolutePath}"),
arguments.fragmentSources?.toList(),
)
assertEquals(
listOf("linuxX64Main:commonMain"),
arguments.fragmentRefines?.toList(),
)
assertTrue(
linuxX64SourceFile.absolutePath in arguments.freeArgs,
"Expected linuxX64 source file to be present in 'freeArgs'"
)
}
}