Skip to content

Commit 20b83aa

Browse files
authored
Get openZip working on KotlinNative (#1439)
* Get openZip working on KotlinNative * No assertk for wasmJs or wasmWasi * Make okioRoot lazy
1 parent 3bcb813 commit 20b83aa

File tree

18 files changed

+123
-17
lines changed

18 files changed

+123
-17
lines changed

build.gradle.kts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import org.jetbrains.dokka.gradle.DokkaTask
1515
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootExtension
1616
import org.jetbrains.kotlin.gradle.targets.js.nodejs.NodeJsRootPlugin
1717
import org.jetbrains.kotlin.gradle.targets.js.npm.tasks.KotlinNpmInstallTask
18+
import org.jetbrains.kotlin.gradle.targets.js.testing.KotlinJsTest
19+
import org.jetbrains.kotlin.gradle.targets.jvm.tasks.KotlinJvmTest
20+
import org.jetbrains.kotlin.gradle.targets.native.tasks.KotlinNativeTest
1821
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
1922

2023
plugins {
@@ -246,3 +249,22 @@ plugins.withType<NodeJsRootPlugin> {
246249
args += "--ignore-engines"
247250
}
248251
}
252+
253+
/**
254+
* Set the `OKIO_ROOT` environment variable for tests to access it.
255+
* https://publicobject.com/2023/04/16/read-a-project-file-in-a-kotlin-multiplatform-test/
256+
*/
257+
allprojects {
258+
tasks.withType<KotlinJvmTest>().configureEach {
259+
environment("OKIO_ROOT", rootDir)
260+
}
261+
262+
tasks.withType<KotlinNativeTest>().configureEach {
263+
environment("SIMCTL_CHILD_OKIO_ROOT", rootDir)
264+
environment("OKIO_ROOT", rootDir)
265+
}
266+
267+
tasks.withType<KotlinJsTest>().configureEach {
268+
environment("OKIO_ROOT", rootDir.toString())
269+
}
270+
}

okio-testing-support/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ kotlin {
2828
}
2929
}
3030

31+
val zlibMain by creating {
32+
dependsOn(commonMain)
33+
}
34+
3135
if (kmpJsEnabled) {
3236
val jsMain by getting {
3337
dependsOn(nonWasmMain)
@@ -36,6 +40,7 @@ kotlin {
3640

3741
val jvmMain by getting {
3842
dependsOn(nonWasmMain)
43+
dependsOn(zlibMain)
3944
dependencies {
4045
// On the JVM the kotlin-test library resolves to one of three implementations based on
4146
// which testing framework is in use. JUnit is used downstream, but Gradle can't know that
@@ -48,6 +53,7 @@ kotlin {
4853
createSourceSet("nativeMain", children = nativeTargets)
4954
.also { nativeMain ->
5055
nativeMain.dependsOn(nonWasmMain)
56+
nativeMain.dependsOn(zlibMain)
5157
}
5258
}
5359

okio-testing-support/src/commonMain/kotlin/okio/TestingCommon.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import kotlin.random.Random
1919
import kotlin.test.assertEquals
2020
import kotlin.time.Duration
2121
import okio.ByteString.Companion.toByteString
22+
import okio.Path.Companion.toPath
2223

2324
fun Char.repeat(count: Int): String {
2425
return toString().repeat(count)
@@ -90,3 +91,9 @@ expect val FileSystem.allowSymlinks: Boolean
9091
expect val FileSystem.allowReadsWhileWriting: Boolean
9192

9293
expect var FileSystem.workingDirectory: Path
94+
95+
expect fun getEnv(name: String): String?
96+
97+
val okioRoot: Path by lazy {
98+
getEnv("OKIO_ROOT")!!.toPath()
99+
}
Binary file not shown.

okio-testing-support/src/jsMain/kotlin/okio/TestingJs.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ actual fun isBrowser(): Boolean {
2020
}
2121

2222
actual fun isWasm() = false
23+
24+
actual fun getEnv(name: String): String? =
25+
js("globalThis.process.env[name]") as String?

okio-testing-support/src/jvmMain/kotlin/okio/TestingJvm.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
package okio
1717

18+
actual val SYSTEM_FILE_SYSTEM = FileSystem.SYSTEM
19+
1820
actual fun isBrowser() = false
1921

2022
actual fun isWasm() = false
23+
24+
actual fun getEnv(name: String): String? = System.getenv(name)

okio-testing-support/src/nativeMain/kotlin/okio/TestingNative.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
*/
1616
package okio
1717

18+
import kotlinx.cinterop.ExperimentalForeignApi
19+
import kotlinx.cinterop.toKString
20+
import platform.posix.getenv
21+
22+
actual val SYSTEM_FILE_SYSTEM = FileSystem.SYSTEM
23+
1824
actual fun isBrowser() = false
1925

2026
actual fun isWasm() = false
27+
28+
@OptIn(ExperimentalForeignApi::class)
29+
actual fun getEnv(name: String): String? = getenv(name)?.toKString()

okio-testing-support/src/wasmMain/kotlin/okio/TestingWasm.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ actual val FileSystem.allowReadsWhileWriting: Boolean
5959
actual var FileSystem.workingDirectory: Path
6060
get() = error("unexpected call")
6161
set(_) = error("unexpected call")
62+
63+
actual fun getEnv(name: String): String? = error("unexpected call")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (C) 2024 Square, Inc.
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+
package okio
17+
18+
expect val SYSTEM_FILE_SYSTEM: FileSystem

okio/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ kotlin {
9797

9898
val zlibTest by creating {
9999
dependsOn(commonTest)
100+
dependencies {
101+
implementation(libs.test.assertk)
102+
}
100103
}
101104

102105
val jvmMain by getting {

0 commit comments

Comments
 (0)