Migrate ConfigurationAvoidanceIT tests into new DSL.

^KT-45745 In Progress
This commit is contained in:
Yahor Berdnikau
2021-10-06 16:57:33 +02:00
parent 2f87c28634
commit 82d947d3a4
7 changed files with 114 additions and 76 deletions
@@ -5,70 +5,90 @@
package org.jetbrains.kotlin.gradle package org.jetbrains.kotlin.gradle
import org.jetbrains.kotlin.gradle.util.AGPVersion import org.gradle.util.GradleVersion
import org.junit.Test import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.appendText
class ConfigurationAvoidanceIT : BaseGradleIT() { @DisplayName("Tasks configuration avoidance")
@SimpleGradlePluginTests
@OptIn(ExperimentalPathApi::class)
class ConfigurationAvoidanceIT : KGPBaseTest() {
@Test @DisplayName("Unrelated tasks are not configured")
fun testUnrelatedTaskNotConfigured() = with(Project("simpleProject")) { @GradleTest
setupWorkingDir() fun testUnrelatedTaskNotConfigured(gradleVersion: GradleVersion) {
project("simpleProject", gradleVersion) {
val expensivelyConfiguredTaskName = "expensivelyConfiguredTask" val expensivelyConfiguredTaskName = "expensivelyConfiguredTask"
val triggeredExpensiveConfigurationText = "Triggered expensive configuration!"
gradleBuildScript().appendText("\n" + """ @Suppress("GroovyAssignabilityCheck")
tasks.register("$expensivelyConfiguredTaskName") { rootBuildGradle.appendText(
println("$triggeredExpensiveConfigurationText") //language=Groovy
} """
""".trimIndent())
tasks.register("$expensivelyConfiguredTaskName") {
build("compileKotlin") { throw new GradleException("Should not configure expensive task!")
assertSuccessful()
assertNotContains(triggeredExpensiveConfigurationText)
}
}
@Test
fun testAndroidUnrelatedTaskNotConfigured() = with(
Project(
"AndroidProject",
gradleVersionRequirement = GradleVersionRequired.AtLeast("6.7.1")
)
) {
setupWorkingDir()
listOf("Android", "Test").forEach { subproject ->
gradleBuildScript(subproject).appendText("\n" + """
android {
applicationVariants.all {
it.getAidlCompileProvider().configure {
throw new RuntimeException("Task should not be configured.")
}
}
} }
""".trimIndent() """.trimIndent()
) )
build("compileKotlin")
} }
}
gradleBuildScript("Lib").appendText( @DisplayName("Android tasks are not configured")
"\n" + """ @GradleTestVersions(minVersion = "6.7.1")
android { @GradleTest
libraryVariants.all { fun testAndroidUnrelatedTaskNotConfigured(gradleVersion: GradleVersion) {
it.getAidlCompileProvider().configure { project(
throw new RuntimeException("Task should not be configured.") "AndroidProject",
} gradleVersion
}
}
""".trimIndent()
)
build(
"help", options = defaultBuildOptions().copy(
androidGradlePluginVersion = AGPVersion.v4_2_0
)
) { ) {
assertSuccessful()
listOf("Android", "Test").forEach { subproject ->
projectPath
.resolve(subproject)
.resolve("build.gradle")
.appendText(
//language=Groovy
"""
android {
applicationVariants.all {
it.getAidlCompileProvider().configure {
throw new RuntimeException("Task should not be configured.")
}
}
}
""".trimIndent()
)
}
projectPath
.resolve("Lib")
.resolve("build.gradle")
.appendText(
//language=Groovy
"""
android {
libraryVariants.all {
it.getAidlCompileProvider().configure {
throw new RuntimeException("Task should not be configured.")
}
}
}
""".trimIndent()
)
build(
"help",
buildOptions = defaultBuildOptions.copy(
androidVersion = TestVersions.AGP.AGP_42
)
)
} }
} }
} }
@@ -102,7 +102,7 @@ class PureAndroidAndJavaConsumeMppLibIT : BaseGradleIT() {
gradleBuildScript("Lib").apply { gradleBuildScript("Lib").apply {
writeText( writeText(
// Remove the Kotlin plugin from the consumer project to check how pure-AGP Kotlin-less consumers resolve the dependency // Remove the Kotlin plugin from the consumer project to check how pure-AGP Kotlin-less consumers resolve the dependency
readText().checkedReplace("apply plugin: 'kotlin-android'", "//").let { text -> readText().checkedReplace("id 'org.jetbrains.kotlin.android'", "//").let { text ->
// If the test case doesn't assume flavors, remove the flavor setup lines: // If the test case doesn't assume flavors, remove the flavor setup lines:
if (useFlavors) text else text.lines().filter { !it.trim().startsWith("flavor") }.joinToString("\n") if (useFlavors) text else text.lines().filter { !it.trim().startsWith("flavor") }.joinToString("\n")
} + "\n" + """ } + "\n" + """
@@ -1,7 +1,9 @@
import org.gradle.util.VersionNumber import org.gradle.util.VersionNumber
apply plugin: 'com.android.application' plugins {
apply plugin: 'kotlin-android' id 'com.android.application'
id 'org.jetbrains.kotlin.android'
}
android { android {
compileSdkVersion 22 compileSdkVersion 22
@@ -1,5 +1,7 @@
apply plugin: 'com.android.library' plugins {
apply plugin: 'kotlin-android' id 'com.android.library'
id 'org.jetbrains.kotlin.android'
}
dependencies { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
@@ -1,7 +1,9 @@
import org.gradle.util.VersionNumber import org.gradle.util.VersionNumber
apply plugin: 'com.android.test' plugins {
apply plugin: 'kotlin-android' id 'com.android.test'
id 'org.jetbrains.kotlin.android'
}
dependencies { dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
@@ -1,21 +1,7 @@
buildscript {
repositories {
mavenLocal()
google()
mavenCentral()
maven { url = uri("https://jcenter.bintray.com/") }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath('com.android.tools.build:gradle:' + android_tools_version)
}
}
allprojects { allprojects {
repositories { repositories {
mavenLocal() mavenLocal()
google() google()
mavenCentral() mavenCentral()
maven { url = uri("https://jcenter.bintray.com/") }
} }
} }
@@ -1 +1,27 @@
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
google()
gradlePluginPortal()
}
plugins {
id "org.jetbrains.kotlin.jvm" version "$kotlin_version"
id "org.jetbrains.kotlin.kapt" version "$kotlin_version"
id "org.jetbrains.kotlin.android" version "$kotlin_version"
}
resolutionStrategy {
eachPlugin {
if (requested.id.id == "com.android.application" ||
requested.id.id == "com.android.library" ||
requested.id.id == "com.android.feature" ||
requested.id.id == "com.android.test") {
useModule("com.android.tools.build:gradle:$android_tools_version")
}
}
}
}
include ':Android', ':Lib', ':Test' include ':Android', ':Lib', ':Test'