From 278f77713d7ab48b8e0796449ba2e6eaeff60377 Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Tue, 31 Mar 2020 16:57:41 +0200 Subject: [PATCH] Fix default base classloader initialization for scripting hosts also refactor runner functions #KT-37823 fixed --- .../jvm/jvmScriptingHostConfiguration.kt | 2 +- .../kotlin/mainKts/test/mainKtsIT.kt | 26 +++++++++++++++ .../testData/use-reflect.main.kts | 2 ++ .../scripting/compiler/plugin/testUtil.kt | 33 ++++++++++++++++--- 4 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 libraries/tools/kotlin-main-kts-test/testData/use-reflect.main.kts diff --git a/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/jvmScriptingHostConfiguration.kt b/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/jvmScriptingHostConfiguration.kt index 727f3b15c65..f51f812abed 100644 --- a/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/jvmScriptingHostConfiguration.kt +++ b/libraries/scripting/jvm/src/kotlin/script/experimental/jvm/jvmScriptingHostConfiguration.kt @@ -30,7 +30,7 @@ val JvmScriptingHostConfigurationKeys.jdkHome by PropertiesCollection.key( val JvmScriptingHostConfigurationKeys.baseClassLoader by PropertiesCollection.key( { get(ScriptingHostConfiguration.configurationDependencies)?.let { - URLClassLoader(it.toClassPathOrEmpty().map { f -> f.toURI().toURL() }.toTypedArray()) + URLClassLoader(it.toClassPathOrEmpty().map { f -> f.toURI().toURL() }.toTypedArray(), null) } }, isTransient = true diff --git a/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt b/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt index 82ae581dd4f..a8e3f114498 100644 --- a/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt +++ b/libraries/tools/kotlin-main-kts-test/test/org/jetbrains/kotlin/mainKts/test/mainKtsIT.kt @@ -9,6 +9,7 @@ import junit.framework.Assert import org.jetbrains.kotlin.mainKts.COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR import org.jetbrains.kotlin.mainKts.COMPILED_SCRIPTS_CACHE_DIR_PROPERTY import org.jetbrains.kotlin.scripting.compiler.plugin.runWithK2JVMCompiler +import org.jetbrains.kotlin.scripting.compiler.plugin.runWithKotlinLauncherScript import org.jetbrains.kotlin.scripting.compiler.plugin.runWithKotlinc import org.junit.Ignore import org.junit.Test @@ -46,6 +47,19 @@ class MainKtsIT { fun testThreadContextClassLoader() { runWithKotlincAndMainKts("$TEST_DATA_ROOT/context-classloader.main.kts", listOf("MainKtsConfigurator")) } + + @Test + fun testCachedReflection() { + val cache = createTempDir("main.kts.test") + + try { + runWithKotlinRunner("$TEST_DATA_ROOT/use-reflect.main.kts", listOf("false"), cacheDir = cache) + // second run uses the cached script + runWithKotlinRunner("$TEST_DATA_ROOT/use-reflect.main.kts", listOf("false"), cacheDir = cache) + } finally { + cache.deleteRecursively() + } + } } fun runWithKotlincAndMainKts( @@ -65,6 +79,18 @@ fun runWithKotlincAndMainKts( ) } +fun runWithKotlinRunner( + scriptPath: String, + expectedOutPatterns: List = emptyList(), + expectedExitCode: Int = 0, + cacheDir: File? = null +) { + runWithKotlinLauncherScript( + "kotlin", listOf(scriptPath), expectedOutPatterns, expectedExitCode, + additionalEnvVars = listOf(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR to (cacheDir?.absolutePath ?: "")) + ) +} + fun runWithK2JVMCompilerAndMainKts( scriptPath: String, expectedOutPatterns: List = emptyList(), diff --git a/libraries/tools/kotlin-main-kts-test/testData/use-reflect.main.kts b/libraries/tools/kotlin-main-kts-test/testData/use-reflect.main.kts new file mode 100644 index 00000000000..2b3f6d9b6b0 --- /dev/null +++ b/libraries/tools/kotlin-main-kts-test/testData/use-reflect.main.kts @@ -0,0 +1,2 @@ + +println(""::class.isOpen) diff --git a/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt b/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt index bd196ca0f27..7a16e153c0d 100644 --- a/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt +++ b/plugins/scripting/scripting-compiler/tests/org/jetbrains/kotlin/scripting/compiler/plugin/testUtil.kt @@ -33,18 +33,17 @@ fun runWithKotlinc( ) } -fun runWithKotlinc( - compilerArgs: Array, +fun runWithKotlinLauncherScript( + launcherScriptName: String, + compilerArgs: Iterable, expectedOutPatterns: List = emptyList(), expectedExitCode: Int = 0, workDirectory: File? = null, classpath: List = emptyList(), additionalEnvVars: Iterable>? = null ) { - val executableName = "kotlinc" - // TODO: val executableFileName = - if (System.getProperty("os.name").contains("windows", ignoreCase = true)) "$executableName.bat" else executableName + if (System.getProperty("os.name").contains("windows", ignoreCase = true)) "$launcherScriptName.bat" else launcherScriptName val launcherFile = File("dist/kotlinc/bin/$executableFileName") Assert.assertTrue("Launcher script not found, run dist task: ${launcherFile.absolutePath}", launcherFile.exists()) @@ -55,6 +54,30 @@ fun runWithKotlinc( } addAll(compilerArgs) } + + runAndCheckResults(args, expectedOutPatterns, expectedExitCode, workDirectory, additionalEnvVars) +} + +fun runWithKotlinc( + compilerArgs: Array, + expectedOutPatterns: List = emptyList(), + expectedExitCode: Int = 0, + workDirectory: File? = null, + classpath: List = emptyList(), + additionalEnvVars: Iterable>? = null +) { + runWithKotlinLauncherScript( + "kotlinc", compilerArgs.asIterable(), expectedOutPatterns, expectedExitCode, workDirectory, classpath, additionalEnvVars + ) +} + +fun runAndCheckResults( + args: List, + expectedOutPatterns: List = emptyList(), + expectedExitCode: Int = 0, + workDirectory: File? = null, + additionalEnvVars: Iterable>? = null +) { val processBuilder = ProcessBuilder(args) if (workDirectory != null) { processBuilder.directory(workDirectory)