Move main-kts cache test to out-of-process compilation

to avoid clashes with coroutine debugger when running
tests from IntelliJ
This commit is contained in:
Ilya Chernikov
2020-06-23 13:59:12 +02:00
parent 879e22f274
commit 4c34e9dd03
2 changed files with 30 additions and 32 deletions
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.mainKts.test
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.runAndCheckResults
import org.jetbrains.kotlin.scripting.compiler.plugin.runWithK2JVMCompiler
import org.jetbrains.kotlin.scripting.compiler.plugin.runWithKotlinLauncherScript
import org.jetbrains.kotlin.scripting.compiler.plugin.runWithKotlinc
@@ -63,6 +64,32 @@ class MainKtsIT {
}
}
@Test
fun testCache() {
val script = File("$TEST_DATA_ROOT/import-test.main.kts").absolutePath
val cache = createTempDir("main.kts.test")
try {
Assert.assertTrue(cache.exists() && cache.listFiles { f: File -> f.extension == "jar" }?.isEmpty() == true)
runWithKotlinRunner(script, OUT_FROM_IMPORT_TEST, cacheDir = cache)
val cacheFile = cache.listFiles { f: File -> f.extension.equals("jar", ignoreCase = true) }?.firstOrNull()
Assert.assertTrue(cacheFile != null && cacheFile.exists())
// run generated jar with java
val javaExecutable = File(File(System.getProperty("java.home"), "bin"), "java")
val args = listOf(javaExecutable.absolutePath, "-jar", cacheFile!!.path)
runAndCheckResults(
args, OUT_FROM_IMPORT_TEST,
additionalEnvVars = listOf(COMPILED_SCRIPTS_CACHE_DIR_ENV_VAR to cache.absolutePath)
)
// this run should use the cached script
runWithKotlinRunner(script, OUT_FROM_IMPORT_TEST, cacheDir = cache)
} finally {
cache.deleteRecursively()
}
}
@Test
fun testHelloSerialization() {
val paths = PathUtil.kotlinPathsForDistDirectory
@@ -10,7 +10,6 @@ import org.jetbrains.kotlin.mainKts.MainKtsScript
import org.junit.Assert
import org.junit.Test
import java.io.*
import java.net.URLClassLoader
import java.util.*
import kotlin.script.experimental.api.*
import kotlin.script.experimental.host.toScriptSource
@@ -38,6 +37,8 @@ fun evalFile(scriptFile: File, cacheDir: File? = null): ResultWithDiagnostics<Ev
const val TEST_DATA_ROOT = "libraries/tools/kotlin-main-kts-test/testData"
val OUT_FROM_IMPORT_TEST = listOf("Hi from common", "Hi from middle", "sharedVar == 5")
class MainKtsTest {
@@ -96,8 +97,6 @@ class MainKtsTest {
assertSucceeded(res)
}
private val outFromImportTest = listOf("Hi from common", "Hi from middle", "sharedVar == 5")
@Test
fun testImport() {
@@ -106,7 +105,7 @@ class MainKtsTest {
assertSucceeded(res)
}.lines()
Assert.assertEquals(outFromImportTest, out)
Assert.assertEquals(OUT_FROM_IMPORT_TEST, out)
}
@Test
@@ -121,34 +120,6 @@ class MainKtsTest {
Assert.assertEquals(listOf("Hi from sub", "Hi from super", "Hi from random"), out)
}
@Test
fun testCache() {
val script = File("$TEST_DATA_ROOT/import-test.main.kts")
val cache = createTempDir("main.kts.test")
try {
Assert.assertTrue(cache.exists() && cache.listFiles { f: File -> f.extension == "jar" }?.isEmpty() == true)
val out1 = evalSuccessWithOut(script)
Assert.assertEquals(outFromImportTest, out1)
Assert.assertTrue(cache.listFiles { f: File -> f.extension.equals("jar", ignoreCase = true) }?.isEmpty() == true)
val out2 = evalSuccessWithOut(script, cache)
Assert.assertEquals(outFromImportTest, out2)
val casheFile = cache.listFiles { f: File -> f.extension.equals("jar", ignoreCase = true) }?.firstOrNull()
Assert.assertTrue(casheFile != null && casheFile.exists())
val out3 = captureOut {
val classLoader = URLClassLoader(arrayOf(casheFile!!.toURI().toURL()), null)
val clazz = classLoader.loadClass("Import_test_main")
val mainFn = clazz.getDeclaredMethod("main", Array<String>::class.java)
mainFn.invoke(null, arrayOf<String>())
}.lines()
Assert.assertEquals(outFromImportTest, out3)
} finally {
cache.deleteRecursively()
}
}
private fun assertIsJava6Bytecode(res: ResultWithDiagnostics<EvaluationResult>) {
val scriptClassResource = res.valueOrThrow().returnValue.scriptClass!!.java.run {
getResource("$simpleName.class")