Isolate JS compiler in proto tests

Proto tests are still in the 'jps-tests' module
which is included in non-compiler tests.

It is not safe to call the compiler
directly in non-compiler tests
because it might affect IDE tests.

Original commit: 1cce1ef1f6
This commit is contained in:
Alexey Tsvetkov
2017-07-31 22:01:31 +03:00
parent 8ffde5fa69
commit 0a78d2d299
4 changed files with 68 additions and 15 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.jps.incremental
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JsArgumentConstants
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import org.jetbrains.kotlin.compilerRunner.OutputItemsCollectorImpl
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.incremental.ClassProtoData
import org.jetbrains.kotlin.incremental.Difference
@@ -46,7 +47,6 @@ abstract class AbstractJsProtoComparisonTest : AbstractProtoComparisonTest<Proto
override fun compileAndGetClasses(sourceDir: File, outputDir: File): Map<ClassId, ProtoData> {
val incrementalResults = IncrementalResultsConsumerImpl()
// todo: find out if it is safe to call directly
val services = Services.Builder().run {
register(IncrementalResultsConsumer::class.java, incrementalResults)
build()
@@ -54,6 +54,7 @@ abstract class AbstractJsProtoComparisonTest : AbstractProtoComparisonTest<Proto
val ktFiles = sourceDir.walkMatching { it.name.endsWith(".kt") }.map { it.canonicalPath }.toList()
val messageCollector = TestMessageCollector()
val outputItemsCollector = OutputItemsCollectorImpl()
val args = K2JSCompilerArguments().apply {
outputFile = File(outputDir, "out.js").canonicalPath
metaInfo = true
@@ -61,9 +62,10 @@ abstract class AbstractJsProtoComparisonTest : AbstractProtoComparisonTest<Proto
freeArgs.addAll(ktFiles)
}
K2JSCompiler().exec(messageCollector, services, args).let { exitCode ->
val env = createTestingCompilerEnvironment(messageCollector, outputItemsCollector, services)
runJSCompiler(args, env).let { exitCode ->
val expectedOutput = "OK"
val actualOutput = (listOf(exitCode.name) + messageCollector.errors).joinToString("\n")
val actualOutput = (listOf(exitCode?.name) + messageCollector.errors).joinToString("\n")
Assert.assertEquals(expectedOutput, actualOutput)
}
@@ -16,14 +16,11 @@
package org.jetbrains.kotlin.jps.incremental
import com.intellij.openapi.util.io.FileUtil
import com.intellij.testFramework.UsefulTestCase
import org.jetbrains.kotlin.TestWithWorkingDir
import org.jetbrains.kotlin.incremental.Difference
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.test.KotlinTestUtils
import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.keysToMap
import java.io.File
abstract class AbstractProtoComparisonTest<PROTO_DATA> : TestWithWorkingDir() {
@@ -0,0 +1,49 @@
/*
* 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.jps.incremental
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.K2JSCompilerArguments
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.cli.js.K2JSCompiler
import org.jetbrains.kotlin.compilerRunner.*
import org.jetbrains.kotlin.config.Services
import org.jetbrains.kotlin.jps.build.KotlinBuilder
import org.jetbrains.kotlin.utils.PathUtil
import java.io.*
fun createTestingCompilerEnvironment(
messageCollector: MessageCollector,
outputItemsCollector: OutputItemsCollectorImpl,
services: Services
): JpsCompilerEnvironment {
val paths = PathUtil.kotlinPathsForDistDirectory
val wrappedMessageCollector = MessageCollectorToOutputItemsCollectorAdapter(messageCollector, outputItemsCollector)
return JpsCompilerEnvironment(paths, services, KotlinBuilder.classesToLoadByParent, wrappedMessageCollector, outputItemsCollector)
}
fun runJSCompiler(args: K2JSCompilerArguments, env: JpsCompilerEnvironment): ExitCode? {
val argsArray = ArgumentUtils.convertArgumentsToStringList(args).toTypedArray()
val stream = ByteArrayOutputStream()
val out = PrintStream(stream)
val exitCode = CompilerRunnerUtil.invokeExecMethod(K2JSCompiler::class.java.name, argsArray, env, out)
val reader = BufferedReader(StringReader(stream.toString()))
CompilerOutputParser.parseCompilerMessagesFromReader(env.messageCollector, reader, env.outputItemsCollector)
return exitCode as? ExitCode
}
@@ -81,6 +81,19 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
const val JVM_BUILD_META_INFO_FILE_NAME = "jvm-build-meta-info.txt"
const val SKIP_CACHE_VERSION_CHECK_PROPERTY = "kotlin.jps.skip.cache.version.check"
const val JPS_KOTLIN_HOME_PROPERTY = "jps.kotlin.home"
val classesToLoadByParent: ClassCondition
get() = ClassCondition { className ->
className.startsWith("org.jetbrains.kotlin.load.kotlin.incremental.components.")
|| className.startsWith("org.jetbrains.kotlin.incremental.components.")
|| className.startsWith("org.jetbrains.kotlin.incremental.js")
|| className == "org.jetbrains.kotlin.config.Services"
|| className.startsWith("org.apache.log4j.") // For logging from compiler
|| className == "org.jetbrains.kotlin.progress.CompilationCanceledStatus"
|| className == "org.jetbrains.kotlin.progress.CompilationCanceledException"
|| className == "org.jetbrains.kotlin.modules.TargetId"
|| className == "org.jetbrains.kotlin.cli.common.ExitCode"
}
}
private val statisticsLogger = TeamcityStatisticsLogger()
@@ -467,15 +480,7 @@ class KotlinBuilder : ModuleLevelBuilder(BuilderCategory.SOURCE_PROCESSOR) {
return JpsCompilerEnvironment(
paths,
compilerServices,
ClassCondition { className ->
className.startsWith("org.jetbrains.kotlin.load.kotlin.incremental.components.")
|| className.startsWith("org.jetbrains.kotlin.incremental.components.")
|| className == "org.jetbrains.kotlin.config.Services"
|| className.startsWith("org.apache.log4j.") // For logging from compiler
|| className == "org.jetbrains.kotlin.progress.CompilationCanceledStatus"
|| className == "org.jetbrains.kotlin.progress.CompilationCanceledException"
|| className == "org.jetbrains.kotlin.modules.TargetId"
},
classesToLoadByParent,
messageCollector,
OutputItemsCollectorImpl()
)