Ensure `File.open_buffer` doesn't rewrite unchanged data.
This commit is contained in:
parent
f5e19db273
commit
14b63f68db
|
@ -30,7 +30,7 @@ Metrics/CyclomaticComplexity:
|
||||||
# Offense count: 47
|
# Offense count: 47
|
||||||
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
|
# Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
|
||||||
Metrics/MethodLength:
|
Metrics/MethodLength:
|
||||||
Max: 32
|
Max: 34
|
||||||
|
|
||||||
# Offense count: 5
|
# Offense count: 5
|
||||||
# Configuration parameters: CountKeywordArgs.
|
# Configuration parameters: CountKeywordArgs.
|
||||||
|
|
|
@ -90,6 +90,7 @@ module Zip
|
||||||
end
|
end
|
||||||
elsif buffer && path_or_io.size > 0
|
elsif buffer && path_or_io.size > 0
|
||||||
# This zip is probably a non-empty StringIO.
|
# This zip is probably a non-empty StringIO.
|
||||||
|
@create = false
|
||||||
read_from_stream(path_or_io)
|
read_from_stream(path_or_io)
|
||||||
elsif @create
|
elsif @create
|
||||||
# This zip is completely new/empty and is to be created.
|
# 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
|
# Write buffer write changes to buffer and return
|
||||||
def write_buffer(io = ::StringIO.new)
|
def write_buffer(io = ::StringIO.new)
|
||||||
|
return unless commit_required?
|
||||||
|
|
||||||
::Zip::OutputStream.write_buffer(io) do |zos|
|
::Zip::OutputStream.write_buffer(io) do |zos|
|
||||||
@entry_set.each { |e| e.write_to_zip_output_stream(zos) }
|
@entry_set.each { |e| e.write_to_zip_output_stream(zos) }
|
||||||
zos.comment = comment
|
zos.comment = comment
|
||||||
|
|
|
@ -111,17 +111,27 @@ class ZipFileTest < MiniTest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_open_buffer_with_string
|
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|
|
::Zip::File.open_buffer string do |zf|
|
||||||
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Ensure the buffer hasn't changed.
|
||||||
|
assert_equal(data, string)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_open_buffer_with_stringio
|
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|
|
::Zip::File.open_buffer string_io do |zf|
|
||||||
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Ensure the buffer hasn't changed.
|
||||||
|
assert_equal(data, string_io.string)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_close_buffer_with_stringio
|
def test_close_buffer_with_stringio
|
||||||
|
@ -181,6 +191,18 @@ class ZipFileTest < MiniTest::Test
|
||||||
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
||||||
end
|
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
|
def test_open_file_with_max_length_comment
|
||||||
# Should not raise any errors.
|
# Should not raise any errors.
|
||||||
Zip::File.open('test/data/max_length_file_comment.zip')
|
Zip::File.open('test/data/max_length_file_comment.zip')
|
||||||
|
|
Loading…
Reference in New Issue