Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "FilePathsBase"
uuid = "48062228-2e41-5def-b9a4-89aafe57970f"
authors = ["Rory Finnegan"]
version = "0.9.12"
version = "0.9.13"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
14 changes: 5 additions & 9 deletions src/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,19 @@ end

Base.isreadable(buffer::FileBuffer) = buffer.read
Base.iswritable(buffer::FileBuffer) = buffer.write
Base.seek(buffer::FileBuffer, n::Integer) = seek(buffer.io, n)
Base.seek(buffer::FileBuffer, n::Integer) = (_read(buffer); seek(buffer.io, n))
Base.skip(buffer::FileBuffer, n::Integer) = (_read(buffer); skip(buffer.io, n))
Base.seekstart(buffer::FileBuffer) = seekstart(buffer.io)
Base.seekend(buffer::FileBuffer) = seekend(buffer.io)
Base.seekend(buffer::FileBuffer) = (_read(buffer); seekend(buffer.io))
Base.position(buffer::FileBuffer) = position(buffer.io)
function Base.eof(buffer::FileBuffer)
if position(buffer) == 0
_read(buffer)
seekstart(buffer)
end
return eof(buffer.io)
end
Base.eof(buffer::FileBuffer) = (_read(buffer); eof(buffer.io))

function _read(buffer::FileBuffer)
# If our IOBuffer is empty then populate it with the
# filepath contents
if buffer.io.size == 0
write(buffer.io, read(buffer.path))
seekstart(buffer.io)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could definitely clean up Base.read(buffer::FileBuffer, ::Type{UInt8}) but we should probably leave the other _read(...); seekstart(...) calls alone as they would rewind the stream on each call after the FileBuffer was populated

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think when I initially wrote this the automatic rewinding seemed helpful for my use case. In retrospect, it seems like it has caused more problems with the IO API. I'm a little surprised this didn't break any of the existing tests though.

end
end

Expand Down
34 changes: 34 additions & 0 deletions test/buffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,40 @@ using FilePathsBase: FileBuffer
end
end

@testset "seek" begin
p = p"../README.md"
io = FileBuffer(p)
try
@test position(io) == 0
@test position(seekend(io)) > 0
@test position(seekstart(io)) == 0
@test position(seek(io, 5)) == 5
@test position(skip(io, 10)) == 15
finally
close(io)
end
end

@testset "populate" begin
funcs = (
:seek => io -> seek(io, 1),
:skip => io -> skip(io, 1),
:seekend => seekend,
:eof => eof,
)

@testset "$name" for (name, f) in funcs
p = p"../README.md"
buffer = FileBuffer(p)
try
f(buffer)
@test buffer.io.size > 0
finally
close(buffer)
end
end
end

@testset "write" begin
mktmpdir() do d
p1 = absolute(p"../README.md")
Expand Down