-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to choose non-default preopens (#1317)
I need to find a proper document explaining how this all works. As is it works but is not following a proper spec.
- Loading branch information
1 parent
0aac566
commit c7e7ad3
Showing
6 changed files
with
171 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
okio-wasifilesystem/src/wasmTest/kotlin/okio/WasiFileSystemPreopensTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
/* | ||
* Copyright (C) 2023 Square, Inc. | ||
* | ||
* 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 okio | ||
|
||
import kotlin.test.BeforeTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertNull | ||
import okio.Path.Companion.toPath | ||
import okio.WasiFileSystem.Companion.DEFAULT_FIRST_PREOPEN | ||
|
||
/** | ||
* Confirm the [WasiFileSystem] can operate on different preopened directories independently. | ||
* | ||
* This tracks the `preopens` attribute in `.mjs` script in `okio-wasifilesystem/build.gradle.kts`. | ||
*/ | ||
class WasiFileSystemPreopensTest { | ||
private val fileSystem = WasiFileSystem( | ||
relativePathPreopen = DEFAULT_FIRST_PREOPEN, | ||
pathToPreopen = mapOf( | ||
"/tmp".toPath() to DEFAULT_FIRST_PREOPEN, | ||
"/a".toPath() to DEFAULT_FIRST_PREOPEN + 1, | ||
"/b".toPath() to DEFAULT_FIRST_PREOPEN + 2, | ||
), | ||
) | ||
|
||
private val testId = "${this::class.simpleName}-${randomToken(16)}" | ||
private val baseA: Path = "/a".toPath() / testId | ||
private val baseB: Path = "/b".toPath() / testId | ||
|
||
@BeforeTest | ||
fun setUp() { | ||
fileSystem.createDirectory(baseA) | ||
fileSystem.createDirectory(baseB) | ||
} | ||
|
||
@Test | ||
fun operateOnPreopens() { | ||
fileSystem.write(baseA / "a.txt") { | ||
writeUtf8("hello world a") | ||
} | ||
fileSystem.write(baseB / "b.txt") { | ||
writeUtf8("bello burld") | ||
} | ||
assertEquals( | ||
"hello world a".length.toLong(), | ||
fileSystem.metadata(baseA / "a.txt").size, | ||
) | ||
assertEquals( | ||
"bello burld".length.toLong(), | ||
fileSystem.metadata(baseB / "b.txt").size, | ||
) | ||
} | ||
|
||
@Test | ||
fun operateAcrossPreopens() { | ||
fileSystem.write(baseA / "a.txt") { | ||
writeUtf8("hello world") | ||
} | ||
|
||
fileSystem.atomicMove(baseA / "a.txt", baseB / "b.txt") | ||
|
||
assertEquals( | ||
"hello world", | ||
fileSystem.read(baseB / "b.txt") { | ||
readUtf8() | ||
}, | ||
) | ||
} | ||
|
||
@Test | ||
fun cannotOperateOutsideOfPreopens() { | ||
val noPreopen = "/c".toPath() / testId | ||
assertFailsWith<FileNotFoundException> { | ||
fileSystem.createDirectory(noPreopen) | ||
} | ||
assertFailsWith<FileNotFoundException> { | ||
fileSystem.sink(noPreopen) | ||
} | ||
assertNull(fileSystem.metadataOrNull(noPreopen)) | ||
assertFailsWith<FileNotFoundException> { | ||
fileSystem.metadata(noPreopen) | ||
} | ||
assertNull(fileSystem.listOrNull(noPreopen)) | ||
assertFailsWith<FileNotFoundException> { | ||
fileSystem.list(noPreopen) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters