Enable K2 scripting tests

This commit is contained in:
Ilya Chernikov
2023-05-03 16:27:30 +02:00
committed by Space Team
parent 266a223460
commit 480ea80fc4
20 changed files with 148 additions and 92 deletions
@@ -69,7 +69,7 @@ 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")
systemProperty("kotlin.script.test.base.compiler.arguments", "-language-version 2.0")
systemProperty("kotlin.script.base.compiler.arguments", "-language-version 2.0")
}
@@ -26,12 +26,12 @@ class ScriptingWithCliCompilerTest {
}
@Test
fun testResultValue() {
fun testResultValue() = expectTestToFailOnK2 {
runWithK2JVMCompiler("$TEST_DATA_DIR/integration/intResult.kts", listOf("10"))
}
@Test
fun testResultValueViaKotlinc() {
fun testResultValueViaKotlinc() = expectTestToFailOnK2 {
runWithKotlinc("$TEST_DATA_DIR/integration/intResult.kts", listOf("10"))
}
@@ -76,7 +76,7 @@ class ScriptingWithCliCompilerTest {
expectedExitCode = 1,
expectedSomeErrPatterns = listOf(
"unresolved reference: CompilerOptions"
)
),
)
runWithK2JVMCompiler(
arrayOf(
@@ -132,13 +132,14 @@ class ScriptingWithCliCompilerTest {
}
@Test
fun testExpressionWithComma() {
fun testExpressionWithComma() = expectTestToFailOnK2 {
runWithK2JVMCompiler(
arrayOf(
"-expression",
"listOf(1,2)"
),
listOf("\\[1, 2\\]")
listOf("\\[1, 2\\]"),
expectErrorOnK2 = true
)
}
@@ -10,6 +10,7 @@ import com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.cli.common.CLITool
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.SCRIPT_BASE_COMPILER_ARGUMENTS_PROPERTY
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.updateWithCompilerOptions
import org.junit.Assert
import java.io.ByteArrayOutputStream
@@ -32,11 +33,12 @@ fun runWithKotlinc(
expectedExitCode: Int = 0,
workDirectory: File? = null,
classpath: List<File> = emptyList(),
additionalEnvVars: Iterable<Pair<String, String>>? = null
additionalEnvVars: Iterable<Pair<String, String>>? = null,
expectErrorOnK2: Boolean = false
) {
runWithKotlinc(
arrayOf("-script", scriptPath),
expectedOutPatterns, expectedExitCode, workDirectory, classpath, additionalEnvVars
expectedOutPatterns, expectedExitCode, workDirectory, classpath, additionalEnvVars, expectErrorOnK2
)
}
@@ -47,7 +49,8 @@ fun runWithKotlinLauncherScript(
expectedExitCode: Int = 0,
workDirectory: File? = null,
classpath: List<File> = emptyList(),
additionalEnvVars: Iterable<Pair<String, String>>? = null
additionalEnvVars: Iterable<Pair<String, String>>? = null,
expectErrorOnK2: Boolean = false
) {
val executableFileName =
if (System.getProperty("os.name").contains("windows", ignoreCase = true)) "$launcherScriptName.bat" else launcherScriptName
@@ -63,7 +66,7 @@ fun runWithKotlinLauncherScript(
addAll(compilerArgs)
}
runAndCheckResults(args, expectedOutPatterns, expectedExitCode, workDirectory, additionalEnvVars)
runAndCheckResults(args, expectedOutPatterns, expectedExitCode, workDirectory, additionalEnvVars, expectErrorOnK2 = expectErrorOnK2)
}
fun runWithKotlinc(
@@ -72,10 +75,12 @@ fun runWithKotlinc(
expectedExitCode: Int = 0,
workDirectory: File? = null,
classpath: List<File> = emptyList(),
additionalEnvVars: Iterable<Pair<String, String>>? = null
additionalEnvVars: Iterable<Pair<String, String>>? = null,
expectErrorOnK2: Boolean = false
) {
runWithKotlinLauncherScript(
"kotlinc", compilerArgs.asIterable(), expectedOutPatterns, expectedExitCode, workDirectory, classpath, additionalEnvVars
"kotlinc", compilerArgs.asIterable(), expectedOutPatterns, expectedExitCode, workDirectory, classpath,
additionalEnvVars, expectErrorOnK2
)
}
@@ -84,7 +89,8 @@ fun runAndCheckResults(
expectedOutPatterns: List<String> = emptyList(),
expectedExitCode: Int = 0,
workDirectory: File? = null,
additionalEnvVars: Iterable<Pair<String, String>>? = null
additionalEnvVars: Iterable<Pair<String, String>>? = null,
expectErrorOnK2: Boolean = false
) {
val processBuilder = ProcessBuilder(args)
if (workDirectory != null) {
@@ -130,14 +136,22 @@ fun runAndCheckResults(
stderrThread.join(300)
Assert.assertFalse("stderr thread not finished", stderrThread.isAlive)
Assert.assertNull(stderrException.value)
Assert.assertEquals(expectedOutPatterns.size, processOut.size)
for ((expectedPattern, actualLine) in expectedOutPatterns.zip(processOut)) {
if (expectedExitCode == 0 && expectErrorOnK2 && processOut.contains("Language version 2.0")) {
Assert.assertTrue(
"line \"$actualLine\" do not match with expected pattern \"$expectedPattern\"",
Regex(expectedPattern).matches(actualLine)
"Expecting an error on K2 compilation, but got: exit code: ${process.exitValue()}\n$processOut",
process.exitValue() != 0 || processOut.contains("error: ")
)
} else {
Assert.assertEquals(expectedOutPatterns.size, processOut.size)
for ((expectedPattern, actualLine) in expectedOutPatterns.zip(processOut)) {
Assert.assertTrue(
"line \"$actualLine\" do not match with expected pattern \"$expectedPattern\"",
Regex(expectedPattern).matches(actualLine)
)
}
Assert.assertEquals(expectedExitCode, process.exitValue())
}
Assert.assertEquals(expectedExitCode, process.exitValue())
} catch (e: Throwable) {
println("OUT:\n${processOut.joinToString("\n")}")
@@ -150,7 +164,8 @@ fun runWithK2JVMCompiler(
scriptPath: String,
expectedOutPatterns: List<String> = emptyList(),
expectedExitCode: Int = 0,
classpath: List<File> = emptyList()
classpath: List<File> = emptyList(),
expectErrorOnK2: Boolean = false,
) {
val args = arrayListOf("-kotlin-home", "dist/kotlinc").apply {
if (classpath.isNotEmpty()) {
@@ -160,14 +175,15 @@ fun runWithK2JVMCompiler(
add("-script")
add(scriptPath)
}
runWithK2JVMCompiler(args.toTypedArray(), expectedOutPatterns, expectedExitCode)
runWithK2JVMCompiler(args.toTypedArray(), expectedOutPatterns, expectedExitCode, expectErrorOnK2 = expectErrorOnK2)
}
fun runWithK2JVMCompiler(
args: Array<String>,
expectedAllOutPatterns: List<String> = emptyList(),
expectedExitCode: Int = 0,
expectedSomeErrPatterns: List<String>? = null
expectedSomeErrPatterns: List<String>? = null,
expectErrorOnK2: Boolean = false
) {
val argsWithBasefromProp = getBaseCompilerArgumentsFromProperty()?.let { (it + args).toTypedArray() } ?: args
val (out, err, ret) = captureOutErrRet {
@@ -178,27 +194,35 @@ fun runWithK2JVMCompiler(
}
try {
val outLines = if (out.isEmpty()) emptyList() else out.lines()
Assert.assertEquals(
"Expecting pattern:\n ${expectedAllOutPatterns.joinToString("\n ")}\nGot:\n ${outLines.joinToString("\n ")}",
expectedAllOutPatterns.size, outLines.size
)
for ((expectedPattern, actualLine) in expectedAllOutPatterns.zip(outLines)) {
val errLines by lazy { err.lines() }
if (expectErrorOnK2 && errLines.any { it.contains("language version 2.0") }) {
Assert.assertTrue(
"line \"$actualLine\" do not match with expected pattern \"$expectedPattern\"",
Regex(expectedPattern).matches(actualLine)
"Expecting an error on K2 compilation, but got: exit code: ${ret.code}\n" +
" ${outLines.joinToString("\n ")}${errLines.joinToString("\n")}",
ret.code != 0 || err.lines().any { it.contains(("error: ")) }
)
}
if (expectedSomeErrPatterns != null) {
val errLines = err.lines()
for (expectedPattern in expectedSomeErrPatterns) {
val re = Regex(expectedPattern)
} else {
Assert.assertEquals(
"Expecting pattern:\n ${expectedAllOutPatterns.joinToString("\n ")}\nGot:\n ${outLines.joinToString("\n ")}",
expectedAllOutPatterns.size, outLines.size
)
for ((expectedPattern, actualLine) in expectedAllOutPatterns.zip(outLines)) {
Assert.assertTrue(
"Expected pattern \"$expectedPattern\" is not found in the stderr:\n${errLines.joinToString("\n")}",
errLines.any { re.find(it) != null }
"line \"$actualLine\" do not match with expected pattern \"$expectedPattern\"",
Regex(expectedPattern).matches(actualLine)
)
}
if (expectedSomeErrPatterns != null) {
for (expectedPattern in expectedSomeErrPatterns) {
val re = Regex(expectedPattern)
Assert.assertTrue(
"Expected pattern \"$expectedPattern\" is not found in the stderr:\n${errLines.joinToString("\n")}",
errLines.any { re.find(it) != null }
)
}
}
Assert.assertEquals(expectedExitCode, ret.code)
}
Assert.assertEquals(expectedExitCode, ret.code)
} catch (e: Throwable) {
println("OUT:\n$out")
println("ERR:\n$err")
@@ -258,3 +282,15 @@ fun CompilerConfiguration.updateWithBaseCompilerArguments() {
}
}
fun expectTestToFailOnK2(test: () -> Unit) {
val isK2 = System.getProperty(SCRIPT_BASE_COMPILER_ARGUMENTS_PROPERTY)?.contains("-language-version 2.0") == true ||
System.getProperty(SCRIPT_TEST_BASE_COMPILER_ARGUMENTS_PROPERTY)?.contains("-language-version 2.0") == true
var testFailure: Throwable? = null
try {
test()
} catch (e: Throwable) {
testFailure = e
}
if (isK2 && testFailure == null) throw AssertionError("The test is expected to fail on K2")
else if (!isK2 && testFailure != null) throw testFailure
}
@@ -12,6 +12,7 @@ import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.script.loadScriptingPlugin
import org.jetbrains.kotlin.scripting.compiler.plugin.TestDisposable
import org.jetbrains.kotlin.scripting.compiler.plugin.expectTestToFailOnK2
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerFromEnvironment
import org.jetbrains.kotlin.scripting.compiler.plugin.updateWithBaseCompilerArguments
import org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys
@@ -37,7 +38,7 @@ private const val testDataPath = "plugins/scripting/scripting-compiler/testData/
class CompileTimeFibonacciTest : TestCase() {
private val testRootDisposable: Disposable = TestDisposable()
fun testFibonacciWithSupportedNumbersImplementsTheCorrectConstants() {
fun testFibonacciWithSupportedNumbersImplementsTheCorrectConstants() = expectTestToFailOnK2 {
val outputLines = runScript("supported.fib.kts")
.valueOr { failure ->
val message = failure.reports.joinToString("\n") { it.message }
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.scripting.compiler.test
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.scripting.compiler.plugin.expectTestToFailOnK2
import org.jetbrains.kotlin.scripting.compiler.plugin.getBaseCompilerArgumentsFromProperty
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated
import org.jetbrains.kotlin.utils.tryConstructClassFromStringArgs
@@ -85,7 +86,8 @@ class ScriptCompilerTest : TestCase() {
assertEquals("Clazz", nestedClasses[0].simpleName)
}
fun testDestructingDeclarations() {
// Fails on K2, see KT-60501
fun testDestructingDeclarations() = expectTestToFailOnK2 {
val res = compileToClass(
"""
val c = 3
@@ -1,15 +1,10 @@
import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.scripting.compiler.plugin.expectTestToFailOnK2
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.ScriptJvmCompilerIsolated
import org.jetbrains.kotlin.scripting.compiler.test.assertEqualsTrimmed
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.InputStream
import java.io.PrintStream
import java.nio.file.attribute.FileTime
import java.util.zip.ZipEntry
import java.util.zip.ZipFile
import java.util.zip.ZipOutputStream
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.BasicJvmScriptEvaluator
@@ -24,7 +19,7 @@ import kotlin.script.experimental.jvm.util.renderError
class ScriptEvaluationTest : TestCase() {
fun testExceptionWithCause() {
fun testExceptionWithCause() = expectTestToFailOnK2 {
checkEvaluateAsError(
"""
try {
@@ -43,7 +38,7 @@ class ScriptEvaluationTest : TestCase() {
}
// KT-19423
fun testClassCapturingScriptInstance() {
fun testClassCapturingScriptInstance() = expectTestToFailOnK2 {
val res = checkEvaluate(
"""
val used = "abc"
@@ -17,6 +17,7 @@ import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.script.loadScriptingPlugin
import org.jetbrains.kotlin.scripting.compiler.plugin.TestMessageCollector
import org.jetbrains.kotlin.scripting.compiler.plugin.assertHasMessage
import org.jetbrains.kotlin.scripting.compiler.plugin.expectTestToFailOnK2
import org.jetbrains.kotlin.scripting.compiler.plugin.updateWithBaseCompilerArguments
import org.jetbrains.kotlin.scripting.configuration.ScriptingConfigurationKeys
import org.jetbrains.kotlin.scripting.definitions.KotlinScriptDefinition
@@ -71,7 +72,7 @@ class ScriptTemplateTest : TestCase() {
assertEqualsTrimmed(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
}
fun testScriptWithBaseClassWithParam() {
fun testScriptWithBaseClassWithParam() = expectTestToFailOnK2 {
val messageCollector = TestMessageCollector()
val aClass =
compileScript("fib_dsl.kts", ScriptWithBaseClass::class, null, runIsolated = false, messageCollector = messageCollector)
@@ -119,7 +120,8 @@ class ScriptTemplateTest : TestCase() {
assertEqualsTrimmed(NUM_4_LINE + FIB_SCRIPT_OUTPUT_TAIL, out)
}
fun testScriptWithoutParams() {
// Fails on K2, see KT-60452
fun testScriptWithoutParams() = expectTestToFailOnK2 {
val messageCollector = TestMessageCollector()
val aClass = compileScript("without_params.kts", ScriptWithoutParams::class, null, messageCollector = messageCollector)
Assert.assertNotNull("Compilation failed:\n$messageCollector", aClass)