How do I access `jsTest/resources` or `commonTest/...
# javascript
v
How do I access
jsTest/resources
or
commonTest/resources
from
jsTest/kotlin
?
m
image.png
💩 1
👎 1
e
yeah that's wrong. first part (
js("...")
) 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?
👆 1
under nodejs, there is a
__dirname
global which you should be able to see with
Copy code
external val __dirname: dynamic
in Kotlin; then resolving the relative path from that to to
../../../processedResources
should work
in a browser test, resources should be served in the same directory so you can Fetch them
that said, I'm not sure if
commonTest/resources
gets merged, haven't tried
v
Hm, ok, so no real way in node like resources-handling in JVM code, thx. I guess I'll stay with injecting the path as environment variable to the test task using Gradle.
e
This is how I do it under Node.js (1.9.24 + kotlinx-io)
Copy code
public 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:
Copy code
val xmlStr = loadResourceText("template/template.xml")
Where
template.txt
is under
commonTest/resources/template/
e
if node starts in the correct working directory, that is
✔️ 1
v
Yeah like always when you use a relative
File
or
Path
without supplying the base. Almost never the right thing to do in any Jvm code at least.
e
It really depends on how much effort you want to put into it. For testing I really want resources to be loaded easily - cross OS - and that's it.
t
How we use test resources in Seskar - link.
import
also should work (if you have valid path)
v
Otherwise https://github.com/goncalossilva/kotlinx-resources
Hm, 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.
t
Copy code
@file:JsModule("/kotlin/my-cancellable-response-library.mjs")
.mjs
- my resource
Also possible:
Copy code
val text = import("/kotlin/my-data.txt")
v
Oh, yeah, too tired, thanks. Hm, interesting. I think not applicable to my case where I really want a directory and then list it in the test code but maybe for the future.
t
Test resources dir -
/kotlin
Resources will be copied in it
m
I once had a similar problem where I had to iterate over the content of a resource directory which is not directly possible. I solved the issue by generating a table of contents JSON file of and inside this directory. So, instead of trying to list the contents of the directory I just read the JSON file which has a well known name and can be accessed directly.
v
What's the advantage over just injecting the path as environment variable and then listing the directory?
m
Well, for a test environment this is OK but I had the problem in a packaged application. A TOC file works for both scenarios.
👌 1