Add BuildCacheRelocationIT, remove same files testing as redundant.

This commit is contained in:
Sergey Igushkin
2017-12-13 19:54:36 +03:00
parent 6bea643176
commit c5a88c5fb7
2 changed files with 161 additions and 45 deletions
@@ -164,51 +164,12 @@ class BuildCacheIT : BaseGradleIT() {
assertContains(":kaptKotlin FROM-CACHE")
}
}
}
@Test
fun testCacheSameFiles() = testSameFiles(listOf(
Project("simpleProject", GRADLE_VERSION),
Project("simple", GRADLE_VERSION, directoryPrefix = "kapt2").apply {
setupWorkingDir()
File(projectDir, "build.gradle").appendText("\nafterEvaluate { kaptKotlin.useBuildCache = true }")
}))
private fun testSameFiles(projects: List<Project>) {
projects.forEach { project ->
try {
project.prepareLocalBuildCache()
lateinit var nonCacheOutputHashes: Map<File, Int>
project.build("build") {
assertSuccessful()
nonCacheOutputHashes = getOutputFiles(File(project.projectDir, "build"))
}
project.build("clean", "build") {
assertSuccessful()
val outputFromCacheHashes = getOutputFiles(File(project.projectDir, "build"))
assertEquals(nonCacheOutputHashes, outputFromCacheHashes)
}
}
catch (e: AssertionError) {
throw AssertionError("Failed for project ${project.projectName}", e)
}
}
fun BaseGradleIT.Project.prepareLocalBuildCache(directory: File = Files.createTempDirectory("GradleTestBuildCache").toFile()): File {
if (!projectDir.exists()) {
setupWorkingDir()
}
private fun Project.prepareLocalBuildCache() {
if (!projectDir.exists()) {
setupWorkingDir()
}
val newCacheDirPath = Files.createTempDirectory("GradleTestBuildCache").toFile().absolutePath.replace("\\", "/")
File(projectDir, "settings.gradle").appendText("\nbuildCache.local.directory = '$newCacheDirPath'")
}
private val outputExtensions = setOf("java", "kt", "class", "kotlin_module")
private fun getOutputFiles(directory: File) = directory.walkTopDown()
.filter { it.extension in outputExtensions }
.associate { it to it.readBytes().contentHashCode() }
File(projectDir, "settings.gradle").appendText("\nbuildCache.local.directory = '${directory.absolutePath.replace("\\", "/")}'")
return directory
}
@@ -0,0 +1,155 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.gradle
import org.jetbrains.kotlin.gradle.util.modify
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.io.File
import kotlin.test.assertEquals
@RunWith(Parameterized::class)
class BuildCacheRelocationIT : BaseGradleIT() {
override fun defaultBuildOptions(): BuildOptions =
super.defaultBuildOptions().copy(
withBuildCache = true,
androidGradlePluginVersion = "3.0.0",
androidHome = File(ANDROID_HOME_PATH))
@Parameterized.Parameter
lateinit var testCase: TestCase
@Test
fun testRelocation() = with(testCase) {
val localBuildCacheDirectory = createTempDir("buildCache$projectName")
val originalWorkingDir = workingDir
val workingDirs = (0..1).map { createTempDir("BuildCacheRelocationIT$it") }
val (firstProject, secondProject) = (0..1).map { id ->
workingDir = workingDirs[id]
Project(projectName, "4.4", projectDirectoryPrefix).apply {
setupWorkingDir()
initProject()
prepareLocalBuildCache(localBuildCacheDirectory)
}
}
try {
lateinit var firstOutputHashes: List<Pair<File, Int>>
workingDir = workingDirs[0]
firstProject.build("build") {
assertSuccessful()
firstOutputHashes = hashOutputFiles(outputRoots)
cacheableTaskNames.forEach { assertContains("Packing task ':$it") }
}
workingDir = workingDirs[1]
secondProject.build("build") {
assertSuccessful()
val secondOutputHashes = hashOutputFiles(outputRoots)
assertEquals(firstOutputHashes, secondOutputHashes)
cacheableTaskNames.forEach { assertContains(":$it FROM-CACHE") }
}
}
finally {
workingDir = originalWorkingDir
workingDirs.forEach { it.deleteRecursively() }
}
}
class TestCase(
val projectName: String,
val cacheableTaskNames: List<String>,
val projectDirectoryPrefix: String? = null,
val outputRootPaths: List<String> = listOf("build"),
val initProject: Project.() -> Unit = { }) {
override fun toString(): String = (projectDirectoryPrefix?.plus("/") ?: "") + projectName
val CompiledProject.outputRoots
get() = outputRootPaths.map { outputRoot ->
File(project.projectDir, outputRoot)
}
}
companion object {
@JvmStatic
@Parameterized.Parameters(name = "project: {0}")
fun testCases(): List<Array<TestCase>> = listOf(
TestCase("simpleProject",
cacheableTaskNames = listOf("compileKotlin", "compileTestKotlin")
),
TestCase("simple", projectDirectoryPrefix = "kapt2",
cacheableTaskNames = listOf(
"kaptKotlin", "kaptGenerateStubsKotlin", "compileKotlin", "compileTestKotlin", "compileJava"),
initProject = { File(projectDir, "build.gradle").appendText("\nkapt.useBuildCache = true") }
),
TestCase("kotlin2JsDceProject",
cacheableTaskNames = listOf("mainProject", "libraryProject").map { "$it:compileKotlin2Js" } +
"mainProject:runDceKotlinJs",
initProject = {
// Fix the problem that the destinationDir of the compile task (i.e. buildDir) contains files from other tasks:
File(projectDir, "mainProject/build.gradle").modify { it.replace("/exampleapp.js", "/web/exampleapp.js") }
File(projectDir, "libraryProject/build.gradle").modify { it.replace("/examplelib.js", "/web/examplelib.js") }
// Fix assembling the JAR from the whole buildDir
File(projectDir, "libraryProject/build.gradle").modify {
it.replace("from buildDir", "from compileKotlin2Js.destinationDir")
}
}
),
TestCase("multiplatformProject",
cacheableTaskNames = listOf(
"lib:compileKotlinCommon", "libJvm:compileKotlin", "libJvm:compileTestKotlin",
"libJs:compileKotlin2Js", "libJs:compileTestKotlin2Js"),
outputRootPaths = listOf("lib", "libJvm", "libJs").map { "$it/build" }
),
TestCase("AndroidProject",
cacheableTaskNames = listOf("Lib", "Android").flatMap { module ->
listOf("Flavor1", "Flavor2").flatMap { flavor ->
listOf("Debug", "Release").map { buildType ->
"$module:compile$flavor${buildType}Kotlin"
}
}
},
outputRootPaths = listOf("Lib", "Android", "Test").map { "$it/build" }
),
TestCase("android-dagger", projectDirectoryPrefix = "kapt2",
cacheableTaskNames = listOf("Debug", "Release").flatMap { buildType ->
listOf("kapt", "kaptGenerateStubs", "compile").map { kotlinTask ->
"app:$kotlinTask${buildType}Kotlin"
}
},
outputRootPaths = listOf("app/build"),
initProject = { File(projectDir, "app/build.gradle").appendText("\nkapt.useBuildCache = true") }
)
).map { arrayOf(it) }
}
private val outputExtensions = setOf("java", "kt", "class", "js", "kotlin_module")
fun hashOutputFiles(directories: List<File>) =
directories.flatMap { dir ->
dir.walkTopDown()
.filter { it.extension in outputExtensions }
.map { it.relativeTo(dir) to it.readBytes().contentHashCode() }
.toList()
}
}