From 01a58219362da59021e458eb0a9b53de94f43910 Mon Sep 17 00:00:00 2001 From: emilb Date: Tue, 31 Dec 2024 12:09:43 +0100 Subject: [PATCH] Fix `runToFile` not creating a file --- core/src/main/scala/ox/flow/FlowIOOps.scala | 4 ++-- .../test/scala/ox/flow/FlowIOOpsTest.scala | 19 ++++++++++++++++--- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/ox/flow/FlowIOOps.scala b/core/src/main/scala/ox/flow/FlowIOOps.scala index 6c0a5b13..a3ab119f 100644 --- a/core/src/main/scala/ox/flow/FlowIOOps.scala +++ b/core/src/main/scala/ox/flow/FlowIOOps.scala @@ -65,11 +65,11 @@ trait FlowIOOps[+T]: def runToFile(path: Path)(using T <:< Chunk[Byte]): Unit = if Files.isDirectory(path) then throw new IOException(s"Path $path is a directory") val jFileChannel = - try FileChannel.open(path, StandardOpenOption.WRITE) + try FileChannel.open(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE) catch case _: UnsupportedOperationException => // Some file systems don't support file channels - Files.newByteChannel(path, StandardOpenOption.WRITE) + Files.newByteChannel(path, StandardOpenOption.WRITE, StandardOpenOption.CREATE) try runForeach(chunk => jFileChannel.write(ByteBuffer.wrap(chunk.toArray)).discard) diff --git a/core/src/test/scala/ox/flow/FlowIOOpsTest.scala b/core/src/test/scala/ox/flow/FlowIOOpsTest.scala index 26ea4004..30b8dc4c 100644 --- a/core/src/test/scala/ox/flow/FlowIOOpsTest.scala +++ b/core/src/test/scala/ox/flow/FlowIOOpsTest.scala @@ -95,7 +95,7 @@ class FlowIOOpsTest extends AnyWordSpec with Matchers: "toFile" should: - "create a file and write a single chunk with bytes" in supervised: + "open existing file and write a single chunk with bytes" in supervised: val path = useInScope(Files.createTempFile("ox", "test-writefile1"))(Files.deleteIfExists(_).discard) val sourceContent = "source.toFile test1 content" val source = Flow.fromValues(Chunk.fromArray(sourceContent.getBytes)) @@ -103,7 +103,7 @@ class FlowIOOpsTest extends AnyWordSpec with Matchers: fileContent(path) shouldBe List(sourceContent) - "create a file and write multiple chunks with bytes" in supervised: + "open existing file and write multiple chunks with bytes" in supervised: val path = useInScope(Files.createTempFile("ox", "test-writefile2"))(Files.deleteIfExists(_).discard) val sourceContent = "source.toFile test2 content" val source = Flow.fromIterable(sourceContent.grouped(4).toList.map(chunkStr => Chunk.fromArray(chunkStr.getBytes))) @@ -111,6 +111,19 @@ class FlowIOOpsTest extends AnyWordSpec with Matchers: fileContent(path) shouldBe List(sourceContent) + "create file and write multiple chunks with bytes" in supervised: + var filePath = null: Path + val folderPath = useInScope(Files.createTempDirectory("ox")) { path => + Files.deleteIfExists(filePath) + Files.deleteIfExists(path).discard + } + val sourceContent = "source.toFile test3 content" + filePath = folderPath.resolve("test-writefile3") + Flow.fromIterable(sourceContent.grouped(4).toList.map(chunkStr => Chunk.fromArray(chunkStr.getBytes))) + .runToFile(filePath) + + fileContent(filePath) shouldBe List(sourceContent) + "use an existing file and overwrite it a single chunk with bytes" in supervised: val path = useInScope(Files.createTempFile("ox", "test-writefile3"))(Files.deleteIfExists(_).discard) Files.write(path, "Some initial content".getBytes) @@ -142,7 +155,7 @@ class FlowIOOpsTest extends AnyWordSpec with Matchers: exception.getMessage should endWith("is a directory") "throw an exception if file cannot be opened" in: - val path = Paths.get("/").resolve("/not-existing-directory/not-existing-file.txt") + val path = Paths.get("/").resolve("/directory-without-permissions/file-without-access.txt") val source = Flow.fromValues(Chunk.empty[Byte]) assertThrows[NoSuchFileException](source.runToFile(path))