KT-34862: Clean up tests for incremental compilation after cache hit

Make sure that:
  - Compilation with -Pkotlin.incremental.useClasspathSnapshot=true or
    -Dkotlin.incremental.classpath.snapshot.enabled=true is incremental
    after cache hit.
  - Default compilation (using build history files) is non-incremental
    after cache hit.

Also unify the related tests into
BuildCacheRelocationIT.testKotlinIncrementalCompilation*. We don't need
corresponding tests in BuildCacheIT because BuildCacheRelocationIT
already covers it.
This commit is contained in:
Hung Nguyen
2022-02-09 10:52:21 +00:00
committed by nataliya.valtman
parent 33931bf010
commit 39b7c6e326
8 changed files with 56 additions and 33 deletions
@@ -22,7 +22,6 @@ import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.gradle.testbase.*
import org.junit.jupiter.api.DisplayName
import kotlin.io.path.createDirectory
import kotlin.io.path.pathString
@DisplayName("Build cache relocation")
@SimpleGradlePluginTests
@@ -328,12 +327,25 @@ class BuildCacheRelocationIT : KGPBaseTest() {
@DisplayName("Kotlin incremental compilation with `kotlin.incremental.useClasspathSnapshot` feature should work correctly")
@GradleTest
fun testKotlinIncrementalCompilation_withClasspathSnapshot(gradleVersion: GradleVersion) {
checkKotlinIncrementalCompilation(gradleVersion, useClasspathSnapshot = true)
fun testKotlinIncrementalCompilation_withGradleClasspathSnapshot(gradleVersion: GradleVersion) {
checkKotlinIncrementalCompilation(gradleVersion, useGradleClasspathSnapshot = true)
}
private fun checkKotlinIncrementalCompilation(gradleVersion: GradleVersion, useClasspathSnapshot: Boolean? = null) {
val buildOptions = defaultBuildOptions.copy(useClasspathSnapshot = useClasspathSnapshot)
@DisplayName("Kotlin incremental compilation with `kotlin.incremental.classpath.snapshot.enabled` feature should work correctly")
@GradleTest
fun testKotlinIncrementalCompilation_withICClasspathSnapshot(gradleVersion: GradleVersion) {
checkKotlinIncrementalCompilation(gradleVersion, useICClasspathSnapshot = true)
}
private fun checkKotlinIncrementalCompilation(
gradleVersion: GradleVersion,
useGradleClasspathSnapshot: Boolean? = null,
useICClasspathSnapshot: Boolean? = null
) {
val buildOptions = defaultBuildOptions.copy(
useGradleClasspathSnapshot = useGradleClasspathSnapshot,
useICClasspathSnapshot = useICClasspathSnapshot
)
val (firstProject, secondProject) = prepareTestProjects("buildCacheSimple", gradleVersion, buildOptions)
// First build, should be stored into the build cache:
@@ -346,14 +358,15 @@ class BuildCacheRelocationIT : KGPBaseTest() {
assertTasksFromCache(":compileKotlin")
}
// Change the return type of foo() from Int to String in foo.kt, and check that fooUsage.kt is recompiled as well:
// Check whether compilation after a cache hit is incremental (KT-34862)
val fooKtSourceFile = secondProject.kotlinSourcesDir().resolve("foo.kt")
val fooUsageKtSourceFile = secondProject.kotlinSourcesDir().resolve("fooUsage.kt")
fooKtSourceFile.modify { it.replace("Int = 1", "String = \"abc\"") }
secondProject.build("assemble", buildOptions = buildOptions.copy(logLevel = LogLevel.DEBUG)) {
assertIncrementalCompilation(
listOf(fooKtSourceFile, fooUsageKtSourceFile).relativizeTo(secondProject.projectPath).map { it.pathString }
)
if (useGradleClasspathSnapshot == true || useICClasspathSnapshot == true) {
assertIncrementalCompilation(listOf("src/main/kotlin/foo.kt", "src/main/kotlin/fooUsage.kt"))
} else {
assertNonIncrementalCompilation()
}
}
// Revert the change to the return type of foo(), and check if we get a cache hit
@@ -9,6 +9,7 @@ import org.gradle.api.logging.LogLevel
import org.gradle.api.logging.configuration.WarningMode
import org.gradle.util.GradleVersion
import org.jetbrains.kotlin.cli.common.CompilerSystemProperties.COMPILE_INCREMENTAL_WITH_ARTIFACT_TRANSFORM
import org.jetbrains.kotlin.cli.common.CompilerSystemProperties.COMPILE_INCREMENTAL_WITH_CLASSPATH_SNAPSHOTS
import org.jetbrains.kotlin.gradle.BaseGradleIT
import org.jetbrains.kotlin.gradle.plugin.KotlinJsCompilerType
import org.jetbrains.kotlin.gradle.report.BuildReportType
@@ -22,7 +23,8 @@ data class BuildOptions(
val configurationCacheProblems: BaseGradleIT.ConfigurationCacheProblems = BaseGradleIT.ConfigurationCacheProblems.FAIL,
val parallel: Boolean = true,
val incremental: Boolean? = null,
val useClasspathSnapshot: Boolean? = null,
val useGradleClasspathSnapshot: Boolean? = null,
val useICClasspathSnapshot: Boolean? = null,
val maxWorkers: Int = (Runtime.getRuntime().availableProcessors() / 4 - 1).coerceAtLeast(2),
val fileSystemWatchEnabled: Boolean = false,
val buildCacheEnabled: Boolean = false,
@@ -83,7 +85,8 @@ data class BuildOptions(
arguments.add("-Pkotlin.incremental=$incremental")
}
useClasspathSnapshot?.let { arguments.add("-P${COMPILE_INCREMENTAL_WITH_ARTIFACT_TRANSFORM.property}=$it") }
useGradleClasspathSnapshot?.let { arguments.add("-P${COMPILE_INCREMENTAL_WITH_ARTIFACT_TRANSFORM.property}=$it") }
useICClasspathSnapshot?.let { arguments.add("-D${COMPILE_INCREMENTAL_WITH_CLASSPATH_SNAPSHOTS.property}=$it") }
if (gradleVersion >= GradleVersion.version("6.5")) {
if (fileSystemWatchEnabled) {
@@ -84,6 +84,16 @@ fun assertCompiledKotlinSources(
assertSameFiles(expectedSources, actualSources, "Compiled Kotlin files differ${errorMessageSuffix}:\n")
}
/**
* Asserts that compilation was non-incremental.
*
* Note: Log level of output must be set to [LogLevel.DEBUG].
*/
@Suppress("unused")
fun BuildResult.assertNonIncrementalCompilation() {
assertOutputContains("Non-incremental compilation will be performed")
}
/**
* Asserts that compilation was incremental and the set of compiled .kt files exactly match [expectedCompiledKotlinFiles].
*