Skip to content

Commit

Permalink
Fix a bug where FakeFilesystem.appendingSink cleared the file
Browse files Browse the repository at this point in the history
It was restored on flush, but it should have been readable before that.
  • Loading branch information
squarejesse committed Dec 28, 2020
1 parent 60c016f commit 60afd39
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions okio-testing/src/commonMain/kotlin/okio/FakeFilesystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ class FakeFilesystem(
val regularFile = File(createdAt = existing?.createdAt ?: now)
val result = FakeFileSink(canonicalPath, regularFile)
if (append && existing is File) {
val existingBytes = Buffer().write(existing.data)
result.write(existingBytes, existingBytes.size)
result.buffer.write(existing.data)
regularFile.data = existing.data
}
elements[canonicalPath] = regularFile
regularFile.access(now = now, modified = true)
Expand Down Expand Up @@ -284,7 +284,7 @@ class FakeFilesystem(
private val path: Path,
private val file: File
) : Sink {
private var buffer = Buffer()
val buffer = Buffer()
private var closed = false

override fun write(source: Buffer, byteCount: Long) {
Expand Down
12 changes: 11 additions & 1 deletion okio/src/commonTest/kotlin/okio/AbstractFilesystemTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class AbstractFilesystemTest(
val windowsLimitations: Boolean,
temporaryDirectory: Path
) {
val base: Path = temporaryDirectory / "FileSystemTest-${randomToken()}"
val base: Path = temporaryDirectory / "${this::class.simpleName}-${randomToken()}"
private val isJs = filesystem::class.simpleName?.startsWith("NodeJs") ?: false

@BeforeTest
Expand Down Expand Up @@ -127,6 +127,16 @@ abstract class AbstractFilesystemTest(
assertEquals("hello, world!\nthis is added later!", path.readUtf8())
}

@Test
fun appendingSinkDoesNotImpactExistingFile() {
val path = base / "appending-sink-does-not-impact-existing-file"
path.writeUtf8("hello, world!\n")
val sink = filesystem.appendingSink(path)
assertEquals("hello, world!\n", path.readUtf8())
sink.close()
assertEquals("hello, world!\n", path.readUtf8())
}

@Test
fun appendingSinkCreatesNewFile() {
val path = base / "appending-sink-creates-new-file"
Expand Down

0 comments on commit 60afd39

Please sign in to comment.