rubyzip/test/test_helper.rb

226 lines
5.4 KiB
Ruby
Raw Normal View History

require 'simplecov'
require 'minitest/autorun'
require 'minitest/unit'
require 'fileutils'
require 'tmpdir'
require 'digest/sha1'
require 'zip'
require 'gentestfiles'
TestFiles.create_test_files
TestZipFile.create_test_zips
2015-11-21 18:07:30 +08:00
if defined? JRUBY_VERSION
require 'jruby'
JRuby.objectspace = true
end
::MiniTest.after_run do
FileUtils.rm_rf('test/data/generated')
end
module IOizeString
attr_reader :tell
def read(count = nil)
@tell ||= 0
2020-02-09 18:39:04 +08:00
count ||= size
retVal = slice(@tell, count)
@tell += count
2015-03-23 00:27:29 +08:00
retVal
end
def seek(index, offset)
@tell ||= 0
case offset
when IO::SEEK_END
newPos = size + index
when IO::SEEK_SET
newPos = index
when IO::SEEK_CUR
newPos = @tell + index
else
2015-03-21 16:27:44 +08:00
raise 'Error in test method IOizeString::seek'
end
2020-02-09 23:50:00 +08:00
raise Errno::EINVAL if newPos < 0 || newPos >= size
@tell = newPos
end
def reset
@tell = 0
end
end
module DecompressorTests
# expects @refText, @refLines and @decompressor
2015-03-21 16:27:44 +08:00
TEST_FILE = 'test/data/file1.txt'
def setup
@refText = ''
File.open(TEST_FILE) { |f| @refText = f.read }
2019-09-27 04:46:00 +08:00
@refLines = @refText.split($INPUT_RECORD_SEPARATOR)
end
2015-03-25 00:02:54 +08:00
def test_read_everything
2020-01-05 23:06:05 +08:00
assert_equal(@refText, @decompressor.read)
end
2015-03-25 00:02:54 +08:00
def test_read_in_chunks
chunkSize = 5
2020-01-05 23:06:05 +08:00
while (decompressedChunk = @decompressor.read(chunkSize))
assert_equal(@refText.slice!(0, chunkSize), decompressedChunk)
end
assert_equal(0, @refText.size)
end
end
module AssertEntry
def assert_next_entry(filename, zis)
assert_entry(filename, zis, zis.get_next_entry.name)
end
def assert_entry(filename, zis, entryName)
assert_equal(filename, entryName)
2015-03-25 00:02:54 +08:00
assert_entry_contents_for_stream(filename, zis, entryName)
end
2015-03-25 00:02:54 +08:00
def assert_entry_contents_for_stream(filename, zis, entryName)
2015-03-21 16:27:44 +08:00
File.open(filename, 'rb') do |file|
expected = file.read
actual = zis.read
2017-06-29 10:57:12 +08:00
if expected != actual
if (expected && actual) && (expected.length > 400 || actual.length > 400)
zipEntryFilename = entryName + '.zipEntry'
2015-03-21 16:27:44 +08:00
File.open(zipEntryFilename, 'wb') { |entryfile| entryfile << actual }
2019-09-26 20:58:43 +08:00
raise("File '#{filename}' is different from '#{zipEntryFilename}'")
else
assert_equal(expected, actual)
end
end
2015-03-21 16:10:37 +08:00
end
end
2015-03-25 00:37:12 +08:00
def self.assert_contents(filename, aString)
2015-03-21 16:27:44 +08:00
fileContents = ''
File.open(filename, 'rb') { |f| fileContents = f.read }
2020-02-09 23:50:00 +08:00
return unless fileContents != aString
if fileContents.length > 400 || aString.length > 400
stringFile = filename + '.other'
File.open(stringFile, 'wb') { |f| f << aString }
2019-09-26 20:58:43 +08:00
raise("File '#{filename}' is different from contents of string stored in '#{stringFile}'")
2020-02-09 23:50:00 +08:00
else
assert_equal(fileContents, aString)
end
end
def assert_stream_contents(zis, testZipFile)
2015-06-08 15:29:08 +08:00
assert(!zis.nil?)
2020-02-18 06:35:08 +08:00
testZipFile.entry_names.each do |entry_name|
assert_next_entry(entry_name, zis)
end
assert_nil(zis.get_next_entry)
end
def assert_test_zip_contents(testZipFile)
::Zip::InputStream.open(testZipFile.zip_name) do |zis|
assert_stream_contents(zis, testZipFile)
end
end
2015-03-25 00:02:54 +08:00
def assert_entry_contents(zipFile, entryName, filename = entryName.to_s)
zis = zipFile.get_input_stream(entryName)
2015-03-25 00:02:54 +08:00
assert_entry_contents_for_stream(filename, zis, entryName)
ensure
zis.close if zis
end
end
module CrcTest
class TestOutputStream
include ::Zip::IOExtras::AbstractOutputStream
attr_accessor :buffer
def initialize
2015-03-21 16:27:44 +08:00
@buffer = ''
end
def <<(data)
@buffer << data
self
end
end
def run_crc_test(compressorClass)
str = "Here's a nice little text to compute the crc for! Ho hum, it is nice nice nice nice indeed."
fakeOut = TestOutputStream.new
deflater = compressorClass.new(fakeOut)
deflater << str
assert_equal(0x919920fc, deflater.crc)
end
end
module Enumerable
def compare_enumerables(otherEnumerable)
otherAsArray = otherEnumerable.to_a
2015-03-21 16:10:37 +08:00
each_with_index do |element, index|
return false unless yield(element, otherAsArray[index])
2015-03-21 16:10:37 +08:00
end
2015-03-23 00:30:24 +08:00
size == otherAsArray.size
end
end
module CommonZipFileFixture
include AssertEntry
2015-03-21 16:27:44 +08:00
EMPTY_FILENAME = 'emptyZipFile.zip'
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
2015-03-21 16:27:44 +08:00
TEST_ZIP.zip_name = 'test/data/generated/5entry_copy.zip'
def setup
2014-02-07 07:00:38 +08:00
File.delete(EMPTY_FILENAME) if File.exist?(EMPTY_FILENAME)
FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
end
end
module ExtraAssertions
def assert_forwarded(anObject, method, retVal, *expectedArgs)
callArgs = nil
setCallArgsProc = proc { |args| callArgs = args }
2019-09-22 21:36:45 +08:00
anObject.instance_eval <<-END_EVAL, __FILE__, __LINE__ + 1
alias #{method}_org #{method}
def #{method}(*args)
ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
ObjectSpace._id2ref(#{retVal.object_id})
end
2019-09-22 21:36:45 +08:00
END_EVAL
assert_equal(retVal, yield) # Invoke test
assert_equal(expectedArgs, callArgs)
ensure
2019-09-22 21:36:45 +08:00
anObject.instance_eval <<-END_EVAL, __FILE__, __LINE__ + 1
undef #{method}
alias #{method} #{method}_org
END_EVAL
end
end
module ZipEntryData
TEST_ZIPFILE = 'someZipFile.zip'
TEST_COMMENT = 'a comment'
TEST_COMPRESSED_SIZE = 1234
TEST_CRC = 325_324
TEST_EXTRA = 'Some data here'
TEST_COMPRESSIONMETHOD = ::Zip::Entry::DEFLATED
TEST_NAME = 'entry name'
TEST_SIZE = 8432
TEST_ISDIRECTORY = false
TEST_TIME = Time.now
end