[Gradle] Fix test plugins application
This commit is contained in:
committed by
Space Team
parent
4f820230fd
commit
52303a1b23
+1
-1
@@ -120,7 +120,7 @@ class MultiplatformGradleIT : BaseGradleIT() {
|
||||
@Test
|
||||
fun testSubprojectWithAnotherClassLoader() {
|
||||
with(Project("multiplatformProject")) {
|
||||
setupWorkingDir()
|
||||
setupWorkingDir(applyLanguageVersion = false)
|
||||
|
||||
// Make sure there is a plugin applied with the plugins DSL, so that Gradle loads the
|
||||
// plugins separately for the subproject, with a different class loader:
|
||||
|
||||
+1
-73
@@ -5,8 +5,6 @@
|
||||
|
||||
package org.jetbrains.kotlin.gradle.testbase
|
||||
|
||||
import org.jetbrains.kotlin.gradle.util.modify
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
import java.nio.file.Paths
|
||||
import kotlin.io.path.createFile
|
||||
@@ -25,75 +23,5 @@ internal fun Path.applyAndroidTestFixes() {
|
||||
""".trimMargin()
|
||||
)
|
||||
|
||||
val pathFile = toFile()
|
||||
|
||||
pathFile.walkTopDown()
|
||||
.filter { it.name == "build.gradle" || it.name == "build.gradle.kts" }
|
||||
.forEach { file ->
|
||||
when (file.name) {
|
||||
"build.gradle" -> file.updateBuildGradle()
|
||||
"build.gradle.kts" -> file.updateBuildGradleKts()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.updateBuildGradle() {
|
||||
modify {
|
||||
if (it.contains("plugins {")) {
|
||||
"""
|
||||
|${it.substringBefore("plugins {")}
|
||||
|plugins {
|
||||
| id "org.jetbrains.kotlin.test.fixes.android"
|
||||
|${it.substringAfter("plugins {")}
|
||||
""".trimMargin()
|
||||
} else if (it.contains("apply plugin:")) {
|
||||
it.modifyBuildScript().run {
|
||||
"""
|
||||
|${substringBefore("apply plugin:")}
|
||||
|apply plugin: 'org.jetbrains.kotlin.test.fixes.android'
|
||||
|apply plugin:${substringAfter("apply plugin:")}
|
||||
""".trimMargin()
|
||||
}
|
||||
} else {
|
||||
it.modifyBuildScript()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.modifyBuildScript(isKts: Boolean = false): String =
|
||||
if (contains("buildscript {") &&
|
||||
contains("classpath")
|
||||
) {
|
||||
val kotlinVersionStr = if (isKts) "${'$'}{property(\"test_fixes_version\")}" else "${'$'}test_fixes_version"
|
||||
"""
|
||||
|${substringBefore("classpath")}
|
||||
|classpath("org.jetbrains.kotlin:android-test-fixes:$kotlinVersionStr")
|
||||
|classpath${substringAfter("classpath")}
|
||||
""".trimMargin()
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
|
||||
private fun File.updateBuildGradleKts() {
|
||||
modify {
|
||||
if (it.contains("plugins {")) {
|
||||
"""
|
||||
|${it.substringBefore("plugins {")}
|
||||
|plugins {
|
||||
| id("org.jetbrains.kotlin.test.fixes.android")
|
||||
|${it.substringAfter("plugins {")}
|
||||
""".trimMargin()
|
||||
} else if (it.contains("apply(plugin")) {
|
||||
it.modifyBuildScript(true).run {
|
||||
"""
|
||||
|${substringBefore("apply(plugin")}
|
||||
|apply(plugin = "org.jetbrains.kotlin.test.fixes.android")
|
||||
|apply(plugin${substringAfter("apply(plugin")}
|
||||
""".trimMargin()
|
||||
}
|
||||
} else {
|
||||
it.modifyBuildScript(true)
|
||||
}
|
||||
}
|
||||
applyPlugin("org.jetbrains.kotlin.test.fixes.android", "org.jetbrains.kotlin:android-test-fixes", "test_fixes_version")
|
||||
}
|
||||
+6
-58
@@ -5,64 +5,12 @@
|
||||
|
||||
package org.jetbrains.kotlin.gradle.testbase
|
||||
|
||||
import org.jetbrains.kotlin.gradle.util.modify
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
fun Path.applyKotlinCompilerArgsPlugin() {
|
||||
toFile().walkTopDown()
|
||||
.filter { it.name in listOf("build.gradle", "build.gradle.kts", "settings.gradle", "settings.gradle.kts") }
|
||||
.forEach { file ->
|
||||
when (file.name) {
|
||||
"build.gradle" -> file.updateBuildGradle()
|
||||
"build.gradle.kts" -> file.updateBuildKtsGradle()
|
||||
"settings.gradle.kts" -> file.updateSettingsKtsGradle()
|
||||
"settings.gradle" -> file.updateSettingsGradle()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.updateSettingsKtsGradle() {
|
||||
modify {
|
||||
if (it.contains("pluginManagement {")) {
|
||||
it.replaceFirst(
|
||||
"plugins {", "plugins {\n" +
|
||||
"id(\"org.jetbrains.kotlin.test.kotlin-compiler-args-properties\") version test_fixes_version"
|
||||
)
|
||||
} else it
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.updateSettingsGradle() {
|
||||
modify {
|
||||
if (it.contains("pluginManagement {")) {
|
||||
it.replaceFirst("plugins {", "plugins {\n"+
|
||||
"id \"org.jetbrains.kotlin.test.kotlin-compiler-args-properties\" version \"${'$'}test_fixes_version\"")
|
||||
} else it
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.updateBuildKtsGradle() {
|
||||
modify {
|
||||
it.replace(
|
||||
"plugins {",
|
||||
"plugins {\nid(\"org.jetbrains.kotlin.test.kotlin-compiler-args-properties\")"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.updateBuildGradle() {
|
||||
modify {
|
||||
if (it.contains("buildscript {")) {
|
||||
it.replaceFirst(
|
||||
"dependencies {", "dependencies {\n" +
|
||||
"classpath \"org.jetbrains.kotlin:kotlin-compiler-args-properties:${'$'}test_fixes_version\""
|
||||
)
|
||||
} else {
|
||||
it.replace(
|
||||
"plugins {",
|
||||
"plugins {\nid \"org.jetbrains.kotlin.test.kotlin-compiler-args-properties\""
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
applyPlugin(
|
||||
"org.jetbrains.kotlin.test.kotlin-compiler-args-properties",
|
||||
"org.jetbrains.kotlin:kotlin-compiler-args-properties",
|
||||
"test_fixes_version"
|
||||
)
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* 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.testbase
|
||||
|
||||
import org.jetbrains.kotlin.gradle.util.modify
|
||||
import java.io.File
|
||||
import java.nio.file.Path
|
||||
|
||||
internal fun Path.applyPlugin(pluginId: String, artifactId: String, artifactVersionProperty: String) {
|
||||
val pathFile = toFile()
|
||||
|
||||
val groovyBuildScripts = setOf("build.gradle", "build-js.gradle")
|
||||
val kotlinBuildScripts = setOf("build.gradle.kts", "build-js.gradle.kts", "build.gradle.kts.alternative")
|
||||
pathFile.walkTopDown()
|
||||
.filter { it.name in groovyBuildScripts || it.name in kotlinBuildScripts }
|
||||
.forEach { file ->
|
||||
when (file.name) {
|
||||
in groovyBuildScripts -> file.updateBuildGradle(pluginId, artifactId, artifactVersionProperty)
|
||||
in kotlinBuildScripts -> file.updateBuildGradleKts(pluginId, artifactId, artifactVersionProperty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun File.updateBuildGradle(pluginId: String, artifactId: String, artifactVersionProperty: String) {
|
||||
modify {
|
||||
if (it.contains("plugins {")) {
|
||||
"""
|
||||
|${it.substringBefore("plugins {")}
|
||||
|plugins {
|
||||
| id "$pluginId"
|
||||
|${it.substringAfter("plugins {")}
|
||||
""".trimMargin()
|
||||
} else if (it.contains("apply plugin:")) {
|
||||
it.modifyBuildScript(artifactId, artifactVersionProperty).run {
|
||||
"""
|
||||
|${substringBefore("apply plugin:")}
|
||||
|apply plugin: '$pluginId'
|
||||
|apply plugin:${substringAfter("apply plugin:")}
|
||||
""".trimMargin()
|
||||
}
|
||||
} else {
|
||||
it.modifyBuildScript(artifactId, artifactVersionProperty)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun String.modifyBuildScript(artifactId: String, artifactVersionProperty: String, isKts: Boolean = false): String =
|
||||
if (contains("buildscript {") &&
|
||||
contains("classpath")
|
||||
) {
|
||||
val kotlinVersionStr = if (isKts) "${'$'}{property(\"$artifactVersionProperty\")}" else "${'$'}$artifactVersionProperty"
|
||||
"""
|
||||
|${substringBefore("classpath")}
|
||||
|classpath("$artifactId:$kotlinVersionStr")
|
||||
|classpath${substringAfter("classpath")}
|
||||
""".trimMargin()
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
|
||||
private fun File.updateBuildGradleKts(pluginId: String, artifactId: String, artifactVersionProperty: String) {
|
||||
modify {
|
||||
if (it.contains("plugins {")) {
|
||||
"""
|
||||
|${it.substringBefore("plugins {")}
|
||||
|plugins {
|
||||
| id("$pluginId")
|
||||
|${it.substringAfter("plugins {")}
|
||||
""".trimMargin()
|
||||
} else if (it.contains("apply(plugin")) {
|
||||
it.modifyBuildScript(artifactId, artifactVersionProperty, true).run {
|
||||
"""
|
||||
|${substringBefore("apply(plugin")}
|
||||
|apply(plugin = "$pluginId")
|
||||
|apply(plugin${substringAfter("apply(plugin")}
|
||||
""".trimMargin()
|
||||
}
|
||||
} else {
|
||||
it.modifyBuildScript(artifactId, artifactVersionProperty, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
-22
@@ -57,6 +57,9 @@ fun KGPBaseTest.project(
|
||||
projectPath.addDefaultBuildFiles()
|
||||
projectPath.enableCacheRedirector()
|
||||
projectPath.enableAndroidSdk()
|
||||
if (buildOptions.languageVersion != null || buildOptions.languageApiVersion != null) {
|
||||
projectPath.applyKotlinCompilerArgsPlugin()
|
||||
}
|
||||
|
||||
val gradleRunner = GradleRunner
|
||||
.create()
|
||||
@@ -78,7 +81,6 @@ fun KGPBaseTest.project(
|
||||
addHeapDumpOptions.ifTrue { testProject.addHeapDumpOptions() }
|
||||
localRepoDir?.let { testProject.configureLocalRepository(localRepoDir) }
|
||||
if (buildJdk != null) testProject.setupNonDefaultJdk(buildJdk)
|
||||
testProject.addKotlinCompilerArgumentsPlugin()
|
||||
|
||||
val result = runCatching {
|
||||
testProject.test()
|
||||
@@ -144,6 +146,7 @@ fun TestProject.build(
|
||||
assertions: BuildResult.() -> Unit = {},
|
||||
) {
|
||||
if (enableBuildScan) agreeToBuildScanService()
|
||||
ensureKotlinCompilerArgumentsPluginAppliedCorrectly(buildOptions)
|
||||
|
||||
val allBuildArguments = commonBuildSetup(
|
||||
buildArguments.toList(),
|
||||
@@ -181,6 +184,7 @@ fun TestProject.buildAndFail(
|
||||
assertions: BuildResult.() -> Unit = {},
|
||||
) {
|
||||
if (enableBuildScan) agreeToBuildScanService()
|
||||
ensureKotlinCompilerArgumentsPluginAppliedCorrectly(buildOptions)
|
||||
|
||||
val allBuildArguments = commonBuildSetup(
|
||||
buildArguments.toList(),
|
||||
@@ -210,6 +214,14 @@ private fun BuildResult.additionalAssertions(buildOptions: BuildOptions) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun TestProject.ensureKotlinCompilerArgumentsPluginAppliedCorrectly(buildOptions: BuildOptions) {
|
||||
if (this.buildOptions.languageVersion != null || this.buildOptions.languageApiVersion != null) return // plugin is applied
|
||||
// plugin's not applied
|
||||
check(buildOptions.languageVersion == null && buildOptions.languageApiVersion == null) {
|
||||
"Kotlin language or API version passed on the build level, but the plugin wasn't applied"
|
||||
}
|
||||
}
|
||||
|
||||
internal inline fun <reified T> TestProject.getModels(
|
||||
crossinline assertions: ModelContainer<T>.() -> Unit,
|
||||
) {
|
||||
@@ -345,27 +357,6 @@ class TestProject(
|
||||
) : GradleProject(projectName, projectPath) {
|
||||
fun subProject(name: String) = GradleProject(name, projectPath.resolve(name))
|
||||
|
||||
fun addKotlinCompilerArgumentsPlugin() {
|
||||
if (buildOptions.languageVersion != null || buildOptions.languageApiVersion != null) {
|
||||
projectPath.toFile().walkTopDown().forEach { file ->
|
||||
when {
|
||||
file.name.equals("build.gradle") -> file.modify {
|
||||
it.replaceFirst(
|
||||
"plugins {",
|
||||
"plugins {\nid \"org.jetbrains.kotlin.test.kotlin-compiler-args-properties\""
|
||||
)
|
||||
}
|
||||
file.name.equals("build.gradle.kts") -> file.modify {
|
||||
it.replaceFirst(
|
||||
"plugins {",
|
||||
"plugins {\nid(\"org.jetbrains.kotlin.test.kotlin-compiler-args-properties\")"
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Includes another project as a submodule in the current project.
|
||||
* @param otherProjectName The name of the other project to include as a submodule.
|
||||
|
||||
-13
@@ -1,13 +0,0 @@
|
||||
pluginManagement {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
gradlePluginPortal()
|
||||
}
|
||||
|
||||
val test_fixes_version: String by settings
|
||||
plugins {
|
||||
val kotlin_version: String by settings
|
||||
kotlin("multiplatform").version(kotlin_version)
|
||||
id("org.jetbrains.kotlin.test.fixes.android") version test_fixes_version
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user