icerockdev / moko-resources

Resources access for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev/
Apache License 2.0
1.07k stars 120 forks source link

Improve cross-platform static file reading in tests #660

Open peterkos opened 5 months ago

peterkos commented 5 months ago

Hello!

Been using 0.24.alpha-5, and it doesn't appear possible to readText() when running a unit test via iOS in KMP. FileResource requires a context, but it's been tricky to implement this.

There's a similar library kotlinx-resources that implements this for text files:

/*
 * For Apple platforms, move resources into the binary's output directory so that they
 * can be loaded using `NSBundle.mainBundle` and related APIs.
 */
if (isAppleCompilation(kotlinCompilation)) {
    val target = kotlinCompilation.target
    target.binaries.forEach { binary ->
        setupCopyResourcesTask(
            kotlinCompilation = kotlinCompilation,
            taskName = getTaskName("copyResources", binary.name, target.targetName),
            outputDir = binary.outputDirectory,
            mustRunAfterTasks = listOf(kotlinCompilation.processResourcesTaskName),
            dependantTasks = listOf(binary.linkTaskName)
        )
    }
}

Would be cool to have something similar here! 🚀

Alex009 commented 4 months ago

we have unit tests that run on all platforms and successful read resources - https://github.com/icerockdev/moko-resources/tree/master/samples/resources-gallery/mpp-library/src/commonTest/kotlin/com/icerock/library

@ExNDY need to check is it works. maybe something with files specially