https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
d

Derek Ellis

05/04/2020, 12:15 AM
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

edenman

05/04/2020, 12:35 AM
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

Javier

05/04/2020, 2:09 AM
Copy code
internal fun Any.readResource(jsonFileName: String): String {
    return this::class.java.classLoader.getResource(jsonFileName)!!.readText()
}
d

Derek Ellis

05/04/2020, 3:59 AM
@Javier that works fine for JVM, but not for JS or Native
c

Chilli

05/04/2020, 10:49 AM
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

Derek Ellis

05/04/2020, 10:15 PM
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

Chilli

05/04/2020, 10:24 PM
You'd still have to get them into the directory yourself, I believe
d

Derek Ellis

05/06/2020, 2:37 AM
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

edenman

05/06/2020, 3:48 AM
nice!
tbh i’ll probably just stick with keeping them as vars in .kt files but this is cool
👍 1
d

Derek Ellis

05/06/2020, 8:41 PM
Impressively IntelliJ has code navigation for the resource files (Ctrl+Click works)
😮 1
19 Views