Don't add explicitApi mode for test sources
^KT-58571 Fixed
This commit is contained in:
committed by
Space Team
parent
40b8b682f9
commit
04b39ba2e1
+48
@@ -8,6 +8,7 @@ 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.gradle.testbase.*
|
import org.jetbrains.kotlin.gradle.testbase.*
|
||||||
|
import org.jetbrains.kotlin.gradle.util.AGPVersion
|
||||||
import org.jetbrains.kotlin.konan.target.HostManager
|
import org.jetbrains.kotlin.konan.target.HostManager
|
||||||
import org.jetbrains.kotlin.konan.target.KonanTarget
|
import org.jetbrains.kotlin.konan.target.KonanTarget
|
||||||
import org.junit.jupiter.api.DisplayName
|
import org.junit.jupiter.api.DisplayName
|
||||||
@@ -37,6 +38,12 @@ class ExplicitApiIT : KGPBaseTest() {
|
|||||||
|
|
||||||
assertCompilerArgument(":compileKotlin", "-Xexplicit-api=warning")
|
assertCompilerArgument(":compileKotlin", "-Xexplicit-api=warning")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
build(":compileTestKotlin") {
|
||||||
|
assertTasksExecuted(":compileTestKotlin")
|
||||||
|
|
||||||
|
assertNoCompilerArgument(":compileTestKotlin", "-Xexplicit-api=warning")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -142,4 +149,45 @@ class ExplicitApiIT : KGPBaseTest() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@DisplayName("Explicit api mode is enabled only for non-test variants in Android project")
|
||||||
|
@AndroidGradlePluginTests
|
||||||
|
@GradleAndroidTest
|
||||||
|
fun explicitApiAndroid(
|
||||||
|
gradleVersion: GradleVersion,
|
||||||
|
agpVersion: String,
|
||||||
|
jdkVersion: JdkVersions.ProvidedJdk
|
||||||
|
) {
|
||||||
|
project(
|
||||||
|
"AndroidSimpleApp",
|
||||||
|
gradleVersion,
|
||||||
|
buildOptions = defaultBuildOptions.copy(
|
||||||
|
androidVersion = agpVersion,
|
||||||
|
logLevel = LogLevel.DEBUG
|
||||||
|
),
|
||||||
|
buildJdk = jdkVersion.location
|
||||||
|
) {
|
||||||
|
|
||||||
|
buildGradle.appendText(
|
||||||
|
//language=groovy
|
||||||
|
"""
|
||||||
|
|
|
||||||
|
|kotlin.explicitApiWarning()
|
||||||
|
|
|
||||||
|
""".trimMargin()
|
||||||
|
)
|
||||||
|
|
||||||
|
build(":compileDebugKotlin") {
|
||||||
|
assertTasksExecuted(":compileDebugKotlin")
|
||||||
|
|
||||||
|
assertCompilerArgument(":compileDebugKotlin", "-Xexplicit-api=warning")
|
||||||
|
}
|
||||||
|
|
||||||
|
build(":compileDebugUnitTestKotlin") {
|
||||||
|
assertTasksExecuted(":compileDebugUnitTestKotlin")
|
||||||
|
|
||||||
|
assertNoCompilerArgument(":compileDebugUnitTestKotlin", "-Xexplicit-api=warning")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+16
@@ -254,6 +254,22 @@ fun BuildResult.assertCompilerArgument(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun BuildResult.assertNoCompilerArgument(
|
||||||
|
taskPath: String,
|
||||||
|
notExpectedArgument: String,
|
||||||
|
) {
|
||||||
|
val taskOutput = getOutputForTask(taskPath)
|
||||||
|
val compilerArguments = taskOutput.lines().first {
|
||||||
|
it.contains("Kotlin compiler args:")
|
||||||
|
}.substringAfter("Kotlin compiler args:")
|
||||||
|
|
||||||
|
assert(!compilerArguments.contains(notExpectedArgument)) {
|
||||||
|
printBuildOutput()
|
||||||
|
|
||||||
|
"$taskPath task compiler arguments contains $notExpectedArgument. Actual content: $compilerArguments"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Asserts command line arguments of the given K/N compiler for given tasks' paths
|
* Asserts command line arguments of the given K/N compiler for given tasks' paths
|
||||||
*
|
*
|
||||||
|
|||||||
+12
-1
@@ -20,6 +20,7 @@ import org.gradle.api.provider.Provider
|
|||||||
import org.gradle.api.tasks.Exec
|
import org.gradle.api.tasks.Exec
|
||||||
import org.gradle.api.tasks.TaskProvider
|
import org.gradle.api.tasks.TaskProvider
|
||||||
import org.gradle.language.base.plugins.LifecycleBasePlugin
|
import org.gradle.language.base.plugins.LifecycleBasePlugin
|
||||||
|
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
|
||||||
import org.jetbrains.kotlin.gradle.dsl.KotlinNativeCompilerOptions
|
import org.jetbrains.kotlin.gradle.dsl.KotlinNativeCompilerOptions
|
||||||
import org.jetbrains.kotlin.gradle.dsl.topLevelExtension
|
import org.jetbrains.kotlin.gradle.dsl.topLevelExtension
|
||||||
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.TEST_COMPILATION_NAME
|
import org.jetbrains.kotlin.gradle.plugin.KotlinCompilation.Companion.TEST_COMPILATION_NAME
|
||||||
@@ -404,7 +405,17 @@ open class KotlinNativeTargetConfigurator<T : KotlinNativeTarget> : AbstractKotl
|
|||||||
it.runViaBuildToolsApi.value(false).disallowChanges() // K/N is not yet supported
|
it.runViaBuildToolsApi.value(false).disallowChanges() // K/N is not yet supported
|
||||||
|
|
||||||
it.explicitApiMode
|
it.explicitApiMode
|
||||||
.value(project.providers.provider { ext.explicitApi })
|
.value(
|
||||||
|
project.providers.provider {
|
||||||
|
// Plugin explicitly does not configures 'explicitApi' mode for test sources
|
||||||
|
// compilation, as test sources are not published
|
||||||
|
if (compilationInfo.isMain) {
|
||||||
|
ext.explicitApi
|
||||||
|
} else {
|
||||||
|
ExplicitApiMode.Disabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
.finalizeValueOnRead()
|
.finalizeValueOnRead()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -112,7 +112,7 @@ abstract class AbstractKotlinCompile<T : CommonCompilerArguments> @Inject constr
|
|||||||
|
|
||||||
@get:Input
|
@get:Input
|
||||||
@get:Optional
|
@get:Optional
|
||||||
internal abstract val explicitApiMode: Property<ExplicitApiMode>
|
abstract val explicitApiMode: Property<ExplicitApiMode>
|
||||||
|
|
||||||
@get:Internal
|
@get:Internal
|
||||||
internal abstract val suppressKotlinOptionsFreeArgsModificationWarning: Property<Boolean>
|
internal abstract val suppressKotlinOptionsFreeArgsModificationWarning: Property<Boolean>
|
||||||
|
|||||||
+25
@@ -14,6 +14,7 @@ import org.gradle.api.provider.ProviderFactory
|
|||||||
import org.gradle.api.tasks.TaskProvider
|
import org.gradle.api.tasks.TaskProvider
|
||||||
import org.jetbrains.kotlin.compilerRunner.CompilerSystemPropertiesService
|
import org.jetbrains.kotlin.compilerRunner.CompilerSystemPropertiesService
|
||||||
import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner
|
import org.jetbrains.kotlin.compilerRunner.GradleCompilerRunner
|
||||||
|
import org.jetbrains.kotlin.gradle.dsl.ExplicitApiMode
|
||||||
import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension
|
import org.jetbrains.kotlin.gradle.dsl.KotlinTopLevelExtension
|
||||||
import org.jetbrains.kotlin.gradle.dsl.topLevelExtension
|
import org.jetbrains.kotlin.gradle.dsl.topLevelExtension
|
||||||
import org.jetbrains.kotlin.gradle.incremental.IncrementalModuleInfoBuildService
|
import org.jetbrains.kotlin.gradle.incremental.IncrementalModuleInfoBuildService
|
||||||
@@ -21,6 +22,8 @@ import org.jetbrains.kotlin.gradle.internal.ClassLoadersCachingBuildService
|
|||||||
import org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask
|
import org.jetbrains.kotlin.gradle.internal.KaptGenerateStubsTask
|
||||||
import org.jetbrains.kotlin.gradle.plugin.*
|
import org.jetbrains.kotlin.gradle.plugin.*
|
||||||
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
|
import org.jetbrains.kotlin.gradle.plugin.PropertiesProvider.Companion.kotlinPropertiesProvider
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinJvmAndroidCompilation
|
||||||
|
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinMetadataTarget
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.associateWithClosure
|
import org.jetbrains.kotlin.gradle.plugin.mpp.associateWithClosure
|
||||||
import org.jetbrains.kotlin.gradle.plugin.mpp.internal
|
import org.jetbrains.kotlin.gradle.plugin.mpp.internal
|
||||||
import org.jetbrains.kotlin.gradle.plugin.sources.applyLanguageSettingsToCompilerOptions
|
import org.jetbrains.kotlin.gradle.plugin.sources.applyLanguageSettingsToCompilerOptions
|
||||||
@@ -162,6 +165,28 @@ internal abstract class AbstractKotlinCompileConfig<TASK : AbstractKotlinCompile
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
task.explicitApiMode
|
||||||
|
.value(
|
||||||
|
project.providers.provider {
|
||||||
|
// Plugin explicitly does not configures 'explicitApi' mode for test sources
|
||||||
|
// compilation, as test sources are not published
|
||||||
|
val compilation = compilationInfo.tcsOrNull?.compilation
|
||||||
|
val isCommonCompilation = compilation?.target is KotlinMetadataTarget
|
||||||
|
|
||||||
|
val androidCompilation = compilationInfo.tcsOrNull?.compilation as? KotlinJvmAndroidCompilation
|
||||||
|
val isMainAndroidCompilation = androidCompilation?.let {
|
||||||
|
getTestedVariantData(it.androidVariant) == null
|
||||||
|
} ?: false
|
||||||
|
|
||||||
|
if (compilationInfo.isMain || isCommonCompilation || isMainAndroidCompilation) {
|
||||||
|
ext.explicitApi
|
||||||
|
} else {
|
||||||
|
ExplicitApiMode.Disabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
.finalizeValueOnRead()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user