Vampire
01/16/2025, 12:02 AMjsTest/resources
or commonTest/resources
from jsTest/kotlin
?marlonlom
01/16/2025, 3:20 AMephemient
01/16/2025, 6:41 AMjs("...")
) may conceivably work in a nodejs test if you have the whole path, but that isn't using resources. second part won't work as there's no File
in JS. try not to just copy AI, ok?ephemient
01/16/2025, 6:46 AM__dirname
global which you should be able to see with
external val __dirname: dynamic
in Kotlin; then resolving the relative path from that to to ../../../processedResources
should workephemient
01/16/2025, 6:47 AMephemient
01/16/2025, 6:48 AMcommonTest/resources
gets merged, haven't triedVampire
01/16/2025, 8:00 AMEdoardo Luppi
01/16/2025, 8:19 AMpublic actual fun loadResourceText(path: String): String {
val buffer = readToBuffer(path)
return buffer.readString()
}
private fun readToBuffer(path: String): Buffer {
val normalizedPath = Path("kotlin/$path")
val fs = SystemFileSystem
if (!fs.exists(normalizedPath)) {
throw FileNotFoundException("File does not exist: $normalizedPath")
}
val source = fs.source(normalizedPath)
val dest = Buffer()
var read: Long
do {
read = source.readAtMostTo(dest, 4096L)
} while (read > 0)
return dest
}
Usage:
val xmlStr = loadResourceText("template/template.xml")
Where template.txt
is under commonTest/resources/template/
ephemient
01/16/2025, 8:19 AMVampire
01/16/2025, 8:49 AMFile
or Path
without supplying the base. Almost never the right thing to do in any Jvm code at least.Edoardo Luppi
01/16/2025, 8:57 AMEdoardo Luppi
01/16/2025, 8:57 AMturansky
01/16/2025, 9:35 AMturansky
01/16/2025, 9:37 AMimport
also should work (if you have valid path)Vampire
01/16/2025, 10:25 AMOtherwise https://github.com/goncalossilva/kotlinx-resourcesHm, thanks, not fully workable for me as I need to get the directory and then list its contents. And I somewhat do not like that you specify the full source path. But from gut feeling this is mainly also just injecting the path from Gradle and then providing a unified API to access it, so I guess I just stay with pushing the path in from the build script, thanks.
How we use test resources in Seskar - link.Maybe I'm too tired, but I don't see how you access resources in that class.
turansky
01/16/2025, 10:26 AM@file:JsModule("/kotlin/my-cancellable-response-library.mjs")
turansky
01/16/2025, 10:26 AM.mjs
- my resourceturansky
01/16/2025, 10:27 AMval text = import("/kotlin/my-data.txt")
Vampire
01/16/2025, 10:28 AMturansky
01/16/2025, 10:29 AM/kotlin
turansky
01/16/2025, 10:30 AMMichael Paus
01/16/2025, 11:07 AMVampire
01/16/2025, 11:17 AMMichael Paus
01/16/2025, 12:37 PM