Extract IncrementalJvmCompilerRunnerTest

This commit is contained in:
Alexey Tsvetkov
2017-07-10 06:18:43 +03:00
parent 6fedf07f56
commit 20b14d4121
4 changed files with 128 additions and 47 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2016 JetBrains s.r.o.
* 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.
@@ -19,20 +19,17 @@ package org.jetbrains.kotlin.incremental
import com.intellij.util.containers.HashMap
import org.jetbrains.kotlin.TestWithWorkingDir
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.incremental.testingUtils.*
import org.jetbrains.kotlin.incremental.utils.TestMessageCollector
import org.jetbrains.kotlin.incremental.utils.TestCompilationResult
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import java.io.File
import java.util.*
@RunWith(Parameterized::class)
class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
abstract class IncrementalCompilerRunnerTestBase<Args : CommonCompilerArguments> : TestWithWorkingDir() {
@Parameterized.Parameter
lateinit var testDir: File
@@ -40,6 +37,8 @@ class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
@Parameterized.Parameter(value = 1)
lateinit var readableName: String
protected abstract fun createCompilerArguments(destinationDir: File, testDir: File): Args
@Test
fun testFromJps() {
fun Iterable<File>.relativePaths() =
@@ -51,10 +50,7 @@ class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
val mapWorkingToOriginalFile = HashMap(copyTestSources(testDir, srcDir, filePrefix = ""))
val sourceRoots = listOf(srcDir)
val args = K2JVMCompilerArguments()
args.destination = outDir.path
args.moduleName = testDir.name
args.classpath = compileClasspath()
val args = createCompilerArguments(outDir, testDir)
// initial build
make(cacheDir, sourceRoots, args)
@@ -62,9 +58,9 @@ class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
val buildLogFile = buildLogFinder.findBuildLog(testDir) ?: throw IllegalStateException("build log file not found in $workingDir")
val buildLogSteps = parseTestBuildLog(buildLogFile)
val modifications = getModificationsToPerform(testDir,
moduleNames = null,
allowNoFilesWithSuffixInTestData = false,
touchPolicy = TouchPolicy.CHECKSUM)
moduleNames = null,
allowNoFilesWithSuffixInTestData = false,
touchPolicy = TouchPolicy.CHECKSUM)
assert(modifications.size == buildLogSteps.size) {
"Modifications count (${modifications.size}) != expected build log steps count (${buildLogSteps.size})"
@@ -91,19 +87,18 @@ class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
}
if (expectedSBWithoutErrors.toString() != actualSBWithoutErrors.toString()) {
assertEquals(expectedSB.toString(), actualSB.toString())
Assert.assertEquals(expectedSB.toString(), actualSB.toString())
}
// todo: also compare caches
run rebuildAndCompareOutput@ {
val rebuildOutDir = File(workingDir, "rebuild-out").apply { mkdirs() }
val rebuildCacheDir = File(workingDir, "rebuild-cache").apply { mkdirs() }
args.destination = rebuildOutDir.path
val rebuildResult = make(rebuildCacheDir, sourceRoots, args)
val rebuildResult = make(rebuildCacheDir, sourceRoots, createCompilerArguments(rebuildOutDir, testDir))
val rebuildExpectedToSucceed = buildLogSteps.last().compileSucceeded
val rebuildSucceeded = rebuildResult.exitCode == ExitCode.OK
assertEquals("Rebuild exit code differs from incremental exit code", rebuildExpectedToSucceed, rebuildSucceeded)
Assert.assertEquals("Rebuild exit code differs from incremental exit code", rebuildExpectedToSucceed, rebuildSucceeded)
if (rebuildSucceeded) {
assertEqualDirectories(outDir, rebuildOutDir, forgiveExtraFiles = rebuildSucceeded)
@@ -111,33 +106,7 @@ class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
}
}
private fun compileClasspath(): String {
val currentClasspath = System.getProperty("java.class.path").split(File.pathSeparator)
val stdlib = currentClasspath.find { it.contains("kotlin-stdlib") }
val runtime = currentClasspath.find { it.contains("kotlin-runtime") }
return listOf(stdlib, runtime).joinToString(File.pathSeparator)
}
data class CompilationResult(val exitCode: ExitCode, val compiledSources: Iterable<File>, val compileErrors: Collection<String>)
private fun make(cacheDir: File, sourceRoots: Iterable<File>, args: K2JVMCompilerArguments): CompilationResult {
val compiledSources = arrayListOf<File>()
var resultExitCode = ExitCode.OK
val reporter = object : ICReporter {
override fun report(message: ()->String) {
}
override fun reportCompileIteration(sourceFiles: Collection<File>, exitCode: ExitCode) {
compiledSources.addAll(sourceFiles)
resultExitCode = exitCode
}
}
val messageCollector = TestMessageCollector()
makeIncrementally(cacheDir, sourceRoots, args, reporter = reporter, messageCollector = messageCollector)
return CompilationResult(resultExitCode, compiledSources, messageCollector.errors)
}
protected abstract fun make(cacheDir: File, sourceRoots: Iterable<File>, args: Args): TestCompilationResult
private fun stepLogAsString(step: Int, ktSources: Iterable<String>, errors: Collection<String>, includeErrors: Boolean = true): String {
val sb = StringBuilder()
@@ -167,6 +136,9 @@ class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
}
companion object {
@JvmStatic
protected val bootstrapKotlincLib: File = File("dependencies/bootstrap-compiler/Kotlin/kotlinc/lib")
private val jpsResourcesPath = File("jps-plugin/testData/incremental")
private val ignoredDirs = setOf(File(jpsResourcesPath, "cacheVersionChanged"),
File(jpsResourcesPath, "changeIncrementalOption"),
@@ -201,4 +173,4 @@ class KotlinStandaloneIncrementalCompilationTest : TestWithWorkingDir() {
.toList()
}
}
}
}
@@ -0,0 +1,39 @@
/*
* 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.incremental
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.incremental.utils.TestCompilationResult
import org.jetbrains.kotlin.incremental.utils.TestICReporter
import org.jetbrains.kotlin.incremental.utils.TestMessageCollector
import java.io.File
class IncrementalJvmCompilerRunnerTest : IncrementalCompilerRunnerTestBase<K2JVMCompilerArguments>() {
override fun make(cacheDir: File, sourceRoots: Iterable<File>, args: K2JVMCompilerArguments): TestCompilationResult {
val reporter = TestICReporter()
val messageCollector = TestMessageCollector()
makeIncrementally(cacheDir, sourceRoots, args, reporter = reporter, messageCollector = messageCollector)
return TestCompilationResult(reporter, messageCollector)
}
override fun createCompilerArguments(destinationDir: File, testDir: File): K2JVMCompilerArguments =
K2JVMCompilerArguments().apply {
moduleName = testDir.name
destination = destinationDir.path
classpathAsList = listOf(File(bootstrapKotlincLib, "kotlin-stdlib.jar"))
}
}
@@ -0,0 +1,31 @@
/*
* 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.incremental.utils
import org.jetbrains.kotlin.cli.common.ExitCode
import java.io.File
data class TestCompilationResult(
val exitCode: ExitCode,
val compiledSources: Iterable<File>,
val compileErrors: Collection<String>
) {
constructor(
icReporter: TestICReporter,
messageCollector: TestMessageCollector
) : this(icReporter.exitCode, icReporter.compiledSources, messageCollector.errors)
}
@@ -0,0 +1,39 @@
/*
* 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.incremental.utils
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.incremental.ICReporter
import java.io.File
class TestICReporter : ICReporter {
private val compiledSourcesMutable = arrayListOf<File>()
val compiledSources: List<File>
get() = compiledSourcesMutable
var exitCode: ExitCode = ExitCode.OK
private set
override fun report(message: ()->String) {
}
override fun reportCompileIteration(sourceFiles: Collection<File>, exitCode: ExitCode) {
compiledSourcesMutable.addAll(sourceFiles)
this.exitCode = exitCode
}
}