Skip to content

Commit

Permalink
Merge pull request #168 from hainesr/fix-and-cleanup-tests
Browse files Browse the repository at this point in the history
Fix and cleanup tests (fixes #164)
  • Loading branch information
simonoff committed Jul 16, 2014
2 parents 2f42434 + 2b5795e commit 05916bf
Show file tree
Hide file tree
Showing 34 changed files with 80 additions and 84 deletions.
15 changes: 0 additions & 15 deletions .gitignore
@@ -1,20 +1,5 @@
.idea
*.gem
test/5entry_copy.zip
test/cdirtest.bin
test/cdir64test.bin
test/huge.zip
test/centralEntryHeader.bin
test/data/generated/
test/deflatertest.bin
test/compressiontest_*.bin
test/dummy.txt
test/localEntryHeader.bin
test/okToDeleteMoved.txt
test/output.zip
test/test_putOnClosedStream.zip
test/zipWithDirs_copy.zip
test/coverage
.bundle
Gemfile.lock
+samples/*.zip
Expand Down
4 changes: 0 additions & 4 deletions Gemfile
@@ -1,7 +1,3 @@
source 'https://rubygems.org'

gemspec
gem 'rake'
gem 'coveralls', :require => false
gem 'pry'
gem 'minitest'
4 changes: 4 additions & 0 deletions rubyzip.gemspec
Expand Up @@ -16,4 +16,8 @@ spec = Gem::Specification.new do |s|
s.require_paths = ['lib']
s.license = 'BSD 2-Clause'
s.required_ruby_version = '>= 1.9.2'
s.add_development_dependency 'rake', '~> 10.3'
s.add_development_dependency 'pry', '~> 0.10'
s.add_development_dependency 'minitest', '~> 5.2.0'
s.add_development_dependency 'coveralls', '~> 0.7'
end
2 changes: 1 addition & 1 deletion test/basic_zip_file_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class BasicZipFileTest < MiniTest::Unit::TestCase
class BasicZipFileTest < MiniTest::Test
include AssertEntry

def setup
Expand Down
2 changes: 1 addition & 1 deletion test/central_directory_entry_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class ZipCentralDirectoryEntryTest < MiniTest::Unit::TestCase
class ZipCentralDirectoryEntryTest < MiniTest::Test

def test_read_from_stream
File.open("test/data/testDirectory.bin", "rb") {
Expand Down
2 changes: 1 addition & 1 deletion test/central_directory_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class ZipCentralDirectoryTest < MiniTest::Unit::TestCase
class ZipCentralDirectoryTest < MiniTest::Test

def test_read_from_stream
::File.open(TestZipFile::TEST_ZIP2.zip_name, "rb") {
Expand Down
23 changes: 14 additions & 9 deletions test/deflater_test.rb
@@ -1,28 +1,33 @@
require 'test_helper'

class DeflaterTest < MiniTest::Unit::TestCase
class DeflaterTest < MiniTest::Test
include CrcTest

DEFLATER_TEST_FILE = 'test/data/generated/deflatertest.bin'
BEST_COMP_FILE = 'test/data/generated/compressiontest_best_compression.bin'
DEFAULT_COMP_FILE = 'test/data/generated/compressiontest_default_compression.bin'
NO_COMP_FILE = 'test/data/generated/compressiontest_no_compression.bin'

def test_outputOperator
txt = load_file("test/data/file2.txt")
deflate(txt, "deflatertest.bin")
inflatedTxt = inflate("deflatertest.bin")
deflate(txt, DEFLATER_TEST_FILE)
inflatedTxt = inflate(DEFLATER_TEST_FILE)
assert_equal(txt, inflatedTxt)
end

def test_default_compression
txt = load_file("test/data/file2.txt")

Zip.default_compression = ::Zlib::BEST_COMPRESSION
deflate(txt, "compressiontest_best_compression.bin")
deflate(txt, BEST_COMP_FILE)
Zip.default_compression = ::Zlib::DEFAULT_COMPRESSION
deflate(txt, "compressiontest_default_compression.bin")
deflate(txt, DEFAULT_COMP_FILE)
Zip.default_compression = ::Zlib::NO_COMPRESSION
deflate(txt, "compressiontest_no_compression.bin")
deflate(txt, NO_COMP_FILE)

best = File.size("compressiontest_best_compression.bin")
default = File.size("compressiontest_default_compression.bin")
no = File.size("compressiontest_no_compression.bin")
best = File.size(BEST_COMP_FILE)
default = File.size(DEFAULT_COMP_FILE)
no = File.size(NO_COMP_FILE)

assert(best < default)
assert(best < no)
Expand Down
2 changes: 1 addition & 1 deletion test/entry_set_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class ZipEntrySetTest < MiniTest::Unit::TestCase
class ZipEntrySetTest < MiniTest::Test
ZIP_ENTRIES = [
::Zip::Entry.new("zipfile.zip", "name1", "comment1"),
::Zip::Entry.new("zipfile.zip", "name3", "comment1"),
Expand Down
4 changes: 2 additions & 2 deletions test/entry_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class ZipEntryTest < MiniTest::Unit::TestCase
class ZipEntryTest < MiniTest::Test
TEST_ZIPFILE = "someZipFile.zip"
TEST_COMMENT = "a comment"
TEST_COMPRESSED_SIZE = 1234
Expand Down Expand Up @@ -131,7 +131,7 @@ def test_entry_name_cannot_start_with_slash
end

def test_store_file_without_compression
File.delete('/tmp/no_compress.zip') if File.exists?('/tmp/no_compress.zip')
File.delete('/tmp/no_compress.zip') if File.exist?('/tmp/no_compress.zip')
files = Dir[File.join('test/data/globTest', '**', '**')]

Zip.setup do |z|
Expand Down
2 changes: 1 addition & 1 deletion test/errors_test.rb
@@ -1,7 +1,7 @@
# encoding: utf-8
require 'test_helper'

class ErrorsTest < MiniTest::Unit::TestCase
class ErrorsTest < MiniTest::Test

def test_rescue_legacy_zip_error
raise ::Zip::Error
Expand Down
2 changes: 1 addition & 1 deletion test/extra_field_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class ZipExtraFieldTest < MiniTest::Unit::TestCase
class ZipExtraFieldTest < MiniTest::Test
def test_new
extra_pure = ::Zip::ExtraField.new("")
extra_withstr = ::Zip::ExtraField.new("foo")
Expand Down
5 changes: 3 additions & 2 deletions test/file_extract_directory_test.rb
@@ -1,8 +1,9 @@
require 'test_helper'

class ZipFileExtractDirectoryTest < MiniTest::Unit::TestCase
class ZipFileExtractDirectoryTest < MiniTest::Test
include CommonZipFileFixture
TEST_OUT_NAME = "emptyOutDir"

TEST_OUT_NAME = "test/data/generated/emptyOutDir"

def open_zip(&aProc)
assert(aProc != nil)
Expand Down
4 changes: 2 additions & 2 deletions test/file_extract_test.rb
@@ -1,8 +1,8 @@
require 'test_helper'

class ZipFileExtractTest < MiniTest::Unit::TestCase
class ZipFileExtractTest < MiniTest::Test
include CommonZipFileFixture
EXTRACTED_FILENAME = "extEntry"
EXTRACTED_FILENAME = "test/data/generated/extEntry"
ENTRY_TO_EXTRACT, *REMAINING_ENTRIES = TEST_ZIP.entry_names.reverse

def setup
Expand Down
4 changes: 2 additions & 2 deletions test/file_split_test.rb
@@ -1,9 +1,9 @@
require 'test_helper'

class ZipFileSplitTest < MiniTest::Unit::TestCase
class ZipFileSplitTest < MiniTest::Test
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
TEST_ZIP.zip_name = "large_zip_file.zip"
EXTRACTED_FILENAME = "test/data/generated/extEntry"
EXTRACTED_FILENAME = "test/data/generated/extEntrySplit"
UNSPLITTED_FILENAME = "test/data/generated/unsplitted.zip"
ENTRY_TO_EXTRACT = TEST_ZIP.entry_names.first

Expand Down
19 changes: 10 additions & 9 deletions test/file_test.rb
@@ -1,9 +1,12 @@
require 'test_helper'


class ZipFileTest < MiniTest::Unit::TestCase
class ZipFileTest < MiniTest::Test
include CommonZipFileFixture

OK_DELETE_FILE = 'test/data/generated/okToDelete.txt'
OK_DELETE_MOVED_FILE = 'test/data/generated/okToDeleteMoved.txt'

def teardown
::Zip.write_zip64_support = false
end
Expand Down Expand Up @@ -87,19 +90,17 @@ def test_get_output_stream
end

def test_cleans_up_tempfiles_after_close
comment = "a short comment"

zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
zf.get_output_stream("myFile") do |os|
@tempfile_path = os.path
os.write "myFile contains just this"
end

assert_equal(true, File.exists?(@tempfile_path))
assert_equal(true, File.exist?(@tempfile_path))

zf.close

assert_equal(false, File.exists?(@tempfile_path))
assert_equal(false, File.exist?(@tempfile_path))
end

def test_add
Expand Down Expand Up @@ -386,13 +387,13 @@ def test_write_buffer
# can delete the file you used to add the entry to the zip file
# with
def test_commitUseZipEntry
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, "okToDelete.txt")
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
zf = ::Zip::File.open(TEST_ZIP.zip_name)
zf.add("okToDelete.txt", "okToDelete.txt")
zf.add("okToDelete.txt", OK_DELETE_FILE)
assert_contains(zf, "okToDelete.txt")
zf.commit
File.rename("okToDelete.txt", "okToDeleteMoved.txt")
assert_contains(zf, "okToDelete.txt", "okToDeleteMoved.txt")
File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
assert_contains(zf, "okToDelete.txt", OK_DELETE_MOVED_FILE)
end

# def test_close
Expand Down
2 changes: 1 addition & 1 deletion test/filesystem/dir_iterator_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/filesystem'

class ZipFsDirIteratorTest < MiniTest::Unit::TestCase
class ZipFsDirIteratorTest < MiniTest::Test

FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]

Expand Down
2 changes: 1 addition & 1 deletion test/filesystem/directory_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/filesystem'

class ZipFsDirectoryTest < MiniTest::Unit::TestCase
class ZipFsDirectoryTest < MiniTest::Test
TEST_ZIP = "test/data/generated/zipWithDirs_copy.zip"

def setup
Expand Down
2 changes: 1 addition & 1 deletion test/filesystem/file_mutating_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/filesystem'

class ZipFsFileMutatingTest < MiniTest::Unit::TestCase
class ZipFsFileMutatingTest < MiniTest::Test
TEST_ZIP = "test/data/generated/zipWithDirs_copy.zip"
def setup
FileUtils.cp("test/data/zipWithDirs.zip", TEST_ZIP)
Expand Down
2 changes: 1 addition & 1 deletion test/filesystem/file_nonmutating_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/filesystem'

class ZipFsFileNonmutatingTest < MiniTest::Unit::TestCase
class ZipFsFileNonmutatingTest < MiniTest::Test
def setup
@zipsha = Digest::SHA1.file("test/data/zipWithDirs.zip")
@zip_file = ::Zip::File.new("test/data/zipWithDirs.zip")
Expand Down
2 changes: 1 addition & 1 deletion test/filesystem/file_stat_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/filesystem'

class ZipFsFileStatTest < MiniTest::Unit::TestCase
class ZipFsFileStatTest < MiniTest::Test

def setup
@zip_file = ::Zip::File.new("test/data/zipWithDirs.zip")
Expand Down
2 changes: 1 addition & 1 deletion test/inflater_test.rb
@@ -1,5 +1,5 @@
require 'test_helper'
class InflaterTest < MiniTest::Unit::TestCase
class InflaterTest < MiniTest::Test
include DecompressorTests

def setup
Expand Down
2 changes: 1 addition & 1 deletion test/input_stream_test.rb
@@ -1,6 +1,6 @@
require 'test_helper'

class ZipInputStreamTest < MiniTest::Unit::TestCase
class ZipInputStreamTest < MiniTest::Test
include AssertEntry

def test_new
Expand Down
2 changes: 1 addition & 1 deletion test/ioextras/abstract_input_stream_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/ioextras'

class AbstractInputStreamTest < MiniTest::Unit::TestCase
class AbstractInputStreamTest < MiniTest::Test
# AbstractInputStream subclass that provides a read method

TEST_LINES = ["Hello world#{$/}",
Expand Down
2 changes: 1 addition & 1 deletion test/ioextras/abstract_output_stream_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/ioextras'

class AbstractOutputStreamTest < MiniTest::Unit::TestCase
class AbstractOutputStreamTest < MiniTest::Test
class TestOutputStream
include ::Zip::IOExtras::AbstractOutputStream

Expand Down
2 changes: 1 addition & 1 deletion test/ioextras/fake_io_test.rb
@@ -1,7 +1,7 @@
require 'test_helper'
require 'zip/ioextras'

class FakeIOTest < MiniTest::Unit::TestCase
class FakeIOTest < MiniTest::Test
class FakeIOUsingClass
include ::Zip::IOExtras::FakeIO
end
Expand Down
25 changes: 14 additions & 11 deletions test/local_entry_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'

class ZipLocalEntryTest < MiniTest::Unit::TestCase
class ZipLocalEntryTest < MiniTest::Test

CEH_FILE = 'test/data/generated/centralEntryHeader.bin'
LEH_FILE = 'test/data/generated/localEntryHeader.bin'

def teardown
::Zip.write_zip64_support = false
Expand Down Expand Up @@ -54,8 +57,8 @@ def test_writeEntry
entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment",
"thisIsSomeExtraInformation", 100, 987654,
::Zip::Entry::DEFLATED, 400)
write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry)
entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin")
write_to_file(LEH_FILE, CEH_FILE, entry)
entryReadLocal, entryReadCentral = read_from_file(LEH_FILE, CEH_FILE)
assert(entryReadCentral.extra['Zip64Placeholder'].nil?, 'zip64 placeholder should not be used in central directory')
compare_local_entry_headers(entry, entryReadLocal)
compare_c_dir_entry_headers(entry, entryReadCentral)
Expand All @@ -66,8 +69,8 @@ def test_writeEntryWithZip64
entry = ::Zip::Entry.new("file.zip", "entryName", "my little comment",
"thisIsSomeExtraInformation", 100, 987654,
::Zip::Entry::DEFLATED, 400)
write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry)
entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin")
write_to_file(LEH_FILE, CEH_FILE, entry)
entryReadLocal, entryReadCentral = read_from_file(LEH_FILE, CEH_FILE)
assert(entryReadLocal.extra['Zip64Placeholder'], 'zip64 placeholder should be used in local file header')
entryReadLocal.extra.delete('Zip64Placeholder') # it was removed when writing the c_dir_entry, so remove from compare
assert(entryReadCentral.extra['Zip64Placeholder'].nil?, 'zip64 placeholder should not be used in central directory')
Expand All @@ -81,8 +84,8 @@ def test_write64Entry
"malformed extra field because why not",
0x7766554433221100, 0xDEADBEEF, ::Zip::Entry::DEFLATED,
0x9988776655443322)
write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry)
entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin")
write_to_file(LEH_FILE, CEH_FILE, entry)
entryReadLocal, entryReadCentral = read_from_file(LEH_FILE, CEH_FILE)
compare_local_entry_headers(entry, entryReadLocal)
compare_c_dir_entry_headers(entry, entryReadCentral)
end
Expand All @@ -106,19 +109,19 @@ def test_rewriteLocalHeader64
def test_readLocalOffset
entry = ::Zip::Entry.new("file.zip", "entryName")
entry.local_header_offset = 12345
::File.open('centralEntryHeader.bin', 'wb') { |f| entry.write_c_dir_entry(f) }
::File.open(CEH_FILE, 'wb') { |f| entry.write_c_dir_entry(f) }
read_entry = nil
::File.open('centralEntryHeader.bin', 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
::File.open(CEH_FILE, 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
compare_c_dir_entry_headers(entry, read_entry)
end

def test_read64LocalOffset
::Zip.write_zip64_support = true
entry = ::Zip::Entry.new("file.zip", "entryName")
entry.local_header_offset = 0x0123456789ABCDEF
::File.open('centralEntryHeader.bin', 'wb') { |f| entry.write_c_dir_entry(f) }
::File.open(CEH_FILE, 'wb') { |f| entry.write_c_dir_entry(f) }
read_entry = nil
::File.open('centralEntryHeader.bin', 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
::File.open(CEH_FILE, 'rb') { |f| read_entry = ::Zip::Entry.read_c_dir_entry(f) }
compare_c_dir_entry_headers(entry, read_entry)
end

Expand Down

0 comments on commit 05916bf

Please sign in to comment.