rubyzip/test/output_stream_test.rb

164 lines
4.7 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
require 'test_helper'
class ZipOutputStreamTest < MiniTest::Test
include AssertEntry
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
2015-03-21 16:27:44 +08:00
TEST_ZIP.zip_name = 'test/data/generated/output.zip'
def test_new
zos = ::Zip::OutputStream.new(TEST_ZIP.zip_name)
zos.comment = TEST_ZIP.comment
write_test_zip(zos)
zos.close
assert_test_zip_contents(TEST_ZIP)
end
def test_open
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
zos.comment = TEST_ZIP.comment
write_test_zip(zos)
end
assert_test_zip_contents(TEST_ZIP)
end
def test_write_buffer
2021-06-09 01:09:22 +08:00
buffer = ::Zip::OutputStream.write_buffer(::StringIO.new) do |zos|
zos.comment = TEST_ZIP.comment
write_test_zip(zos)
end
File.binwrite(TEST_ZIP.zip_name, buffer.string)
assert_test_zip_contents(TEST_ZIP)
end
2020-02-20 01:48:13 +08:00
def test_write_buffer_binmode
2021-06-09 01:09:22 +08:00
buffer = ::Zip::OutputStream.write_buffer(::StringIO.new) do |zos|
2020-02-20 01:48:13 +08:00
zos.comment = TEST_ZIP.comment
write_test_zip(zos)
end
assert_equal Encoding::ASCII_8BIT, buffer.external_encoding
2020-02-20 01:48:13 +08:00
end
def test_write_buffer_with_temp_file
tmp_file = Tempfile.new('')
::Zip::OutputStream.write_buffer(tmp_file) do |zos|
zos.comment = TEST_ZIP.comment
write_test_zip(zos)
end
tmp_file.rewind
File.binwrite(TEST_ZIP.zip_name, tmp_file.read)
tmp_file.unlink
assert_test_zip_contents(TEST_ZIP)
end
def test_write_buffer_with_temp_file2
tmp_file = ::File.join(Dir.tmpdir, 'zos.zip')
::File.open(tmp_file, 'wb') do |f|
::Zip::OutputStream.write_buffer(f) do |zos|
zos.comment = TEST_ZIP.comment
write_test_zip(zos)
end
end
::Zip::File.open(tmp_file) # Should open without error.
ensure
::File.unlink(tmp_file)
end
2021-06-09 01:09:22 +08:00
def test_write_buffer_with_default_io
buffer = ::Zip::OutputStream.write_buffer do |zos|
zos.comment = TEST_ZIP.comment
write_test_zip(zos)
end
File.binwrite(TEST_ZIP.zip_name, buffer.string)
2021-06-09 01:09:22 +08:00
assert_test_zip_contents(TEST_ZIP)
end
2015-03-25 00:02:54 +08:00
def test_writing_to_closed_stream
2015-03-21 16:27:44 +08:00
assert_i_o_error_in_closed_stream { |zos| zos << 'hello world' }
assert_i_o_error_in_closed_stream { |zos| zos.puts 'hello world' }
assert_i_o_error_in_closed_stream { |zos| zos.write 'hello world' }
end
2015-03-25 00:02:54 +08:00
def test_cannot_open_file
name = TestFiles::EMPTY_TEST_DIR
begin
::Zip::OutputStream.open(name)
2020-02-09 19:15:17 +08:00
rescue SystemCallError
2019-09-27 04:46:00 +08:00
assert($ERROR_INFO.kind_of?(Errno::EISDIR) || # Linux
$ERROR_INFO.kind_of?(Errno::EEXIST) || # Windows/cygwin
$ERROR_INFO.kind_of?(Errno::EACCES), # Windows
"Expected Errno::EISDIR (or on win/cygwin: Errno::EEXIST), but was: #{$ERROR_INFO.class}")
end
end
def test_put_next_entry
2015-03-21 16:27:44 +08:00
stored_text = 'hello world in stored text'
entry_name = 'file1'
comment = 'my comment'
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
zos << stored_text
end
assert(File.read(TEST_ZIP.zip_name, mode: 'rb')[stored_text])
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
assert_equal(stored_text, zf.read(entry_name))
end
end
def test_put_next_entry_using_zip_entry_creates_entries_with_correct_timestamps
2015-03-21 16:27:44 +08:00
file = ::File.open('test/data/file2.txt', 'rb')
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
zip_entry = ::Zip::Entry.new(
zos, file.path, time: ::Zip::DOSTime.at(file.mtime)
)
zos.put_next_entry(zip_entry)
zos << file.read
end
2015-03-21 16:16:06 +08:00
::Zip::InputStream.open(TEST_ZIP.zip_name) do |io|
while (entry = io.get_next_entry)
# Compare DOS Times, since they are stored with two seconds accuracy
assert(::Zip::DOSTime.at(file.mtime) == ::Zip::DOSTime.at(entry.mtime))
end
end
end
2014-03-01 06:31:10 +08:00
def test_chained_put_into_next_entry
2015-03-21 16:27:44 +08:00
stored_text = 'hello world in stored text'
stored_text2 = 'with chain'
entry_name = 'file1'
comment = 'my comment'
2014-03-01 06:31:10 +08:00
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
zos << stored_text << stored_text2
end
assert(File.read(TEST_ZIP.zip_name)[stored_text])
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
assert_equal(stored_text + stored_text2, zf.read(entry_name))
end
end
def assert_i_o_error_in_closed_stream
2015-03-21 16:10:37 +08:00
assert_raises(IOError) do
2015-03-21 16:27:44 +08:00
zos = ::Zip::OutputStream.new('test/data/generated/test_putOnClosedStream.zip')
zos.close
yield zos
2015-03-21 16:10:37 +08:00
end
end
def write_test_zip(zos)
2020-02-18 06:35:08 +08:00
TEST_ZIP.entry_names.each do |entry_name|
zos.put_next_entry(entry_name)
File.open(entry_name, 'rb') { |f| zos.write(f.read) }
end
end
end