From 28a2d4727a1e1c51d33c46f488c7c9f798a57d1c Mon Sep 17 00:00:00 2001 From: Alexey Tsvetkov Date: Fri, 4 Aug 2017 16:30:02 +0300 Subject: [PATCH] Test JPS with Daemon and IC Original commit: 220fab0d3f0267df8c06d6c3a09429a515c66557 --- .../kotlin/jps/build/KotlinJpsBuildTest.kt | 3 +- .../build/KotlinJpsBuildTestIncremental.kt | 57 +++++++++++++++++++ .../kotlin/jps/build/testingUtils.kt | 40 +++++++++++++ .../compilerRunner/JpsKotlinCompilerRunner.kt | 6 ++ .../general/JpsDaemonIC/kotlinProject.iml | 12 ++++ .../general/JpsDaemonIC/kotlinProject.ipr | 14 +++++ .../testData/general/JpsDaemonIC/src/Foo.kt | 1 + .../testData/general/JpsDaemonIC/src/main.kt | 3 + 8 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt create mode 100644 jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.iml create mode 100644 jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.ipr create mode 100644 jps/jps-plugin/testData/general/JpsDaemonIC/src/Foo.kt create mode 100644 jps/jps-plugin/testData/general/JpsDaemonIC/src/main.kt diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt index 01a7c13db28..e4a100c8626 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTest.kt @@ -169,7 +169,8 @@ open class KotlinJpsBuildTest : AbstractKotlinJpsBuildTestCase() { } } - private fun findFileInOutputDir(module: JpsModule, relativePath: String): File { + @JvmStatic + protected fun findFileInOutputDir(module: JpsModule, relativePath: String): File { val outputUrl = JpsJavaExtensionService.getInstance().getOutputUrl(module, false) assertNotNull(outputUrl) val outputDir = File(JpsPathUtil.urlToPath(outputUrl)) diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt index a6dd9a06a2e..92e4dec5ee2 100644 --- a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/KotlinJpsBuildTestIncremental.kt @@ -1,10 +1,33 @@ +/* + * 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.build +import com.intellij.openapi.util.io.FileUtil import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments +import org.jetbrains.jps.builders.JpsBuildTestCase +import org.jetbrains.kotlin.compilerRunner.JpsKotlinCompilerRunner import org.jetbrains.kotlin.config.IncrementalCompilation import org.jetbrains.kotlin.config.LanguageVersion import org.jetbrains.kotlin.jps.JpsKotlinCompilerSettings import kotlin.reflect.KMutableProperty1 +import org.jetbrains.kotlin.daemon.common.COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS +import org.jetbrains.kotlin.daemon.common.COMPILE_DAEMON_ENABLED_PROPERTY +import org.jetbrains.kotlin.daemon.common.isDaemonEnabled +import java.io.File class KotlinJpsBuildTestIncremental : KotlinJpsBuildTest() { var isICEnabledBackup: Boolean = false @@ -20,6 +43,40 @@ class KotlinJpsBuildTestIncremental : KotlinJpsBuildTest() { super.tearDown() } + fun testJpsDaemonIC() { + fun testImpl() { + assertTrue("Daemon was not enabled!", isDaemonEnabled()) + + doTest() + val module = myProject.modules.get(0) + val mainKtClassFile = findFileInOutputDir(module, "MainKt.class") + assertTrue("$mainKtClassFile does not exist!", mainKtClassFile.exists()) + + val fooKt = File(workDir, "src/Foo.kt") + JpsBuildTestCase.change(fooKt.path, null) + buildAllModules().assertSuccessful() + assertCompiled(KotlinBuilder.KOTLIN_BUILDER_NAME, "src/Foo.kt") + + JpsBuildTestCase.change(fooKt.path, "class Foo(val x: Int = 0)") + buildAllModules().assertSuccessful() + assertCompiled(KotlinBuilder.KOTLIN_BUILDER_NAME, "src/main.kt", "src/Foo.kt") + } + + val daemonHome = FileUtil.createTempDirectory("daemon-home", "testJpsDaemonIC") + try { + withSystemProperty(COMPILE_DAEMON_CUSTOM_RUN_FILES_PATH_FOR_TESTS, daemonHome.absolutePath) { + withSystemProperty(COMPILE_DAEMON_ENABLED_PROPERTY, "true") { + withSystemProperty(JpsKotlinCompilerRunner.FAIL_ON_FALLBACK_PROPERTY, "true") { + testImpl() + } + } + } + } + finally { + daemonHome.deleteRecursively() + } + } + fun testManyFiles() { doTest() diff --git a/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt new file mode 100644 index 00000000000..a5b77e06594 --- /dev/null +++ b/jps/jps-plugin/jps-tests/test/org/jetbrains/kotlin/jps/build/testingUtils.kt @@ -0,0 +1,40 @@ +/* + * 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.build + +inline fun withSystemProperty(property: String, newValue: String?, fn: ()->Unit) { + val backup = System.getProperty(property) + setOrClearSysProperty(property, newValue) + + try { + fn() + } + finally { + setOrClearSysProperty(property, backup) + } +} + + +@Suppress("NOTHING_TO_INLINE") +inline fun setOrClearSysProperty(property: String, newValue: String?) { + if (newValue != null) { + System.setProperty(property, newValue) + } + else { + System.clearProperty(property) + } +} \ No newline at end of file diff --git a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt index aea4e742fd8..031a869af69 100644 --- a/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt +++ b/jps/jps-plugin/src/org/jetbrains/kotlin/compilerRunner/JpsKotlinCompilerRunner.kt @@ -69,6 +69,8 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { return _jpsCompileServiceSession } + + const val FAIL_ON_FALLBACK_PROPERTY = "test.kotlin.jps.compiler.runner.fail.on.fallback" } fun runK2JvmCompiler( @@ -190,6 +192,10 @@ class JpsKotlinCompilerRunner : KotlinCompilerRunner() { compilerClassName: String, environment: JpsCompilerEnvironment ): ExitCode { + if ("true" == System.getProperty("kotlin.jps.tests") && "true" == System.getProperty(FAIL_ON_FALLBACK_PROPERTY)) { + error("Fallback strategy is disabled in tests!") + } + // otherwise fallback to in-process log.info("Compile in-process") diff --git a/jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.iml b/jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.iml new file mode 100644 index 00000000000..0c4fb67a3d4 --- /dev/null +++ b/jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + diff --git a/jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.ipr b/jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.ipr new file mode 100644 index 00000000000..8226e21ade3 --- /dev/null +++ b/jps/jps-plugin/testData/general/JpsDaemonIC/kotlinProject.ipr @@ -0,0 +1,14 @@ + + + + + + + + + + + + + diff --git a/jps/jps-plugin/testData/general/JpsDaemonIC/src/Foo.kt b/jps/jps-plugin/testData/general/JpsDaemonIC/src/Foo.kt new file mode 100644 index 00000000000..5ff6c09c578 --- /dev/null +++ b/jps/jps-plugin/testData/general/JpsDaemonIC/src/Foo.kt @@ -0,0 +1 @@ +class Foo() \ No newline at end of file diff --git a/jps/jps-plugin/testData/general/JpsDaemonIC/src/main.kt b/jps/jps-plugin/testData/general/JpsDaemonIC/src/main.kt new file mode 100644 index 00000000000..27c36e10d47 --- /dev/null +++ b/jps/jps-plugin/testData/general/JpsDaemonIC/src/main.kt @@ -0,0 +1,3 @@ +fun main() { + Foo() +} \ No newline at end of file