viva-rubyzip 0.9.1.1

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