Skip to content

Kotlin Multiplatform (KMP) library for reading resources in tests

License

Notifications You must be signed in to change notification settings

goncalossilva/kotlinx-resources

Folders and files

NameName
Last commit message
Last commit date
Dec 19, 2024
May 24, 2023
Mar 25, 2025
Dec 19, 2024
Dec 19, 2024
Dec 17, 2024
Dec 19, 2024
Dec 19, 2024
Dec 19, 2024
Apr 21, 2023
Dec 19, 2024
Dec 8, 2021
Feb 19, 2023
Feb 25, 2025
Jul 11, 2024
Feb 9, 2022

Repository files navigation

kotlinx-resources

badge-library-version badge-plugin-version badge-jvm badge-js badge-nodejs badge-android badge-ios badge-watchos badge-tvos badge-macos badge-windows badge-linux

Kotlin Multiplatform (KMP) plugin and library that add support for reading resources in tests.

The plugin and a library work in tandem to provide a unified API across platforms for reading resources from each source set's resources folder.

Setup

List the plugin in your build.gradle.kts:

plugins {
    id("com.goncalossilva.resources") version "<version>"
}

And add the dependency to your commonTest source set:

kotlin {
    sourceSets {
        val commonTest by getting {
            dependencies {
                implementation("com.goncalossilva:resources:<version>")
            }
        }
    }
}

Compatibility

Different Kotlin versions require different versions of the plugin/library:

Kotlin kotlinx-resources
2.1 and above 0.10 and above
2.0 0.9
1.9 and below 0.8 and below (plus k1 branch)

Usage

Once setup is done done, a Resource class becomes available in all test sources, with a simple API:

class Resource(path: String) {
    fun exists(): Boolean
    fun readText(): String
    fun readBytes(): ByteArray
}

To setup resources correctly and avoid FilNotFoundException & co:

  1. Put them in the resources folder of a source set. For example, src/commonTest/resources/ or src/jsTest/resources/.
  2. Specify the path relative to the project's directory. For example, src/commonTest/resources/a-folder/a-file.txt.

With these in mind, you're ready to go.

Example

Library tests use the library itself, so they serve as a practical example.

See ResourceTest for example usage, and resources-test/src/commonTest/resources for the associated folder structure for resources.

Acknowledgements

This library is inspired by this gist by @dellisd.

License

Released under the MIT License.