Merge branch 'master' into rubocop_fixes

Conflicts:
	lib/zip/entry.rb
This commit is contained in:
Pavel Lobashov 2015-03-21 11:37:28 +03:00
commit da863e4b55
5 changed files with 89 additions and 10 deletions

View File

@ -37,7 +37,7 @@ end
module Zip module Zip
extend self extend self
attr_accessor :unicode_names, :on_exists_proc, :continue_on_exists_proc, :sort_entries, :default_compression, :write_zip64_support, :warn_invalid_date attr_accessor :unicode_names, :on_exists_proc, :continue_on_exists_proc, :sort_entries, :default_compression, :write_zip64_support, :warn_invalid_date, :case_insensitive_match
def reset! def reset!
@_ran_once = false @_ran_once = false
@ -48,6 +48,7 @@ module Zip
@default_compression = ::Zlib::DEFAULT_COMPRESSION @default_compression = ::Zlib::DEFAULT_COMPRESSION
@write_zip64_support = false @write_zip64_support = false
@warn_invalid_date = true @warn_invalid_date = true
@case_insensitive_match = false
end end
def setup def setup

View File

@ -570,7 +570,7 @@ module Zip
def set_time(binary_dos_date, binary_dos_time) def set_time(binary_dos_date, binary_dos_time)
@time = ::Zip::DOSTime.parse_binary_dos_format(binary_dos_date, binary_dos_time) @time = ::Zip::DOSTime.parse_binary_dos_format(binary_dos_date, binary_dos_time)
rescue ArgumentError rescue ArgumentError
puts 'Invalid date/time in zip entry' if ::Zip.warn_invalid_date STDERR.puts "Invalid date/time in zip entry" if ::Zip.warn_invalid_date
end end
def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exists_proc }) def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exists_proc })

View File

@ -13,10 +13,8 @@ module Zip
@entry_set.include?(to_key(entry)) @entry_set.include?(to_key(entry))
end end
def find_entry(entry, case_sensitively = true) def find_entry(entry)
return @entry_set[to_key(entry)] if case_sensitively @entry_set[to_key(entry)]
entry = @entry_set.find { |k, _| k.downcase == to_key(entry).downcase }
entry.last if entry
end end
def <<(entry) def <<(entry)
@ -80,7 +78,9 @@ module Zip
private private
def to_key(entry) def to_key(entry)
entry.to_s.chomp('/') k = entry.to_s.chomp('/')
k.downcase! if ::Zip.case_insensitive_match
k
end end
end end
end end

View File

@ -0,0 +1,70 @@
require 'test_helper'
class ZipFileTest < MiniTest::Test
include CommonZipFileFixture
SRC_FILES = [ [ "test/data/file1.txt", "testfile.rb" ],
[ "test/data/file2.txt", "testFILE.rb" ] ]
def teardown
::Zip.case_insensitive_match = false
end
# Ensure that everything functions normally when +case_insensitive_match = false+
def test_add_case_sensitive
::Zip.case_insensitive_match = false
SRC_FILES.each { |fn, en| assert(::File.exist?(fn)) }
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
SRC_FILES.each { |fn, en| zf.add(en, fn) }
zf.close
zfRead = ::Zip::File.new(EMPTY_FILENAME)
assert_equal(SRC_FILES.size, zfRead.entries.length)
SRC_FILES.each_with_index { |a, i|
assert_equal(a.last, zfRead.entries[i].name)
AssertEntry.assert_contents(a.first,
zfRead.get_input_stream(a.last) { |zis| zis.read })
}
end
# Ensure that names are treated case insensitively when adding files and +case_insensitive_match = false+
def test_add_case_insensitive
::Zip.case_insensitive_match = true
SRC_FILES.each { |fn, en| assert(::File.exist?(fn)) }
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
assert_raises Zip::EntryExistsError do
SRC_FILES.each { |fn, en| zf.add(en, fn) }
end
end
# Ensure that names are treated case insensitively when reading files and +case_insensitive_match = true+
def test_add_case_sensitive_read_case_insensitive
::Zip.case_insensitive_match = false
SRC_FILES.each { |fn, en| assert(::File.exist?(fn)) }
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
SRC_FILES.each { |fn, en| zf.add(en, fn) }
zf.close
::Zip.case_insensitive_match = true
zfRead = ::Zip::File.new(EMPTY_FILENAME)
assert_equal(SRC_FILES.collect{ |fn, en| en.downcase}.uniq.size, zfRead.entries.length)
assert_equal(SRC_FILES.last.last.downcase, zfRead.entries.first.name.downcase)
AssertEntry.assert_contents(SRC_FILES.last.first,
zfRead.get_input_stream(SRC_FILES.last.last) { |zis| zis.read })
end
private
def assert_contains(zf, entryName, filename = entryName)
assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
assert_entryContents(zf, entryName, filename) if File.exist?(filename)
end
end

View File

@ -66,9 +66,17 @@ class ZipEntrySetTest < MiniTest::Test
end end
def test_find_entry def test_find_entry
# by default, #find_entry is case-sensitive entries = [::Zip::Entry.new("zipfile.zip", "MiXeDcAsEnAmE", "comment1")]
assert_equal(ZIP_ENTRIES[0], @zipEntrySet.find_entry('name1'))
assert_equal(ZIP_ENTRIES[0], @zipEntrySet.find_entry('NaMe1', false)) ::Zip.case_insensitive_match = true
zipEntrySet = ::Zip::EntrySet.new(entries)
assert_equal(entries[0], zipEntrySet.find_entry('MiXeDcAsEnAmE'))
assert_equal(entries[0], zipEntrySet.find_entry('mixedcasename'))
::Zip.case_insensitive_match = false
zipEntrySet = ::Zip::EntrySet.new(entries)
assert_equal(entries[0], zipEntrySet.find_entry('MiXeDcAsEnAmE'))
assert_equal(nil, zipEntrySet.find_entry('mixedcasename'))
end end
def test_entries_with_sort def test_entries_with_sort