|
| 1 | +require 'test_helper' |
| 2 | + |
| 3 | + |
| 4 | +class ZipFileTest < MiniTest::Test |
| 5 | + include CommonZipFileFixture |
| 6 | + |
| 7 | + SRC_FILES = [ [ "test/data/file1.txt", "testfile.rb" ], |
| 8 | + [ "test/data/file2.txt", "testFILE.rb" ] ] |
| 9 | + |
| 10 | + def teardown |
| 11 | + ::Zip.case_insensitive_match = false |
| 12 | + end |
| 13 | + |
| 14 | + # Ensure that everything functions normally when +case_insensitive_match = false+ |
| 15 | + def test_add_case_sensitive |
| 16 | + ::Zip.case_insensitive_match = false |
| 17 | + |
| 18 | + SRC_FILES.each { |fn, en| assert(::File.exist?(fn)) } |
| 19 | + zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE) |
| 20 | + |
| 21 | + SRC_FILES.each { |fn, en| zf.add(en, fn) } |
| 22 | + zf.close |
| 23 | + |
| 24 | + zfRead = ::Zip::File.new(EMPTY_FILENAME) |
| 25 | + assert_equal(SRC_FILES.size, zfRead.entries.length) |
| 26 | + SRC_FILES.each_with_index { |a, i| |
| 27 | + assert_equal(a.last, zfRead.entries[i].name) |
| 28 | + AssertEntry.assert_contents(a.first, |
| 29 | + zfRead.get_input_stream(a.last) { |zis| zis.read }) |
| 30 | + } |
| 31 | + end |
| 32 | + |
| 33 | + # Ensure that names are treated case insensitively when adding files and +case_insensitive_match = false+ |
| 34 | + def test_add_case_insensitive |
| 35 | + ::Zip.case_insensitive_match = true |
| 36 | + |
| 37 | + SRC_FILES.each { |fn, en| assert(::File.exist?(fn)) } |
| 38 | + zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE) |
| 39 | + |
| 40 | + assert_raises Zip::EntryExistsError do |
| 41 | + SRC_FILES.each { |fn, en| zf.add(en, fn) } |
| 42 | + end |
| 43 | + |
| 44 | + end |
| 45 | + |
| 46 | + # Ensure that names are treated case insensitively when reading files and +case_insensitive_match = true+ |
| 47 | + def test_add_case_sensitive_read_case_insensitive |
| 48 | + ::Zip.case_insensitive_match = false |
| 49 | + |
| 50 | + SRC_FILES.each { |fn, en| assert(::File.exist?(fn)) } |
| 51 | + zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE) |
| 52 | + |
| 53 | + SRC_FILES.each { |fn, en| zf.add(en, fn) } |
| 54 | + zf.close |
| 55 | + |
| 56 | + ::Zip.case_insensitive_match = true |
| 57 | + |
| 58 | + zfRead = ::Zip::File.new(EMPTY_FILENAME) |
| 59 | + assert_equal(SRC_FILES.collect{ |fn, en| en.downcase}.uniq.size, zfRead.entries.length) |
| 60 | + assert_equal(SRC_FILES.last.last.downcase, zfRead.entries.first.name.downcase) |
| 61 | + AssertEntry.assert_contents(SRC_FILES.last.first, |
| 62 | + zfRead.get_input_stream(SRC_FILES.last.last) { |zis| zis.read }) |
| 63 | + end |
| 64 | + |
| 65 | + private |
| 66 | + def assert_contains(zf, entryName, filename = entryName) |
| 67 | + assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}") |
| 68 | + assert_entryContents(zf, entryName, filename) if File.exist?(filename) |
| 69 | + end |
| 70 | +end |
0 commit comments