From a110fcce19122b42d45afda40aa7f3ac09243ad7 Mon Sep 17 00:00:00 2001 From: xxfast Date: Thu, 24 Oct 2024 10:40:23 +1100 Subject: [PATCH] Revert "Add directories initialiser and tests (#132)" This reverts commit 9a439c3798d44d45aedd47f91437ffec828f84fe. --- docs/topics/using-platform-paths.md | 101 ++++-------------- gradle/libs.versions.toml | 8 -- kstore-file/api/android/kstore-file.api | 20 ---- kstore-file/api/desktop/kstore-file.api | 13 --- kstore-file/build.gradle.kts | 24 +---- .../src/androidMain/AndroidManifest.xml | 15 --- .../kstore/file/AndroidDirectoryProvider.kt | 26 ----- .../kstore/file/DirectoriesInitializer.kt | 13 --- .../kstore/file/DirectoryProviderTest.kt | 46 -------- .../kstore/file/AppleDirectoryProvider.kt | 56 ---------- .../kstore/file/DirectoryProviderTest.kt | 28 ----- .../xxfast/kstore/file/DirectoryProvider.kt | 22 ---- .../xxfast/kstore/file/FileCodecTests.kt | 3 - .../github/xxfast/kstore/file/TestModels.kt | 2 + .../kstore/file/DesktopDirectoryProvider.kt | 37 ------- .../kstore/file/DirectoryProviderTest.kt | 29 ----- .../xxfast/kstore/file/JsDirectoryProvider.kt | 18 ---- .../kotlin/io/github/xxfast/kstore/file/Os.kt | 12 --- .../kstore/file/DirectoryProviderTest.kt | 19 ---- .../kstore/file/LinuxDirectoryProvider.kt | 22 ---- .../kstore/file/DirectoryProviderTest.kt | 31 ------ .../kstore/file/WindowsDirectoryProvider.kt | 28 ----- .../kstore/file/DirectoryProviderTest.kt | 18 ---- 23 files changed, 26 insertions(+), 565 deletions(-) delete mode 100644 kstore-file/src/androidMain/AndroidManifest.xml delete mode 100644 kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/AndroidDirectoryProvider.kt delete mode 100644 kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/DirectoriesInitializer.kt delete mode 100644 kstore-file/src/androidUnitTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt delete mode 100644 kstore-file/src/appleMain/kotlin/io/github/xxfast/kstore/file/AppleDirectoryProvider.kt delete mode 100644 kstore-file/src/appleTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt delete mode 100644 kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/DirectoryProvider.kt delete mode 100644 kstore-file/src/desktopMain/kotlin/io/github/xxfast/kstore/file/DesktopDirectoryProvider.kt delete mode 100644 kstore-file/src/desktopTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt delete mode 100644 kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/JsDirectoryProvider.kt delete mode 100644 kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/Os.kt delete mode 100644 kstore-file/src/jsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt delete mode 100644 kstore-file/src/linuxMain/kotlin/io/github/xxfast/kstore/file/LinuxDirectoryProvider.kt delete mode 100644 kstore-file/src/linuxTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt delete mode 100644 kstore-file/src/windowsMain/kotlin/io/github/xxfast/kstore/file/WindowsDirectoryProvider.kt delete mode 100644 kstore-file/src/windowsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt diff --git a/docs/topics/using-platform-paths.md b/docs/topics/using-platform-paths.md index a880aa2..d0a5283 100644 --- a/docs/topics/using-platform-paths.md +++ b/docs/topics/using-platform-paths.md @@ -1,112 +1,55 @@ # Using Platform Specific Paths -## Using Default Directory Provider - -> This is experimental API and may be removed in future releases -> {style="note"} - -`DefaultDirectories` provides a path to directories where you can store your files for each platform. - -```kotlin -// For files directory -val files: KStore = storeOf(file = Path("${DefaultDirectories.files}/my_cats.json")) - -// For caches directory -val caches: KStore = storeOf(file = Path("${DefaultDirectories.caches}/my_cats.json")) -``` - -This will resolve to the appropriate directory for each platform -// Generate a table of 3 columns, 6 rows -| Platform | Files directory | Caches directory | -|----------|-----------------|------------------| -| Android | `context.filesDir` | `context.cacheDir` | -| iOS | `NSDocumentDirectory` | `NSCachesDirectory` | -| Desktop (Mac)* | `/Users//Library/Application Support/io.github.xxfast.kstore` | `/Users//Library/Caches/io.github.xxfast.kstore` | -| Desktop (Windows)* | `C:\\ave\AppData\Local\xxfast\io.github.xxfast.kstore` | `C:\Users\ave\AppData\Local\xxfast\myapp\Cache\io.github.xxfast.kstore` | -| Desktop (Linux)* | `home//.local/share/io.github.xxfast.kstore` | `/home//.cache/io.github.xxfast.kstore` | - -> * - via [harawata/appdirs](https://github.com/harawata/appdirs) - -## Defining Your Own Directory Provider - -If you want to put your files elsewhere, define your own directory provider for each platform. +Getting a path to a file is different for each platform, and you will need to define how this works for each platform ```kotlin -var directories: DirectoryProvider -val files: KStore = storeOf(file = Path("${directories.files}/my_cats.json")) -val caches: KStore = storeOf(file = Path("${directories.cache}/my_cats.json")) +var storageDir: String ``` -> For this example, we are keeping this as a top level variable. But do use your favorite DI framework instead +> For this example, we are keeping this as a top level variable. > { style="note" } -### On Android +## On Android Getting a path on android involves invoking from `filesDir`/`cacheDir` from a `Context`. ```kotlin -import kotlin.io.path.Path - class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - directories = DirectoryProvider( - files = Path(context.filesDir), - cache = Path(context.cacheDir), - ) + // for documents directory + storageDir = filesDir.path + + // or caches directory + storageDir = cacheDir.path } } ``` -### On Desktop (JVM) +## On Desktop (JVM) This depends on where you want to save your files, but generally you should save your files in a user data directory. Recommending to use [harawata's appdirs](https://github.com/harawata/appdirs) to get the platform specific app dir ```kotlin -const val PACKAGE_NAME = "io.github.xxfast.kstore" -const val VERSION = "1.0" -const val ORGANISATION = "xxfast" - -directories = DirectoryProvider( - files = AppDirsFactory.getInstance().getUserDataDir(PACKAGE_NAME, VERSION, ORGANISATION), - cache = AppDirsFactory.getInstance().getUserCacheDir(PACKAGE_NAME, VERSION, ORGANISATION), -) +storageDir = AppDirsFactory.getInstance() + .getUserDataDir(PACKAGE_NAME, VERSION, ORGANISATION) ``` -> Make sure to create those directories if they don't already exist. The store won't create them for you -> { style="note" } - -### On iOS & other Apple platforms +## On iOS & other Apple platforms This depends on where you want to place your files. For most common use-cases, you will want either `NSDocumentDirectory` or `NSCachesDirectory` KStore provides you a convenience extensions to resolve these for you ```kotlin -val fileManager:NSFileManager = NSFileManager.defaultManager -val documentsUrl: NSURL? = fileManager.URLForDirectory( - directory = NSDocumentDirectory, - appropriateForURL = null, - create = false, - inDomain = NSUserDomainMask, - error = null -) - -val cachesUrl:NSURL? = fileManager.URLForDirectory( - directory = NSCachesDirectory, - appropriateForURL = null, - create = false, - inDomain = NSUserDomainMask, - error = null -) - -val files = requireNotNull(documentsUrl?.path) -val caches = requireNotNull(cachesUrl?.path) - -directories = DirectoryProvider( - files = Path(files), - caches = Path(caches) -) +// for documents directory +storageDir = NSFileManager.defaultManager.DocumentDirectory?.relativePath + +// or caches directory +storageDir = NSFileManager.defaultManager.CachesDirectory?.relativePath ``` +> This is experimental API and may be removed in future releases +> {style="note"} + > `NSHomeDirectory()` _(though it works on the simulator)_ is **not** suitable for physical devices as the security policies on physical devices does not permit read/writes to this directory > {style="warning"} @@ -115,4 +58,4 @@ directories = DirectoryProvider( Learn how to work with files and directories when developing iOS applications - + \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 14dab0f..103455f 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,22 +1,17 @@ [versions] agp = "8.0.2" androidx-test-junit = "1.2.1" -androidx-startup = "1.1.1" -harawata-appdirs = "1.2.1" junit = "4.13.2" junit-jupiter="5.10.1" kotlin = "2.0.10" kotlinx-coroutines = "1.9.0-RC" kotlinx-serialization = "1.7.1" kotlinx-io = "0.5.0" -mockk = "1.13.13" turbine = "1.1.0" [libraries] agp = { module = "com.android.tools.build:gradle", version.ref = "agp" } androidx-test-junit = { module = "androidx.test.ext:junit", version.ref = "androidx-test-junit" } -androidx-startup = { module = "androidx.startup:startup-runtime", version.ref = "androidx-startup" } -harawata-appdirs = { module = "net.harawata:appdirs", version.ref = "harawata-appdirs" } junit = { module = "junit:junit", version.ref = "junit" } junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "junit-jupiter" } junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "junit-jupiter" } @@ -27,7 +22,4 @@ kotlin-serialization = { module = "org.jetbrains.kotlin:kotlin-serialization", v kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization" } kotlinx-serialization-json-io = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json-io", version.ref = "kotlinx-serialization" } kotlinx-io = { module = "org.jetbrains.kotlinx:kotlinx-io-core", version.ref = "kotlinx-io" } -mockk = { module = "io.mockk:mockk", version.ref = "mockk" } -mockk-agent = { module = "io.mockk:mockk-agent", version.ref = "mockk" } -mockk-android = { module = "io.mockk:mockk-android", version.ref = "mockk" } turbine = { module = "app.cash.turbine:turbine", version.ref = "turbine" } diff --git a/kstore-file/api/android/kstore-file.api b/kstore-file/api/android/kstore-file.api index 3047e8b..d0c86ed 100644 --- a/kstore-file/api/android/kstore-file.api +++ b/kstore-file/api/android/kstore-file.api @@ -1,23 +1,3 @@ -public final class io/github/xxfast/kstore/file/AndroidDirectoryProviderKt { - public static final fun getDefaultDirectories ()Lio/github/xxfast/kstore/file/DirectoryProvider; -} - -public final class io/github/xxfast/kstore/file/DirectoriesInitializer : androidx/startup/Initializer { - public fun ()V - public fun create (Landroid/content/Context;)Lio/github/xxfast/kstore/file/DirectoryProvider; - public synthetic fun create (Landroid/content/Context;)Ljava/lang/Object; - public fun dependencies ()Ljava/util/List; -} - -public abstract interface class io/github/xxfast/kstore/file/DirectoryProvider { - public abstract fun getCaches ()Lkotlinx/io/files/Path; - public abstract fun getFiles ()Lkotlinx/io/files/Path; -} - -public final class io/github/xxfast/kstore/file/DirectoryProviderKt { - public static final fun DirectoryProvider (Lkotlinx/io/files/Path;Lkotlinx/io/files/Path;)Lio/github/xxfast/kstore/file/DirectoryProvider; -} - public final class io/github/xxfast/kstore/file/FileCodec : io/github/xxfast/kstore/Codec { public fun (Lkotlinx/io/files/Path;Lkotlinx/io/files/Path;Lkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;)V public fun decode (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; diff --git a/kstore-file/api/desktop/kstore-file.api b/kstore-file/api/desktop/kstore-file.api index c27461b..d0c86ed 100644 --- a/kstore-file/api/desktop/kstore-file.api +++ b/kstore-file/api/desktop/kstore-file.api @@ -1,16 +1,3 @@ -public final class io/github/xxfast/kstore/file/DesktopDirectoryProviderKt { - public static final fun getDefaultDirectories ()Lio/github/xxfast/kstore/file/DirectoryProvider; -} - -public abstract interface class io/github/xxfast/kstore/file/DirectoryProvider { - public abstract fun getCaches ()Lkotlinx/io/files/Path; - public abstract fun getFiles ()Lkotlinx/io/files/Path; -} - -public final class io/github/xxfast/kstore/file/DirectoryProviderKt { - public static final fun DirectoryProvider (Lkotlinx/io/files/Path;Lkotlinx/io/files/Path;)Lio/github/xxfast/kstore/file/DirectoryProvider; -} - public final class io/github/xxfast/kstore/file/FileCodec : io/github/xxfast/kstore/Codec { public fun (Lkotlinx/io/files/Path;Lkotlinx/io/files/Path;Lkotlinx/serialization/json/Json;Lkotlinx/serialization/KSerializer;)V public fun decode (Lkotlin/coroutines/Continuation;)Ljava/lang/Object; diff --git a/kstore-file/build.gradle.kts b/kstore-file/build.gradle.kts index 252db7b..4b89d21 100644 --- a/kstore-file/build.gradle.kts +++ b/kstore-file/build.gradle.kts @@ -23,12 +23,6 @@ android { abortOnError = false } - testOptions { - unitTests { - isReturnDefaultValues = true - } - } - namespace = "io.github.xxfast.kstore.file" } @@ -108,27 +102,13 @@ kotlin { } } - val androidMain by getting { - dependencies { - implementation(libs.androidx.startup) - } - } - - val androidUnitTest by getting { - dependencies { - implementation(libs.androidx.startup) - implementation(libs.mockk) - implementation(libs.mockk.android) - implementation(libs.mockk.agent) - } - } + val androidMain by getting + val androidUnitTest by getting val desktopMain by getting { dependencies { - implementation(libs.harawata.appdirs) } } - val desktopTest by getting val jsMain by getting diff --git a/kstore-file/src/androidMain/AndroidManifest.xml b/kstore-file/src/androidMain/AndroidManifest.xml deleted file mode 100644 index af3955b..0000000 --- a/kstore-file/src/androidMain/AndroidManifest.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - - diff --git a/kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/AndroidDirectoryProvider.kt b/kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/AndroidDirectoryProvider.kt deleted file mode 100644 index a6d8b95..0000000 --- a/kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/AndroidDirectoryProvider.kt +++ /dev/null @@ -1,26 +0,0 @@ -package io.github.xxfast.kstore.file - -import android.content.Context -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlinx.io.files.Path - -/** - * When no context is available, this will point to root directory. - */ -@ExperimentalKStoreApi -internal var _directories: DirectoryProvider = RootDirectoryProvider - -@ExperimentalKStoreApi -public actual val DefaultDirectories: DirectoryProvider get() = _directories - -@ExperimentalKStoreApi -internal class AndroidDirectoryProvider(context: Context) : DirectoryProvider { - override val files: Path = Path(context.filesDir.path) - override val caches: Path = Path(context.cacheDir.path) -} - -@ExperimentalKStoreApi -internal object RootDirectoryProvider: DirectoryProvider { - override val files: Path = Path("/") - override val caches: Path = Path("/") -} diff --git a/kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/DirectoriesInitializer.kt b/kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/DirectoriesInitializer.kt deleted file mode 100644 index 300b763..0000000 --- a/kstore-file/src/androidMain/kotlin/io/github/xxfast/kstore/file/DirectoriesInitializer.kt +++ /dev/null @@ -1,13 +0,0 @@ -package io.github.xxfast.kstore.file - -import android.content.Context -import androidx.startup.Initializer -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi - -@ExperimentalKStoreApi -public class DirectoriesInitializer: Initializer { - override fun create(context: Context): DirectoryProvider = AndroidDirectoryProvider(context) - .also { _directories = it } - - override fun dependencies(): List>> = emptyList() -} \ No newline at end of file diff --git a/kstore-file/src/androidUnitTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt b/kstore-file/src/androidUnitTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt deleted file mode 100644 index 2504542..0000000 --- a/kstore-file/src/androidUnitTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt +++ /dev/null @@ -1,46 +0,0 @@ -package io.github.xxfast.kstore.file - -import android.content.Context -import androidx.startup.AppInitializer -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import io.mockk.every -import io.mockk.mockk -import java.io.File -import kotlin.test.AfterTest -import kotlin.test.Test - -@OptIn(ExperimentalKStoreApi::class) -class DirectoryProviders { - - @AfterTest - fun cleanUp() { - _directories = RootDirectoryProvider - } - - @Test - fun testRootDirectoryWhenNotInitialised() { - val provider: DirectoryProvider = DefaultDirectories - val files: String = provider.files.toString() - val caches: String = provider.caches.toString() - assert(files == "/") { "$files doesn't match expected" } - assert(caches == "/") { "$caches doesn't match expected" } - } - - @Test - fun testRootDirectoryWhenInitialised() { - val context: Context = mockk() - - every { context.applicationContext } returns context - every { context.filesDir } returns File("files") - every { context.cacheDir } returns File("caches") - - AppInitializer.getInstance(context) - .initializeComponent(DirectoriesInitializer::class.java) - - val provider: DirectoryProvider = DefaultDirectories - val files: String = provider.files.toString() - val caches: String = provider.caches.toString() - assert(files == "files") { "$files doesn't match expected" } - assert(caches == "caches") { "$caches doesn't match expected" } - } -} diff --git a/kstore-file/src/appleMain/kotlin/io/github/xxfast/kstore/file/AppleDirectoryProvider.kt b/kstore-file/src/appleMain/kotlin/io/github/xxfast/kstore/file/AppleDirectoryProvider.kt deleted file mode 100644 index fa329be..0000000 --- a/kstore-file/src/appleMain/kotlin/io/github/xxfast/kstore/file/AppleDirectoryProvider.kt +++ /dev/null @@ -1,56 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlinx.cinterop.ExperimentalForeignApi -import kotlinx.cinterop.UnsafeNumber -import kotlinx.io.files.Path -import kotlinx.io.files.SystemFileSystem -import platform.Foundation.NSCachesDirectory -import platform.Foundation.NSDocumentDirectory -import platform.Foundation.NSFileManager -import platform.Foundation.NSURL -import platform.Foundation.NSUserDomainMask - -@ExperimentalKStoreApi -public actual val DefaultDirectories: DirectoryProvider = AppleDirectoryProvider(NSFileManager.defaultManager) - -@ExperimentalKStoreApi -private class AppleDirectoryProvider(private val fileManager: NSFileManager): DirectoryProvider { - @OptIn(UnsafeNumber::class, ExperimentalForeignApi::class) - override val files: Path get() { - val url: NSURL? = fileManager.URLForDirectory( - directory = NSDocumentDirectory, - appropriateForURL = null, - create = false, - inDomain = NSUserDomainMask, - error = null - ) - - val urlPath: String = requireNotNull(url?.path) { - "Unable to locate or optionally create documents" - } - - val path = Path(urlPath) - if (!SystemFileSystem.exists(path)) SystemFileSystem.createDirectories(path, true) - return path - } - - @OptIn(UnsafeNumber::class, ExperimentalForeignApi::class) - override val caches: Path get() { - val url: NSURL? = fileManager.URLForDirectory( - directory = NSCachesDirectory, - appropriateForURL = null, - create = false, - inDomain = NSUserDomainMask, - error = null - ) - - val urlPath: String = requireNotNull(url?.path) { - "Unable to locate or optionally create caches" - } - - val path = Path(urlPath) - if (!SystemFileSystem.exists(path)) SystemFileSystem.createDirectories(path, true) - return path - } -} diff --git a/kstore-file/src/appleTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt b/kstore-file/src/appleTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt deleted file mode 100644 index 568b43a..0000000 --- a/kstore-file/src/appleTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt +++ /dev/null @@ -1,28 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlin.experimental.ExperimentalNativeApi -import kotlin.test.Test - -private val FILE_REGEX = Regex(""" - /Users/.+/Documents - """.trimIndent() -) - -private val CACHES_REGEX = Regex(""" - /Users/.+/Caches - """.trimIndent() -) - -@OptIn(ExperimentalKStoreApi::class, ExperimentalNativeApi::class) -class DirectoryProviders { - @Test - fun testDefaultDirectory() { - val provider: DirectoryProvider = DefaultDirectories - val files = provider.files.toString() - val caches= provider.caches.toString() - - assert(files.matches(FILE_REGEX)) { "$files doesn't match expected" } - assert(caches.matches(CACHES_REGEX)) { "$caches doesn't match expected" } - } -} diff --git a/kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/DirectoryProvider.kt b/kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/DirectoryProvider.kt deleted file mode 100644 index 34b582f..0000000 --- a/kstore-file/src/commonMain/kotlin/io/github/xxfast/kstore/file/DirectoryProvider.kt +++ /dev/null @@ -1,22 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlinx.io.files.Path - -@ExperimentalKStoreApi -public expect val DefaultDirectories: DirectoryProvider - -@ExperimentalKStoreApi -public interface DirectoryProvider { - public val files: Path - public val caches: Path -} - -@ExperimentalKStoreApi -public fun DirectoryProvider( - files: Path, - caches: Path -): DirectoryProvider = object : DirectoryProvider { - override val files: Path get() = files - override val caches: Path get() = caches -} diff --git a/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/FileCodecTests.kt b/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/FileCodecTests.kt index 78b240f..6aebe06 100644 --- a/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/FileCodecTests.kt +++ b/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/FileCodecTests.kt @@ -1,7 +1,6 @@ package io.github.xxfast.kstore.file import io.github.xxfast.kstore.DefaultJson -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi import kotlinx.coroutines.test.runTest import kotlinx.io.buffered import kotlinx.io.files.Path @@ -16,9 +15,7 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith -const val FILE_PATH ="test.json" -@OptIn(ExperimentalKStoreApi::class) class FileCodecTests { private val codec: FileCodec> = FileCodec(file = Path(FILE_PATH)) diff --git a/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/TestModels.kt b/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/TestModels.kt index dff24db..4fca357 100644 --- a/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/TestModels.kt +++ b/kstore-file/src/commonTest/kotlin/io/github/xxfast/kstore/file/TestModels.kt @@ -43,3 +43,5 @@ data class RobotCat( internal val MYLO = Cat(name = "Mylo", age = 1) internal val OREO = Cat(name = "Oreo", age = 1) internal val KAT = RobotCat(name = "Kat", age = 12, id = Id(123L)) + +const val FILE_PATH = "test.json" diff --git a/kstore-file/src/desktopMain/kotlin/io/github/xxfast/kstore/file/DesktopDirectoryProvider.kt b/kstore-file/src/desktopMain/kotlin/io/github/xxfast/kstore/file/DesktopDirectoryProvider.kt deleted file mode 100644 index 9461f9c..0000000 --- a/kstore-file/src/desktopMain/kotlin/io/github/xxfast/kstore/file/DesktopDirectoryProvider.kt +++ /dev/null @@ -1,37 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlinx.io.files.Path -import kotlinx.io.files.SystemFileSystem -import net.harawata.appdirs.AppDirs -import net.harawata.appdirs.AppDirsFactory - -@ExperimentalKStoreApi -public actual val DefaultDirectories: DirectoryProvider = DesktopDirectoryProvider( - packageName = "io.github.xxfast.kstore", - version = "", - author = "xxfast" -) - -@ExperimentalKStoreApi -private class DesktopDirectoryProvider( - private val packageName: String, - private val version: String, - private val author: String, -) : DirectoryProvider { - private val appDirs: AppDirs = AppDirsFactory.getInstance() - - override val files: Path get() { - val filesDir: String = appDirs.getUserDataDir(packageName, version, author) - val path = Path(filesDir) - SystemFileSystem.createDirectories(path) - return path - } - - override val caches: Path get() { - val filesDir: String = appDirs.getUserCacheDir(packageName, version, author) - val path = Path(filesDir) - SystemFileSystem.createDirectories(path) - return path - } -} \ No newline at end of file diff --git a/kstore-file/src/desktopTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt b/kstore-file/src/desktopTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt deleted file mode 100644 index 3db2711..0000000 --- a/kstore-file/src/desktopTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt +++ /dev/null @@ -1,29 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlin.test.Test - -/** - * desktop tests are run on linux agents, so the paths are linux specific. - */ -private val FILE_REGEX = Regex(""" - .+/io.github.xxfast.kstore - """.trimIndent() -) - -private val CACHES_REGEX = Regex(""" - .+/io.github.xxfast.kstore - """.trimIndent() -) - -@OptIn(ExperimentalKStoreApi::class) -class DirectoryProviders { - @Test - fun testDefaultDirectory() { - val provider: DirectoryProvider = DefaultDirectories - val files: String = provider.files.toString() - val caches: String = provider.caches.toString() - assert(files.matches(FILE_REGEX)) { "$files doesn't match expected" } - assert(caches.matches(CACHES_REGEX)) { "$caches doesn't match expected" } - } -} diff --git a/kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/JsDirectoryProvider.kt b/kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/JsDirectoryProvider.kt deleted file mode 100644 index 0b504c7..0000000 --- a/kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/JsDirectoryProvider.kt +++ /dev/null @@ -1,18 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlinx.io.files.Path - -@ExperimentalKStoreApi -public actual val DefaultDirectories: DirectoryProvider = JsDirectoryProvider() - -@ExperimentalKStoreApi -public class JsDirectoryProvider: DirectoryProvider { - private val os: Os by lazy { - try { js("eval('require')('os')") } - catch (e: Throwable) { throw UnsupportedOperationException("Module 'os' could not be imported", e) } - } - - public override val files: Path get() = Path(os.homedir()) - public override val caches: Path = Path(os.tmpdir()) -} diff --git a/kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/Os.kt b/kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/Os.kt deleted file mode 100644 index 003b6ac..0000000 --- a/kstore-file/src/jsMain/kotlin/io/github/xxfast/kstore/file/Os.kt +++ /dev/null @@ -1,12 +0,0 @@ -package io.github.xxfast.kstore.file - -internal external interface Os { - /** - * See https://nodejs.org/api/os.html#oshomedir - */ - fun homedir(): String - /** - * See https://nodejs.org/api/os.html#ostmpdir - */ - fun tmpdir(): String -} \ No newline at end of file diff --git a/kstore-file/src/jsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt b/kstore-file/src/jsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt deleted file mode 100644 index 396107a..0000000 --- a/kstore-file/src/jsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt +++ /dev/null @@ -1,19 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlin.test.Test -import kotlin.test.assertTrue - -@OptIn(ExperimentalKStoreApi::class) -class DirectoryProviders { - @Test - fun testDefaultDirectory() { - val provider: DirectoryProvider = DefaultDirectories - val files: String = provider.files.toString() - val caches: String = provider.caches.toString() - - // Node doesn't have a default directory, so we can't check the exact path - assertTrue("$files is empty") { files.isNotEmpty() } - assertTrue("$caches is empty") { caches.isNotEmpty() } - } -} diff --git a/kstore-file/src/linuxMain/kotlin/io/github/xxfast/kstore/file/LinuxDirectoryProvider.kt b/kstore-file/src/linuxMain/kotlin/io/github/xxfast/kstore/file/LinuxDirectoryProvider.kt deleted file mode 100644 index 4508e99..0000000 --- a/kstore-file/src/linuxMain/kotlin/io/github/xxfast/kstore/file/LinuxDirectoryProvider.kt +++ /dev/null @@ -1,22 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlinx.cinterop.ExperimentalForeignApi -import kotlinx.cinterop.toKString -import kotlinx.io.files.Path -import platform.posix.getenv - -@ExperimentalKStoreApi -public actual val DefaultDirectories: DirectoryProvider = LinuxDirectoryProvider() - -@ExperimentalKStoreApi -private class LinuxDirectoryProvider: DirectoryProvider { - - @OptIn(ExperimentalForeignApi::class) - private fun env(vararg names: String) = names - .firstNotNullOf { getenv(it) } - .toKString() - - override val files: Path = Path(env("HOME")) - override val caches: Path = Path(env("TMPDIR", "TMP")) -} diff --git a/kstore-file/src/linuxTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt b/kstore-file/src/linuxTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt deleted file mode 100644 index 817e40e..0000000 --- a/kstore-file/src/linuxTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt +++ /dev/null @@ -1,31 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlin.experimental.ExperimentalNativeApi - -private val FILE_REGEX = Regex(""" - /Users/.+ - """.trimIndent() -) - -private val CACHES_REGEX = Regex(""" - /var/folders/.+/T - """.trimIndent() -) - -@OptIn(ExperimentalKStoreApi::class) -class DirectoryProviders { - /** - * TODO: Fix this test, we are getting a runtime exception: - * kotlin.native.internal.FileFailedToInitializeException: There was an error during file or class initialization - */ - // @Test - @OptIn(ExperimentalNativeApi::class) - fun testDefaultDirectory() { - val provider: DirectoryProvider = DefaultDirectories - val files: String = provider.files.toString() - val caches: String = provider.caches.toString() - assert(files.matches(FILE_REGEX)) { "$files doesn't match expected" } - assert(caches.matches(CACHES_REGEX)) { "$caches doesn't match expected" } - } -} diff --git a/kstore-file/src/windowsMain/kotlin/io/github/xxfast/kstore/file/WindowsDirectoryProvider.kt b/kstore-file/src/windowsMain/kotlin/io/github/xxfast/kstore/file/WindowsDirectoryProvider.kt deleted file mode 100644 index bdf7829..0000000 --- a/kstore-file/src/windowsMain/kotlin/io/github/xxfast/kstore/file/WindowsDirectoryProvider.kt +++ /dev/null @@ -1,28 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlinx.cinterop.ExperimentalForeignApi -import kotlinx.cinterop.toKString -import kotlinx.io.files.Path -import platform.posix.getenv - -@ExperimentalKStoreApi -public actual val DefaultDirectories: DirectoryProvider get() = WindowsDirectoryProvider() - -@ExperimentalKStoreApi -private class WindowsDirectoryProvider : DirectoryProvider { - - @OptIn(ExperimentalForeignApi::class) - private fun env(vararg names: String) = names - .find { getenv(it) != null } - ?.let { name -> getenv(name) } - ?.toKString() - - override val files: Path = env("CSIDL_LOCAL_APPDATA") - ?.let { Path(it) } - ?: Path("C:\\Users") - - override val caches: Path = env("TMPDIR", "TMP") - ?.let { Path(it) } - ?: Path("C:\\AppData\\Local\\Temp") -} diff --git a/kstore-file/src/windowsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt b/kstore-file/src/windowsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt deleted file mode 100644 index 6e18023..0000000 --- a/kstore-file/src/windowsTest/kotlin/io/github/xxfast/kstore/file/DirectoryProviderTest.kt +++ /dev/null @@ -1,18 +0,0 @@ -package io.github.xxfast.kstore.file - -import io.github.xxfast.kstore.utils.ExperimentalKStoreApi -import kotlin.experimental.ExperimentalNativeApi -import kotlin.test.Test - -@OptIn(ExperimentalKStoreApi::class) -class DirectoryProviders { - @OptIn(ExperimentalNativeApi::class) - @Test - fun testDefaultDirectory() { - val provider: DirectoryProvider = DefaultDirectories - val files: String = provider.files.toString() - val caches: String = provider.caches.toString() - assert(files.isNotEmpty()) { "$files is empty" } - assert(caches.isNotEmpty()) { "$caches is empty" } - } -}