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.Tag
@Tag("JUnit5")
@DisplayName("KGP simple tests")
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.jetbrains.kotlin.gradle.BaseGradleIT
import org.jetbrains.kotlin.test.WithMuteInDatabase
import org.junit.jupiter.api.Tag
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtensionContext
import org.junit.jupiter.api.io.TempDir
@@ -23,6 +24,7 @@ import kotlin.streams.asStream
/**
* Base class for all Kotlin Gradle plugin integration tests.
*/
@Tag("JUnit5")
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@WithMuteInDatabase
abstract class KGPBaseTest {
@@ -40,6 +42,7 @@ abstract class KGPBaseTest {
val parallel: Boolean = true,
val maxWorkers: Int = (Runtime.getRuntime().availableProcessors() / 4 - 1).coerceAtLeast(2),
val fileSystemWatchEnabled: Boolean = false,
val buildCacheEnabled: Boolean = false,
) {
fun toArguments(
gradleVersion: GradleVersion
@@ -79,6 +82,8 @@ abstract class KGPBaseTest {
}
}
arguments.add(if (buildCacheEnabled) "--build-cache" else "--no-build-cache")
return arguments.toList()
}
}
@@ -34,12 +34,14 @@ fun KGPBaseTest.project(
forceOutput: Boolean = false,
addHeapDumpOptions: Boolean = true,
enableGradleDebug: Boolean = false,
projectPathAdditionalSuffix: String = "",
test: TestProject.() -> Unit
): TestProject {
val projectPath = setupProjectFromTestResources(
projectName,
gradleVersion,
workingDir
workingDir,
projectPathAdditionalSuffix
)
projectPath.addDefaultBuildFiles()
if (addHeapDumpOptions) projectPath.addHeapDumpOptions()
@@ -93,13 +95,38 @@ fun TestProject.buildAndFail(
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(
val gradleRunner: GradleRunner,
val projectName: String,
val buildOptions: KGPBaseTest.BuildOptions,
val projectPath: Path,
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(
buildArguments: List<String>
@@ -123,7 +150,8 @@ private val testKitDir get() = Paths.get(".").resolve(".testKitDir")
private fun setupProjectFromTestResources(
projectName: String,
gradleVersion: GradleVersion,
tempDir: Path
tempDir: Path,
optionalSubDir: String
): Path {
val testProjectPath = Paths.get("src", "test", "resources", "testProject", projectName)
assertTrue("Test project exists") { Files.exists(testProjectPath) }
@@ -132,6 +160,7 @@ private fun setupProjectFromTestResources(
return tempDir
.resolve(projectName)
.resolve(gradleVersion.version)
.resolve(optionalSubDir)
.also {
testProjectPath.copyRecursively(it)
}