Skip to content

Commit

Permalink
Fix runToFile not creating a file (#262)
Browse files Browse the repository at this point in the history
  • Loading branch information
emil-bar authored Dec 31, 2024
1 parent 3e9bb64 commit 7c00633
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions core/src/main/scala/ox/flow/FlowIOOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 16 additions & 3 deletions core/src/test/scala/ox/flow/FlowIOOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,22 +95,35 @@ 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))
source.runToFile(path)

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)))
source.runToFile(path)

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)
Expand Down Expand Up @@ -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))

Expand Down

0 comments on commit 7c00633

Please sign in to comment.