Skip to content

Commit c0e86c5

Browse files
committed
platform specefic test
1 parent 8d9cde8 commit c0e86c5

File tree

7 files changed

+277
-0
lines changed

7 files changed

+277
-0
lines changed

multiplatform-paths/build.gradle.kts

+17
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ kotlin {
5252
}
5353
}
5454

55+
val androidUnitTest by getting {
56+
dependencies {
57+
implementation(libs.junit)
58+
implementation(libs.ext.junit)
59+
implementation(libs.espresso.core)
60+
implementation(libs.robolectric)
61+
}
62+
}
63+
5564
val darwinMain by creating {
5665
dependsOn(commonMain)
5766
}
@@ -90,6 +99,14 @@ kotlin {
9099
}
91100
}
92101

102+
android {
103+
testOptions {
104+
unitTests {
105+
isIncludeAndroidResources = true
106+
}
107+
}
108+
}
109+
93110
@Suppress("ktlint:standard:max-line-length")
94111
mavenPublishing {
95112
pom {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2024 Sujan Poudel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package me.sujanpoudel.utils.paths
18+
19+
import android.content.Context
20+
import androidx.test.core.app.ApplicationProvider
21+
import kotlinx.io.files.Path
22+
import me.sujanpoudel.utils.paths.utils.toPath
23+
import org.junit.Test
24+
import org.junit.runner.RunWith
25+
import org.robolectric.RobolectricTestRunner
26+
import kotlin.test.assertEquals
27+
28+
@RunWith(RobolectricTestRunner::class)
29+
class AndroidDirectoriesTest {
30+
private val appId: String = "me.sujanpoudel.utils.paths.test"
31+
32+
private fun exceptedCacheDir(appId: String): Path {
33+
val applicationContext = ApplicationProvider.getApplicationContext<Context>()
34+
return applicationContext.cacheDir.absolutePath.toPath()
35+
}
36+
37+
private fun exceptedDataDir(appId: String): Path {
38+
val applicationContext = ApplicationProvider.getApplicationContext<Context>()
39+
return applicationContext.dataDir.absolutePath.toPath()
40+
}
41+
42+
@Test
43+
fun testCacheDirectory() {
44+
assertEquals(exceptedCacheDir(appId), cacheDirectory(appId))
45+
}
46+
47+
@Test
48+
fun testDataDirectory() {
49+
assertEquals(exceptedDataDir(appId), dataDirectory(appId))
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2024 Sujan Poudel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package me.sujanpoudel.utils.paths
18+
19+
import kotlinx.io.files.Path
20+
import kotlin.test.Test
21+
import kotlin.test.assertEquals
22+
23+
abstract class DirectoriesTest {
24+
private val appId: String = "me.sujanpoudel.utils.paths.test"
25+
26+
abstract fun exceptedCacheDir(appId: String): Path
27+
28+
abstract fun exceptedDataDir(appId: String): Path
29+
30+
@Test
31+
fun testCacheDirectory() {
32+
assertEquals(exceptedCacheDir(appId), cacheDirectory(appId))
33+
}
34+
35+
@Test
36+
fun testDataDirectory() {
37+
assertEquals(exceptedDataDir(appId), dataDirectory(appId))
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024 Sujan Poudel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package me.sujanpoudel.utils.paths
18+
19+
import kotlinx.io.files.Path
20+
import me.sujanpoudel.utils.paths.utils.div
21+
import me.sujanpoudel.utils.paths.utils.toPath
22+
import me.sujanpoudel.utils.platformIdentifier.Platform
23+
import me.sujanpoudel.utils.platformIdentifier.hostOs
24+
25+
class DesktopDirectoriesTest : DirectoriesTest() {
26+
private fun getEnv(key: String) = System.getenv(key) as String
27+
28+
override fun exceptedCacheDir(appId: String): Path {
29+
return when (hostOs) {
30+
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".cache" / appId
31+
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Caches" / appId
32+
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / "Caches" / appId
33+
else -> error("not supported")
34+
}
35+
}
36+
37+
override fun exceptedDataDir(appId: String): Path {
38+
return when (hostOs) {
39+
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".local/share" / appId
40+
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Application Support" / appId
41+
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / appId
42+
else -> error("not supported")
43+
}
44+
}
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 Sujan Poudel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package me.sujanpoudel.utils.paths
18+
19+
import kotlinx.io.files.Path
20+
import me.sujanpoudel.utils.paths.utils.div
21+
import me.sujanpoudel.utils.paths.utils.toPath
22+
import platform.Foundation.NSApplicationSupportDirectory
23+
import platform.Foundation.NSCachesDirectory
24+
import platform.Foundation.NSSearchPathForDirectoriesInDomains
25+
import platform.Foundation.NSUserDomainMask
26+
27+
class IosDirectoriesTest : DirectoriesTest() {
28+
override fun exceptedCacheDir(appId: String): Path {
29+
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
30+
.firstOrNull()?.toString()?.toPath() ?: error("Unable to get 'NSCachesDirectory'")
31+
}
32+
33+
override fun exceptedDataDir(appId: String): Path {
34+
return NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
35+
.firstOrNull()?.toString()?.toPath()
36+
?.let { it / appId } ?: error("Unable to get 'NSApplicationSupportDirectory'")
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2024 Sujan Poudel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package me.sujanpoudel.utils.paths
18+
19+
import kotlinx.io.files.Path
20+
import me.sujanpoudel.utils.paths.utils.div
21+
import me.sujanpoudel.utils.paths.utils.toPath
22+
import me.sujanpoudel.utils.platformIdentifier.Platform
23+
import me.sujanpoudel.utils.platformIdentifier.platform
24+
25+
class NodeDirectoriesTest : DirectoriesTest() {
26+
private fun getEnv(key: String) = eval("""process.env["$key"]""") as String
27+
28+
override fun exceptedCacheDir(appId: String): Path {
29+
val platform = platform() as? Platform.JS.Node ?: error("not supported")
30+
31+
return when (platform.os) {
32+
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".cache" / appId
33+
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Caches" / appId
34+
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / "Caches" / appId
35+
36+
else -> error("not supported")
37+
}
38+
}
39+
40+
override fun exceptedDataDir(appId: String): Path {
41+
val platform = platform() as? Platform.JS.Node ?: error("not supported")
42+
43+
return when (platform.os) {
44+
is Platform.OS.Linux -> getEnv("HOME").toPath() / ".local/share" / appId
45+
is Platform.OS.MacOs -> getEnv("HOME").toPath() / "Library/Application Support" / appId
46+
is Platform.OS.Windows -> getEnv("APPDATA").toPath() / appId
47+
else -> error("not supported")
48+
}
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2024 Sujan Poudel
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package me.sujanpoudel.utils.paths
18+
19+
import kotlinx.io.files.Path
20+
import me.sujanpoudel.utils.paths.utils.div
21+
import me.sujanpoudel.utils.paths.utils.toPath
22+
import platform.Foundation.NSApplicationSupportDirectory
23+
import platform.Foundation.NSCachesDirectory
24+
import platform.Foundation.NSSearchPathForDirectoriesInDomains
25+
import platform.Foundation.NSUserDomainMask
26+
27+
class MacosDirectoriesTest : DirectoriesTest() {
28+
override fun exceptedCacheDir(appId: String): Path {
29+
return NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, true)
30+
.firstOrNull()?.toString()?.toPath()?.let { it / appId } ?: error("Unable to get 'NSCachesDirectory'")
31+
}
32+
33+
override fun exceptedDataDir(appId: String): Path {
34+
return NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, true)
35+
.firstOrNull()?.toString()?.toPath()?.let { it / appId } ?: error("Unable to get 'NSApplicationSupportDirectory'")
36+
}
37+
}

0 commit comments

Comments
 (0)