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 {

okio/src/jvmMain/kotlin/okio/JvmOkio.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,6 @@ fun Sink.hashingSink(digest: MessageDigest): HashingSink = HashingSink(this, dig
224224
*/
225225
fun Source.hashingSource(digest: MessageDigest): HashingSource = HashingSource(this, digest)
226226

227-
@Throws(IOException::class)
228-
fun FileSystem.openZip(zipPath: Path): FileSystem = okio.internal.openZip(zipPath, this)
229-
230227
fun ClassLoader.asResourceFileSystem(): FileSystem = ResourceFileSystem(this, indexEagerly = true)
231228

232229
/**

okio/src/nativeMain/kotlin/okio/internal/-ZlibNative.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal actual val DEFAULT_COMPRESSION: Int = platform.zlib.Z_DEFAULT_COMPRESSI
2020

2121
/**
2222
* Roll our own date math because Kotlin doesn't include a built-in date math API, and the
23-
* kotlinx.datetime library doesn't offer a stable at this time.
23+
* kotlinx.datetime library doesn't offer a stable release at this time.
2424
*
2525
* Also, we don't necessarily want to take on that dependency for Okio.
2626
*

okio/src/jvmMain/kotlin/okio/ZipFileSystem.kt renamed to okio/src/zlibMain/kotlin/okio/ZipFileSystem.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
*/
1717
package okio
1818

19-
import java.io.FileNotFoundException
20-
import java.util.zip.Inflater
2119
import okio.Path.Companion.toPath
2220
import okio.internal.COMPRESSION_METHOD_STORED
2321
import okio.internal.FixedLengthSource
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2014 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+
17+
@file:JvmMultifileClass
18+
@file:JvmName("Okio")
19+
20+
package okio
21+
22+
import kotlin.jvm.JvmMultifileClass
23+
import kotlin.jvm.JvmName
24+
25+
@Throws(IOException::class)
26+
fun FileSystem.openZip(zipPath: Path): FileSystem = okio.internal.openZip(zipPath, this)

okio/src/jvmMain/kotlin/okio/internal/ZipFiles.kt renamed to okio/src/zlibMain/kotlin/okio/internal/ZipFiles.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import okio.Path
2424
import okio.Path.Companion.toPath
2525
import okio.ZipFileSystem
2626
import okio.buffer
27+
import okio.use
2728

2829
private const val LOCAL_FILE_HEADER_SIGNATURE = 0x4034b50
2930
private const val CENTRAL_FILE_HEADER_SIGNATURE = 0x2014b50

okio/src/jvmTest/kotlin/okio/ZipFileSystemTest.kt renamed to okio/src/zlibTest/kotlin/okio/ZipFileSystemTest.kt

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,30 @@
1515
*/
1616
package okio
1717

18+
import assertk.assertThat
19+
import assertk.assertions.containsExactly
20+
import assertk.assertions.containsExactlyInAnyOrder
21+
import assertk.assertions.isEmpty
22+
import assertk.assertions.isEqualTo
23+
import assertk.assertions.isFalse
24+
import assertk.assertions.isGreaterThan
25+
import assertk.assertions.isLessThan
26+
import assertk.assertions.isNotNull
27+
import assertk.assertions.isNull
28+
import assertk.assertions.isTrue
29+
import kotlin.test.BeforeTest
30+
import kotlin.test.Test
1831
import kotlin.test.assertFailsWith
1932
import kotlinx.datetime.Instant
2033
import okio.ByteString.Companion.decodeHex
2134
import okio.ByteString.Companion.encodeUtf8
2235
import okio.Path.Companion.toPath
23-
import org.assertj.core.api.Assertions.assertThat
24-
import org.junit.Before
25-
import org.junit.Test
2636

2737
class ZipFileSystemTest {
28-
private val fileSystem = FileSystem.SYSTEM
29-
private var base = "../okio-testing-support/src/commonMain/resources/okio/zipfilesystem".toPath()
38+
private val fileSystem = SYSTEM_FILE_SYSTEM
39+
private var base = okioRoot / "okio-testing-support/src/commonMain/resources/okio/zipfilesystem"
3040

31-
@Before
41+
@BeforeTest
3242
fun setUp() {
3343
fileSystem.createDirectory(base)
3444
}
@@ -83,7 +93,7 @@ class ZipFileSystemTest {
8393
.isEqualTo("Another file!")
8494

8595
assertThat(zipFileSystem.list("/".toPath()))
86-
.hasSameElementsAs(listOf("/hello.txt".toPath(), "/directory".toPath()))
96+
.containsExactlyInAnyOrder("/hello.txt".toPath(), "/directory".toPath())
8797
assertThat(zipFileSystem.list("/directory".toPath()))
8898
.containsExactly("/directory/subdirectory".toPath())
8999
assertThat(zipFileSystem.list("/directory/subdirectory".toPath()))
@@ -112,7 +122,7 @@ class ZipFileSystemTest {
112122
fun zipWithDeflate() {
113123
val content = "Android\n".repeat(1000)
114124
val zipPath = base / "zipWithDeflate.zip"
115-
assertThat(fileSystem.metadata(zipPath).size).isLessThan(content.length.toLong())
125+
assertThat(fileSystem.metadata(zipPath).size).isNotNull().isLessThan(content.length.toLong())
116126
val zipFileSystem = fileSystem.openZip(zipPath)
117127

118128
assertThat(zipFileSystem.read("a.txt".toPath()) { readUtf8() })
@@ -138,7 +148,7 @@ class ZipFileSystemTest {
138148
fun zipWithStore() {
139149
val content = "Android\n".repeat(1000)
140150
val zipPath = base / "zipWithStore.zip"
141-
assertThat(fileSystem.metadata(zipPath).size).isGreaterThan(content.length.toLong())
151+
assertThat(fileSystem.metadata(zipPath).size).isNotNull().isGreaterThan(content.length.toLong())
142152
val zipFileSystem = fileSystem.openZip(zipPath)
143153

144154
assertThat(zipFileSystem.read("a.txt".toPath()) { readUtf8() })
@@ -524,8 +534,8 @@ class ZipFileSystemTest {
524534
* `META-INF/kotlin-gradle-statistics.kotlin_module`.
525535
*
526536
* We used to crash on duplicates, but they are common in practice so now we prefer the last
527-
* entry. This behavior is consistent with both [java.util.zip.ZipFile] and
528-
* [java.nio.file.FileSystem].
537+
* entry. This behavior is consistent with both `java.util.zip.ZipFile` and
538+
* `java.nio.file.FileSystem`.
529539
*
530540
* ```
531541
* echo "This is the first hello.txt" > hello.txt

0 commit comments

Comments
 (0)