If I want to load json files that I have in my com...
# multiplatform
d
If I want to load json files that I have in my commonTest resources folder as part of my common tests, what kind of paths would I use to actually access those tests? I think I can make do with the actual file reading, I just can't figure out the paths i'd use to point to the proper resource files
e
if you end up figuring this out lmk! i ended up just putting json in kotlin files with the “”" syntax to keep it semi-readable
j
Copy code
internal fun Any.readResource(jsonFileName: String): String {
    return this::class.java.classLoader.getResource(jsonFileName)!!.readText()
}
d
@Javier that works fine for JVM, but not for JS or Native
c
I had some question on the #kotlin-native channel about it and I think these resources don't get packed into the exe if it's not a JVM target
👍 1
d
I'm not too worried about them not being packed with the executable since I only need them for tests. Knowing the tests' working directory would help. I saw your question over in #kotlin-native and the cwd solution suggested looks somewhat promising
c
You'd still have to get them into the directory yourself, I believe
d
Here's a solution I came up with (@edenman) https://gist.github.com/dellisd/a1df42787d42b41cd3ce16f573984674 Works with any native target (except wasm) and for node targets. Doesn't work in browser targets since the browser runner can't access the filesystem, but in theory it would be possible to copy the resource files into karma's included files and then access them using
fetch
instead of
fs
but that would require a whole other set of
actual
implementations for browser targets. Is that even possible? Maybe with HMPP?
e
nice!
tbh i’ll probably just stick with keeping them as vars in .kt files but this is cool
👍 1
d
Impressively IntelliJ has code navigation for the resource files (Ctrl+Click works)
😮 1