rzip 2.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,827 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'helper'
4
+ require 'zip/zipfilesystem'
5
+
6
+ module ExtraAssertions
7
+
8
+ def assert_forwarded(anObject, method, retVal, *expectedArgs)
9
+ callArgs = nil
10
+ setCallArgsProc = proc { |args| callArgs = args }
11
+ anObject.instance_eval <<-"end_eval"
12
+ alias #{method}_org #{method}
13
+ def #{method}(*args)
14
+ ObjectSpace._id2ref(#{setCallArgsProc.object_id}).call(args)
15
+ ObjectSpace._id2ref(#{retVal.object_id})
16
+ end
17
+ end_eval
18
+
19
+ assert_equal(retVal, yield) # Invoke test
20
+ assert_equal(expectedArgs, callArgs)
21
+ ensure
22
+ anObject.instance_eval "alias #{method} #{method}_org"
23
+ end
24
+
25
+ end
26
+
27
+ include Zip
28
+
29
+ class ZipFsFileNonmutatingTest < Test::Unit::TestCase
30
+ def setup
31
+ @zipFile = ZipFile.new("data/zipWithDirs.zip")
32
+ end
33
+
34
+ def teardown
35
+ @zipFile.close if @zipFile
36
+ end
37
+
38
+ def test_umask
39
+ assert_equal(File.umask, @zipFile.file.umask)
40
+ @zipFile.file.umask(0006)
41
+ end
42
+
43
+ def test_exists?
44
+ assert(! @zipFile.file.exists?("notAFile"))
45
+ assert(@zipFile.file.exists?("file1"))
46
+ assert(@zipFile.file.exists?("dir1"))
47
+ assert(@zipFile.file.exists?("dir1/"))
48
+ assert(@zipFile.file.exists?("dir1/file12"))
49
+ assert(@zipFile.file.exist?("dir1/file12")) # notice, tests exist? alias of exists? !
50
+
51
+ @zipFile.dir.chdir "dir1/"
52
+ assert(!@zipFile.file.exists?("file1"))
53
+ assert(@zipFile.file.exists?("file12"))
54
+ end
55
+
56
+ def test_open_read
57
+ blockCalled = false
58
+ @zipFile.file.open("file1", "r") {
59
+ |f|
60
+ blockCalled = true
61
+ assert_equal("this is the entry 'file1' in my test archive!",
62
+ f.readline.chomp)
63
+ }
64
+ assert(blockCalled)
65
+
66
+ blockCalled = false
67
+ @zipFile.dir.chdir "dir2"
68
+ @zipFile.file.open("file21", "r") {
69
+ |f|
70
+ blockCalled = true
71
+ assert_equal("this is the entry 'dir2/file21' in my test archive!",
72
+ f.readline.chomp)
73
+ }
74
+ assert(blockCalled)
75
+ @zipFile.dir.chdir "/"
76
+
77
+ assert_raise(Errno::ENOENT) {
78
+ @zipFile.file.open("noSuchEntry")
79
+ }
80
+
81
+ begin
82
+ is = @zipFile.file.open("file1")
83
+ assert_equal("this is the entry 'file1' in my test archive!",
84
+ is.readline.chomp)
85
+ ensure
86
+ is.close if is
87
+ end
88
+ end
89
+
90
+ def test_new
91
+ begin
92
+ is = @zipFile.file.new("file1")
93
+ assert_equal("this is the entry 'file1' in my test archive!",
94
+ is.readline.chomp)
95
+ ensure
96
+ is.close if is
97
+ end
98
+ begin
99
+ is = @zipFile.file.new("file1") {
100
+ fail "should not call block"
101
+ }
102
+ ensure
103
+ is.close if is
104
+ end
105
+ end
106
+
107
+ def test_symlink
108
+ assert_raise(NotImplementedError) {
109
+ @zipFile.file.symlink("file1", "aSymlink")
110
+ }
111
+ end
112
+
113
+ def test_size
114
+ assert_raise(Errno::ENOENT) { @zipFile.file.size("notAFile") }
115
+ assert_equal(72, @zipFile.file.size("file1"))
116
+ assert_equal(0, @zipFile.file.size("dir2/dir21"))
117
+
118
+ assert_equal(72, @zipFile.file.stat("file1").size)
119
+ assert_equal(0, @zipFile.file.stat("dir2/dir21").size)
120
+ end
121
+
122
+ def test_size?
123
+ assert_equal(nil, @zipFile.file.size?("notAFile"))
124
+ assert_equal(72, @zipFile.file.size?("file1"))
125
+ assert_equal(nil, @zipFile.file.size?("dir2/dir21"))
126
+
127
+ assert_equal(72, @zipFile.file.stat("file1").size?)
128
+ assert_equal(nil, @zipFile.file.stat("dir2/dir21").size?)
129
+ end
130
+
131
+
132
+ def test_file?
133
+ assert(@zipFile.file.file?("file1"))
134
+ assert(@zipFile.file.file?("dir2/file21"))
135
+ assert(! @zipFile.file.file?("dir1"))
136
+ assert(! @zipFile.file.file?("dir1/dir11"))
137
+
138
+ assert(@zipFile.file.stat("file1").file?)
139
+ assert(@zipFile.file.stat("dir2/file21").file?)
140
+ assert(! @zipFile.file.stat("dir1").file?)
141
+ assert(! @zipFile.file.stat("dir1/dir11").file?)
142
+ end
143
+
144
+ include ExtraAssertions
145
+
146
+ def test_dirname
147
+ assert_forwarded(File, :dirname, "retVal", "a/b/c/d") {
148
+ @zipFile.file.dirname("a/b/c/d")
149
+ }
150
+ end
151
+
152
+ def test_basename
153
+ assert_forwarded(File, :basename, "retVal", "a/b/c/d") {
154
+ @zipFile.file.basename("a/b/c/d")
155
+ }
156
+ end
157
+
158
+ def test_split
159
+ assert_forwarded(File, :split, "retVal", "a/b/c/d") {
160
+ @zipFile.file.split("a/b/c/d")
161
+ }
162
+ end
163
+
164
+ def test_join
165
+ assert_equal("a/b/c", @zipFile.file.join("a/b", "c"))
166
+ assert_equal("a/b/c/d", @zipFile.file.join("a/b", "c/d"))
167
+ assert_equal("/c/d", @zipFile.file.join("", "c/d"))
168
+ assert_equal("a/b/c/d", @zipFile.file.join("a", "b", "c", "d"))
169
+ end
170
+
171
+ def test_utime
172
+ t_now = Time.now
173
+ t_bak = @zipFile.file.mtime("file1")
174
+ @zipFile.file.utime(t_now, "file1")
175
+ assert_equal(t_now, @zipFile.file.mtime("file1"))
176
+ @zipFile.file.utime(t_bak, "file1")
177
+ assert_equal(t_bak, @zipFile.file.mtime("file1"))
178
+ end
179
+
180
+
181
+ def assert_always_false(operation)
182
+ assert(! @zipFile.file.send(operation, "noSuchFile"))
183
+ assert(! @zipFile.file.send(operation, "file1"))
184
+ assert(! @zipFile.file.send(operation, "dir1"))
185
+ assert(! @zipFile.file.stat("file1").send(operation))
186
+ assert(! @zipFile.file.stat("dir1").send(operation))
187
+ end
188
+
189
+ def assert_true_if_entry_exists(operation)
190
+ assert(! @zipFile.file.send(operation, "noSuchFile"))
191
+ assert(@zipFile.file.send(operation, "file1"))
192
+ assert(@zipFile.file.send(operation, "dir1"))
193
+ assert(@zipFile.file.stat("file1").send(operation))
194
+ assert(@zipFile.file.stat("dir1").send(operation))
195
+ end
196
+
197
+ def test_pipe?
198
+ assert_always_false(:pipe?)
199
+ end
200
+
201
+ def test_blockdev?
202
+ assert_always_false(:blockdev?)
203
+ end
204
+
205
+ def test_symlink?
206
+ assert_always_false(:symlink?)
207
+ end
208
+
209
+ def test_socket?
210
+ assert_always_false(:socket?)
211
+ end
212
+
213
+ def test_chardev?
214
+ assert_always_false(:chardev?)
215
+ end
216
+
217
+ def test_truncate
218
+ assert_raise(StandardError, "truncate not supported") {
219
+ @zipFile.file.truncate("file1", 100)
220
+ }
221
+ end
222
+
223
+ def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"])
224
+ assert_raise(Errno::ENOENT) {
225
+ @zipFile.file.send(operation, *args)
226
+ }
227
+ end
228
+
229
+ def test_ftype
230
+ assert_e_n_o_e_n_t(:ftype)
231
+ assert_equal("file", @zipFile.file.ftype("file1"))
232
+ assert_equal("directory", @zipFile.file.ftype("dir1/dir11"))
233
+ assert_equal("directory", @zipFile.file.ftype("dir1/dir11/"))
234
+ end
235
+
236
+ def test_link
237
+ assert_raise(NotImplementedError) {
238
+ @zipFile.file.link("file1", "someOtherString")
239
+ }
240
+ end
241
+
242
+ def test_directory?
243
+ assert(! @zipFile.file.directory?("notAFile"))
244
+ assert(! @zipFile.file.directory?("file1"))
245
+ assert(! @zipFile.file.directory?("dir1/file11"))
246
+ assert(@zipFile.file.directory?("dir1"))
247
+ assert(@zipFile.file.directory?("dir1/"))
248
+ assert(@zipFile.file.directory?("dir2/dir21"))
249
+
250
+ assert(! @zipFile.file.stat("file1").directory?)
251
+ assert(! @zipFile.file.stat("dir1/file11").directory?)
252
+ assert(@zipFile.file.stat("dir1").directory?)
253
+ assert(@zipFile.file.stat("dir1/").directory?)
254
+ assert(@zipFile.file.stat("dir2/dir21").directory?)
255
+ end
256
+
257
+ def test_chown
258
+ assert_equal(2, @zipFile.file.chown(1,2, "dir1", "file1"))
259
+ assert_equal(1, @zipFile.file.stat("dir1").uid)
260
+ assert_equal(2, @zipFile.file.stat("dir1").gid)
261
+ assert_equal(2, @zipFile.file.chown(nil, nil, "dir1", "file1"))
262
+ end
263
+
264
+ def test_zero?
265
+ assert(! @zipFile.file.zero?("notAFile"))
266
+ assert(! @zipFile.file.zero?("file1"))
267
+ assert(@zipFile.file.zero?("dir1"))
268
+ blockCalled = false
269
+ ZipFile.open("data/generated/5entry.zip") {
270
+ |zf|
271
+ blockCalled = true
272
+ assert(zf.file.zero?("data/generated/empty.txt"))
273
+ }
274
+ assert(blockCalled)
275
+
276
+ assert(! @zipFile.file.stat("file1").zero?)
277
+ assert(@zipFile.file.stat("dir1").zero?)
278
+ blockCalled = false
279
+ ZipFile.open("data/generated/5entry.zip") {
280
+ |zf|
281
+ blockCalled = true
282
+ assert(zf.file.stat("data/generated/empty.txt").zero?)
283
+ }
284
+ assert(blockCalled)
285
+ end
286
+
287
+ def test_expand_path
288
+ ZipFile.open("data/zipWithDirs.zip") {
289
+ |zf|
290
+ assert_equal("/", zf.file.expand_path("."))
291
+ zf.dir.chdir "dir1"
292
+ assert_equal("/dir1", zf.file.expand_path("."))
293
+ assert_equal("/dir1/file12", zf.file.expand_path("file12"))
294
+ assert_equal("/", zf.file.expand_path(".."))
295
+ assert_equal("/dir2/dir21", zf.file.expand_path("../dir2/dir21"))
296
+ }
297
+ end
298
+
299
+ def test_mtime
300
+ assert_equal(Time.at(1027694306),
301
+ @zipFile.file.mtime("dir2/file21"))
302
+ assert_equal(Time.at(1027690863),
303
+ @zipFile.file.mtime("dir2/dir21"))
304
+ assert_raise(Errno::ENOENT) {
305
+ @zipFile.file.mtime("noSuchEntry")
306
+ }
307
+
308
+ assert_equal(Time.at(1027694306),
309
+ @zipFile.file.stat("dir2/file21").mtime)
310
+ assert_equal(Time.at(1027690863),
311
+ @zipFile.file.stat("dir2/dir21").mtime)
312
+ end
313
+
314
+ def test_ctime
315
+ assert_nil(@zipFile.file.ctime("file1"))
316
+ assert_nil(@zipFile.file.stat("file1").ctime)
317
+ end
318
+
319
+ def test_atime
320
+ assert_nil(@zipFile.file.atime("file1"))
321
+ assert_nil(@zipFile.file.stat("file1").atime)
322
+ end
323
+
324
+ def test_readable?
325
+ assert(! @zipFile.file.readable?("noSuchFile"))
326
+ assert(@zipFile.file.readable?("file1"))
327
+ assert(@zipFile.file.readable?("dir1"))
328
+ assert(@zipFile.file.stat("file1").readable?)
329
+ assert(@zipFile.file.stat("dir1").readable?)
330
+ end
331
+
332
+ def test_readable_real?
333
+ assert(! @zipFile.file.readable_real?("noSuchFile"))
334
+ assert(@zipFile.file.readable_real?("file1"))
335
+ assert(@zipFile.file.readable_real?("dir1"))
336
+ assert(@zipFile.file.stat("file1").readable_real?)
337
+ assert(@zipFile.file.stat("dir1").readable_real?)
338
+ end
339
+
340
+ def test_writable?
341
+ assert(! @zipFile.file.writable?("noSuchFile"))
342
+ assert(@zipFile.file.writable?("file1"))
343
+ assert(@zipFile.file.writable?("dir1"))
344
+ assert(@zipFile.file.stat("file1").writable?)
345
+ assert(@zipFile.file.stat("dir1").writable?)
346
+ end
347
+
348
+ def test_writable_real?
349
+ assert(! @zipFile.file.writable_real?("noSuchFile"))
350
+ assert(@zipFile.file.writable_real?("file1"))
351
+ assert(@zipFile.file.writable_real?("dir1"))
352
+ assert(@zipFile.file.stat("file1").writable_real?)
353
+ assert(@zipFile.file.stat("dir1").writable_real?)
354
+ end
355
+
356
+ def test_executable?
357
+ assert(! @zipFile.file.executable?("noSuchFile"))
358
+ assert(! @zipFile.file.executable?("file1"))
359
+ assert(@zipFile.file.executable?("dir1"))
360
+ assert(! @zipFile.file.stat("file1").executable?)
361
+ assert(@zipFile.file.stat("dir1").executable?)
362
+ end
363
+
364
+ def test_executable_real?
365
+ assert(! @zipFile.file.executable_real?("noSuchFile"))
366
+ assert(! @zipFile.file.executable_real?("file1"))
367
+ assert(@zipFile.file.executable_real?("dir1"))
368
+ assert(! @zipFile.file.stat("file1").executable_real?)
369
+ assert(@zipFile.file.stat("dir1").executable_real?)
370
+ end
371
+
372
+ def test_owned?
373
+ assert_true_if_entry_exists(:owned?)
374
+ end
375
+
376
+ def test_grpowned?
377
+ assert_true_if_entry_exists(:grpowned?)
378
+ end
379
+
380
+ def test_setgid?
381
+ assert_always_false(:setgid?)
382
+ end
383
+
384
+ def test_setuid?
385
+ assert_always_false(:setgid?)
386
+ end
387
+
388
+ def test_sticky?
389
+ assert_always_false(:sticky?)
390
+ end
391
+
392
+ def test_readlink
393
+ assert_raise(NotImplementedError) {
394
+ @zipFile.file.readlink("someString")
395
+ }
396
+ end
397
+
398
+ def test_stat
399
+ s = @zipFile.file.stat("file1")
400
+ assert(s.kind_of?(File::Stat)) # It pretends
401
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
402
+ @zipFile.file.stat("noSuchFile")
403
+ }
404
+ end
405
+
406
+ def test_lstat
407
+ assert(@zipFile.file.lstat("file1").file?)
408
+ end
409
+
410
+
411
+ def test_chmod
412
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
413
+ @zipFile.file.chmod(0644, "file1", "NoSuchFile")
414
+ }
415
+ assert_equal(2, @zipFile.file.chmod(0644, "file1", "dir1"))
416
+ end
417
+
418
+ def test_pipe
419
+ assert_raise(NotImplementedError) {
420
+ @zipFile.file.pipe
421
+ }
422
+ end
423
+
424
+ def test_foreach
425
+ ZipFile.open("data/generated/zipWithDir.zip") {
426
+ |zf|
427
+ ref = []
428
+ File.foreach("data/file1.txt") { |e| ref << e }
429
+
430
+ index = 0
431
+ zf.file.foreach("data/file1.txt") {
432
+ |l|
433
+ assert_equal(ref[index], l)
434
+ index = index.next
435
+ }
436
+ assert_equal(ref.size, index)
437
+ }
438
+
439
+ ZipFile.open("data/generated/zipWithDir.zip") {
440
+ |zf|
441
+ ref = []
442
+ File.foreach("data/file1.txt", " ") { |e| ref << e }
443
+
444
+ index = 0
445
+ zf.file.foreach("data/file1.txt", " ") {
446
+ |l|
447
+ assert_equal(ref[index], l)
448
+ index = index.next
449
+ }
450
+ assert_equal(ref.size, index)
451
+ }
452
+ end
453
+
454
+ def test_popen
455
+ cmd = /mswin/i =~ RUBY_PLATFORM ? 'dir' : 'ls'
456
+
457
+ assert_equal(File.popen(cmd) { |f| f.read },
458
+ @zipFile.file.popen(cmd) { |f| f.read })
459
+ end
460
+
461
+ # Can be added later
462
+ # def test_select
463
+ # fail "implement test"
464
+ # end
465
+
466
+ def test_readlines
467
+ ZipFile.open("data/generated/zipWithDir.zip") {
468
+ |zf|
469
+ assert_equal(File.readlines("data/file1.txt"),
470
+ zf.file.readlines("data/file1.txt"))
471
+ }
472
+ end
473
+
474
+ def test_read
475
+ ZipFile.open("data/generated/zipWithDir.zip") {
476
+ |zf|
477
+ assert_equal(File.read("data/file1.txt"),
478
+ zf.file.read("data/file1.txt"))
479
+ }
480
+ end
481
+
482
+ end
483
+
484
+ class ZipFsFileStatTest < Test::Unit::TestCase
485
+
486
+ def setup
487
+ @zipFile = ZipFile.new("data/zipWithDirs.zip")
488
+ end
489
+
490
+ def teardown
491
+ @zipFile.close if @zipFile
492
+ end
493
+
494
+ def test_blocks
495
+ assert_equal(nil, @zipFile.file.stat("file1").blocks)
496
+ end
497
+
498
+ def test_ino
499
+ assert_equal(0, @zipFile.file.stat("file1").ino)
500
+ end
501
+
502
+ def test_uid
503
+ assert_equal(0, @zipFile.file.stat("file1").uid)
504
+ end
505
+
506
+ def test_gid
507
+ assert_equal(0, @zipFile.file.stat("file1").gid)
508
+ end
509
+
510
+ def test_ftype
511
+ assert_equal("file", @zipFile.file.stat("file1").ftype)
512
+ assert_equal("directory", @zipFile.file.stat("dir1").ftype)
513
+ end
514
+
515
+ def test_mode
516
+ assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
517
+ assert_equal(0600, @zipFile.file.stat("file1").mode & 0777)
518
+ assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
519
+ assert_equal(0755, @zipFile.file.stat("dir1").mode & 0777)
520
+ end
521
+
522
+ def test_dev
523
+ assert_equal(0, @zipFile.file.stat("file1").dev)
524
+ end
525
+
526
+ def test_rdev
527
+ assert_equal(0, @zipFile.file.stat("file1").rdev)
528
+ end
529
+
530
+ def test_rdev_major
531
+ assert_equal(0, @zipFile.file.stat("file1").rdev_major)
532
+ end
533
+
534
+ def test_rdev_minor
535
+ assert_equal(0, @zipFile.file.stat("file1").rdev_minor)
536
+ end
537
+
538
+ def test_nlink
539
+ assert_equal(1, @zipFile.file.stat("file1").nlink)
540
+ end
541
+
542
+ def test_blksize
543
+ assert_nil(@zipFile.file.stat("file1").blksize)
544
+ end
545
+
546
+ end
547
+
548
+ class ZipFsFileMutatingTest < Test::Unit::TestCase
549
+ TEST_ZIP = "zipWithDirs_copy.zip"
550
+ def setup
551
+ FileUtils.cp("data/zipWithDirs.zip", TEST_ZIP)
552
+ end
553
+
554
+ def teardown
555
+ end
556
+
557
+ def test_delete
558
+ do_test_delete_or_unlink(:delete)
559
+ end
560
+
561
+ def test_unlink
562
+ do_test_delete_or_unlink(:unlink)
563
+ end
564
+
565
+ def test_open_write
566
+ ZipFile.open(TEST_ZIP) {
567
+ |zf|
568
+
569
+ zf.file.open("test_open_write_entry", "w") {
570
+ |f|
571
+ blockCalled = true
572
+ f.write "This is what I'm writing"
573
+ }
574
+ assert_equal("This is what I'm writing",
575
+ zf.file.read("test_open_write_entry"))
576
+
577
+ # Test with existing entry
578
+ zf.file.open("file1", "w") {
579
+ |f|
580
+ blockCalled = true
581
+ f.write "This is what I'm writing too"
582
+ }
583
+ assert_equal("This is what I'm writing too",
584
+ zf.file.read("file1"))
585
+ }
586
+ end
587
+
588
+ def test_rename
589
+ ZipFile.open(TEST_ZIP) {
590
+ |zf|
591
+ assert_raise(Errno::ENOENT, "") {
592
+ zf.file.rename("NoSuchFile", "bimse")
593
+ }
594
+ zf.file.rename("file1", "newNameForFile1")
595
+ }
596
+
597
+ ZipFile.open(TEST_ZIP) {
598
+ |zf|
599
+ assert(! zf.file.exists?("file1"))
600
+ assert(zf.file.exists?("newNameForFile1"))
601
+ }
602
+ end
603
+
604
+ def do_test_delete_or_unlink(symbol)
605
+ ZipFile.open(TEST_ZIP) {
606
+ |zf|
607
+ assert(zf.file.exists?("dir2/dir21/dir221/file2221"))
608
+ zf.file.send(symbol, "dir2/dir21/dir221/file2221")
609
+ assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
610
+
611
+ assert(zf.file.exists?("dir1/file11"))
612
+ assert(zf.file.exists?("dir1/file12"))
613
+ zf.file.send(symbol, "dir1/file11", "dir1/file12")
614
+ assert(! zf.file.exists?("dir1/file11"))
615
+ assert(! zf.file.exists?("dir1/file12"))
616
+
617
+ assert_raise(Errno::ENOENT) { zf.file.send(symbol, "noSuchFile") }
618
+ assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11") }
619
+ assert_raise(Errno::EISDIR) { zf.file.send(symbol, "dir1/dir11/") }
620
+ }
621
+
622
+ ZipFile.open(TEST_ZIP) {
623
+ |zf|
624
+ assert(! zf.file.exists?("dir2/dir21/dir221/file2221"))
625
+ assert(! zf.file.exists?("dir1/file11"))
626
+ assert(! zf.file.exists?("dir1/file12"))
627
+
628
+ assert(zf.file.exists?("dir1/dir11"))
629
+ assert(zf.file.exists?("dir1/dir11/"))
630
+ }
631
+ end
632
+
633
+ end
634
+
635
+ class ZipFsDirectoryTest < Test::Unit::TestCase
636
+ TEST_ZIP = "zipWithDirs_copy.zip"
637
+
638
+ def setup
639
+ FileUtils.cp("data/zipWithDirs.zip", TEST_ZIP)
640
+ end
641
+
642
+ def test_delete
643
+ ZipFile.open(TEST_ZIP) {
644
+ |zf|
645
+ assert_raise(Errno::ENOENT, "No such file or directory - NoSuchFile.txt") {
646
+ zf.dir.delete("NoSuchFile.txt")
647
+ }
648
+ assert_raise(Errno::EINVAL, "Invalid argument - file1") {
649
+ zf.dir.delete("file1")
650
+ }
651
+ assert(zf.file.exists?("dir1"))
652
+ zf.dir.delete("dir1")
653
+ assert(! zf.file.exists?("dir1"))
654
+ }
655
+ end
656
+
657
+ def test_mkdir
658
+ ZipFile.open(TEST_ZIP) {
659
+ |zf|
660
+ assert_raise(Errno::EEXIST, "File exists - dir1") {
661
+ zf.dir.mkdir("file1")
662
+ }
663
+ assert_raise(Errno::EEXIST, "File exists - dir1") {
664
+ zf.dir.mkdir("dir1")
665
+ }
666
+ assert(!zf.file.exists?("newDir"))
667
+ zf.dir.mkdir("newDir")
668
+ assert(zf.file.directory?("newDir"))
669
+ assert(!zf.file.exists?("newDir2"))
670
+ zf.dir.mkdir("newDir2", 3485)
671
+ assert(zf.file.directory?("newDir2"))
672
+ }
673
+ end
674
+
675
+ def test_pwd_chdir_entries
676
+ ZipFile.open(TEST_ZIP) {
677
+ |zf|
678
+ assert_equal("/", zf.dir.pwd)
679
+
680
+ assert_raise(Errno::ENOENT, "No such file or directory - no such dir") {
681
+ zf.dir.chdir "no such dir"
682
+ }
683
+
684
+ assert_raise(Errno::EINVAL, "Invalid argument - file1") {
685
+ zf.dir.chdir "file1"
686
+ }
687
+
688
+ assert_equal(["dir1", "dir2", "file1"].sort, zf.dir.entries(".").sort)
689
+ zf.dir.chdir "dir1"
690
+ assert_equal("/dir1", zf.dir.pwd)
691
+ assert_equal(["dir11", "file11", "file12"], zf.dir.entries(".").sort)
692
+
693
+ zf.dir.chdir "../dir2/dir21"
694
+ assert_equal("/dir2/dir21", zf.dir.pwd)
695
+ assert_equal(["dir221"].sort, zf.dir.entries(".").sort)
696
+ }
697
+ end
698
+
699
+ def test_foreach
700
+ ZipFile.open(TEST_ZIP) {
701
+ |zf|
702
+
703
+ blockCalled = false
704
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchDir") {
705
+ zf.dir.foreach("noSuchDir") { |e| blockCalled = true }
706
+ }
707
+ assert(! blockCalled)
708
+
709
+ assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
710
+ zf.dir.foreach("file1") { |e| blockCalled = true }
711
+ }
712
+ assert(! blockCalled)
713
+
714
+ entries = []
715
+ zf.dir.foreach(".") { |e| entries << e }
716
+ assert_equal(["dir1", "dir2", "file1"].sort, entries.sort)
717
+
718
+ entries = []
719
+ zf.dir.foreach("dir1") { |e| entries << e }
720
+ assert_equal(["dir11", "file11", "file12"], entries.sort)
721
+ }
722
+ end
723
+
724
+ def test_chroot
725
+ ZipFile.open(TEST_ZIP) {
726
+ |zf|
727
+ assert_raise(NotImplementedError) {
728
+ zf.dir.chroot
729
+ }
730
+ }
731
+ end
732
+
733
+ # Globbing not supported yet
734
+ #def test_glob
735
+ # # test alias []-operator too
736
+ # fail "implement test"
737
+ #end
738
+
739
+ def test_open_new
740
+ ZipFile.open(TEST_ZIP) {
741
+ |zf|
742
+
743
+ assert_raise(Errno::ENOTDIR, "Not a directory - file1") {
744
+ zf.dir.new("file1")
745
+ }
746
+
747
+ assert_raise(Errno::ENOENT, "No such file or directory - noSuchFile") {
748
+ zf.dir.new("noSuchFile")
749
+ }
750
+
751
+ d = zf.dir.new(".")
752
+ assert_equal(["file1", "dir1", "dir2"].sort, d.entries.sort)
753
+ d.close
754
+
755
+ zf.dir.open("dir1") {
756
+ |d|
757
+ assert_equal(["dir11", "file11", "file12"].sort, d.entries.sort)
758
+ }
759
+ }
760
+ end
761
+
762
+ end
763
+
764
+ class ZipFsDirIteratorTest < Test::Unit::TestCase
765
+
766
+ FILENAME_ARRAY = [ "f1", "f2", "f3", "f4", "f5", "f6" ]
767
+
768
+ def setup
769
+ @dirIt = ZipFileSystem::ZipFsDirIterator.new(FILENAME_ARRAY)
770
+ end
771
+
772
+ def test_close
773
+ @dirIt.close
774
+ assert_raise(IOError, "closed directory") {
775
+ @dirIt.each { |e| p e }
776
+ }
777
+ assert_raise(IOError, "closed directory") {
778
+ @dirIt.read
779
+ }
780
+ assert_raise(IOError, "closed directory") {
781
+ @dirIt.rewind
782
+ }
783
+ assert_raise(IOError, "closed directory") {
784
+ @dirIt.seek(0)
785
+ }
786
+ assert_raise(IOError, "closed directory") {
787
+ @dirIt.tell
788
+ }
789
+
790
+ end
791
+
792
+ def test_each
793
+ # Tested through Enumerable.entries
794
+ assert_equal(FILENAME_ARRAY, @dirIt.entries)
795
+ end
796
+
797
+ def test_read
798
+ FILENAME_ARRAY.size.times {
799
+ |i|
800
+ assert_equal(FILENAME_ARRAY[i], @dirIt.read)
801
+ }
802
+ end
803
+
804
+ def test_rewind
805
+ @dirIt.read
806
+ @dirIt.read
807
+ assert_equal(FILENAME_ARRAY[2], @dirIt.read)
808
+ @dirIt.rewind
809
+ assert_equal(FILENAME_ARRAY[0], @dirIt.read)
810
+ end
811
+
812
+ def test_tell_seek
813
+ @dirIt.read
814
+ @dirIt.read
815
+ pos = @dirIt.tell
816
+ valAtPos = @dirIt.read
817
+ @dirIt.read
818
+ @dirIt.seek(pos)
819
+ assert_equal(valAtPos, @dirIt.read)
820
+ end
821
+
822
+ end
823
+
824
+
825
+ # Copyright (C) 2002, 2003 Thomas Sondergaard
826
+ # rubyzip is free software; you can redistribute it and/or
827
+ # modify it under the terms of the ruby license.