Add initial test setup to enable and test Gradle build cache.

This will allow to write JUnit5 tests to verify tasks are re-using
build cache on rebuilding same project from clean state.

^KT-45611 In Progress
This commit is contained in:
Yahor Berdnikau
2021-04-26 10:43:01 +02:00
committed by TeamCityServer
parent ddf451739b
commit 007f11e22e
3 changed files with 37 additions and 4 deletions
@@ -7,7 +7,6 @@ import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Tag
@Tag("JUnit5")
@DisplayName("KGP simple tests") @DisplayName("KGP simple tests")
class SimpleKotlinGradleIT : KGPBaseTest() { class SimpleKotlinGradleIT : KGPBaseTest() {
@@ -11,6 +11,7 @@ import org.gradle.internal.impldep.org.junit.platform.commons.support.Annotation
import org.gradle.util.GradleVersion import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.BaseGradleIT import org.jetbrains.kotlin.gradle.BaseGradleIT
import org.jetbrains.kotlin.test.WithMuteInDatabase import org.jetbrains.kotlin.test.WithMuteInDatabase
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.TestInstance import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtensionContext import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.io.TempDir import org.junit.jupiter.api.io.TempDir
@@ -23,6 +24,7 @@ import kotlin.streams.asStream
/** /**
* Base class for all Kotlin Gradle plugin integration tests. * Base class for all Kotlin Gradle plugin integration tests.
*/ */
@Tag("JUnit5")
@TestInstance(TestInstance.Lifecycle.PER_CLASS) @TestInstance(TestInstance.Lifecycle.PER_CLASS)
@WithMuteInDatabase @WithMuteInDatabase
abstract class KGPBaseTest { abstract class KGPBaseTest {
@@ -40,6 +42,7 @@ abstract class KGPBaseTest {
val parallel: Boolean = true, val parallel: Boolean = true,
val maxWorkers: Int = (Runtime.getRuntime().availableProcessors() / 4 - 1).coerceAtLeast(2), val maxWorkers: Int = (Runtime.getRuntime().availableProcessors() / 4 - 1).coerceAtLeast(2),
val fileSystemWatchEnabled: Boolean = false, val fileSystemWatchEnabled: Boolean = false,
val buildCacheEnabled: Boolean = false,
) { ) {
fun toArguments( fun toArguments(
gradleVersion: GradleVersion gradleVersion: GradleVersion
@@ -79,6 +82,8 @@ abstract class KGPBaseTest {
} }
} }
arguments.add(if (buildCacheEnabled) "--build-cache" else "--no-build-cache")
return arguments.toList() return arguments.toList()
} }
} }
@@ -34,12 +34,14 @@ fun KGPBaseTest.project(
forceOutput: Boolean = false, forceOutput: Boolean = false,
addHeapDumpOptions: Boolean = true, addHeapDumpOptions: Boolean = true,
enableGradleDebug: Boolean = false, enableGradleDebug: Boolean = false,
projectPathAdditionalSuffix: String = "",
test: TestProject.() -> Unit test: TestProject.() -> Unit
): TestProject { ): TestProject {
val projectPath = setupProjectFromTestResources( val projectPath = setupProjectFromTestResources(
projectName, projectName,
gradleVersion, gradleVersion,
workingDir workingDir,
projectPathAdditionalSuffix
) )
projectPath.addDefaultBuildFiles() projectPath.addDefaultBuildFiles()
if (addHeapDumpOptions) projectPath.addHeapDumpOptions() if (addHeapDumpOptions) projectPath.addHeapDumpOptions()
@@ -93,13 +95,38 @@ fun TestProject.buildAndFail(
assertions(buildResult) assertions(buildResult)
} }
fun TestProject.enableLocalBuildCache(
buildCacheLocation: Path
) {
// language=Groovy
settingsGradle.append(
"""
buildCache {
local {
directory = '${buildCacheLocation.toUri()}'
}
}
""".trimIndent()
)
}
fun TestProject.enableBuildCacheDebug() {
gradleProperties.append(
"org.gradle.caching.debug=true"
)
}
class TestProject( class TestProject(
val gradleRunner: GradleRunner, val gradleRunner: GradleRunner,
val projectName: String, val projectName: String,
val buildOptions: KGPBaseTest.BuildOptions, val buildOptions: KGPBaseTest.BuildOptions,
val projectPath: Path, val projectPath: Path,
val gradleVersion: GradleVersion val gradleVersion: GradleVersion
) ) {
val rootBuildGradle: Path get() = projectPath.resolve("build.gradle")
val settingsGradle: Path get() = projectPath.resolve("settings.gradle")
val gradleProperties: Path get() = projectPath.resolve("gradle.properties")
}
private fun TestProject.commonBuildSetup( private fun TestProject.commonBuildSetup(
buildArguments: List<String> buildArguments: List<String>
@@ -123,7 +150,8 @@ private val testKitDir get() = Paths.get(".").resolve(".testKitDir")
private fun setupProjectFromTestResources( private fun setupProjectFromTestResources(
projectName: String, projectName: String,
gradleVersion: GradleVersion, gradleVersion: GradleVersion,
tempDir: Path tempDir: Path,
optionalSubDir: String
): Path { ): Path {
val testProjectPath = Paths.get("src", "test", "resources", "testProject", projectName) val testProjectPath = Paths.get("src", "test", "resources", "testProject", projectName)
assertTrue("Test project exists") { Files.exists(testProjectPath) } assertTrue("Test project exists") { Files.exists(testProjectPath) }
@@ -132,6 +160,7 @@ private fun setupProjectFromTestResources(
return tempDir return tempDir
.resolve(projectName) .resolve(projectName)
.resolve(gradleVersion.version) .resolve(gradleVersion.version)
.resolve(optionalSubDir)
.also { .also {
testProjectPath.copyRecursively(it) testProjectPath.copyRecursively(it)
} }