From 9f2ee09ddb129667cc291924b6c7d093edb7bd91 Mon Sep 17 00:00:00 2001 From: Sebastian Sellmair Date: Wed, 29 Mar 2023 10:11:42 +0200 Subject: [PATCH] [Gradle] Fix CompilerOptionsIT.combinesOptInFromLanguageSettingsNative KTIJ-24976 --- .../kotlin/gradle/CompilerOptionsIT.kt | 33 ++++++++----------- .../gradle/util/compilerArgumentUtils.kt | 29 ++++++++++++++++ 2 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/util/compilerArgumentUtils.kt diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt index 9ccb6dff91c..e54eb7f37a0 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/CompilerOptionsIT.kt @@ -10,9 +10,11 @@ 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.util.parseCompilerArguments import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.fail import kotlin.io.path.appendText +import kotlin.test.assertEquals import kotlin.test.fail internal class CompilerOptionsIT : KGPBaseTest() { @@ -249,24 +251,20 @@ internal class CompilerOptionsIT : KGPBaseTest() { build("compileNativeMainKotlinMetadata") { assertTasksExecuted(":compileNativeMainKotlinMetadata") - assert( - output.contains("-opt-in=another.custom.UnderOptIn") && - output.contains("-opt-in=my.custom.OptInAnnotation") - ) { - printBuildOutput() - "Output does not contain '-opt-in=another.custom.UnderOptIn, -opt-in=my.custom.OptInAnnotation'!" - } + val arguments = parseCompilerArguments() + assertEquals( + setOf("another.custom.UnderOptIn", "my.custom.OptInAnnotation"), arguments.optIn?.toSet(), + "Arguments optIn does not match '-opt-in=another.custom.UnderOptIn, -opt-in=my.custom.OptInAnnotation'" + ) } build("compileKotlinLinux64") { assertTasksExecuted(":compileKotlinLinux64") - assert( - output.contains("-opt-in=another.custom.UnderOptIn") && - output.contains("-opt-in=my.custom.OptInAnnotation") - ) { - printBuildOutput() - "Output does not contain '-opt-in=another.custom.UnderOptIn, -opt-in=my.custom.OptInAnnotation'!" - } + val arguments = parseCompilerArguments() + assertEquals( + setOf("another.custom.UnderOptIn", "my.custom.OptInAnnotation"), arguments.optIn?.toSet(), + "Arguments optIn does not match '-opt-in=another.custom.UnderOptIn, -opt-in=my.custom.OptInAnnotation'" + ) } } } @@ -290,12 +288,7 @@ internal class CompilerOptionsIT : KGPBaseTest() { build("compileKotlinHost", forceOutput = true) { val expectedOptIn = listOf("kotlin.RequiresOptIn", "my.CustomOptIn") - val argumentsString = output - .substringAfter("Arguments = [") - .substringBefore("]") - - val rawArguments = argumentsString.lines().map { it.trim() } - val arguments = parseCommandLineArguments(rawArguments) + val arguments = parseCompilerArguments() if (arguments.optIn?.toList() != listOf("kotlin.RequiresOptIn", "my.CustomOptIn")) { fail( "compiler arguments does not contain expected optIns'${expectedOptIn.joinToString()}': ${arguments.optIn}" diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/util/compilerArgumentUtils.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/util/compilerArgumentUtils.kt new file mode 100644 index 00000000000..6f61c251afa --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/util/compilerArgumentUtils.kt @@ -0,0 +1,29 @@ +/* + * 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.gradle.util + +import org.gradle.testkit.runner.BuildResult +import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.kotlin.cli.common.arguments.parseCommandLineArguments +import kotlin.reflect.KClass + +inline fun BuildResult.parseCompilerArguments(): T { + return parseCompilerArguments(T::class) +} + +fun BuildResult.parseCompilerArguments(type: KClass): T { + val arguments = findCommandLineArguments(output).lines().map { it.trim() } + return parseCommandLineArguments(type, arguments) +} + +fun findCommandLineArguments(buildOutput: String): String { + val delimiter = "Arguments = [" + require(delimiter in buildOutput) + + return buildOutput + .substringAfter("Arguments = [") + .substringBefore("]") +}