2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-03-17 06:09:01 +08:00
|
|
|
require 'test_helper'
|
|
|
|
|
2015-09-03 21:14:23 +08:00
|
|
|
class ZipCaseSensitivityTest < MiniTest::Test
|
2015-03-17 06:09:01 +08:00
|
|
|
include CommonZipFileFixture
|
|
|
|
|
2021-05-26 04:50:06 +08:00
|
|
|
SRC_FILES = [
|
|
|
|
['test/data/file1.txt', 'testfile.rb'],
|
|
|
|
['test/data/file2.txt', 'testFILE.rb']
|
|
|
|
].freeze
|
2015-03-17 06:09:01 +08:00
|
|
|
|
|
|
|
def teardown
|
2020-06-21 00:34:57 +08:00
|
|
|
::Zip.reset!
|
2015-03-17 06:09:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Ensure that everything functions normally when +case_insensitive_match = false+
|
|
|
|
def test_add_case_sensitive
|
|
|
|
::Zip.case_insensitive_match = false
|
|
|
|
|
2024-03-02 06:14:48 +08:00
|
|
|
SRC_FILES.each { |(fn, _en)| assert(::File.exist?(fn)) }
|
2021-06-09 04:24:52 +08:00
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, create: true)
|
2015-03-17 06:09:01 +08:00
|
|
|
|
|
|
|
SRC_FILES.each { |fn, en| zf.add(en, fn) }
|
|
|
|
zf.close
|
|
|
|
|
2020-02-19 18:52:49 +08:00
|
|
|
zf_read = ::Zip::File.new(EMPTY_FILENAME)
|
|
|
|
assert_equal(SRC_FILES.size, zf_read.entries.length)
|
2019-09-14 23:05:23 +08:00
|
|
|
SRC_FILES.each_with_index do |a, i|
|
2020-02-19 18:52:49 +08:00
|
|
|
assert_equal(a.last, zf_read.entries[i].name)
|
2015-03-17 06:09:01 +08:00
|
|
|
AssertEntry.assert_contents(a.first,
|
2020-02-19 18:52:49 +08:00
|
|
|
zf_read.get_input_stream(a.last, &:read))
|
2019-09-14 23:05:23 +08:00
|
|
|
end
|
2015-03-17 06:09:01 +08:00
|
|
|
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
|
|
|
|
|
2024-03-02 06:14:48 +08:00
|
|
|
SRC_FILES.each { |(fn, _en)| assert(::File.exist?(fn)) }
|
2021-06-09 04:24:52 +08:00
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, create: true)
|
2015-03-17 06:09:01 +08:00
|
|
|
|
2022-08-15 05:05:42 +08:00
|
|
|
error = assert_raises Zip::EntryExistsError do
|
2015-03-23 00:22:56 +08:00
|
|
|
SRC_FILES.each { |fn, en| zf.add(en, fn) }
|
2015-03-17 06:09:01 +08:00
|
|
|
end
|
2022-08-15 05:05:42 +08:00
|
|
|
assert_match(/'add'/, error.message)
|
2015-03-17 06:09:01 +08:00
|
|
|
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
|
|
|
|
|
2024-03-02 06:14:48 +08:00
|
|
|
SRC_FILES.each { |(fn, _en)| assert(::File.exist?(fn)) }
|
2021-06-09 04:24:52 +08:00
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, create: true)
|
2015-03-17 06:09:01 +08:00
|
|
|
|
|
|
|
SRC_FILES.each { |fn, en| zf.add(en, fn) }
|
|
|
|
zf.close
|
|
|
|
|
|
|
|
::Zip.case_insensitive_match = true
|
|
|
|
|
2020-02-19 18:52:49 +08:00
|
|
|
zf_read = ::Zip::File.new(EMPTY_FILENAME)
|
|
|
|
assert_equal(SRC_FILES.collect { |_fn, en| en.downcase }.uniq.size, zf_read.entries.length)
|
|
|
|
assert_equal(SRC_FILES.last.last.downcase, zf_read.entries.first.name.downcase)
|
2019-09-27 05:38:28 +08:00
|
|
|
AssertEntry.assert_contents(
|
2020-02-19 18:52:49 +08:00
|
|
|
SRC_FILES.last.first, zf_read.get_input_stream(SRC_FILES.last.last, &:read)
|
2019-09-27 05:38:28 +08:00
|
|
|
)
|
2015-03-17 06:09:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2015-03-25 00:24:17 +08:00
|
|
|
|
2020-02-18 07:13:46 +08:00
|
|
|
def assert_contains(zip_file, entry_name, filename = entry_name)
|
|
|
|
refute_nil(
|
|
|
|
zip_file.entries.detect { |e| e.name == entry_name },
|
|
|
|
"entry #{entry_name} not in #{zip_file.entries.join(', ')} in zip file #{zip_file}"
|
|
|
|
)
|
|
|
|
assert_entry_contents(zip_file, entry_name, filename) if File.exist?(filename)
|
2015-03-17 06:09:01 +08:00
|
|
|
end
|
|
|
|
end
|