From c5a88c5fb720ad81ad9771dd7e1034606d7bfbde Mon Sep 17 00:00:00 2001 From: Sergey Igushkin Date: Wed, 13 Dec 2017 19:54:36 +0300 Subject: [PATCH] Add BuildCacheRelocationIT, remove same files testing as redundant. --- .../jetbrains/kotlin/gradle/BuildCacheIT.kt | 51 +----- .../kotlin/gradle/BuildCacheRelocationIT.kt | 155 ++++++++++++++++++ 2 files changed, 161 insertions(+), 45 deletions(-) create mode 100644 libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheRelocationIT.kt diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheIT.kt index 2a19a2e1110..507581cbb8e 100644 --- a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheIT.kt +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheIT.kt @@ -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) { - projects.forEach { project -> - try { - project.prepareLocalBuildCache() - - lateinit var nonCacheOutputHashes: Map - - 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 } \ No newline at end of file diff --git a/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheRelocationIT.kt b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheRelocationIT.kt new file mode 100644 index 00000000000..49286be4e7f --- /dev/null +++ b/libraries/tools/kotlin-gradle-plugin-integration-tests/src/test/kotlin/org/jetbrains/kotlin/gradle/BuildCacheRelocationIT.kt @@ -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> + + 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, + val projectDirectoryPrefix: String? = null, + val outputRootPaths: List = 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> = 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) = + directories.flatMap { dir -> + dir.walkTopDown() + .filter { it.extension in outputExtensions } + .map { it.relativeTo(dir) to it.readBytes().contentHashCode() } + .toList() + } +} \ No newline at end of file