Does anyone know a good & easy way how I can r...
# multiplatform
s
Does anyone know a good & easy way how I can replace this in a way that lets me move that class to commonTest? https://github.com/Ashampoo/kim/blob/f56c3b267d310841ba0357eadaecc7e424513b46/src/jvmTest/kotlin/com/ashampoo/kim/testdata/KimTestData.kt#L145 All I can think of is creating an Gradle task that copies the resources to an temp folder first. Does kotlinx-io or anything else give me an easy alternative?
1
e
there's no multiplatform I/O either
however there is https://square.github.io/okio/file_system/#multiplatform which wraps the different platforms' native I/O in some expect/actual which may be of use
j
I described how I'm doing this in a couple threads. See here and here.
e
I don't think that covers the mingwX64 target that OP is using
personally I find it easier to simply pass in the directory by environment, https://github.com/ephemient/aoc2022/blob/main/kt/build.gradle.kts#L72 https://github.com/ephemient/aoc2022/blob/main/kt/src/nativeMain/kotlin/com/github/ephemient/aoc2022/Main.kt#L8 in that project I read the files with POSIX functions because I didn't want to add okio dependency, but a much smaller expect/actual around which okio filesystem implementation to use would be easier
s
Thanks for your input. Did you try https://github.com/goncalossilva/kotlinx-resources/ ? Just found that and sounds promising.
j
I'm using Okio's
FileSystem.SYSTEM
for native Linux and Windows targets. The base path is relative to the project directory. So you could reference the resources from the source set resources directly, e.g.:
Copy code
FileSystem.SYSTEM.source("src/commonTest/resources/$asset".toPath())
s
Jeff, does that work with JVM? When tests are executed often they are already in a JAR and those local paths are not available anymore. That’s why I found Resource loader to be more relieable.
j
I'm using `expect`/`actual` to implement
getAsset(asset: String): Source
for each platform target. JVM I've implemented similar to how you're currently accessing resources from the .jar.
e
the way I've set it up, when tests are executed locally by Gradle, Gradle tells the test what the right paths are. no need to guess what the working directory or paths are from inside the program
j
JVM:
Copy code
javaClass.getResource("/$asset")?.openStream()?.source()
s
Okay, thank you guys 🙏 I understand my options way better now.
j
ephemient, in what case would the working directory be anything but the project's path?