Skip to content

Commit

Permalink
Use KotlinTest for kotlin-scripting-jvm-host-test
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianGM authored and Space Team committed Feb 18, 2025
1 parent 8ac468e commit 6819951
Show file tree
Hide file tree
Showing 13 changed files with 186 additions and 179 deletions.
2 changes: 1 addition & 1 deletion libraries/scripting/jvm-host-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies {
testRuntimeOnly(commonDependency("org.jetbrains.intellij.deps", "trove4j"))

embeddableTestRuntime(project(":kotlin-scripting-jvm-host"))
embeddableTestRuntime(kotlinTest("junit"))
allTestsRuntime(kotlinTest("junit"))
embeddableTestRuntime(projectTests(":compiler:tests-common")) { isTransitive = false }
embeddableTestRuntime(testSourceSet.output)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@

package kotlin.script.experimental.jvmhost.test

import junit.framework.TestCase
import kotlinx.coroutines.runBlocking
import org.jetbrains.kotlin.cli.common.ExitCode
import org.jetbrains.kotlin.cli.common.arguments.CommonCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.K2JVMCompilerArguments
import org.jetbrains.kotlin.cli.common.arguments.cliArgument
import org.jetbrains.kotlin.cli.jvm.K2JVMCompiler
import org.jetbrains.kotlin.scripting.compiler.plugin.impl.SCRIPT_BASE_COMPILER_ARGUMENTS_PROPERTY
import org.junit.Assert
import org.junit.Ignore
import org.junit.Test
import java.io.*
import java.net.URLClassLoader
import java.security.MessageDigest
Expand All @@ -30,8 +26,9 @@ import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.CompiledScriptJarsCache
import kotlin.script.experimental.jvmhost.JvmScriptCompiler
import kotlin.script.experimental.jvmhost.loadScriptFromJar
import kotlin.test.*

class CachingTest : TestCase() {
class CachingTest {

val simpleScript = "val x = 1\nprintln(\"x = \$x\")".toScriptSource()
val simpleScriptExpectedOutput = listOf("x = 1")
Expand Down Expand Up @@ -59,7 +56,7 @@ class CachingTest : TestCase() {
fun testFileCache() {
withTempDir("scriptingTestCache") { cacheDir ->
val cache = FileBasedScriptCache(cacheDir)
Assert.assertEquals(true, cache.baseDir.listFiles()?.isEmpty())
assertEquals(true, cache.baseDir.listFiles()?.isEmpty())

checkWithCache(cache, simpleScript, simpleScriptExpectedOutput)
}
Expand All @@ -69,7 +66,7 @@ class CachingTest : TestCase() {
fun testSimpleImportWithFileCache() {
withTempDir("scriptingTestCache") { cacheDir ->
val cache = FileBasedScriptCache(cacheDir)
Assert.assertEquals(true, cache.baseDir.listFiles()?.isEmpty())
assertEquals(true, cache.baseDir.listFiles()?.isEmpty())

checkWithCache(
cache, scriptWithImport, scriptWithImportExpectedOutput,
Expand All @@ -82,21 +79,21 @@ class CachingTest : TestCase() {
fun testJarCache() {
withTempDir("scriptingTestJarCache") { cacheDir ->
val cache = TestCompiledScriptJarsCache(cacheDir)
Assert.assertTrue(cache.baseDir.listFiles()!!.isEmpty())
assertTrue(cache.baseDir.listFiles()!!.isEmpty())

checkWithCache(cache, simpleScript, simpleScriptExpectedOutput)

val scriptOut = runScriptFromJar(cache.baseDir.listFiles()!!.first { it.extension == "jar" })

Assert.assertEquals(simpleScriptExpectedOutput, scriptOut)
assertEquals(simpleScriptExpectedOutput, scriptOut)
}
}

@Test
fun testSimpleImportWithJarCache() {
withTempDir("scriptingTestJarCache") { cacheDir ->
val cache = TestCompiledScriptJarsCache(cacheDir)
Assert.assertTrue(cache.baseDir.listFiles()!!.isEmpty())
assertTrue(cache.baseDir.listFiles()!!.isEmpty())

checkWithCache(
cache, scriptWithImport, scriptWithImportExpectedOutput,
Expand All @@ -108,15 +105,15 @@ class CachingTest : TestCase() {
// TODO: find a way to make it work
// val scriptOut = runScriptFromJar(cache.baseDir.listFiles()!!.first { it.extension == "jar" })
//
// Assert.assertEquals(scriptWithImportExpectedOutput, scriptOut)
// assertEquals(scriptWithImportExpectedOutput, scriptOut)
}
}

@Test
fun testImplicitReceiversWithJarCache() {
withTempDir("scriptingTestJarCache") { cacheDir ->
val cache = TestCompiledScriptJarsCache(cacheDir)
Assert.assertTrue(cache.baseDir.listFiles()!!.isEmpty())
assertTrue(cache.baseDir.listFiles()!!.isEmpty())

checkWithCache(
cache, simpleScript, simpleScriptExpectedOutput, checkDirectEval = false,
Expand All @@ -139,7 +136,7 @@ class CachingTest : TestCase() {

withTempDir("scriptingTestJarChacheWithDep") { cacheDir ->
val cache = TestCompiledScriptJarsCache(cacheDir)
Assert.assertTrue(cache.baseDir.listFiles()!!.isEmpty())
assertTrue(cache.baseDir.listFiles()!!.isEmpty())

val hostConfiguration = defaultJvmScriptingHostConfiguration.with {
jvm {
Expand All @@ -158,13 +155,13 @@ class CachingTest : TestCase() {

val res0 = host.eval(script, scriptCompilationConfiguration, null).valueOrThrow().returnValue
assertEquals(42, (res0 as? ResultValue.Value)?.value)
Assert.assertEquals(1, cache.storedScripts)
Assert.assertEquals(0, cache.retrievedScripts)
assertEquals(1, cache.storedScripts)
assertEquals(0, cache.retrievedScripts)

val res1 = host.eval(script, scriptCompilationConfiguration, null).valueOrThrow().returnValue
assertEquals(42, (res1 as? ResultValue.Value)?.value)
Assert.assertEquals(1, cache.storedScripts)
Assert.assertEquals(1, cache.retrievedScripts)
assertEquals(1, cache.storedScripts)
assertEquals(1, cache.retrievedScripts)

val outJar2 = File(depDir, "dependency2.jar")
outJar.renameTo(outJar2)
Expand All @@ -191,7 +188,7 @@ class CachingTest : TestCase() {

withTempDir("scriptingTestJarChacheWithExtLoadedDep") { cacheDir ->
val cache = TestCompiledScriptJarsCache(cacheDir)
Assert.assertTrue(cache.baseDir.listFiles()!!.isEmpty())
assertTrue(cache.baseDir.listFiles()!!.isEmpty())

val hostConfiguration = defaultJvmScriptingHostConfiguration.with {
jvm {
Expand Down Expand Up @@ -244,8 +241,8 @@ class CachingTest : TestCase() {
inKt.path
)
assertTrue(
"Compilation Failed:\n$outStream",
outStream.size() == 0 && compileExitCode == ExitCode.OK && outJar.exists()
outStream.size() == 0 && compileExitCode == ExitCode.OK && outJar.exists(),
"Compilation Failed:\n$outStream"
)
return outJar
}
Expand All @@ -272,7 +269,7 @@ class CachingTest : TestCase() {

val scriptEvaluationConfiguration = ScriptEvaluationConfiguration(body = evaluationConfiguration)

Assert.assertEquals(0, cache.storedScripts)
assertEquals(0, cache.storedScripts)
var compiledScript: CompiledScript? = null
val output = captureOut {
runBlocking {
Expand All @@ -282,37 +279,37 @@ class CachingTest : TestCase() {
}.throwOnFailure()
}
}.lines()
Assert.assertEquals(expectedOutput, output)
Assert.assertEquals(1, cache.storedScripts)
Assert.assertEquals(0, cache.retrievedScripts)
assertEquals(expectedOutput, output)
assertEquals(1, cache.storedScripts)
assertEquals(0, cache.retrievedScripts)

if (checkDirectEval) {
val cachedScript = cache.get(script, scriptCompilationConfiguration)
Assert.assertNotNull(cachedScript)
Assert.assertEquals(1, cache.retrievedScripts)
assertNotNull(cachedScript)
assertEquals(1, cache.retrievedScripts)

val compiledScriptClassRes = runBlocking { compiledScript!!.getClass(null) }
val cachedScriptClassRes = runBlocking { cachedScript!!.getClass(null) }

val compiledScriptClass = compiledScriptClassRes.valueOrThrow()
val cachedScriptClass = cachedScriptClassRes.valueOrThrow()

Assert.assertEquals(compiledScriptClass.qualifiedName, cachedScriptClass.qualifiedName)
Assert.assertEquals(compiledScriptClass.java.supertypes(), cachedScriptClass.java.supertypes())
assertEquals(compiledScriptClass.qualifiedName, cachedScriptClass.qualifiedName)
assertEquals(compiledScriptClass.java.supertypes(), cachedScriptClass.java.supertypes())

val output2 = captureOut {
runBlocking {
evaluator(cachedScript!!, scriptEvaluationConfiguration).throwOnFailure()
}
}.lines()
Assert.assertEquals(output, output2)
assertEquals(output, output2)
}

val output3 = captureOut {
host.eval(script, scriptCompilationConfiguration, scriptEvaluationConfiguration).throwOnFailure()
}.lines()
Assert.assertEquals(if (checkDirectEval) 2 else 1, cache.retrievedScripts)
Assert.assertEquals(output, output3)
assertEquals(if (checkDirectEval) 2 else 1, cache.retrievedScripts)
assertEquals(output, output3)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
package kotlin.script.experimental.jvmhost.test

import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.junit.Test
import kotlin.script.experimental.api.*
import kotlin.script.experimental.api.EvaluationResult
import kotlin.script.experimental.api.ResultValue
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.implicitReceivers
import kotlin.test.Test
import kotlin.test.assertTrue

class CapturingTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
package kotlin.script.experimental.jvmhost.test

import junit.framework.TestCase
import org.junit.Assert
import org.junit.Test
import java.io.File
import java.net.URLClassLoader
import java.nio.file.Path
Expand All @@ -21,27 +19,29 @@ import kotlin.script.experimental.jvm.util.classPathFromTypicalResourceUrls
import kotlin.script.experimental.jvm.util.classpathFromClass
import kotlin.script.experimental.jvm.util.classpathFromClassloader
import kotlin.script.experimental.jvm.util.scriptCompilationClasspathFromContextOrNull
import kotlin.test.*

class ClassPathTest : TestCase() {
class ClassPathTest {

lateinit var tempDir: Path

override fun setUp() {
@BeforeTest
fun setUp() {
tempDir = createTempDirectory(ClassPathTest::class.simpleName!!)
super.setUp()
}

override fun tearDown() {
super.tearDown()
@AfterTest
fun tearDown() {
tempDir.toFile().deleteRecursively()
}

@Test
fun testExtractFromFat() {
val collection = createTempFile(directory = tempDir, "col", ".jar").apply { createCollectionJar(emulatedCollectionFiles, "BOOT-INF") }
val collection =
createTempFile(directory = tempDir, "col", ".jar").apply { createCollectionJar(emulatedCollectionFiles, "BOOT-INF") }
val cl = URLClassLoader(arrayOf(collection.toUri().toURL()), null)
val cp = classpathFromClassloader(cl, true)
Assert.assertTrue(cp != null && cp.isNotEmpty())
assertTrue(cp != null && cp.isNotEmpty())

testUnpackedCollection(cp!!, emulatedCollectionFiles)
}
Expand All @@ -57,9 +57,9 @@ class ClassPathTest : TestCase() {
)
val cp = cl.classPathFromTypicalResourceUrls().toList().map { it.canonicalFile }

Assert.assertTrue(cp.contains(jar.toFile().canonicalFile))
assertTrue(cp.contains(jar.toFile().canonicalFile))
for (el in emulatedClasspath) {
Assert.assertTrue(cp.contains((root1 / el).toFile().canonicalFile))
assertTrue(cp.contains((root1 / el).toFile().canonicalFile))
}
}

Expand All @@ -79,7 +79,7 @@ class ClassPathTest : TestCase() {
val classpath =
scriptCompilationClasspathFromContextOrNull("projX", classLoader = classloader)!!.map { it.toPath().relativeTo(tempDir) }

Assert.assertEquals(files.dropLast(1).map { it.relativeTo(tempDir) }, classpath)
assertEquals(files.dropLast(1).map { it.relativeTo(tempDir) }, classpath)
} finally {
tempDir.toFile().deleteRecursively()
}
Expand All @@ -90,8 +90,8 @@ class ClassPathTest : TestCase() {
val cpFromThis = classpathFromClass(this::class)
val expectedSuffix = File("classes/kotlin/test").path
assertTrue(
"Path should end with $expectedSuffix, got: $cpFromThis",
cpFromThis!!.first().absoluteFile.path.endsWith(expectedSuffix)
cpFromThis!!.first().absoluteFile.path.endsWith(expectedSuffix),
"Path should end with $expectedSuffix, got: $cpFromThis"
)
}
}
Expand Down Expand Up @@ -123,13 +123,13 @@ fun testUnpackedCollection(classpath: List<File>, fileNames: Array<String>) {

fun List<String>.checkFiles(root: File) = forEach {
val file = File(root, it)
Assert.assertTrue(file.exists())
Assert.assertEquals(it, file.readText())
assertTrue(file.exists())
assertEquals(it, file.readText())
}

val (classes, jars) = fileNames.partition { it.startsWith("classes") }
val (cpClasses, cpJars) = classpath.partition { it.isDirectory && it.name == "classes" }
Assert.assertTrue(cpClasses.size == 1)
assertTrue(cpClasses.size == 1)
classes.checkFiles(cpClasses.first().parentFile)
jars.checkFiles(cpJars.first().parentFile.parentFile)
}
Expand Down
Loading

0 comments on commit 6819951

Please sign in to comment.