[Gradle] Fix CompilerOptionsIT.passesOptInAnnotationNative

KTIJ-24976
This commit is contained in:
Sebastian Sellmair
2023-03-28 15:18:41 +02:00
committed by Space Team
parent e76f3fb925
commit 2c8491cf27
4 changed files with 49 additions and 9 deletions
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.cli.common.CompilerSystemProperties
import org.jetbrains.kotlin.utils.SmartList import org.jetbrains.kotlin.utils.SmartList
import kotlin.reflect.KClass import kotlin.reflect.KClass
import kotlin.reflect.KMutableProperty1 import kotlin.reflect.KMutableProperty1
import kotlin.reflect.cast
import kotlin.reflect.full.memberProperties import kotlin.reflect.full.memberProperties
@Target(AnnotationTarget.PROPERTY) @Target(AnnotationTarget.PROPERTY)
@@ -63,6 +64,19 @@ data class ArgumentParseErrors(
val internalArgumentsParsingProblems: MutableList<String> = SmartList() val internalArgumentsParsingProblems: MutableList<String> = SmartList()
) )
inline fun <reified T : CommonToolArguments> parseCommandLineArguments(args: List<String>): T {
return parseCommandLineArguments(T::class, args)
}
fun <T : CommonToolArguments> parseCommandLineArguments(clazz: KClass<T>, args: List<String>): T {
val constructor = clazz.java.constructors.find { it.parameters.isEmpty() }
?: error("Missing empty constructor on '${clazz.java.name}")
val arguments = clazz.cast(constructor.newInstance())
parseCommandLineArguments(args, arguments)
return arguments
}
// Parses arguments into the passed [result] object. Errors related to the parsing will be collected into [CommonToolArguments.errors]. // Parses arguments into the passed [result] object. Errors related to the parsing will be collected into [CommonToolArguments.errors].
fun <A : CommonToolArguments> parseCommandLineArguments(args: List<String>, result: A, overrideArguments: Boolean = false) { fun <A : CommonToolArguments> parseCommandLineArguments(args: List<String>, result: A, overrideArguments: Boolean = false) {
val errors = lazy { result.errors ?: ArgumentParseErrors().also { result.errors = it } } val errors = lazy { result.errors ?: ArgumentParseErrors().also { result.errors = it } }
@@ -73,6 +73,7 @@ dependencies {
testImplementation(project(":kotlin-compiler-embeddable")) testImplementation(project(":kotlin-compiler-embeddable"))
testImplementation(commonDependency("org.jetbrains.intellij.deps:jdom")) testImplementation(commonDependency("org.jetbrains.intellij.deps:jdom"))
testImplementation(project(":compiler:cli-common"))
// testCompileOnly dependency on non-shaded artifacts is needed for IDE support // testCompileOnly dependency on non-shaded artifacts is needed for IDE support
// testRuntimeOnly on shaded artifact is needed for running tests with shaded compiler // testRuntimeOnly on shaded artifact is needed for running tests with shaded compiler
testCompileOnly(project(":kotlin-gradle-plugin-test-utils-embeddable")) testCompileOnly(project(":kotlin-gradle-plugin-test-utils-embeddable"))
@@ -7,9 +7,13 @@ package org.jetbrains.kotlin.gradle
import org.gradle.api.logging.LogLevel import org.gradle.api.logging.LogLevel
import org.gradle.util.GradleVersion import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.cli.common.arguments.K2NativeCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments
import org.jetbrains.kotlin.gradle.testbase.* import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.fail
import kotlin.io.path.appendText import kotlin.io.path.appendText
import kotlin.test.fail
internal class CompilerOptionsIT : KGPBaseTest() { internal class CompilerOptionsIT : KGPBaseTest() {
@@ -25,11 +29,11 @@ internal class CompilerOptionsIT : KGPBaseTest() {
project("buildSrcWithKotlinDslAndKgp", gradleVersion) { project("buildSrcWithKotlinDslAndKgp", gradleVersion) {
gradleProperties gradleProperties
.appendText( .appendText(
""" """
| |
|systemProp.org.gradle.kotlin.dsl.precompiled.accessors.strict=true |systemProp.org.gradle.kotlin.dsl.precompiled.accessors.strict=true
""".trimMargin() """.trimMargin()
) )
build("tasks") { build("tasks") {
assertOutputContains("kotlinOptions.freeCompilerArgs were changed on task :compileKotlin execution phase:") assertOutputContains("kotlinOptions.freeCompilerArgs were changed on task :compileKotlin execution phase:")
@@ -285,16 +289,17 @@ internal class CompilerOptionsIT : KGPBaseTest() {
) )
build("compileKotlinHost", forceOutput = true) { build("compileKotlinHost", forceOutput = true) {
val expectedOptIn = listOf("-opt-in=kotlin.RequiresOptIn", "-opt-in=my.CustomOptIn") val expectedOptIn = listOf("kotlin.RequiresOptIn", "my.CustomOptIn")
val optInArgs = output val argumentsString = output
.substringAfter("Arguments = [") .substringAfter("Arguments = [")
.substringBefore("]") .substringBefore("]")
.lines()
.filter { it.trim() in expectedOptIn }
assert(optInArgs.size == 2) { val rawArguments = argumentsString.lines().map { it.trim() }
printBuildOutput() val arguments = parseCommandLineArguments<K2NativeCompilerArguments>(rawArguments)
"compiler arguments does not contain '${expectedOptIn.joinToString()}': ${optInArgs.joinToString()}" if (arguments.optIn?.toList() != listOf("kotlin.RequiresOptIn", "my.CustomOptIn")) {
fail(
"compiler arguments does not contain expected optIns'${expectedOptIn.joinToString()}': ${arguments.optIn}"
)
} }
} }
} }
@@ -74,4 +74,24 @@ class KotlinNativeCompileArgumentsTest {
assertNull(commonMainCompileTask.createCompilerArguments(lenient).libraries) assertNull(commonMainCompileTask.createCompilerArguments(lenient).libraries)
assertFails { commonMainCompileTask.createCompilerArguments(default) } assertFails { commonMainCompileTask.createCompilerArguments(default) }
} }
@Test
fun `test - opt in`() {
val project = buildProjectWithMPP()
val kotlin = project.multiplatformExtension
val linuxX64Target = kotlin.linuxX64()
linuxX64Target.compilations.all {
it.compilerOptions.options.apply {
optIn.add("my.OptIn")
optIn.add("my.other.OptIn")
}
}
project.evaluate()
val arguments = linuxX64Target.compilations.main.compileTaskProvider.get().createCompilerArguments(lenient)
assertEquals(
listOf("my.OptIn", "my.other.OptIn"), arguments.optIn?.toList()
)
}
} }