rubyzip 1.1.4 → 1.1.5
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubyzip might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/lib/zip/file.rb +2 -1
- data/lib/zip/inflater.rb +15 -44
- data/lib/zip/ioextras/abstract_input_stream.rb +5 -1
- data/lib/zip/version.rb +1 -1
- data/test/alltests.rb +18 -0
- data/test/basic_zip_file_test.rb +64 -0
- data/test/central_directory_entry_test.rb +73 -0
- data/test/central_directory_test.rb +100 -0
- data/test/data/file1.txt +46 -0
- data/test/data/file1.txt.deflatedData +0 -0
- data/test/data/file2.txt +1504 -0
- data/test/data/file2.txt.other +0 -0
- data/test/data/globTest.zip +0 -0
- data/test/data/globTest/foo.txt +0 -0
- data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
- data/test/data/globTest/food.txt +0 -0
- data/test/data/mimetype +1 -0
- data/test/data/notzippedruby.rb +7 -0
- data/test/data/rubycode.zip +0 -0
- data/test/data/rubycode2.zip +0 -0
- data/test/data/testDirectory.bin +0 -0
- data/test/data/zip64-sample.zip +0 -0
- data/test/data/zipWithDirs.zip +0 -0
- data/test/deflater_test.rb +62 -0
- data/test/dummy.txt +1 -0
- data/test/entry_set_test.rb +125 -0
- data/test/entry_test.rb +165 -0
- data/test/errors_test.rb +36 -0
- data/test/extra_field_test.rb +69 -0
- data/test/file_extract_directory_test.rb +55 -0
- data/test/file_extract_test.rb +90 -0
- data/test/file_split_test.rb +60 -0
- data/test/file_test.rb +568 -0
- data/test/filesystem/dir_iterator_test.rb +62 -0
- data/test/filesystem/directory_test.rb +131 -0
- data/test/filesystem/file_mutating_test.rb +100 -0
- data/test/filesystem/file_nonmutating_test.rb +505 -0
- data/test/filesystem/file_stat_test.rb +66 -0
- data/test/gentestfiles.rb +134 -0
- data/test/inflater_test.rb +14 -0
- data/test/input_stream_test.rb +170 -0
- data/test/ioextras/abstract_input_stream_test.rb +103 -0
- data/test/ioextras/abstract_output_stream_test.rb +106 -0
- data/test/ioextras/fake_io_test.rb +18 -0
- data/test/ioextrastest.rb +233 -0
- data/test/local_entry_test.rb +153 -0
- data/test/odt_test_fix.rb +30 -0
- data/test/output_stream_test.rb +114 -0
- data/test/pass_thru_compressor_test.rb +31 -0
- data/test/pass_thru_decompressor_test.rb +15 -0
- data/test/sample.odt +0 -0
- data/test/settings_test.rb +71 -0
- data/test/test_helper.rb +228 -0
- data/test/unicode_file_names_and_comments_test.rb +40 -0
- data/test/zip64_full_test.rb +49 -0
- data/test/zip64_support_test.rb +15 -0
- metadata +107 -3
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'zip/filesystem'
|
3
|
+
|
4
|
+
class ZipFsDirIteratorTest < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@dirIt = ::Zip::FileSystem::ZipFsDirIterator.new(FILENAME_ARRAY)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_close
|
13
|
+
@dirIt.close
|
14
|
+
assert_raises(IOError, "closed directory") {
|
15
|
+
@dirIt.each { |e| p e }
|
16
|
+
}
|
17
|
+
assert_raises(IOError, "closed directory") {
|
18
|
+
@dirIt.read
|
19
|
+
}
|
20
|
+
assert_raises(IOError, "closed directory") {
|
21
|
+
@dirIt.rewind
|
22
|
+
}
|
23
|
+
assert_raises(IOError, "closed directory") {
|
24
|
+
@dirIt.seek(0)
|
25
|
+
}
|
26
|
+
assert_raises(IOError, "closed directory") {
|
27
|
+
@dirIt.tell
|
28
|
+
}
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_each
|
33
|
+
# Tested through Enumerable.entries
|
34
|
+
assert_equal(FILENAME_ARRAY, @dirIt.entries)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_read
|
38
|
+
FILENAME_ARRAY.size.times {
|
39
|
+
|i|
|
40
|
+
assert_equal(FILENAME_ARRAY[i], @dirIt.read)
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_rewind
|
45
|
+
@dirIt.read
|
46
|
+
@dirIt.read
|
47
|
+
assert_equal(FILENAME_ARRAY[2], @dirIt.read)
|
48
|
+
@dirIt.rewind
|
49
|
+
assert_equal(FILENAME_ARRAY[0], @dirIt.read)
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_tell_seek
|
53
|
+
@dirIt.read
|
54
|
+
@dirIt.read
|
55
|
+
pos = @dirIt.tell
|
56
|
+
valAtPos = @dirIt.read
|
57
|
+
@dirIt.read
|
58
|
+
@dirIt.seek(pos)
|
59
|
+
assert_equal(valAtPos, @dirIt.read)
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'zip/filesystem'
|
3
|
+
|
4
|
+
class ZipFsDirectoryTest < MiniTest::Unit::TestCase
|
5
|
+
TEST_ZIP = "test/data/generated/zipWithDirs_copy.zip"
|
6
|
+
|
7
|
+
def setup
|
8
|
+
FileUtils.cp("test/data/zipWithDirs.zip", TEST_ZIP)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_delete
|
12
|
+
::Zip::File.open(TEST_ZIP) {
|
13
|
+
|zf|
|
14
|
+
assert_raises(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") {
|
15
|
+
zf.dir.delete("NoSuchFile.txt")
|
16
|
+
}
|
17
|
+
assert_raises(Errno::EINVAL, "Invalid argument - file1") {
|
18
|
+
zf.dir.delete("file1")
|
19
|
+
}
|
20
|
+
assert(zf.file.exists?("dir1"))
|
21
|
+
zf.dir.delete("dir1")
|
22
|
+
assert(! zf.file.exists?("dir1"))
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_mkdir
|
27
|
+
::Zip::File.open(TEST_ZIP) {
|
28
|
+
|zf|
|
29
|
+
assert_raises(Errno::EEXIST, "File exists - dir1") {
|
30
|
+
zf.dir.mkdir("file1")
|
31
|
+
}
|
32
|
+
assert_raises(Errno::EEXIST, "File exists - dir1") {
|
33
|
+
zf.dir.mkdir("dir1")
|
34
|
+
}
|
35
|
+
assert(!zf.file.exists?("newDir"))
|
36
|
+
zf.dir.mkdir("newDir")
|
37
|
+
assert(zf.file.directory?("newDir"))
|
38
|
+
assert(!zf.file.exists?("newDir2"))
|
39
|
+
zf.dir.mkdir("newDir2", 3485)
|
40
|
+
assert(zf.file.directory?("newDir2"))
|
41
|
+
}
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_pwd_chdir_entries
|
45
|
+
::Zip::File.open(TEST_ZIP) {
|
46
|
+
|zf|
|
47
|
+
assert_equal("/", zf.dir.pwd)
|
48
|
+
|
49
|
+
assert_raises(Errno::ENOENT, "No such file or directory - no such dir") {
|
50
|
+
zf.dir.chdir "no such dir"
|
51
|
+
}
|
52
|
+
|
53
|
+
assert_raises(Errno::EINVAL, "Invalid argument - file1") {
|
54
|
+
zf.dir.chdir "file1"
|
55
|
+
}
|
56
|
+
|
57
|
+
assert_equal(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort)
|
58
|
+
zf.dir.chdir "dir1"
|
59
|
+
assert_equal("/dir1", zf.dir.pwd)
|
60
|
+
assert_equal(["dir11", "file11", "file12"], zf.dir.entries(".").sort)
|
61
|
+
|
62
|
+
zf.dir.chdir "../dir2/dir21"
|
63
|
+
assert_equal("/dir2/dir21", zf.dir.pwd)
|
64
|
+
assert_equal(["dir221"].sort, zf.dir.entries(".").sort)
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_foreach
|
69
|
+
::Zip::File.open(TEST_ZIP) {
|
70
|
+
|zf|
|
71
|
+
|
72
|
+
blockCalled = false
|
73
|
+
assert_raises(Errno::ENOENT, "No such file or directory - noSuchDir") {
|
74
|
+
zf.dir.foreach("noSuchDir") { |e| blockCalled = true }
|
75
|
+
}
|
76
|
+
assert(! blockCalled)
|
77
|
+
|
78
|
+
assert_raises(Errno::ENOTDIR, "Not a directory - file1") {
|
79
|
+
zf.dir.foreach("file1") { |e| blockCalled = true }
|
80
|
+
}
|
81
|
+
assert(! blockCalled)
|
82
|
+
|
83
|
+
entries = []
|
84
|
+
zf.dir.foreach(".") { |e| entries << e }
|
85
|
+
assert_equal(["dir1", "dir2", "file1"].sort, entries.sort)
|
86
|
+
|
87
|
+
entries = []
|
88
|
+
zf.dir.foreach("dir1") { |e| entries << e }
|
89
|
+
assert_equal(["dir11", "file11", "file12"], entries.sort)
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_chroot
|
94
|
+
::Zip::File.open(TEST_ZIP) {
|
95
|
+
|zf|
|
96
|
+
assert_raises(NotImplementedError) {
|
97
|
+
zf.dir.chroot
|
98
|
+
}
|
99
|
+
}
|
100
|
+
end
|
101
|
+
|
102
|
+
# Globbing not supported yet
|
103
|
+
#def test_glob
|
104
|
+
# # test alias []-operator too
|
105
|
+
# fail "implement test"
|
106
|
+
#end
|
107
|
+
|
108
|
+
def test_open_new
|
109
|
+
::Zip::File.open(TEST_ZIP) {
|
110
|
+
|zf|
|
111
|
+
|
112
|
+
assert_raises(Errno::ENOTDIR, "Not a directory - file1") {
|
113
|
+
zf.dir.new("file1")
|
114
|
+
}
|
115
|
+
|
116
|
+
assert_raises(Errno::ENOENT, "No such file or directory - noSuchFile") {
|
117
|
+
zf.dir.new("noSuchFile")
|
118
|
+
}
|
119
|
+
|
120
|
+
d = zf.dir.new(".")
|
121
|
+
assert_equal(["file1", "dir1", "dir2"].sort, d.entries.sort)
|
122
|
+
d.close
|
123
|
+
|
124
|
+
zf.dir.open("dir1") {
|
125
|
+
|dir|
|
126
|
+
assert_equal(["dir11", "file11", "file12"].sort, dir.entries.sort)
|
127
|
+
}
|
128
|
+
}
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'zip/filesystem'
|
3
|
+
|
4
|
+
class ZipFsFileMutatingTest < MiniTest::Unit::TestCase
|
5
|
+
TEST_ZIP = "test/data/generated/zipWithDirs_copy.zip"
|
6
|
+
def setup
|
7
|
+
FileUtils.cp("test/data/zipWithDirs.zip", TEST_ZIP)
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_delete
|
14
|
+
do_test_delete_or_unlink(:delete)
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_unlink
|
18
|
+
do_test_delete_or_unlink(:unlink)
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_open_write
|
22
|
+
::Zip::File.open(TEST_ZIP) {
|
23
|
+
|zf|
|
24
|
+
|
25
|
+
zf.file.open("test_open_write_entry", "w") {
|
26
|
+
|f|
|
27
|
+
f.write "This is what I'm writing"
|
28
|
+
}
|
29
|
+
assert_equal("This is what I'm writing",
|
30
|
+
zf.file.read("test_open_write_entry"))
|
31
|
+
|
32
|
+
# Test with existing entry
|
33
|
+
zf.file.open("file1", "wb") { #also check that 'b' option is ignored
|
34
|
+
|f|
|
35
|
+
f.write "This is what I'm writing too"
|
36
|
+
}
|
37
|
+
assert_equal("This is what I'm writing too",
|
38
|
+
zf.file.read("file1"))
|
39
|
+
}
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_rename
|
43
|
+
::Zip::File.open(TEST_ZIP) {
|
44
|
+
|zf|
|
45
|
+
assert_raises(Errno::ENOENT, "") {
|
46
|
+
zf.file.rename("NoSuchFile", "bimse")
|
47
|
+
}
|
48
|
+
zf.file.rename("file1", "newNameForFile1")
|
49
|
+
}
|
50
|
+
|
51
|
+
::Zip::File.open(TEST_ZIP) {
|
52
|
+
|zf|
|
53
|
+
assert(! zf.file.exists?("file1"))
|
54
|
+
assert(zf.file.exists?("newNameForFile1"))
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_chmod
|
59
|
+
::Zip::File.open(TEST_ZIP) {
|
60
|
+
|zf|
|
61
|
+
|
62
|
+
zf.file.chmod(0765, "file1")
|
63
|
+
}
|
64
|
+
|
65
|
+
::Zip::File.open(TEST_ZIP) {
|
66
|
+
|zf|
|
67
|
+
assert_equal(0100765, zf.file.stat("file1").mode)
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
def do_test_delete_or_unlink(symbol)
|
72
|
+
::Zip::File.open(TEST_ZIP) {
|
73
|
+
|zf|
|
74
|
+
assert(zf.file.exists?("dir2/dir21/dir221/file2221"))
|
75
|
+
zf.file.send(symbol, "dir2/dir21/dir221/file2221")
|
76
|
+
assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
|
77
|
+
|
78
|
+
assert(zf.file.exists?("dir1/file11"))
|
79
|
+
assert(zf.file.exists?("dir1/file12"))
|
80
|
+
zf.file.send(symbol, "dir1/file11", "dir1/file12")
|
81
|
+
assert(! zf.file.exists?("dir1/file11"))
|
82
|
+
assert(! zf.file.exists?("dir1/file12"))
|
83
|
+
|
84
|
+
assert_raises(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") }
|
85
|
+
assert_raises(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") }
|
86
|
+
assert_raises(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") }
|
87
|
+
}
|
88
|
+
|
89
|
+
::Zip::File.open(TEST_ZIP) {
|
90
|
+
|zf|
|
91
|
+
assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
|
92
|
+
assert(! zf.file.exists?("dir1/file11"))
|
93
|
+
assert(! zf.file.exists?("dir1/file12"))
|
94
|
+
|
95
|
+
assert(zf.file.exists?("dir1/dir11"))
|
96
|
+
assert(zf.file.exists?("dir1/dir11/"))
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
@@ -0,0 +1,505 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'zip/filesystem'
|
3
|
+
|
4
|
+
class ZipFsFileNonmutatingTest < MiniTest::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
@zipsha = Digest::SHA1.file("test/data/zipWithDirs.zip")
|
7
|
+
@zip_file = ::Zip::File.new("test/data/zipWithDirs.zip")
|
8
|
+
end
|
9
|
+
|
10
|
+
def teardown
|
11
|
+
@zip_file.close if @zip_file
|
12
|
+
assert_equal(@zipsha, Digest::SHA1.file("test/data/zipWithDirs.zip"))
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_umask
|
16
|
+
assert_equal(::File.umask, @zip_file.file.umask)
|
17
|
+
@zip_file.file.umask(0006)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_exists?
|
21
|
+
assert(! @zip_file.file.exists?("notAFile"))
|
22
|
+
assert(@zip_file.file.exists?("file1"))
|
23
|
+
assert(@zip_file.file.exists?("dir1"))
|
24
|
+
assert(@zip_file.file.exists?("dir1/"))
|
25
|
+
assert(@zip_file.file.exists?("dir1/file12"))
|
26
|
+
assert(@zip_file.file.exist?("dir1/file12")) # notice, tests exist? alias of exists? !
|
27
|
+
|
28
|
+
@zip_file.dir.chdir "dir1/"
|
29
|
+
assert(!@zip_file.file.exists?("file1"))
|
30
|
+
assert(@zip_file.file.exists?("file12"))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_open_read
|
34
|
+
blockCalled = false
|
35
|
+
@zip_file.file.open("file1", "r") {
|
36
|
+
|f|
|
37
|
+
blockCalled = true
|
38
|
+
assert_equal("this is the entry 'file1' in my test archive!",
|
39
|
+
f.readline.chomp)
|
40
|
+
}
|
41
|
+
assert(blockCalled)
|
42
|
+
|
43
|
+
blockCalled = false
|
44
|
+
@zip_file.file.open("file1", "rb") { # test binary flag is ignored
|
45
|
+
|f|
|
46
|
+
blockCalled = true
|
47
|
+
assert_equal("this is the entry 'file1' in my test archive!",
|
48
|
+
f.readline.chomp)
|
49
|
+
}
|
50
|
+
assert(blockCalled)
|
51
|
+
|
52
|
+
blockCalled = false
|
53
|
+
@zip_file.dir.chdir "dir2"
|
54
|
+
@zip_file.file.open("file21", "r") {
|
55
|
+
|f|
|
56
|
+
blockCalled = true
|
57
|
+
assert_equal("this is the entry 'dir2/file21' in my test archive!",
|
58
|
+
f.readline.chomp)
|
59
|
+
}
|
60
|
+
assert(blockCalled)
|
61
|
+
@zip_file.dir.chdir "/"
|
62
|
+
|
63
|
+
assert_raises(Errno::ENOENT) {
|
64
|
+
@zip_file.file.open("noSuchEntry")
|
65
|
+
}
|
66
|
+
|
67
|
+
begin
|
68
|
+
is = @zip_file.file.open("file1")
|
69
|
+
assert_equal("this is the entry 'file1' in my test archive!",
|
70
|
+
is.readline.chomp)
|
71
|
+
ensure
|
72
|
+
is.close if is
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_new
|
77
|
+
begin
|
78
|
+
is = @zip_file.file.new("file1")
|
79
|
+
assert_equal("this is the entry 'file1' in my test archive!",
|
80
|
+
is.readline.chomp)
|
81
|
+
ensure
|
82
|
+
is.close if is
|
83
|
+
end
|
84
|
+
begin
|
85
|
+
is = @zip_file.file.new("file1") {
|
86
|
+
fail "should not call block"
|
87
|
+
}
|
88
|
+
ensure
|
89
|
+
is.close if is
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_symlink
|
94
|
+
assert_raises(NotImplementedError) {
|
95
|
+
@zip_file.file.symlink("file1", "aSymlink")
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_size
|
100
|
+
assert_raises(Errno::ENOENT) { @zip_file.file.size("notAFile") }
|
101
|
+
assert_equal(72, @zip_file.file.size("file1"))
|
102
|
+
assert_equal(0, @zip_file.file.size("dir2/dir21"))
|
103
|
+
|
104
|
+
assert_equal(72, @zip_file.file.stat("file1").size)
|
105
|
+
assert_equal(0, @zip_file.file.stat("dir2/dir21").size)
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_size?
|
109
|
+
assert_equal(nil, @zip_file.file.size?("notAFile"))
|
110
|
+
assert_equal(72, @zip_file.file.size?("file1"))
|
111
|
+
assert_equal(nil, @zip_file.file.size?("dir2/dir21"))
|
112
|
+
|
113
|
+
assert_equal(72, @zip_file.file.stat("file1").size?)
|
114
|
+
assert_equal(nil, @zip_file.file.stat("dir2/dir21").size?)
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
def test_file?
|
119
|
+
assert(@zip_file.file.file?("file1"))
|
120
|
+
assert(@zip_file.file.file?("dir2/file21"))
|
121
|
+
assert(! @zip_file.file.file?("dir1"))
|
122
|
+
assert(! @zip_file.file.file?("dir1/dir11"))
|
123
|
+
|
124
|
+
assert(@zip_file.file.stat("file1").file?)
|
125
|
+
assert(@zip_file.file.stat("dir2/file21").file?)
|
126
|
+
assert(! @zip_file.file.stat("dir1").file?)
|
127
|
+
assert(! @zip_file.file.stat("dir1/dir11").file?)
|
128
|
+
end
|
129
|
+
|
130
|
+
include ExtraAssertions
|
131
|
+
|
132
|
+
def test_dirname
|
133
|
+
assert_forwarded(File, :dirname, "retVal", "a/b/c/d") {
|
134
|
+
@zip_file.file.dirname("a/b/c/d")
|
135
|
+
}
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_basename
|
139
|
+
assert_forwarded(File, :basename, "retVal", "a/b/c/d") {
|
140
|
+
@zip_file.file.basename("a/b/c/d")
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
def test_split
|
145
|
+
assert_forwarded(File, :split, "retVal", "a/b/c/d") {
|
146
|
+
@zip_file.file.split("a/b/c/d")
|
147
|
+
}
|
148
|
+
end
|
149
|
+
|
150
|
+
def test_join
|
151
|
+
assert_equal("a/b/c", @zip_file.file.join("a/b", "c"))
|
152
|
+
assert_equal("a/b/c/d", @zip_file.file.join("a/b", "c/d"))
|
153
|
+
assert_equal("/c/d", @zip_file.file.join("", "c/d"))
|
154
|
+
assert_equal("a/b/c/d", @zip_file.file.join("a", "b", "c", "d"))
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_utime
|
158
|
+
t_now = ::Zip::DOSTime.now
|
159
|
+
t_bak = @zip_file.file.mtime("file1")
|
160
|
+
@zip_file.file.utime(t_now, "file1")
|
161
|
+
assert_equal(t_now, @zip_file.file.mtime("file1"))
|
162
|
+
@zip_file.file.utime(t_bak, "file1")
|
163
|
+
assert_equal(t_bak, @zip_file.file.mtime("file1"))
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
def assert_always_false(operation)
|
168
|
+
assert(! @zip_file.file.send(operation, "noSuchFile"))
|
169
|
+
assert(! @zip_file.file.send(operation, "file1"))
|
170
|
+
assert(! @zip_file.file.send(operation, "dir1"))
|
171
|
+
assert(! @zip_file.file.stat("file1").send(operation))
|
172
|
+
assert(! @zip_file.file.stat("dir1").send(operation))
|
173
|
+
end
|
174
|
+
|
175
|
+
def assert_true_if_entry_exists(operation)
|
176
|
+
assert(! @zip_file.file.send(operation, "noSuchFile"))
|
177
|
+
assert(@zip_file.file.send(operation, "file1"))
|
178
|
+
assert(@zip_file.file.send(operation, "dir1"))
|
179
|
+
assert(@zip_file.file.stat("file1").send(operation))
|
180
|
+
assert(@zip_file.file.stat("dir1").send(operation))
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_pipe?
|
184
|
+
assert_always_false(:pipe?)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_blockdev?
|
188
|
+
assert_always_false(:blockdev?)
|
189
|
+
end
|
190
|
+
|
191
|
+
def test_symlink?
|
192
|
+
assert_always_false(:symlink?)
|
193
|
+
end
|
194
|
+
|
195
|
+
def test_socket?
|
196
|
+
assert_always_false(:socket?)
|
197
|
+
end
|
198
|
+
|
199
|
+
def test_chardev?
|
200
|
+
assert_always_false(:chardev?)
|
201
|
+
end
|
202
|
+
|
203
|
+
def test_truncate
|
204
|
+
assert_raises(StandardError, "truncate not supported") {
|
205
|
+
@zip_file.file.truncate("file1", 100)
|
206
|
+
}
|
207
|
+
end
|
208
|
+
|
209
|
+
def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"])
|
210
|
+
assert_raises(Errno::ENOENT) {
|
211
|
+
@zip_file.file.send(operation, *args)
|
212
|
+
}
|
213
|
+
end
|
214
|
+
|
215
|
+
def test_ftype
|
216
|
+
assert_e_n_o_e_n_t(:ftype)
|
217
|
+
assert_equal("file", @zip_file.file.ftype("file1"))
|
218
|
+
assert_equal("directory", @zip_file.file.ftype("dir1/dir11"))
|
219
|
+
assert_equal("directory", @zip_file.file.ftype("dir1/dir11/"))
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_link
|
223
|
+
assert_raises(NotImplementedError) {
|
224
|
+
@zip_file.file.link("file1", "someOtherString")
|
225
|
+
}
|
226
|
+
end
|
227
|
+
|
228
|
+
def test_directory?
|
229
|
+
assert(! @zip_file.file.directory?("notAFile"))
|
230
|
+
assert(! @zip_file.file.directory?("file1"))
|
231
|
+
assert(! @zip_file.file.directory?("dir1/file11"))
|
232
|
+
assert(@zip_file.file.directory?("dir1"))
|
233
|
+
assert(@zip_file.file.directory?("dir1/"))
|
234
|
+
assert(@zip_file.file.directory?("dir2/dir21"))
|
235
|
+
|
236
|
+
assert(! @zip_file.file.stat("file1").directory?)
|
237
|
+
assert(! @zip_file.file.stat("dir1/file11").directory?)
|
238
|
+
assert(@zip_file.file.stat("dir1").directory?)
|
239
|
+
assert(@zip_file.file.stat("dir1/").directory?)
|
240
|
+
assert(@zip_file.file.stat("dir2/dir21").directory?)
|
241
|
+
end
|
242
|
+
|
243
|
+
def test_chown
|
244
|
+
assert_equal(2, @zip_file.file.chown(1,2, "dir1", "file1"))
|
245
|
+
assert_equal(1, @zip_file.file.stat("dir1").uid)
|
246
|
+
assert_equal(2, @zip_file.file.stat("dir1").gid)
|
247
|
+
assert_equal(2, @zip_file.file.chown(nil, nil, "dir1", "file1"))
|
248
|
+
end
|
249
|
+
|
250
|
+
def test_zero?
|
251
|
+
assert(! @zip_file.file.zero?("notAFile"))
|
252
|
+
assert(! @zip_file.file.zero?("file1"))
|
253
|
+
assert(@zip_file.file.zero?("dir1"))
|
254
|
+
blockCalled = false
|
255
|
+
::Zip::File.open("test/data/generated/5entry.zip") {
|
256
|
+
|zf|
|
257
|
+
blockCalled = true
|
258
|
+
assert(zf.file.zero?("test/data/generated/empty.txt"))
|
259
|
+
}
|
260
|
+
assert(blockCalled)
|
261
|
+
|
262
|
+
assert(! @zip_file.file.stat("file1").zero?)
|
263
|
+
assert(@zip_file.file.stat("dir1").zero?)
|
264
|
+
blockCalled = false
|
265
|
+
::Zip::File.open("test/data/generated/5entry.zip") {
|
266
|
+
|zf|
|
267
|
+
blockCalled = true
|
268
|
+
assert(zf.file.stat("test/data/generated/empty.txt").zero?)
|
269
|
+
}
|
270
|
+
assert(blockCalled)
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_expand_path
|
274
|
+
::Zip::File.open("test/data/zipWithDirs.zip") {
|
275
|
+
|zf|
|
276
|
+
assert_equal("/", zf.file.expand_path("."))
|
277
|
+
zf.dir.chdir "dir1"
|
278
|
+
assert_equal("/dir1", zf.file.expand_path("."))
|
279
|
+
assert_equal("/dir1/file12", zf.file.expand_path("file12"))
|
280
|
+
assert_equal("/", zf.file.expand_path(".."))
|
281
|
+
assert_equal("/dir2/dir21", zf.file.expand_path("../dir2/dir21"))
|
282
|
+
}
|
283
|
+
end
|
284
|
+
|
285
|
+
def test_mtime
|
286
|
+
assert_equal(::Zip::DOSTime.at(1027694306),
|
287
|
+
@zip_file.file.mtime("dir2/file21"))
|
288
|
+
assert_equal(::Zip::DOSTime.at(1027690863),
|
289
|
+
@zip_file.file.mtime("dir2/dir21"))
|
290
|
+
assert_raises(Errno::ENOENT) {
|
291
|
+
@zip_file.file.mtime("noSuchEntry")
|
292
|
+
}
|
293
|
+
|
294
|
+
assert_equal(::Zip::DOSTime.at(1027694306),
|
295
|
+
@zip_file.file.stat("dir2/file21").mtime)
|
296
|
+
assert_equal(::Zip::DOSTime.at(1027690863),
|
297
|
+
@zip_file.file.stat("dir2/dir21").mtime)
|
298
|
+
end
|
299
|
+
|
300
|
+
def test_ctime
|
301
|
+
assert_nil(@zip_file.file.ctime("file1"))
|
302
|
+
assert_nil(@zip_file.file.stat("file1").ctime)
|
303
|
+
end
|
304
|
+
|
305
|
+
def test_atime
|
306
|
+
assert_nil(@zip_file.file.atime("file1"))
|
307
|
+
assert_nil(@zip_file.file.stat("file1").atime)
|
308
|
+
end
|
309
|
+
|
310
|
+
def test_readable?
|
311
|
+
assert(! @zip_file.file.readable?("noSuchFile"))
|
312
|
+
assert(@zip_file.file.readable?("file1"))
|
313
|
+
assert(@zip_file.file.readable?("dir1"))
|
314
|
+
assert(@zip_file.file.stat("file1").readable?)
|
315
|
+
assert(@zip_file.file.stat("dir1").readable?)
|
316
|
+
end
|
317
|
+
|
318
|
+
def test_readable_real?
|
319
|
+
assert(! @zip_file.file.readable_real?("noSuchFile"))
|
320
|
+
assert(@zip_file.file.readable_real?("file1"))
|
321
|
+
assert(@zip_file.file.readable_real?("dir1"))
|
322
|
+
assert(@zip_file.file.stat("file1").readable_real?)
|
323
|
+
assert(@zip_file.file.stat("dir1").readable_real?)
|
324
|
+
end
|
325
|
+
|
326
|
+
def test_writable?
|
327
|
+
assert(! @zip_file.file.writable?("noSuchFile"))
|
328
|
+
assert(@zip_file.file.writable?("file1"))
|
329
|
+
assert(@zip_file.file.writable?("dir1"))
|
330
|
+
assert(@zip_file.file.stat("file1").writable?)
|
331
|
+
assert(@zip_file.file.stat("dir1").writable?)
|
332
|
+
end
|
333
|
+
|
334
|
+
def test_writable_real?
|
335
|
+
assert(! @zip_file.file.writable_real?("noSuchFile"))
|
336
|
+
assert(@zip_file.file.writable_real?("file1"))
|
337
|
+
assert(@zip_file.file.writable_real?("dir1"))
|
338
|
+
assert(@zip_file.file.stat("file1").writable_real?)
|
339
|
+
assert(@zip_file.file.stat("dir1").writable_real?)
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_executable?
|
343
|
+
assert(! @zip_file.file.executable?("noSuchFile"))
|
344
|
+
assert(! @zip_file.file.executable?("file1"))
|
345
|
+
assert(@zip_file.file.executable?("dir1"))
|
346
|
+
assert(! @zip_file.file.stat("file1").executable?)
|
347
|
+
assert(@zip_file.file.stat("dir1").executable?)
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_executable_real?
|
351
|
+
assert(! @zip_file.file.executable_real?("noSuchFile"))
|
352
|
+
assert(! @zip_file.file.executable_real?("file1"))
|
353
|
+
assert(@zip_file.file.executable_real?("dir1"))
|
354
|
+
assert(! @zip_file.file.stat("file1").executable_real?)
|
355
|
+
assert(@zip_file.file.stat("dir1").executable_real?)
|
356
|
+
end
|
357
|
+
|
358
|
+
def test_owned?
|
359
|
+
assert_true_if_entry_exists(:owned?)
|
360
|
+
end
|
361
|
+
|
362
|
+
def test_grpowned?
|
363
|
+
assert_true_if_entry_exists(:grpowned?)
|
364
|
+
end
|
365
|
+
|
366
|
+
def test_setgid?
|
367
|
+
assert_always_false(:setgid?)
|
368
|
+
end
|
369
|
+
|
370
|
+
def test_setuid?
|
371
|
+
assert_always_false(:setgid?)
|
372
|
+
end
|
373
|
+
|
374
|
+
def test_sticky?
|
375
|
+
assert_always_false(:sticky?)
|
376
|
+
end
|
377
|
+
|
378
|
+
def test_readlink
|
379
|
+
assert_raises(NotImplementedError) {
|
380
|
+
@zip_file.file.readlink("someString")
|
381
|
+
}
|
382
|
+
end
|
383
|
+
|
384
|
+
def test_stat
|
385
|
+
s = @zip_file.file.stat("file1")
|
386
|
+
assert(s.kind_of?(File::Stat)) # It pretends
|
387
|
+
assert_raises(Errno::ENOENT, "No such file or directory - noSuchFile") {
|
388
|
+
@zip_file.file.stat("noSuchFile")
|
389
|
+
}
|
390
|
+
end
|
391
|
+
|
392
|
+
def test_lstat
|
393
|
+
assert(@zip_file.file.lstat("file1").file?)
|
394
|
+
end
|
395
|
+
|
396
|
+
def test_pipe
|
397
|
+
assert_raises(NotImplementedError) {
|
398
|
+
@zip_file.file.pipe
|
399
|
+
}
|
400
|
+
end
|
401
|
+
|
402
|
+
def test_foreach
|
403
|
+
::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
|
404
|
+
ref = []
|
405
|
+
File.foreach("test/data/file1.txt") { |e| ref << e }
|
406
|
+
index = 0
|
407
|
+
|
408
|
+
zf.file.foreach("test/data/file1.txt") do |l|
|
409
|
+
#Ruby replaces \n with \r\n automatically on windows
|
410
|
+
newline = Zip::RUNNING_ON_WINDOWS ? l.gsub(/\r\n/, "\n") : l
|
411
|
+
assert_equal(ref[index], newline)
|
412
|
+
index = index.next
|
413
|
+
end
|
414
|
+
assert_equal(ref.size, index)
|
415
|
+
end
|
416
|
+
|
417
|
+
::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
|
418
|
+
ref = []
|
419
|
+
File.foreach("test/data/file1.txt", " ") { |e| ref << e }
|
420
|
+
index = 0
|
421
|
+
|
422
|
+
zf.file.foreach("test/data/file1.txt", " ") do |l|
|
423
|
+
#Ruby replaces \n with \r\n automatically on windows
|
424
|
+
newline = Zip::RUNNING_ON_WINDOWS ? l.gsub(/\r\n/, "\n") : l
|
425
|
+
assert_equal(ref[index], newline)
|
426
|
+
index = index.next
|
427
|
+
end
|
428
|
+
assert_equal(ref.size, index)
|
429
|
+
end
|
430
|
+
end
|
431
|
+
|
432
|
+
def test_glob
|
433
|
+
::Zip::File.open('test/data/globTest.zip') do |zf|
|
434
|
+
{
|
435
|
+
'globTest/foo.txt' => ['globTest/foo.txt'],
|
436
|
+
'*/foo.txt' => ['globTest/foo.txt'],
|
437
|
+
'**/foo.txt' => ['globTest/foo.txt','globTest/foo/bar/baz/foo.txt'],
|
438
|
+
'*/foo/**/*.txt' => ['globTest/foo/bar/baz/foo.txt']
|
439
|
+
}.each do |spec,expected_results|
|
440
|
+
results = zf.glob(spec)
|
441
|
+
assert results.all?{|entry| entry.is_a? ::Zip::Entry }
|
442
|
+
|
443
|
+
result_strings = results.map(&:to_s)
|
444
|
+
missing_matches = expected_results - result_strings
|
445
|
+
extra_matches = result_strings - expected_results
|
446
|
+
|
447
|
+
assert extra_matches.empty?, %Q{spec #{spec.inspect} has extra results #{extra_matches.inspect}}
|
448
|
+
assert missing_matches.empty?, %Q{spec #{spec.inspect} missing results #{missing_matches.inspect}}
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
::Zip::File.open('test/data/globTest.zip') do |zf|
|
453
|
+
results = []
|
454
|
+
zf.glob('**/foo.txt') do |match|
|
455
|
+
results << "<#{match.class.name}: #{match.to_s}>"
|
456
|
+
end
|
457
|
+
assert((not results.empty?), 'block not run, or run out of context')
|
458
|
+
assert_equal 2, results.size
|
459
|
+
assert_operator results, :include?, '<Zip::Entry: globTest/foo.txt>'
|
460
|
+
assert_operator results, :include?, '<Zip::Entry: globTest/foo/bar/baz/foo.txt>'
|
461
|
+
end
|
462
|
+
end
|
463
|
+
|
464
|
+
def test_popen
|
465
|
+
if Zip::RUNNING_ON_WINDOWS
|
466
|
+
#This is pretty much projectile vomit but it allows the test to be
|
467
|
+
#run on windows also
|
468
|
+
system_dir = ::File.popen('dir') { |f| f.read }.gsub(/Dir\(s\).*$/, '')
|
469
|
+
zipfile_dir = @zip_file.file.popen('dir') { |f| f.read }.gsub(/Dir\(s\).*$/, '')
|
470
|
+
assert_equal(system_dir, zipfile_dir)
|
471
|
+
else
|
472
|
+
assert_equal(::File.popen('ls') { |f| f.read },
|
473
|
+
@zip_file.file.popen('ls') { |f| f.read })
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
# Can be added later
|
478
|
+
# def test_select
|
479
|
+
# fail "implement test"
|
480
|
+
# end
|
481
|
+
|
482
|
+
def test_readlines
|
483
|
+
::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
|
484
|
+
orig_file = ::File.readlines("test/data/file1.txt")
|
485
|
+
zip_file = zf.file.readlines("test/data/file1.txt")
|
486
|
+
|
487
|
+
#Ruby replaces \n with \r\n automatically on windows
|
488
|
+
zip_file.each { |l| l.gsub!(/\r\n/, "\n") } if Zip::RUNNING_ON_WINDOWS
|
489
|
+
|
490
|
+
assert_equal(orig_file, zip_file)
|
491
|
+
end
|
492
|
+
end
|
493
|
+
|
494
|
+
def test_read
|
495
|
+
::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
|
496
|
+
orig_file = ::File.read("test/data/file1.txt")
|
497
|
+
|
498
|
+
#Ruby replaces \n with \r\n automatically on windows
|
499
|
+
zip_file = Zip::RUNNING_ON_WINDOWS ? \
|
500
|
+
zf.file.read("test/data/file1.txt").gsub(/\r\n/, "\n") : zf.file.read("test/data/file1.txt")
|
501
|
+
assert_equal(orig_file, zip_file)
|
502
|
+
end
|
503
|
+
end
|
504
|
+
|
505
|
+
end
|