From 20b14d4121c39222dd1302be76f75bc374f851a2 Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Mon, 10 Jul 2017 06:18:43 +0300 Subject: [PATCH] Extract IncrementalJvmCompilerRunnerTest --- ...t => IncrementalCompilerRunnerTestBase.kt} | 66 ++++++------------- .../IncrementalJvmCompilerRunnerTest.kt | 39 +++++++++++ .../utils/TestCompilationResult.kt | 31 +++++++++ .../incremental/utils/TestICReporter.kt | 39 +++++++++++ 4 files changed, 128 insertions(+), 47 deletions(-) rename compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/{KotlinStandaloneIncrementalCompilationTest.kt => IncrementalCompilerRunnerTestBase.kt} (73%) create mode 100644 compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunnerTest.kt create mode 100644 compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestCompilationResult.kt create mode 100644 compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestICReporter.kt diff --git a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/KotlinStandaloneIncrementalCompilationTest.kt b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalCompilerRunnerTestBase.kt similarity index 73% rename from compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/KotlinStandaloneIncrementalCompilationTest.kt rename to compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalCompilerRunnerTestBase.kt index 3df496e5bfb..db0f627fe30 100644 --- a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/KotlinStandaloneIncrementalCompilationTest.kt +++ b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalCompilerRunnerTestBase.kt @@ -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 : 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.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, val compileErrors: Collection) - - private fun make(cacheDir: File, sourceRoots: Iterable, args: K2JVMCompilerArguments): CompilationResult { - val compiledSources = arrayListOf() - var resultExitCode = ExitCode.OK - - val reporter = object : ICReporter { - override fun report(message: ()->String) { - } - - override fun reportCompileIteration(sourceFiles: Collection, 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, args: Args): TestCompilationResult private fun stepLogAsString(step: Int, ktSources: Iterable, errors: Collection, 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() } } -} +} \ No newline at end of file diff --git a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunnerTest.kt b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunnerTest.kt new file mode 100644 index 00000000000..2a5562e0d76 --- /dev/null +++ b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/IncrementalJvmCompilerRunnerTest.kt @@ -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() { + override fun make(cacheDir: File, sourceRoots: Iterable, 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")) + } +} \ No newline at end of file diff --git a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestCompilationResult.kt b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestCompilationResult.kt new file mode 100644 index 00000000000..5cebbd97934 --- /dev/null +++ b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestCompilationResult.kt @@ -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, + val compileErrors: Collection +) { + constructor( + icReporter: TestICReporter, + messageCollector: TestMessageCollector + ) : this(icReporter.exitCode, icReporter.compiledSources, messageCollector.errors) +} \ No newline at end of file diff --git a/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestICReporter.kt b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestICReporter.kt new file mode 100644 index 00000000000..3372e0c7473 --- /dev/null +++ b/compiler/incremental-compilation-impl/test/org/jetbrains/kotlin/incremental/utils/TestICReporter.kt @@ -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() + + val compiledSources: List + get() = compiledSourcesMutable + + var exitCode: ExitCode = ExitCode.OK + private set + + override fun report(message: ()->String) { + } + + override fun reportCompileIteration(sourceFiles: Collection, exitCode: ExitCode) { + compiledSourcesMutable.addAll(sourceFiles) + this.exitCode = exitCode + } +} \ No newline at end of file