K2 Scripting: enable K2 tests in some test projects

This commit is contained in:
Ilya Chernikov
2022-11-17 12:26:00 +01:00
committed by Space Team
parent 90a4f6002e
commit deb2d6d024
7 changed files with 72 additions and 5 deletions
@@ -73,3 +73,11 @@ projectTest(taskName = "testWithIr", parallel = true) {
systemProperty("kotlin.script.test.base.compiler.arguments", "-Xuse-ir")
}
projectTest(taskName = "testWithK2", parallel = true) {
dependsOn(":dist")
workingDir = rootDir
systemProperty("kotlin.test.script.classpath", testSourceSet.output.classesDirs.joinToString(File.pathSeparator))
systemProperty("kotlin.script.test.base.compiler.arguments", "-Xuse-k2")
systemProperty("kotlin.script.base.compiler.arguments", "-Xuse-k2")
}
@@ -22,7 +22,7 @@ import kotlin.concurrent.thread
const val SCRIPT_TEST_BASE_COMPILER_ARGUMENTS_PROPERTY = "kotlin.script.test.base.compiler.arguments"
private fun getBaseCompilerArgumentsFromProperty(): List<String>? =
internal fun getBaseCompilerArgumentsFromProperty(): List<String>? =
System.getProperty(SCRIPT_TEST_BASE_COMPILER_ARGUMENTS_PROPERTY)?.takeIf { it.isNotBlank() }?.split(' ')
// TODO: partially copypasted from LauncherReplTest, consider extracting common parts to some (new) test util module
@@ -7,7 +7,9 @@ package org.jetbrains.kotlin.scripting.compiler.test
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.scripting.compiler.plugin.getBaseCompilerArgumentsFromProperty
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated
import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs
import kotlin.reflect.KClass
import kotlin.reflect.KProperty
import kotlin.reflect.full.createInstance
@@ -32,6 +34,40 @@ class ScriptCompilerTest : TestCase() {
assertTrue(res.reports.none { it.message.contains("nonsense") })
}
fun testSimpleVarAccess() {
val res = compileToClass(
"""
val x = 2
val y = x
""".trimIndent().toScriptSource()
)
val kclass = res.valueOrThrow()
val scriptInstance = kclass.createInstance()
assertNotNull(scriptInstance)
}
fun testLambdaWithProperty() {
val versionProperties = java.util.Properties()
"".reader().use { propInput ->
versionProperties.load(propInput)
}
val res = compileToClass(
"""
val versionProperties = java.util.Properties()
"".reader().use { propInput ->
val x = 1
x.toString()
versionProperties.load(propInput)
}
""".trimIndent().toScriptSource()
)
val kclass = res.valueOrThrow()
val scriptInstance = kclass.createInstance()
assertNotNull(scriptInstance)
}
fun testTypeAliases() {
val res = compileToClass(
"""
@@ -78,7 +114,12 @@ class ScriptCompilerTest : TestCase() {
script: SourceCode,
cfgBody: ScriptCompilationConfiguration.Builder.() -> Unit
): ResultWithDiagnostics<CompiledScript> {
val compilationConfiguration = ScriptCompilationConfiguration(cfgBody)
val compilationConfiguration = ScriptCompilationConfiguration {
cfgBody()
getBaseCompilerArgumentsFromProperty()?.let {
compilerOptions.append(it)
}
}
val compiler = ScriptJvmCompilerIsolated(defaultJvmScriptingHostConfiguration)
return compiler.compile(script, compilationConfiguration)
}