Skip to content

Commit

Permalink
Ensure File.open_buffer doesn't rewrite unchanged data.
Browse files Browse the repository at this point in the history
  • Loading branch information
hainesr committed Nov 30, 2021
1 parent f5e19db commit 14b63f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/zip/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ def initialize(path_or_io, create: false, buffer: false, **options)
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.
Expand Down Expand Up @@ -311,6 +312,8 @@ def commit

# 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
Expand Down
26 changes: 24 additions & 2 deletions test/file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,17 +111,27 @@ def test_get_output_stream
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
Expand Down Expand Up @@ -181,6 +191,18 @@ def test_open_buffer_without_block
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')
Expand Down

0 comments on commit 14b63f6

Please sign in to comment.