Fix entries closing on saving compiled script to jar:

also adding a short sleep in tests, otherwise in some cases the
classloader was not able to find class in the jar.
Fixes script to jar saving tests on windows
This commit is contained in:
Ilya Chernikov
2019-07-15 15:25:22 +02:00
parent 5b3164ee87
commit 747c488a78
2 changed files with 16 additions and 9 deletions
@@ -112,6 +112,7 @@ class ScriptingHostTest : TestCase() {
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScriptTemplate>()
val host = BasicJvmScriptingHost(evaluator = BasicJvmScriptJarGenerator(outJar))
host.eval("println(\"$greeting\")".toScriptSource(name = "SavedScript.kts"), compilationConfiguration, null).throwOnFailure()
Thread.sleep(100)
val classloader = URLClassLoader(arrayOf(outJar.toURI().toURL()), ScriptingHostTest::class.java.classLoader)
val scriptClass = classloader.loadClass("SavedScript")
val output = captureOut {
@@ -139,6 +140,8 @@ class ScriptingHostTest : TestCase() {
saver(compiledScript, ScriptEvaluationConfiguration.Default).throwOnFailure()
}
Thread.sleep(100)
val classpathFromJar = run {
val manifest = JarFile(outJar).manifest
manifest.mainAttributes.getValue("Class-Path").split(" ") // TODO: quoted paths
@@ -70,19 +70,23 @@ fun KJvmCompiledScript<*>.saveToJar(outputJar: File) {
putValue("Created-By", "JetBrains Kotlin")
if (dependencies.isNotEmpty()) {
// TODO: implement options for various cases - paths as is (now), absolute paths (local execution only), names only (most likely as a hint only), fat jar
putValue("Class-Path", dependencies.joinToString(" "))
putValue("Class-Path", dependencies.joinToString(" ") { it.toURI().toURL().toExternalForm() })
}
putValue("Main-Class", scriptClassFQName)
}
// TODO: fat jar/dependencies
val jarStream = JarOutputStream(fileStream, manifest)
jarStream.putNextEntry(JarEntry(scriptMetadataPath(scriptClassFQName)))
jarStream.write(copyWithoutModule().toBytes())
for ((path, bytes) in module.compilerOutputFiles) {
jarStream.putNextEntry(JarEntry(path))
jarStream.write(bytes)
JarOutputStream(fileStream, manifest).use { jarStream ->
jarStream.putNextEntry(JarEntry(scriptMetadataPath(scriptClassFQName)))
jarStream.write(copyWithoutModule().toBytes())
jarStream.closeEntry()
for ((path, bytes) in module.compilerOutputFiles) {
jarStream.putNextEntry(JarEntry(path))
jarStream.write(bytes)
jarStream.closeEntry()
}
jarStream.finish()
jarStream.flush()
}
jarStream.finish()
fileStream.flush()
}
}