Ensure `File.open_buffer` doesn't rewrite unchanged data.

This commit is contained in:
Robert Haines 2021-11-30 22:22:37 +00:00
parent f5e19db273
commit 14b63f68db
3 changed files with 28 additions and 3 deletions

View File

@ -30,7 +30,7 @@ Metrics/CyclomaticComplexity:
# Offense count: 47
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
Metrics/MethodLength:
Max: 32
Max: 34
# Offense count: 5
# Configuration parameters: CountKeywordArgs.

View File

@ -90,6 +90,7 @@ module Zip
end
elsif buffer && path_or_io.size > 0
# This zip is probably a non-empty StringIO.
@create = false
read_from_stream(path_or_io)
elsif @create
# This zip is completely new/empty and is to be created.
@ -311,6 +312,8 @@ module Zip
# Write buffer write changes to buffer and return
def write_buffer(io = ::StringIO.new)
return unless commit_required?
::Zip::OutputStream.write_buffer(io) do |zos|
@entry_set.each { |e| e.write_to_zip_output_stream(zos) }
zos.comment = comment

View File

@ -111,17 +111,27 @@ class ZipFileTest < MiniTest::Test
end
def test_open_buffer_with_string
string = File.read('test/data/rubycode.zip', mode: 'rb')
data = File.read('test/data/rubycode.zip', mode: 'rb')
string = data.dup
::Zip::File.open_buffer string do |zf|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
end
# Ensure the buffer hasn't changed.
assert_equal(data, string)
end
def test_open_buffer_with_stringio
string_io = StringIO.new File.read('test/data/rubycode.zip', mode: 'rb')
data = File.read('test/data/rubycode.zip', mode: 'rb')
string_io = StringIO.new(data.dup)
::Zip::File.open_buffer string_io do |zf|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
end
# Ensure the buffer hasn't changed.
assert_equal(data, string_io.string)
end
def test_close_buffer_with_stringio
@ -181,6 +191,18 @@ class ZipFileTest < MiniTest::Test
assert zf.entries.map(&:name).include?('zippedruby1.rb')
end
def test_open_buffer_without_block_write_buffer_does_nothing
data = File.read('test/data/rubycode.zip', mode: 'rb')
string_io = StringIO.new(data.dup)
zf = ::Zip::File.open_buffer(string_io)
assert zf.entries.map(&:name).include?('zippedruby1.rb')
# Ensure the buffer isn't changed.
zf.write_buffer(string_io)
assert_equal(data, string_io.string)
end
def test_open_file_with_max_length_comment
# Should not raise any errors.
Zip::File.open('test/data/max_length_file_comment.zip')