Migrated BuildCacheIT tests to new DSL.

^KT-45745 In Progress
This commit is contained in:
Yahor Berdnikau
2021-08-06 16:39:29 +02:00
parent 20afb268d1
commit b5ea811b9f
7 changed files with 224 additions and 224 deletions
@@ -16,161 +16,176 @@
package org.jetbrains.kotlin.gradle package org.jetbrains.kotlin.gradle
import org.jetbrains.kotlin.gradle.util.modify import org.gradle.util.GradleVersion
import org.junit.Test import org.jetbrains.kotlin.gradle.testbase.*
import java.io.File import org.junit.jupiter.api.DisplayName
import kotlin.io.path.ExperimentalPathApi
import kotlin.io.path.readText
import kotlin.io.path.writeText
class BuildCacheIT : BaseGradleIT() { @ExperimentalPathApi
override fun defaultBuildOptions(): BuildOptions = @DisplayName("Local build cache")
super.defaultBuildOptions().copy(withBuildCache = true) @SimpleGradlePluginTests
class BuildCacheIT : KGPBaseTest() {
companion object { override val defaultBuildOptions: BuildOptions =
private val GRADLE_VERSION = GradleVersionRequired.None super.defaultBuildOptions.copy(buildCacheEnabled = true)
}
@Test private val localBuildCacheDir get() = workingDir.resolve("custom-jdk-build-cache")
fun testKotlinCachingEnabledFlag() = with(Project("simpleProject", GRADLE_VERSION)) {
prepareLocalBuildCache() @DisplayName("kotlin.caching.enabled flag should enable caching for Kotlin tasks")
@GradleTest
fun testKotlinCachingEnabledFlag(gradleVersion: GradleVersion) {
project("simpleProject", gradleVersion) {
enableLocalBuildCache(localBuildCacheDir)
build("assemble") { build("assemble") {
assertSuccessful() assertTasksPackedToCache(":compileKotlin")
assertTaskPackedToCache(":compileKotlin")
} }
build("clean", "assemble", "-Dkotlin.caching.enabled=false") { build("clean", "assemble", "-Dkotlin.caching.enabled=false") {
assertSuccessful() assertTasksExecuted(":compileKotlin")
assertNotContains(":compileKotlin FROM-CACHE") }
} }
} }
@Test @DisplayName("Kotlin JVM task should be taken from cache")
fun testCacheHitAfterClean() = with(Project("simpleProject", GRADLE_VERSION)) { @GradleTest
prepareLocalBuildCache() fun testCacheHitAfterClean(gradleVersion: GradleVersion) {
project("simpleProject", gradleVersion) {
enableLocalBuildCache(localBuildCacheDir)
build("assemble") { build("assemble") {
assertSuccessful() assertTasksPackedToCache(":compileKotlin")
assertTaskPackedToCache(":compileKotlin")
} }
build("clean", "assemble") { build("clean", "assemble") {
assertSuccessful() assertTasksFromCache(":compileKotlin", ":compileJava")
assertContains(":compileKotlin FROM-CACHE") }
assertContains(":compileJava FROM-CACHE")
} }
} }
@Test @DisplayName("Should correctly handle modification/restoration of source file")
fun testCacheHitAfterCacheHit() = with(Project("simpleProject", GRADLE_VERSION)) { @GradleTest
prepareLocalBuildCache() fun testCacheHitAfterCacheHit(gradleVersion: GradleVersion) {
project("simpleProject", gradleVersion) {
enableLocalBuildCache(localBuildCacheDir)
build("assemble") { build("assemble") {
assertSuccessful()
// Should store the output into the cache: // Should store the output into the cache:
assertTaskPackedToCache(":compileKotlin") assertTasksPackedToCache(":compileKotlin")
} }
val sourceFile = File(projectDir, "src/main/kotlin/helloWorld.kt") val sourceFile = projectPath.resolve("src/main/kotlin/helloWorld.kt")
val originalSource: String = sourceFile.readText() val originalSource: String = sourceFile.readText()
val modifiedSource: String = originalSource.replace(" and ", " + ") val modifiedSource: String = originalSource.replace(" and ", " + ")
sourceFile.writeText(modifiedSource) sourceFile.writeText(modifiedSource)
build("assemble") { build("assemble") {
assertSuccessful() assertTasksPackedToCache(":compileKotlin")
assertTaskPackedToCache(":compileKotlin")
} }
sourceFile.writeText(originalSource) sourceFile.writeText(originalSource)
build("assemble") { build("assemble") {
assertSuccessful()
// Should load the output from cache: // Should load the output from cache:
assertContains(":compileKotlin FROM-CACHE") assertTasksFromCache(":compileKotlin")
} }
sourceFile.writeText(modifiedSource) sourceFile.writeText(modifiedSource)
build("assemble") { build("assemble") {
assertSuccessful()
// And should load the output from cache again, without compilation: // And should load the output from cache again, without compilation:
assertContains(":compileKotlin FROM-CACHE") assertTasksFromCache(":compileKotlin")
}
} }
} }
@Test @DisplayName("Incremental compilation works with cache")
fun testKotlinCompileIncrementalBuildWithoutRelocation() = with(Project("buildCacheSimple", GRADLE_VERSION)) { @GradleTest
prepareLocalBuildCache() fun testKotlinCompileIncrementalBuildWithoutRelocation(gradleVersion: GradleVersion) {
project("buildCacheSimple", gradleVersion) {
enableLocalBuildCache(localBuildCacheDir)
checkKotlinCompileCachingIncrementalBuild(projectDir, this) checkKotlinCompileCachingIncrementalBuild(this, this)
}
@Test
fun testKotlinCompileCachingIncrementalBuildWithRelocation() {
with(Project("buildCacheSimple", GRADLE_VERSION)) {
prepareLocalBuildCache()
// Copy the project to a different directory
val copyProjectParentDir = projectDir.resolveSibling("copy_${projectDir.name}").also { it.mkdirs() }
copyRecursively(projectDir, copyProjectParentDir)
checkKotlinCompileCachingIncrementalBuild(copyProjectParentDir.resolve(projectName), this)
} }
} }
@Test // TODO: move it into relocatable build cache tests
fun testKaptCachingIncrementalBuildWithoutRelocation() { @DisplayName("Incremental compilation build cache does not break relocated cache")
with(Project("kaptAvoidance", GRADLE_VERSION, directoryPrefix = "kapt2")) { @GradleTest
prepareLocalBuildCache() fun testKotlinCompileCachingIncrementalBuildWithRelocation(gradleVersion: GradleVersion) {
val firstProject = project("buildCacheSimple", gradleVersion) {
enableLocalBuildCache(localBuildCacheDir)
}
checkKaptCachingIncrementalBuild(projectDir, this) val secondProject = project("buildCacheSimple", gradleVersion) {
enableLocalBuildCache(localBuildCacheDir)
}
checkKotlinCompileCachingIncrementalBuild(firstProject, secondProject)
}
// TODO: move it into relocatable build cache tests
@DisplayName("Kapt incremental compilation works with cache")
@GradleTest
fun testKaptCachingIncrementalBuildWithoutRelocation(gradleVersion: GradleVersion) {
project("kapt2/kaptAvoidance", gradleVersion) {
enableLocalBuildCache(localBuildCacheDir)
checkKaptCachingIncrementalBuild(this, this)
} }
} }
@Test // TODO: move it into build cache relocation tests
fun testKaptCachingIncrementalBuildWithRelocation() { @DisplayName("Kapt incremental compilation build does not break relocated build cache")
with(Project("kaptAvoidance", GRADLE_VERSION, directoryPrefix = "kapt2")) { @GradleTest
prepareLocalBuildCache() fun testKaptCachingIncrementalBuildWithRelocation(gradleVersion: GradleVersion) {
val firstProject = project("kapt2/kaptAvoidance", gradleVersion) {
// Copy the project to a different directory enableLocalBuildCache(localBuildCacheDir)
val copyProjectParentDir = projectDir.resolveSibling("copy_${projectDir.name}").also { it.mkdirs() }
copyRecursively(projectDir, copyProjectParentDir)
checkKaptCachingIncrementalBuild(copyProjectParentDir.resolve(projectName), this)
}
} }
private fun checkKotlinCompileCachingIncrementalBuild(projectToBeModified: File, project: BaseGradleIT.Project) { val secondProject = project("kapt2/kaptAvoidance", gradleVersion) {
with(project) { enableLocalBuildCache(localBuildCacheDir)
}
checkKaptCachingIncrementalBuild(firstProject, secondProject)
}
private fun checkKotlinCompileCachingIncrementalBuild(
firstProject: TestProject,
secondProject: TestProject
) {
// First build, should be stored into the build cache: // First build, should be stored into the build cache:
build("assemble") { firstProject.build("assemble") {
assertSuccessful() assertTasksPackedToCache(":compileKotlin")
assertTaskPackedToCache(":compileKotlin")
} }
// A cache hit: a clean build without any changes to the project // A cache hit: a clean build without any changes to the project
build("clean", "assemble", projectDir = projectToBeModified) { secondProject.build("clean", "assemble") {
assertSuccessful() assertTasksFromCache(":compileKotlin")
assertContains(":compileKotlin FROM-CACHE")
} }
// Change the return type of foo() from Int to String in foo.kt, and check that fooUsage.kt is recompiled as well: // Change the return type of foo() from Int to String in foo.kt, and check that fooUsage.kt is recompiled as well:
File(projectToBeModified, "src/main/kotlin/foo.kt").modify { it.replace("Int = 1", "String = \"abc\"") } val fooKtSourceFile = secondProject.projectPath.resolve("src/main/kotlin/foo.kt")
build("assemble", projectDir = projectToBeModified) { fooKtSourceFile.modify { it.replace("Int = 1", "String = \"abc\"") }
assertSuccessful() secondProject.build("assemble", forceOutput = true) {
assertCompiledKotlinSources(relativize(allKotlinFiles)) assertIncrementalCompilation(modifiedFiles = setOf(fooKtSourceFile))
} }
// Revert the change to the return type of foo(), and check if we get a cache hit // Revert the change to the return type of foo(), and check if we get a cache hit
File(projectToBeModified, "src/main/kotlin/foo.kt").modify { it.replace("String = \"abc\"", "Int = 1") } fooKtSourceFile.modify { it.replace("String = \"abc\"", "Int = 1") }
build("clean", "assemble", projectDir = projectToBeModified) { secondProject.build("clean", "assemble") {
assertSuccessful() assertTasksFromCache(":compileKotlin")
assertContains(":compileKotlin FROM-CACHE")
}
} }
} }
private fun checkKaptCachingIncrementalBuild(projectToBeModified: File, project: BaseGradleIT.Project) { private fun checkKaptCachingIncrementalBuild(
with(project) { firstProject: TestProject,
val options = defaultBuildOptions().copy( secondProject: TestProject
kaptOptions = KaptOptions( ) {
val options = defaultBuildOptions.copy(
kaptOptions = BuildOptions.KaptOptions(
verbose = true, verbose = true,
useWorkers = false, useWorkers = false,
incrementalKapt = true, incrementalKapt = true,
@@ -179,52 +194,30 @@ class BuildCacheIT : BaseGradleIT() {
) )
// First build, should be stored into the build cache: // First build, should be stored into the build cache:
build(options = options, params = *arrayOf("clean", ":app:build")) { firstProject.build("clean", ":app:build", buildOptions = options) {
assertSuccessful() assertTasksPackedToCache(":app:kaptGenerateStubsKotlin", ":app:kaptKotlin")
assertTaskPackedToCache(":app:kaptGenerateStubsKotlin")
assertTaskPackedToCache(":app:kaptKotlin")
} }
// A cache hit: a clean build without any changes to the project // A cache hit: a clean build without any changes to the project
build(options = options, params = *arrayOf("clean", ":app:build"), projectDir = projectToBeModified) { secondProject.build("clean", ":app:build", buildOptions = options) {
assertSuccessful() assertTasksFromCache(":app:kaptGenerateStubsKotlin", ":app:kaptKotlin")
assertContains(":app:kaptGenerateStubsKotlin FROM-CACHE")
assertContains(":app:kaptKotlin FROM-CACHE")
} }
// Make changes to annotated class and check kapt tasks are re-executed // Make changes to annotated class and check kapt tasks are re-executed
File(projectToBeModified, "app/src/main/kotlin/AppClass.kt").modify { val appClassKtSourceFile = secondProject.projectPath.resolve("app/src/main/kotlin/AppClass.kt")
appClassKtSourceFile.modify {
it.replace("val testVal: String = \"text\"", "val testVal: Int = 1") it.replace("val testVal: String = \"text\"", "val testVal: Int = 1")
} }
build(options = options, params = *arrayOf("build"), projectDir = projectToBeModified) { secondProject.build("build", buildOptions = options) {
assertSuccessful() assertTasksExecuted(":app:kaptGenerateStubsKotlin", ":app:kaptKotlin")
assertContains("':app:kaptGenerateStubsKotlin' is not up-to-date")
assertContains("':app:kaptKotlin' is not up-to-date")
} }
// Revert changes and check kapt tasks are from cache // Revert changes and check kapt tasks are from cache
File(projectToBeModified, "app/src/main/kotlin/AppClass.kt").modify { appClassKtSourceFile.modify {
it.replace("val testVal: Int = 1", "val testVal: String = \"text\"") it.replace("val testVal: Int = 1", "val testVal: String = \"text\"")
} }
build(options = options, params = *arrayOf("clean", "build"), projectDir = projectToBeModified) { secondProject.build("clean", "build", buildOptions = options) {
assertSuccessful() assertTasksFromCache(":app:kaptGenerateStubsKotlin", ":app:kaptKotlin")
assertContains(":app:kaptGenerateStubsKotlin FROM-CACHE")
assertContains(":app:kaptKotlin FROM-CACHE")
} }
} }
} }
}
fun BaseGradleIT.Project.prepareLocalBuildCache(directory: File = File(projectDir.parentFile, "buildCache").apply { mkdir() }): File {
if (!projectDir.exists()) {
setupWorkingDir()
}
File(projectDir, "settings.gradle").appendText("\nbuildCache.local.directory = '${directory.absolutePath.replace("\\", "/")}'")
return directory
}
internal fun BaseGradleIT.CompiledProject.assertTaskPackedToCache(taskPath: String) {
with(project.testCase) {
assertContainsRegex(Regex("(?:Packing|Stored cache entry for) task '${Regex.escape(taskPath)}'"))
}
}
@@ -206,4 +206,18 @@ class BuildCacheRelocationIT : BaseGradleIT() {
.map { it.relativeTo(dir) to it.readBytes().contentHashCode() } .map { it.relativeTo(dir) to it.readBytes().contentHashCode() }
.toList() .toList()
} }
private fun BaseGradleIT.Project.prepareLocalBuildCache(directory: File = File(projectDir.parentFile, "buildCache").apply { mkdir() }): File {
if (!projectDir.exists()) {
setupWorkingDir()
}
File(projectDir, "settings.gradle").appendText("\nbuildCache.local.directory = '${directory.absolutePath.replace("\\", "/")}'")
return directory
}
private fun CompiledProject.assertTaskPackedToCache(taskPath: String) {
with(project.testCase) {
assertContainsRegex(Regex("(?:Packing|Stored cache entry for) task '${Regex.escape(taskPath)}'"))
}
}
} }
@@ -1,21 +1,9 @@
buildscript { plugins {
repositories { id 'org.jetbrains.kotlin.jvm'
mavenLocal() id 'java'
mavenCentral()
} }
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: "kotlin"
apply plugin: 'java'
repositories { repositories {
mavenLocal() mavenLocal()
mavenCentral() mavenCentral()
} }
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
@@ -1,9 +1,10 @@
apply plugin: "kotlin" plugins {
apply plugin: "kotlin-kapt" id "org.jetbrains.kotlin.jvm"
id "org.jetbrains.kotlin.kapt"
}
dependencies { dependencies {
implementation project(":lib") implementation project(":lib")
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version" implementation "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
kapt "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version" kapt "org.jetbrains.kotlin:annotation-processor-example:$kotlin_version"
@@ -1,13 +1,7 @@
buildscript { plugins {
repositories { id "org.jetbrains.kotlin.jvm" apply false
mavenLocal() id "org.jetbrains.kotlin.kapt" apply false
mavenCentral()
} }
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
subprojects { subprojects {
repositories { repositories {
@@ -1,5 +1,3 @@
apply plugin: "kotlin" plugins {
id "org.jetbrains.kotlin.jvm"
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
} }
@@ -1 +1,13 @@
pluginManagement {
repositories {
mavenLocal()
mavenCentral()
}
plugins {
id "org.jetbrains.kotlin.jvm" version "$kotlin_version"
id "org.jetbrains.kotlin.kapt" version "$kotlin_version"
}
}
include ':app', ':lib' include ':app', ':lib'