Skip to content

Commit

Permalink
Use named parameters for File#get_output_stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
hainesr committed Jun 14, 2021
1 parent 5baa70d commit c6da94e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
10 changes: 5 additions & 5 deletions lib/zip/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ def get_input_stream(entry, &a_proc)
# specified. If a block is passed the stream object is passed to the block and
# the stream is automatically closed afterwards just as with ruby's builtin
# File.open method.
def get_output_stream(entry, permission_int = nil, comment = nil,
extra = nil, compressed_size = nil, crc = nil,
compression_method = nil, compression_level = nil,
size = nil, time = nil, &a_proc)
def get_output_stream(entry, permissions: nil, comment: nil,
extra: nil, compressed_size: nil, crc: nil,
compression_method: nil, compression_level: nil,
size: nil, time: nil, &a_proc)

new_entry =
if entry.kind_of?(Entry)
Expand All @@ -204,7 +204,7 @@ def get_output_stream(entry, permission_int = nil, comment = nil,
raise ArgumentError,
"cannot open stream to directory entry - '#{new_entry}'"
end
new_entry.unix_perms = permission_int
new_entry.unix_perms = permissions
zip_streamable_entry = StreamableStream.new(new_entry)
@entry_set << zip_streamable_entry
zip_streamable_entry.get_output_stream(&a_proc)
Expand Down
2 changes: 1 addition & 1 deletion lib/zip/filesystem/zip_file_name_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def get_input_stream(filename, &a_proc)

def get_output_stream(filename, permissions = nil, &a_proc)
@zip_file.get_output_stream(
expand_to_entry(filename), permissions, &a_proc
expand_to_entry(filename), permissions: permissions, &a_proc
)
end

Expand Down
32 changes: 20 additions & 12 deletions test/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,30 @@ def test_get_output_stream
assert_equal(count + 1, zf.size)
assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))

custom_entry_args = [
TEST_COMMENT, TEST_EXTRA, TEST_COMPRESSED_SIZE, TEST_CRC,
::Zip::Entry::STORED, ::Zlib::BEST_SPEED, TEST_SIZE, TEST_TIME
]
zf.get_output_stream('entry_with_custom_args.txt', nil, *custom_entry_args) do |os|
custom_entry_args = {
comment: TEST_COMMENT, compressed_size: TEST_COMPRESSED_SIZE,
crc: TEST_CRC, compression_method: ::Zip::COMPRESSION_METHOD_STORE,
compression_level: ::Zlib::BEST_SPEED, size: TEST_SIZE, time: TEST_TIME
}
zf.get_output_stream(
'entry_with_custom_args.txt', **custom_entry_args
) do |os|
os.write 'Some data'
end

assert_equal(count + 2, zf.size)
entry = zf.get_entry('entry_with_custom_args.txt')
assert_equal(custom_entry_args[0], entry.comment)
assert_equal(custom_entry_args[2], entry.compressed_size)
assert_equal(custom_entry_args[3], entry.crc)
assert_equal(custom_entry_args[4], entry.compression_method)
assert_equal(custom_entry_args[5], entry.compression_level)
assert_equal(custom_entry_args[6], entry.size)
assert_equal(custom_entry_args[7], entry.time)
assert_equal(custom_entry_args[:comment], entry.comment)
assert_equal(custom_entry_args[:compressed_size], entry.compressed_size)
assert_equal(custom_entry_args[:crc], entry.crc)
assert_equal(
custom_entry_args[:compression_method], entry.compression_method
)
assert_equal(
custom_entry_args[:compression_level], entry.compression_level
)
assert_equal(custom_entry_args[:size], entry.size)
assert_equal(custom_entry_args[:time], entry.time)

zf.get_output_stream('entry.bin') do |os|
os.write(::File.open('test/data/generated/5entry.zip', 'rb').read)
Expand Down
2 changes: 1 addition & 1 deletion test/zip64_full_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_large_zip_file
# Write just over 4GB (stored, so the zip file exceeds 4GB).
buf = 'blah' * 16_384
zf.get_output_stream(
'huge_file', nil, nil, nil, nil, nil, ::Zip::Entry::STORED
'huge_file', compression_method: ::Zip::COMPRESSION_METHOD_STORE
) do |io|
65_537.times { io.write(buf) }
end
Expand Down

0 comments on commit c6da94e

Please sign in to comment.