Add possibility to get location of the script.main.kts file

#KT-48414 fixed
This commit is contained in:
Alexey Subach
2021-09-19 21:53:13 +03:00
committed by Ilya Chernikov
parent 7ddf83f32d
commit ca2f37f6eb
13 changed files with 177 additions and 7 deletions
@@ -121,6 +121,26 @@ class MainKtsIT {
}
}
@OptIn(ExperimentalPathApi::class)
@Test
fun testCacheWithFileLocation() {
val scriptPath = File("$TEST_DATA_ROOT/script-file-location-default.main.kts").absolutePath
val cache = createTempDirectory("main.kts.test")
val expectedTestOutput = listOf(Regex.escape(scriptPath))
try {
Assert.assertTrue(cache.exists() && cache.listDirectoryEntries("*.jar").isEmpty())
runWithKotlinRunner(scriptPath, expectedTestOutput, cacheDir = cache)
val cacheFile = cache.listDirectoryEntries("*.jar").firstOrNull()
Assert.assertTrue(cacheFile != null && cacheFile.exists())
// this run should use the cached script
runWithKotlinRunner(scriptPath, expectedTestOutput, cacheDir = cache)
} finally {
cache.toFile().deleteRecursively()
}
}
@Test
fun testHelloSerialization() {
val paths = PathUtil.kotlinPathsForDistDirectory
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.mainKts.test
import org.jetbrains.kotlin.mainKts.COMPILED_SCRIPTS_CACHE_DIR_PROPERTY
import org.jetbrains.kotlin.mainKts.impl.Directories
import org.jetbrains.kotlin.mainKts.MainKtsScript
import org.jetbrains.kotlin.mainKts.SCRIPT_FILE_LOCATION_DEFAULT_VARIABLE_NAME
import org.jetbrains.kotlin.scripting.compiler.plugin.assertTrue
import org.junit.Assert
import org.junit.Assert.assertEquals
@@ -145,6 +146,65 @@ class MainKtsTest {
Assert.assertEquals(listOf("Hi from sub", "Hi from super", "Hi from random"), out)
}
@Test
fun testScriptFileLocationDefaultVariable() {
val resOk = evalFile(File("$TEST_DATA_ROOT/script-file-location-default.main.kts"))
assertSucceeded(resOk)
val resultValue = resOk.valueOrThrow().returnValue
assertTrue(resultValue is ResultValue.Value) { "Result value should be of type Value" }
val value = (resultValue as ResultValue.Value).value!!
assertEquals("String", value::class.simpleName)
val expectedPathSuffix = "libraries/tools/kotlin-main-kts-test/testData/script-file-location-default.main.kts"
val actualPath = (value as String).replace("\\", "/")
assertTrue(actualPath.endsWith(expectedPathSuffix)) { "Script file path does not end with expected path" }
}
@Test
fun testScriptFileLocationCustomizedVariable() {
val resOk = evalFile(File("$TEST_DATA_ROOT/script-file-location-customized.main.kts"))
assertSucceeded(resOk)
val resultValue = resOk.valueOrThrow().returnValue
assertTrue(resultValue is ResultValue.Value) { "Result value should be of type Value" }
val value = (resultValue as ResultValue.Value).value!!
assertEquals("String", value::class.simpleName)
val expectedPathSuffix = "libraries/tools/kotlin-main-kts-test/testData/script-file-location-customized.main.kts"
val actualPath = (value as String).replace("\\", "/")
assertTrue(actualPath.endsWith(expectedPathSuffix)) { "Script file path does not end with expected path" }
}
@Test
fun testScriptFileLocationWithImportedScript() {
val resOk = evalFile(File("$TEST_DATA_ROOT/script-file-location-with-imported-file.main.kts"))
assertSucceeded(resOk)
val resultValue = resOk.valueOrThrow().returnValue
assertTrue(resultValue is ResultValue.Value) { "Result value should be of type Value" }
val value = (resultValue as ResultValue.Value).value!!
assertEquals("Array", value::class.simpleName)
val expectedSelfPathSuffix = "libraries/tools/kotlin-main-kts-test/testData/script-file-location-with-imported-file.main.kts"
val expectedImportedPathSuffix = "libraries/tools/kotlin-main-kts-test/testData/script-file-location-helper-imported-file.main.kts"
val actualPathSelf = (value as Array<*>)[0].toString().replace("\\", "/")
val actualPathImported = value[1].toString().replace("\\", "/")
assertTrue(actualPathSelf.endsWith(expectedSelfPathSuffix)) { "Script file path does not end with expected path" }
assertTrue(actualPathImported.endsWith(expectedImportedPathSuffix)) { "Script file path does not end with expected path" }
}
@Test
fun testScriptFileLocationDefaultVariableNotAvailableIfScriptFileVariableCustomized() {
val resFailed = evalFile(File("$TEST_DATA_ROOT/script-file-location-customized-default-not-available.main.kts"))
assertFailed("Unresolved reference: $SCRIPT_FILE_LOCATION_DEFAULT_VARIABLE_NAME", resFailed)
}
@Test
fun testScriptFileLocationDefaultVariableRedefinition() {
val resOk = evalFile(File("$TEST_DATA_ROOT/script-file-location-redefine-variable.kts"))
assertSucceeded(resOk)
val resultValue = resOk.valueOrThrow().returnValue
assertTrue(resultValue is ResultValue.Value) { "Result value should be of type Value" }
val value = (resultValue as ResultValue.Value).value!!
assertEquals("String", value::class.simpleName)
assertEquals("success", value)
}
private fun assertIsJava6Bytecode(res: ResultWithDiagnostics<EvaluationResult>) {
val scriptClassResource = res.valueOrThrow().returnValue.scriptClass!!.java.run {
getResource("$simpleName.class")
@@ -0,0 +1,4 @@
@file:ScriptFileLocation("scriptFileLocation")
__FILE__.absolutePath
@@ -0,0 +1,4 @@
@file:ScriptFileLocation("scriptFileLocation")
scriptFileLocation.absolutePath
@@ -0,0 +1,2 @@
__FILE__.absolutePath
@@ -0,0 +1,5 @@
import java.io.File
fun getDependentScriptFile(): File {
return __FILE__
}
@@ -0,0 +1,3 @@
val __FILE__ = "success"
__FILE__
@@ -0,0 +1,4 @@
@file:Import("script-file-location-helper-imported-file.main.kts")
arrayOf(__FILE__.absolutePath, getDependentScriptFile().absolutePath)