Skip to content

Commit a5b24e7

Browse files
committed
Fix yield_chunks method and improve error handling
1 parent 1d9a16e commit a5b24e7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/remote_input/downloader.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,12 @@ def download(output_path, &block)
213213
end
214214
end
215215

216-
private def yield_chunks(path)
217-
path.open("rb") do |output|
216+
private def yield_chunks(path, &block)
217+
return unless block_given?
218+
219+
path.open("rb") do |input|
218220
chunk_size = 1024 * 1024
219-
chunk = +""
220-
while output.read(chunk_size, chunk)
221+
while chunk = input.read(chunk_size)
221222
yield(chunk)
222223
end
223224
end

test/test-downloader.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ def teardown
7777
downloader.download(output_path)
7878
assert_equal("cached content", output_path.read)
7979
end
80+
81+
test("yield chunks when using cache") do
82+
output_path = @tmp_dir + "cached_file"
83+
content = "chunk1chunk2chunk3"
84+
output_path.write(content)
85+
86+
downloader = RemoteInput::Downloader.new("https://example.com/file")
87+
88+
chunks = []
89+
downloader.download(output_path) do |chunk|
90+
chunks << chunk
91+
end
92+
93+
assert_equal(content, chunks.join)
94+
end
8095
end
8196

8297
sub_test_case("fallback URLs") do

0 commit comments

Comments
 (0)