Skip to content

Commit

Permalink
platform specefic test
Browse files Browse the repository at this point in the history
  • Loading branch information
psuzn committed May 11, 2024
1 parent 8d9cde8 commit d77a1d6
Show file tree
Hide file tree
Showing 7 changed files with 277 additions and 0 deletions.
17 changes: 17 additions & 0 deletions multiplatform-paths/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ kotlin {
}
}

val androidUnitTest by getting {
dependencies {
implementation(libs.junit)
implementation(libs.ext.junit)
implementation(libs.espresso.core)
implementation(libs.robolectric)
}
}

val darwinMain by creating {
dependsOn(commonMain)
}
Expand Down Expand Up @@ -90,6 +99,14 @@ kotlin {
}
}

android {
testOptions {
unitTests {
isIncludeAndroidResources = true
}
}
}

@Suppress("ktlint:standard:max-line-length")
mavenPublishing {
pom {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2024 Sujan Poudel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.sujanpoudel.utils.paths

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.toPath
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import kotlin.test.assertEquals

@RunWith(RobolectricTestRunner::class)
class AndroidDirectoriesTest {
private val appId: String = "me.sujanpoudel.utils.paths.test"

private fun exceptedCacheDir(appId: String): Path {
val applicationContext = ApplicationProvider.getApplicationContext<Context>()
return applicationContext.cacheDir.absolutePath.toPath()
}

private fun exceptedDataDir(appId: String): Path {
val applicationContext = ApplicationProvider.getApplicationContext<Context>()
return applicationContext.dataDir.absolutePath.toPath()
}

@Test
fun testCacheDirectory() {
assertEquals(exceptedCacheDir(appId), cacheDirectory(appId))
}

@Test
fun testDataDirectory() {
assertEquals(exceptedDataDir(appId), dataDirectory(appId))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 Sujan Poudel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import kotlin.test.Test
import kotlin.test.assertEquals

abstract class DirectoriesTest {
private val appId: String = "me.sujanpoudel.utils.paths.test"

abstract fun exceptedCacheDir(appId: String): Path

abstract fun exceptedDataDir(appId: String): Path

@Test
fun testCacheDirectory() {
assertEquals(exceptedCacheDir(appId), cacheDirectory(appId))
}

@Test
fun testDataDirectory() {
assertEquals(exceptedDataDir(appId), dataDirectory(appId))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 Sujan Poudel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import me.sujanpoudel.utils.platformIdentifier.Platform
import me.sujanpoudel.utils.platformIdentifier.hostOs

class DesktopDirectoriesTest : DirectoriesTest() {
private fun getEnv(key: String) = System.getenv(key) as String

override fun exceptedCacheDir(appId: String): Path {
return when (hostOs) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".cache" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Caches" / appId
is Platform.OS.Windows -> getEnv("AppData").toPath() / "Caches" / appId
else -> error("not supported")
}
}

override fun exceptedDataDir(appId: String): Path {
return when (hostOs) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".local/share" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Application Support" / appId
is Platform.OS.Windows -> getEnv("AppData").toPath() / appId
else -> error("not supported")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2024 Sujan Poudel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import platform.Foundation.NSApplicationSupportDirectory
import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSSearchPathForDirectoriesInDomains
import platform.Foundation.NSUserDomainMask

class IosDirectoriesTest : DirectoriesTest() {
override fun exceptedCacheDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath() ?: error("Unable to get 'NSCachesDirectory'")
}

override fun exceptedDataDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath()
?.let { it / appId } ?: error("Unable to get 'NSApplicationSupportDirectory'")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 2024 Sujan Poudel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import me.sujanpoudel.utils.platformIdentifier.Platform
import me.sujanpoudel.utils.platformIdentifier.platform

class NodeDirectoriesTest : DirectoriesTest() {
private fun getEnv(key: String) = eval("""process.env["$key"]""") as String

override fun exceptedCacheDir(appId: String): Path {
val platform = platform() as? Platform.JS.Node ?: error("not supported")

return when (platform.os) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".cache" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Caches" / appId
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / "Caches" / appId

else -> error("not supported")
}
}

override fun exceptedDataDir(appId: String): Path {
val platform = platform() as? Platform.JS.Node ?: error("not supported")

return when (platform.os) {
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".local/share" / appId
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Application Support" / appId
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / appId
else -> error("not supported")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2024 Sujan Poudel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package me.sujanpoudel.utils.paths

import kotlinx.io.files.Path
import me.sujanpoudel.utils.paths.utils.div
import me.sujanpoudel.utils.paths.utils.toPath
import platform.Foundation.NSApplicationSupportDirectory
import platform.Foundation.NSCachesDirectory
import platform.Foundation.NSSearchPathForDirectoriesInDomains
import platform.Foundation.NSUserDomainMask

class MacosDirectoriesTest : DirectoriesTest() {
override fun exceptedCacheDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath()?.let { it / appId } ?: error("Unable to get 'NSCachesDirectory'")
}

override fun exceptedDataDir(appId: String): Path {
return NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
.firstOrNull()?.toString()?.toPath()?.let { it / appId } ?: error("Unable to get 'NSApplicationSupportDirectory'")
}
}

0 comments on commit d77a1d6

Please sign in to comment.