instiki/vendor/rubyzip-0.5.8/test/zipfilesystemtest.rb

830 lines
21 KiB
Ruby
Raw Normal View History

2005-01-15 21:26:54 +01:00
#!/usr/bin/env ruby
$VERBOSE = true
2005-04-10 18:55:44 +02:00
$: << "../lib"
2005-01-15 21:26:54 +01:00
require 'zip/zipfilesystem'
2005-04-10 18:55:44 +02:00
require 'test/unit'
2005-01-15 21:26:54 +01:00
module ExtraAssertions
def assert_forwarded(anObject, method, retVal, *expectedArgs)
callArgs = nil
setCallArgsProc = proc { |args| callArgs = args }
anObject.instance_eval <<-"end_eval"
alias #{method}_org #{method}
def #{method}(*args)
ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
ObjectSpace._id2ref(#{retVal.object_id})
end
end_eval
2005-04-10 18:55:44 +02:00
assert_equal(retVal, yield) # Invoke test
assert_equal(expectedArgs, callArgs)
2005-01-15 21:26:54 +01:00
ensure
anObject.instance_eval "alias #{method} #{method}_org"
end
end
include Zip
2005-04-10 18:55:44 +02:00
class ZipFsFileNonmutatingTest < Test::Unit::TestCase
2005-01-15 21:26:54 +01:00
def setup
2005-04-10 18:55:44 +02:00
@zipFile = ZipFile.new("data/zipWithDirs.zip")
2005-01-15 21:26:54 +01:00
end
def teardown
@zipFile.close if @zipFile
end
def test_umask
2005-04-10 18:55:44 +02:00
assert_equal(File.umask, @zipFile.file.umask)
2005-01-15 21:26:54 +01:00
@zipFile.file.umask(0006)
end
def test_exists?
assert(! @zipFile.file.exists?("notAFile"))
assert(@zipFile.file.exists?("file1"))
assert(@zipFile.file.exists?("dir1"))
assert(@zipFile.file.exists?("dir1/"))
assert(@zipFile.file.exists?("dir1/file12"))
assert(@zipFile.file.exist?("dir1/file12")) # notice, tests exist? alias of exists? !
@zipFile.dir.chdir "dir1/"
assert(!@zipFile.file.exists?("file1"))
assert(@zipFile.file.exists?("file12"))
end
def test_open_read
blockCalled = false
@zipFile.file.open("file1", "r") {
|f|
blockCalled = true
2005-04-10 18:55:44 +02:00
assert_equal("this is the entry 'file1' in my test archive!",
2005-01-15 21:26:54 +01:00
f.readline.chomp)
}
assert(blockCalled)
blockCalled = false
@zipFile.dir.chdir "dir2"
@zipFile.file.open("file21", "r") {
|f|
blockCalled = true
2005-04-10 18:55:44 +02:00
assert_equal("this is the entry 'dir2/file21' in my test archive!",
2005-01-15 21:26:54 +01:00
f.readline.chomp)
}
assert(blockCalled)
@zipFile.dir.chdir "/"
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT) {
2005-01-15 21:26:54 +01:00
@zipFile.file.open("noSuchEntry")
}
begin
is = @zipFile.file.open("file1")
2005-04-10 18:55:44 +02:00
assert_equal("this is the entry 'file1' in my test archive!",
2005-01-15 21:26:54 +01:00
is.readline.chomp)
ensure
is.close if is
end
end
def test_new
begin
is = @zipFile.file.new("file1")
2005-04-10 18:55:44 +02:00
assert_equal("this is the entry 'file1' in my test archive!",
2005-01-15 21:26:54 +01:00
is.readline.chomp)
ensure
is.close if is
end
begin
is = @zipFile.file.new("file1") {
fail "should not call block"
}
ensure
is.close if is
end
end
def test_symlink
2005-04-10 18:55:44 +02:00
assert_raise(NotImplementedError) {
2005-01-15 21:26:54 +01:00
@zipFile.file.symlink("file1", "aSymlink")
}
end
def test_size
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT) { @zipFile.file.size("notAFile") }
assert_equal(72, @zipFile.file.size("file1"))
assert_equal(0, @zipFile.file.size("dir2/dir21"))
2005-01-15 21:26:54 +01:00
2005-04-10 18:55:44 +02:00
assert_equal(72, @zipFile.file.stat("file1").size)
assert_equal(0, @zipFile.file.stat("dir2/dir21").size)
2005-01-15 21:26:54 +01:00
end
def test_size?
2005-04-10 18:55:44 +02:00
assert_equal(nil, @zipFile.file.size?("notAFile"))
assert_equal(72, @zipFile.file.size?("file1"))
assert_equal(nil, @zipFile.file.size?("dir2/dir21"))
2005-01-15 21:26:54 +01:00
2005-04-10 18:55:44 +02:00
assert_equal(72, @zipFile.file.stat("file1").size?)
assert_equal(nil, @zipFile.file.stat("dir2/dir21").size?)
2005-01-15 21:26:54 +01:00
end
def test_file?
assert(@zipFile.file.file?("file1"))
assert(@zipFile.file.file?("dir2/file21"))
assert(! @zipFile.file.file?("dir1"))
assert(! @zipFile.file.file?("dir1/dir11"))
assert(@zipFile.file.stat("file1").file?)
assert(@zipFile.file.stat("dir2/file21").file?)
assert(! @zipFile.file.stat("dir1").file?)
assert(! @zipFile.file.stat("dir1/dir11").file?)
end
include ExtraAssertions
def test_dirname
assert_forwarded(File, :dirname, "retVal", "a/b/c/d") {
@zipFile.file.dirname("a/b/c/d")
}
end
def test_basename
assert_forwarded(File, :basename, "retVal", "a/b/c/d") {
@zipFile.file.basename("a/b/c/d")
}
end
def test_split
assert_forwarded(File, :split, "retVal", "a/b/c/d") {
@zipFile.file.split("a/b/c/d")
}
end
def test_join
2005-04-10 18:55:44 +02:00
assert_equal("a/b/c", @zipFile.file.join("a/b", "c"))
assert_equal("a/b/c/d", @zipFile.file.join("a/b", "c/d"))
assert_equal("/c/d", @zipFile.file.join("", "c/d"))
assert_equal("a/b/c/d", @zipFile.file.join("a", "b", "c", "d"))
2005-01-15 21:26:54 +01:00
end
def test_utime
t_now = Time.now
t_bak = @zipFile.file.mtime("file1")
@zipFile.file.utime(t_now, "file1")
2005-04-10 18:55:44 +02:00
assert_equal(t_now, @zipFile.file.mtime("file1"))
2005-01-15 21:26:54 +01:00
@zipFile.file.utime(t_bak, "file1")
2005-04-10 18:55:44 +02:00
assert_equal(t_bak, @zipFile.file.mtime("file1"))
2005-01-15 21:26:54 +01:00
end
def assert_always_false(operation)
assert(! @zipFile.file.send(operation, "noSuchFile"))
assert(! @zipFile.file.send(operation, "file1"))
assert(! @zipFile.file.send(operation, "dir1"))
assert(! @zipFile.file.stat("file1").send(operation))
assert(! @zipFile.file.stat("dir1").send(operation))
end
def assert_true_if_entry_exists(operation)
assert(! @zipFile.file.send(operation, "noSuchFile"))
assert(@zipFile.file.send(operation, "file1"))
assert(@zipFile.file.send(operation, "dir1"))
assert(@zipFile.file.stat("file1").send(operation))
assert(@zipFile.file.stat("dir1").send(operation))
end
def test_pipe?
assert_always_false(:pipe?)
end
def test_blockdev?
assert_always_false(:blockdev?)
end
def test_symlink?
assert_always_false(:symlink?)
end
def test_socket?
assert_always_false(:socket?)
end
def test_chardev?
assert_always_false(:chardev?)
end
def test_truncate
2005-04-10 18:55:44 +02:00
assert_raise(StandardError, "truncate not supported") {
2005-01-15 21:26:54 +01:00
@zipFile.file.truncate("file1", 100)
}
end
def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"])
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT) {
2005-01-15 21:26:54 +01:00
@zipFile.file.send(operation, *args)
}
end
def test_ftype
assert_e_n_o_e_n_t(:ftype)
2005-04-10 18:55:44 +02:00
assert_equal("file", @zipFile.file.ftype("file1"))
assert_equal("directory", @zipFile.file.ftype("dir1/dir11"))
assert_equal("directory", @zipFile.file.ftype("dir1/dir11/"))
2005-01-15 21:26:54 +01:00
end
def test_link
2005-04-10 18:55:44 +02:00
assert_raise(NotImplementedError) {
2005-01-15 21:26:54 +01:00
@zipFile.file.link("file1", "someOtherString")
}
end
def test_directory?
assert(! @zipFile.file.directory?("notAFile"))
assert(! @zipFile.file.directory?("file1"))
assert(! @zipFile.file.directory?("dir1/file11"))
assert(@zipFile.file.directory?("dir1"))
assert(@zipFile.file.directory?("dir1/"))
assert(@zipFile.file.directory?("dir2/dir21"))
assert(! @zipFile.file.stat("file1").directory?)
assert(! @zipFile.file.stat("dir1/file11").directory?)
assert(@zipFile.file.stat("dir1").directory?)
assert(@zipFile.file.stat("dir1/").directory?)
assert(@zipFile.file.stat("dir2/dir21").directory?)
end
def test_chown
2005-04-10 18:55:44 +02:00
assert_equal(2, @zipFile.file.chown(1,2, "dir1", "file1"))
assert_equal(1, @zipFile.file.stat("dir1").uid)
assert_equal(2, @zipFile.file.stat("dir1").gid)
assert_equal(2, @zipFile.file.chown(nil, nil, "dir1", "file1"))
2005-01-15 21:26:54 +01:00
end
def test_zero?
assert(! @zipFile.file.zero?("notAFile"))
assert(! @zipFile.file.zero?("file1"))
assert(@zipFile.file.zero?("dir1"))
blockCalled = false
2005-04-10 18:55:44 +02:00
ZipFile.open("data/generated/4entry.zip") {
2005-01-15 21:26:54 +01:00
|zf|
blockCalled = true
2005-04-10 18:55:44 +02:00
assert(zf.file.zero?("data/generated/empty.txt"))
2005-01-15 21:26:54 +01:00
}
assert(blockCalled)
assert(! @zipFile.file.stat("file1").zero?)
assert(@zipFile.file.stat("dir1").zero?)
blockCalled = false
2005-04-10 18:55:44 +02:00
ZipFile.open("data/generated/4entry.zip") {
2005-01-15 21:26:54 +01:00
|zf|
blockCalled = true
2005-04-10 18:55:44 +02:00
assert(zf.file.stat("data/generated/empty.txt").zero?)
2005-01-15 21:26:54 +01:00
}
assert(blockCalled)
end
def test_expand_path
2005-04-10 18:55:44 +02:00
ZipFile.open("data/zipWithDirs.zip") {
2005-01-15 21:26:54 +01:00
|zf|
2005-04-10 18:55:44 +02:00
assert_equal("/", zf.file.expand_path("."))
2005-01-15 21:26:54 +01:00
zf.dir.chdir "dir1"
2005-04-10 18:55:44 +02:00
assert_equal("/dir1", zf.file.expand_path("."))
assert_equal("/dir1/file12", zf.file.expand_path("file12"))
assert_equal("/", zf.file.expand_path(".."))
assert_equal("/dir2/dir21", zf.file.expand_path("../dir2/dir21"))
2005-01-15 21:26:54 +01:00
}
end
def test_mtime
2005-04-10 18:55:44 +02:00
assert_equal(Time.at(1027694306),
2005-01-15 21:26:54 +01:00
@zipFile.file.mtime("dir2/file21"))
2005-04-10 18:55:44 +02:00
assert_equal(Time.at(1027690863),
2005-01-15 21:26:54 +01:00
@zipFile.file.mtime("dir2/dir21"))
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT) {
2005-01-15 21:26:54 +01:00
@zipFile.file.mtime("noSuchEntry")
}
2005-04-10 18:55:44 +02:00
assert_equal(Time.at(1027694306),
2005-01-15 21:26:54 +01:00
@zipFile.file.stat("dir2/file21").mtime)
2005-04-10 18:55:44 +02:00
assert_equal(Time.at(1027690863),
2005-01-15 21:26:54 +01:00
@zipFile.file.stat("dir2/dir21").mtime)
end
def test_ctime
assert_nil(@zipFile.file.ctime("file1"))
assert_nil(@zipFile.file.stat("file1").ctime)
end
def test_atime
assert_nil(@zipFile.file.atime("file1"))
assert_nil(@zipFile.file.stat("file1").atime)
end
def test_readable?
assert(! @zipFile.file.readable?("noSuchFile"))
assert(@zipFile.file.readable?("file1"))
assert(@zipFile.file.readable?("dir1"))
assert(@zipFile.file.stat("file1").readable?)
assert(@zipFile.file.stat("dir1").readable?)
end
def test_readable_real?
assert(! @zipFile.file.readable_real?("noSuchFile"))
assert(@zipFile.file.readable_real?("file1"))
assert(@zipFile.file.readable_real?("dir1"))
assert(@zipFile.file.stat("file1").readable_real?)
assert(@zipFile.file.stat("dir1").readable_real?)
end
def test_writable?
assert(! @zipFile.file.writable?("noSuchFile"))
assert(@zipFile.file.writable?("file1"))
assert(@zipFile.file.writable?("dir1"))
assert(@zipFile.file.stat("file1").writable?)
assert(@zipFile.file.stat("dir1").writable?)
end
def test_writable_real?
assert(! @zipFile.file.writable_real?("noSuchFile"))
assert(@zipFile.file.writable_real?("file1"))
assert(@zipFile.file.writable_real?("dir1"))
assert(@zipFile.file.stat("file1").writable_real?)
assert(@zipFile.file.stat("dir1").writable_real?)
end
def test_executable?
assert(! @zipFile.file.executable?("noSuchFile"))
assert(! @zipFile.file.executable?("file1"))
assert(@zipFile.file.executable?("dir1"))
assert(! @zipFile.file.stat("file1").executable?)
assert(@zipFile.file.stat("dir1").executable?)
end
def test_executable_real?
assert(! @zipFile.file.executable_real?("noSuchFile"))
assert(! @zipFile.file.executable_real?("file1"))
assert(@zipFile.file.executable_real?("dir1"))
assert(! @zipFile.file.stat("file1").executable_real?)
assert(@zipFile.file.stat("dir1").executable_real?)
end
def test_owned?
assert_true_if_entry_exists(:owned?)
end
def test_grpowned?
assert_true_if_entry_exists(:grpowned?)
end
def test_setgid?
assert_always_false(:setgid?)
end
def test_setuid?
assert_always_false(:setgid?)
end
def test_sticky?
assert_always_false(:sticky?)
end
def test_readlink
2005-04-10 18:55:44 +02:00
assert_raise(NotImplementedError) {
2005-01-15 21:26:54 +01:00
@zipFile.file.readlink("someString")
}
end
def test_stat
s = @zipFile.file.stat("file1")
assert(s.kind_of?(File::Stat)) # It pretends
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
2005-01-15 21:26:54 +01:00
@zipFile.file.stat("noSuchFile")
}
end
def test_lstat
assert(@zipFile.file.lstat("file1").file?)
end
def test_chmod
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
2005-01-15 21:26:54 +01:00
@zipFile.file.chmod(0644, "file1", "NoSuchFile")
}
2005-04-10 18:55:44 +02:00
assert_equal(2, @zipFile.file.chmod(0644, "file1", "dir1"))
2005-01-15 21:26:54 +01:00
end
def test_pipe
2005-04-10 18:55:44 +02:00
assert_raise(NotImplementedError) {
2005-01-15 21:26:54 +01:00
@zipFile.file.pipe
}
end
def test_foreach
2005-04-10 18:55:44 +02:00
ZipFile.open("data/generated/zipWithDir.zip") {
2005-01-15 21:26:54 +01:00
|zf|
ref = []
2005-04-10 18:55:44 +02:00
File.foreach("data/file1.txt") { |e| ref << e }
2005-01-15 21:26:54 +01:00
index = 0
2005-04-10 18:55:44 +02:00
zf.file.foreach("data/file1.txt") {
2005-01-15 21:26:54 +01:00
|l|
2005-04-10 18:55:44 +02:00
assert_equal(ref[index], l)
2005-01-15 21:26:54 +01:00
index = index.next
}
2005-04-10 18:55:44 +02:00
assert_equal(ref.size, index)
2005-01-15 21:26:54 +01:00
}
2005-04-10 18:55:44 +02:00
ZipFile.open("data/generated/zipWithDir.zip") {
2005-01-15 21:26:54 +01:00
|zf|
ref = []
2005-04-10 18:55:44 +02:00
File.foreach("data/file1.txt", " ") { |e| ref << e }
2005-01-15 21:26:54 +01:00
index = 0
2005-04-10 18:55:44 +02:00
zf.file.foreach("data/file1.txt", " ") {
2005-01-15 21:26:54 +01:00
|l|
2005-04-10 18:55:44 +02:00
assert_equal(ref[index], l)
2005-01-15 21:26:54 +01:00
index = index.next
}
2005-04-10 18:55:44 +02:00
assert_equal(ref.size, index)
2005-01-15 21:26:54 +01:00
}
end
def test_popen
2005-04-10 18:55:44 +02:00
assert_equal(File.popen("ls") { |f| f.read },
2005-01-15 21:26:54 +01:00
@zipFile.file.popen("ls") { |f| f.read })
end
# Can be added later
# def test_select
# fail "implement test"
# end
def test_readlines
2005-04-10 18:55:44 +02:00
ZipFile.open("data/generated/zipWithDir.zip") {
2005-01-15 21:26:54 +01:00
|zf|
2005-04-10 18:55:44 +02:00
assert_equal(File.readlines("data/file1.txt"),
zf.file.readlines("data/file1.txt"))
2005-01-15 21:26:54 +01:00
}
end
def test_read
2005-04-10 18:55:44 +02:00
ZipFile.open("data/generated/zipWithDir.zip") {
2005-01-15 21:26:54 +01:00
|zf|
2005-04-10 18:55:44 +02:00
assert_equal(File.read("data/file1.txt"),
zf.file.read("data/file1.txt"))
2005-01-15 21:26:54 +01:00
}
end
end
2005-04-10 18:55:44 +02:00
class ZipFsFileStatTest < Test::Unit::TestCase
2005-01-15 21:26:54 +01:00
def setup
2005-04-10 18:55:44 +02:00
@zipFile = ZipFile.new("data/zipWithDirs.zip")
2005-01-15 21:26:54 +01:00
end
def teardown
@zipFile.close if @zipFile
end
def test_blocks
2005-04-10 18:55:44 +02:00
assert_equal(nil, @zipFile.file.stat("file1").blocks)
2005-01-15 21:26:54 +01:00
end
def test_ino
2005-04-10 18:55:44 +02:00
assert_equal(0, @zipFile.file.stat("file1").ino)
2005-01-15 21:26:54 +01:00
end
def test_uid
2005-04-10 18:55:44 +02:00
assert_equal(0, @zipFile.file.stat("file1").uid)
2005-01-15 21:26:54 +01:00
end
def test_gid
2005-04-10 18:55:44 +02:00
assert_equal(0, @zipFile.file.stat("file1").gid)
2005-01-15 21:26:54 +01:00
end
def test_ftype
2005-04-10 18:55:44 +02:00
assert_equal("file", @zipFile.file.stat("file1").ftype)
assert_equal("directory", @zipFile.file.stat("dir1").ftype)
2005-01-15 21:26:54 +01:00
end
def test_mode
2005-04-10 18:55:44 +02:00
assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
2005-01-15 21:26:54 +01:00
end
def test_dev
2005-04-10 18:55:44 +02:00
assert_equal(0, @zipFile.file.stat("file1").dev)
2005-01-15 21:26:54 +01:00
end
def test_rdev
2005-04-10 18:55:44 +02:00
assert_equal(0, @zipFile.file.stat("file1").rdev)
2005-01-15 21:26:54 +01:00
end
def test_rdev_major
2005-04-10 18:55:44 +02:00
assert_equal(0, @zipFile.file.stat("file1").rdev_major)
2005-01-15 21:26:54 +01:00
end
def test_rdev_minor
2005-04-10 18:55:44 +02:00
assert_equal(0, @zipFile.file.stat("file1").rdev_minor)
2005-01-15 21:26:54 +01:00
end
def test_nlink
2005-04-10 18:55:44 +02:00
assert_equal(1, @zipFile.file.stat("file1").nlink)
2005-01-15 21:26:54 +01:00
end
def test_blksize
assert_nil(@zipFile.file.stat("file1").blksize)
end
end
2005-04-10 18:55:44 +02:00
class ZipFsFileMutatingTest < Test::Unit::TestCase
2005-01-15 21:26:54 +01:00
TEST_ZIP = "zipWithDirs_copy.zip"
def setup
2005-04-10 18:55:44 +02:00
File.copy("data/zipWithDirs.zip", TEST_ZIP)
2005-01-15 21:26:54 +01:00
end
def teardown
end
def test_delete
do_test_delete_or_unlink(:delete)
end
def test_unlink
do_test_delete_or_unlink(:unlink)
end
def test_open_write
ZipFile.open(TEST_ZIP) {
|zf|
zf.file.open("test_open_write_entry", "w") {
|f|
blockCalled = true
f.write "This is what I'm writing"
}
2005-04-10 18:55:44 +02:00
assert_equal("This is what I'm writing",
2005-01-15 21:26:54 +01:00
zf.file.read("test_open_write_entry"))
# Test with existing entry
zf.file.open("file1", "w") {
|f|
blockCalled = true
f.write "This is what I'm writing too"
}
2005-04-10 18:55:44 +02:00
assert_equal("This is what I'm writing too",
2005-01-15 21:26:54 +01:00
zf.file.read("file1"))
}
end
def test_rename
ZipFile.open(TEST_ZIP) {
|zf|
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT, "") {
2005-01-15 21:26:54 +01:00
zf.file.rename("NoSuchFile", "bimse")
}
zf.file.rename("file1", "newNameForFile1")
}
ZipFile.open(TEST_ZIP) {
|zf|
assert(! zf.file.exists?("file1"))
assert(zf.file.exists?("newNameForFile1"))
}
end
def do_test_delete_or_unlink(symbol)
ZipFile.open(TEST_ZIP) {
|zf|
assert(zf.file.exists?("dir2/dir21/dir221/file2221"))
zf.file.send(symbol, "dir2/dir21/dir221/file2221")
assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
assert(zf.file.exists?("dir1/file11"))
assert(zf.file.exists?("dir1/file12"))
zf.file.send(symbol, "dir1/file11", "dir1/file12")
assert(! zf.file.exists?("dir1/file11"))
assert(! zf.file.exists?("dir1/file12"))
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") }
assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") }
assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") }
2005-01-15 21:26:54 +01:00
}
ZipFile.open(TEST_ZIP) {
|zf|
assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
assert(! zf.file.exists?("dir1/file11"))
assert(! zf.file.exists?("dir1/file12"))
assert(zf.file.exists?("dir1/dir11"))
assert(zf.file.exists?("dir1/dir11/"))
}
end
end
2005-04-10 18:55:44 +02:00
class ZipFsDirectoryTest < Test::Unit::TestCase
2005-01-15 21:26:54 +01:00
TEST_ZIP = "zipWithDirs_copy.zip"
def setup
2005-04-10 18:55:44 +02:00
File.copy("data/zipWithDirs.zip", TEST_ZIP)
2005-01-15 21:26:54 +01:00
end
def test_delete
ZipFile.open(TEST_ZIP) {
|zf|
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") {
2005-01-15 21:26:54 +01:00
zf.dir.delete("NoSuchFile.txt")
}
2005-04-10 18:55:44 +02:00
assert_raise(Errno::EINVAL, "Invalid argument - file1") {
2005-01-15 21:26:54 +01:00
zf.dir.delete("file1")
}
assert(zf.file.exists?("dir1"))
zf.dir.delete("dir1")
assert(! zf.file.exists?("dir1"))
}
end
def test_mkdir
ZipFile.open(TEST_ZIP) {
|zf|
2005-04-10 18:55:44 +02:00
assert_raise(Errno::EEXIST, "File exists - dir1") {
2005-01-15 21:26:54 +01:00
zf.dir.mkdir("file1")
}
2005-04-10 18:55:44 +02:00
assert_raise(Errno::EEXIST, "File exists - dir1") {
2005-01-15 21:26:54 +01:00
zf.dir.mkdir("dir1")
}
assert(!zf.file.exists?("newDir"))
zf.dir.mkdir("newDir")
assert(zf.file.directory?("newDir"))
assert(!zf.file.exists?("newDir2"))
zf.dir.mkdir("newDir2", 3485)
assert(zf.file.directory?("newDir2"))
}
end
def test_pwd_chdir_entries
ZipFile.open(TEST_ZIP) {
|zf|
2005-04-10 18:55:44 +02:00
assert_equal("/", zf.dir.pwd)
2005-01-15 21:26:54 +01:00
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT, "No such file or directory - no such dir") {
2005-01-15 21:26:54 +01:00
zf.dir.chdir "no such dir"
}
2005-04-10 18:55:44 +02:00
assert_raise(Errno::EINVAL, "Invalid argument - file1") {
2005-01-15 21:26:54 +01:00
zf.dir.chdir "file1"
}
2005-04-10 18:55:44 +02:00
assert_equal(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort)
2005-01-15 21:26:54 +01:00
zf.dir.chdir "dir1"
2005-04-10 18:55:44 +02:00
assert_equal("/dir1", zf.dir.pwd)
assert_equal(["dir11", "file11", "file12"], zf.dir.entries(".").sort)
2005-01-15 21:26:54 +01:00
zf.dir.chdir "../dir2/dir21"
2005-04-10 18:55:44 +02:00
assert_equal("/dir2/dir21", zf.dir.pwd)
assert_equal(["dir221"].sort, zf.dir.entries(".").sort)
2005-01-15 21:26:54 +01:00
}
end
def test_foreach
ZipFile.open(TEST_ZIP) {
|zf|
blockCalled = false
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT, "No such file or directory - noSuchDir") {
2005-01-15 21:26:54 +01:00
zf.dir.foreach("noSuchDir") { |e| blockCalled = true }
}
assert(! blockCalled)
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
2005-01-15 21:26:54 +01:00
zf.dir.foreach("file1") { |e| blockCalled = true }
}
assert(! blockCalled)
entries = []
zf.dir.foreach(".") { |e| entries << e }
2005-04-10 18:55:44 +02:00
assert_equal(["dir1", "dir2", "file1"].sort, entries.sort)
2005-01-15 21:26:54 +01:00
entries = []
zf.dir.foreach("dir1") { |e| entries << e }
2005-04-10 18:55:44 +02:00
assert_equal(["dir11", "file11", "file12"], entries.sort)
2005-01-15 21:26:54 +01:00
}
end
def test_chroot
ZipFile.open(TEST_ZIP) {
|zf|
2005-04-10 18:55:44 +02:00
assert_raise(NotImplementedError) {
2005-01-15 21:26:54 +01:00
zf.dir.chroot
}
}
end
# Globbing not supported yet
#def test_glob
# # test alias []-operator too
# fail "implement test"
#end
def test_open_new
ZipFile.open(TEST_ZIP) {
|zf|
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
2005-01-15 21:26:54 +01:00
zf.dir.new("file1")
}
2005-04-10 18:55:44 +02:00
assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
2005-01-15 21:26:54 +01:00
zf.dir.new("noSuchFile")
}
d = zf.dir.new(".")
2005-04-10 18:55:44 +02:00
assert_equal(["file1", "dir1", "dir2"].sort, d.entries.sort)
2005-01-15 21:26:54 +01:00
d.close
zf.dir.open("dir1") {
|d|
2005-04-10 18:55:44 +02:00
assert_equal(["dir11", "file11", "file12"].sort, d.entries.sort)
2005-01-15 21:26:54 +01:00
}
}
end
end
2005-04-10 18:55:44 +02:00
class ZipFsDirIteratorTest < Test::Unit::TestCase
2005-01-15 21:26:54 +01:00
FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]
def setup
@dirIt = ZipFileSystem::ZipFsDirIterator.new(FILENAME_ARRAY)
end
def test_close
@dirIt.close
2005-04-10 18:55:44 +02:00
assert_raise(IOError, "closed directory") {
2005-01-15 21:26:54 +01:00
@dirIt.each { |e| p e }
}
2005-04-10 18:55:44 +02:00
assert_raise(IOError, "closed directory") {
2005-01-15 21:26:54 +01:00
@dirIt.read
}
2005-04-10 18:55:44 +02:00
assert_raise(IOError, "closed directory") {
2005-01-15 21:26:54 +01:00
@dirIt.rewind
}
2005-04-10 18:55:44 +02:00
assert_raise(IOError, "closed directory") {
2005-01-15 21:26:54 +01:00
@dirIt.seek(0)
}
2005-04-10 18:55:44 +02:00
assert_raise(IOError, "closed directory") {
2005-01-15 21:26:54 +01:00
@dirIt.tell
}
end
def test_each
# Tested through Enumerable.entries
2005-04-10 18:55:44 +02:00
assert_equal(FILENAME_ARRAY, @dirIt.entries)
2005-01-15 21:26:54 +01:00
end
def test_read
FILENAME_ARRAY.size.times {
|i|
2005-04-10 18:55:44 +02:00
assert_equal(FILENAME_ARRAY[i], @dirIt.read)
2005-01-15 21:26:54 +01:00
}
end
def test_rewind
@dirIt.read
@dirIt.read
2005-04-10 18:55:44 +02:00
assert_equal(FILENAME_ARRAY[2], @dirIt.read)
2005-01-15 21:26:54 +01:00
@dirIt.rewind
2005-04-10 18:55:44 +02:00
assert_equal(FILENAME_ARRAY[0], @dirIt.read)
2005-01-15 21:26:54 +01:00
end
def test_tell_seek
@dirIt.read
@dirIt.read
pos = @dirIt.tell
valAtPos = @dirIt.read
@dirIt.read
@dirIt.seek(pos)
2005-04-10 18:55:44 +02:00
assert_equal(valAtPos, @dirIt.read)
2005-01-15 21:26:54 +01:00
end
end
# Copyright (C) 2002, 2003 Thomas Sondergaard
# rubyzip is free software; you can redistribute it and/or
# modify it under the terms of the ruby license.