File: file_nonmutating_test.rb

package info (click to toggle)
ruby-zip 1.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 668 kB
  • sloc: ruby: 7,041; makefile: 11
file content (505 lines) | stat: -rw-r--r-- 15,035 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
require 'test_helper'
require 'zip/filesystem'

class ZipFsFileNonmutatingTest < MiniTest::Unit::TestCase
  def setup
    @zipsha = Digest::SHA1.file("test/data/zipWithDirs.zip")
    @zip_file = ::Zip::File.new("test/data/zipWithDirs.zip")
  end

  def teardown
    @zip_file.close if @zip_file
    assert_equal(@zipsha, Digest::SHA1.file("test/data/zipWithDirs.zip"))
  end

  def test_umask
    assert_equal(::File.umask, @zip_file.file.umask)
    @zip_file.file.umask(0006)
  end

  def test_exists?
    assert(! @zip_file.file.exists?("notAFile"))
    assert(@zip_file.file.exists?("file1"))
    assert(@zip_file.file.exists?("dir1"))
    assert(@zip_file.file.exists?("dir1/"))
    assert(@zip_file.file.exists?("dir1/file12"))
    assert(@zip_file.file.exist?("dir1/file12")) # notice, tests exist? alias of exists? !

    @zip_file.dir.chdir "dir1/"
    assert(!@zip_file.file.exists?("file1"))
    assert(@zip_file.file.exists?("file12"))
  end

  def test_open_read
    blockCalled = false
    @zip_file.file.open("file1", "r") {
      |f|
      blockCalled = true
      assert_equal("this is the entry 'file1' in my test archive!",
        f.readline.chomp)
    }
    assert(blockCalled)

    blockCalled = false
    @zip_file.file.open("file1", "rb") { # test binary flag is ignored
      |f|
      blockCalled = true
      assert_equal("this is the entry 'file1' in my test archive!",
        f.readline.chomp)
    }
    assert(blockCalled)

    blockCalled = false
    @zip_file.dir.chdir "dir2"
    @zip_file.file.open("file21", "r") {
      |f|
      blockCalled = true
      assert_equal("this is the entry 'dir2/file21' in my test archive!",
        f.readline.chomp)
    }
    assert(blockCalled)
    @zip_file.dir.chdir "/"

    assert_raises(Errno::ENOENT) {
      @zip_file.file.open("noSuchEntry")
    }

    begin
      is = @zip_file.file.open("file1")
      assert_equal("this is the entry 'file1' in my test archive!",
        is.readline.chomp)
    ensure
      is.close if is
    end
  end

  def test_new
    begin
      is = @zip_file.file.new("file1")
      assert_equal("this is the entry 'file1' in my test archive!",
        is.readline.chomp)
    ensure
      is.close if is
    end
    begin
      is = @zip_file.file.new("file1") {
  fail "should not call block"
      }
    ensure
      is.close if is
    end
  end

  def test_symlink
    assert_raises(NotImplementedError) {
      @zip_file.file.symlink("file1", "aSymlink")
    }
  end

  def test_size
    assert_raises(Errno::ENOENT) { @zip_file.file.size("notAFile") }
    assert_equal(72, @zip_file.file.size("file1"))
    assert_equal(0, @zip_file.file.size("dir2/dir21"))

    assert_equal(72, @zip_file.file.stat("file1").size)
    assert_equal(0, @zip_file.file.stat("dir2/dir21").size)
  end

  def test_size?
    assert_equal(nil, @zip_file.file.size?("notAFile"))
    assert_equal(72, @zip_file.file.size?("file1"))
    assert_equal(nil, @zip_file.file.size?("dir2/dir21"))

    assert_equal(72, @zip_file.file.stat("file1").size?)
    assert_equal(nil, @zip_file.file.stat("dir2/dir21").size?)
  end


  def test_file?
    assert(@zip_file.file.file?("file1"))
    assert(@zip_file.file.file?("dir2/file21"))
    assert(! @zip_file.file.file?("dir1"))
    assert(! @zip_file.file.file?("dir1/dir11"))

    assert(@zip_file.file.stat("file1").file?)
    assert(@zip_file.file.stat("dir2/file21").file?)
    assert(! @zip_file.file.stat("dir1").file?)
    assert(! @zip_file.file.stat("dir1/dir11").file?)
  end

  include ExtraAssertions

  def test_dirname
    assert_forwarded(File, :dirname, "retVal", "a/b/c/d") {
      @zip_file.file.dirname("a/b/c/d")
    }
  end

  def test_basename
    assert_forwarded(File, :basename, "retVal", "a/b/c/d") {
      @zip_file.file.basename("a/b/c/d")
    }
  end

  def test_split
    assert_forwarded(File, :split, "retVal", "a/b/c/d") {
      @zip_file.file.split("a/b/c/d")
    }
  end

  def test_join
    assert_equal("a/b/c", @zip_file.file.join("a/b", "c"))
    assert_equal("a/b/c/d", @zip_file.file.join("a/b", "c/d"))
    assert_equal("/c/d", @zip_file.file.join("", "c/d"))
    assert_equal("a/b/c/d", @zip_file.file.join("a", "b", "c", "d"))
  end

  def test_utime
    t_now = ::Zip::DOSTime.now
    t_bak = @zip_file.file.mtime("file1")
    @zip_file.file.utime(t_now, "file1")
    assert_equal(t_now, @zip_file.file.mtime("file1"))
    @zip_file.file.utime(t_bak, "file1")
    assert_equal(t_bak, @zip_file.file.mtime("file1"))
  end


  def assert_always_false(operation)
    assert(! @zip_file.file.send(operation, "noSuchFile"))
    assert(! @zip_file.file.send(operation, "file1"))
    assert(! @zip_file.file.send(operation, "dir1"))
    assert(! @zip_file.file.stat("file1").send(operation))
    assert(! @zip_file.file.stat("dir1").send(operation))
  end

  def assert_true_if_entry_exists(operation)
    assert(! @zip_file.file.send(operation, "noSuchFile"))
    assert(@zip_file.file.send(operation, "file1"))
    assert(@zip_file.file.send(operation, "dir1"))
    assert(@zip_file.file.stat("file1").send(operation))
    assert(@zip_file.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
    assert_raises(StandardError, "truncate not supported") {
      @zip_file.file.truncate("file1", 100)
    }
  end

  def assert_e_n_o_e_n_t(operation, args = ["NoSuchFile"])
    assert_raises(Errno::ENOENT) {
      @zip_file.file.send(operation, *args)
    }
  end

  def test_ftype
    assert_e_n_o_e_n_t(:ftype)
    assert_equal("file", @zip_file.file.ftype("file1"))
    assert_equal("directory", @zip_file.file.ftype("dir1/dir11"))
    assert_equal("directory", @zip_file.file.ftype("dir1/dir11/"))
  end

  def test_link
    assert_raises(NotImplementedError) {
      @zip_file.file.link("file1", "someOtherString")
    }
  end

  def test_directory?
    assert(! @zip_file.file.directory?("notAFile"))
    assert(! @zip_file.file.directory?("file1"))
    assert(! @zip_file.file.directory?("dir1/file11"))
    assert(@zip_file.file.directory?("dir1"))
    assert(@zip_file.file.directory?("dir1/"))
    assert(@zip_file.file.directory?("dir2/dir21"))

    assert(! @zip_file.file.stat("file1").directory?)
    assert(! @zip_file.file.stat("dir1/file11").directory?)
    assert(@zip_file.file.stat("dir1").directory?)
    assert(@zip_file.file.stat("dir1/").directory?)
    assert(@zip_file.file.stat("dir2/dir21").directory?)
  end

  def test_chown
    assert_equal(2, @zip_file.file.chown(1,2, "dir1", "file1"))
    assert_equal(1, @zip_file.file.stat("dir1").uid)
    assert_equal(2, @zip_file.file.stat("dir1").gid)
    assert_equal(2, @zip_file.file.chown(nil, nil, "dir1", "file1"))
  end

  def test_zero?
    assert(! @zip_file.file.zero?("notAFile"))
    assert(! @zip_file.file.zero?("file1"))
    assert(@zip_file.file.zero?("dir1"))
    blockCalled = false
    ::Zip::File.open("test/data/generated/5entry.zip") {
      |zf|
      blockCalled = true
      assert(zf.file.zero?("test/data/generated/empty.txt"))
    }
    assert(blockCalled)

    assert(! @zip_file.file.stat("file1").zero?)
    assert(@zip_file.file.stat("dir1").zero?)
    blockCalled = false
    ::Zip::File.open("test/data/generated/5entry.zip") {
      |zf|
      blockCalled = true
      assert(zf.file.stat("test/data/generated/empty.txt").zero?)
    }
    assert(blockCalled)
  end

  def test_expand_path
    ::Zip::File.open("test/data/zipWithDirs.zip") {
      |zf|
      assert_equal("/", zf.file.expand_path("."))
      zf.dir.chdir "dir1"
      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"))
    }
  end

  def test_mtime
    assert_equal(::Zip::DOSTime.at(1027694306),
      @zip_file.file.mtime("dir2/file21"))
    assert_equal(::Zip::DOSTime.at(1027690863),
      @zip_file.file.mtime("dir2/dir21"))
    assert_raises(Errno::ENOENT) {
      @zip_file.file.mtime("noSuchEntry")
    }

    assert_equal(::Zip::DOSTime.at(1027694306),
      @zip_file.file.stat("dir2/file21").mtime)
    assert_equal(::Zip::DOSTime.at(1027690863),
      @zip_file.file.stat("dir2/dir21").mtime)
  end

  def test_ctime
    assert_nil(@zip_file.file.ctime("file1"))
    assert_nil(@zip_file.file.stat("file1").ctime)
  end

  def test_atime
    assert_nil(@zip_file.file.atime("file1"))
    assert_nil(@zip_file.file.stat("file1").atime)
  end

  def test_readable?
    assert(! @zip_file.file.readable?("noSuchFile"))
    assert(@zip_file.file.readable?("file1"))
    assert(@zip_file.file.readable?("dir1"))
    assert(@zip_file.file.stat("file1").readable?)
    assert(@zip_file.file.stat("dir1").readable?)
  end

  def test_readable_real?
    assert(! @zip_file.file.readable_real?("noSuchFile"))
    assert(@zip_file.file.readable_real?("file1"))
    assert(@zip_file.file.readable_real?("dir1"))
    assert(@zip_file.file.stat("file1").readable_real?)
    assert(@zip_file.file.stat("dir1").readable_real?)
  end

  def test_writable?
    assert(! @zip_file.file.writable?("noSuchFile"))
    assert(@zip_file.file.writable?("file1"))
    assert(@zip_file.file.writable?("dir1"))
    assert(@zip_file.file.stat("file1").writable?)
    assert(@zip_file.file.stat("dir1").writable?)
  end

  def test_writable_real?
    assert(! @zip_file.file.writable_real?("noSuchFile"))
    assert(@zip_file.file.writable_real?("file1"))
    assert(@zip_file.file.writable_real?("dir1"))
    assert(@zip_file.file.stat("file1").writable_real?)
    assert(@zip_file.file.stat("dir1").writable_real?)
  end

  def test_executable?
    assert(! @zip_file.file.executable?("noSuchFile"))
    assert(! @zip_file.file.executable?("file1"))
    assert(@zip_file.file.executable?("dir1"))
    assert(! @zip_file.file.stat("file1").executable?)
    assert(@zip_file.file.stat("dir1").executable?)
  end

  def test_executable_real?
    assert(! @zip_file.file.executable_real?("noSuchFile"))
    assert(! @zip_file.file.executable_real?("file1"))
    assert(@zip_file.file.executable_real?("dir1"))
    assert(! @zip_file.file.stat("file1").executable_real?)
    assert(@zip_file.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
    assert_raises(NotImplementedError) {
      @zip_file.file.readlink("someString")
    }
  end

  def test_stat
    s = @zip_file.file.stat("file1")
    assert(s.kind_of?(File::Stat)) # It pretends
    assert_raises(Errno::ENOENT, "No such file or directory - noSuchFile") {
      @zip_file.file.stat("noSuchFile")
    }
  end

  def test_lstat
    assert(@zip_file.file.lstat("file1").file?)
  end

  def test_pipe
    assert_raises(NotImplementedError) {
      @zip_file.file.pipe
    }
  end

  def test_foreach
    ::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
      ref = []
      File.foreach("test/data/file1.txt") { |e| ref << e }
      index = 0

      zf.file.foreach("test/data/file1.txt") do |l|
        #Ruby replaces \n with \r\n automatically on windows
        newline = Zip::RUNNING_ON_WINDOWS ? l.gsub(/\r\n/, "\n") : l
        assert_equal(ref[index], newline)
        index = index.next
      end
      assert_equal(ref.size, index)
    end

    ::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
      ref = []
      File.foreach("test/data/file1.txt", " ") { |e| ref << e }
      index = 0

      zf.file.foreach("test/data/file1.txt", " ") do |l|
        #Ruby replaces \n with \r\n automatically on windows
        newline = Zip::RUNNING_ON_WINDOWS ? l.gsub(/\r\n/, "\n") : l
        assert_equal(ref[index], newline)
        index = index.next
      end
      assert_equal(ref.size, index)
    end
  end

  def test_glob
    ::Zip::File.open('test/data/globTest.zip') do |zf|
      {
        'globTest/foo.txt' => ['globTest/foo.txt'],
        '*/foo.txt' => ['globTest/foo.txt'],
        '**/foo.txt' => ['globTest/foo.txt','globTest/foo/bar/baz/foo.txt'],
        '*/foo/**/*.txt' => ['globTest/foo/bar/baz/foo.txt']
      }.each do |spec,expected_results|
        results = zf.glob(spec)
        assert results.all?{|entry| entry.is_a? ::Zip::Entry }

        result_strings = results.map(&:to_s)
        missing_matches = expected_results - result_strings
        extra_matches = result_strings - expected_results

        assert extra_matches.empty?, %Q{spec #{spec.inspect} has extra results #{extra_matches.inspect}}
        assert missing_matches.empty?, %Q{spec #{spec.inspect} missing results #{missing_matches.inspect}}
      end
    end

    ::Zip::File.open('test/data/globTest.zip') do |zf|
      results = []
      zf.glob('**/foo.txt') do |match|
        results << "<#{match.class.name}: #{match.to_s}>"
      end
      assert((not results.empty?), 'block not run, or run out of context')
      assert_equal 2, results.size
      assert_operator results, :include?, '<Zip::Entry: globTest/foo.txt>'
      assert_operator results, :include?, '<Zip::Entry: globTest/foo/bar/baz/foo.txt>'
    end
  end

  def test_popen
    if Zip::RUNNING_ON_WINDOWS
      #This is pretty much projectile vomit but it allows the test to be
      #run on windows also
      system_dir = ::File.popen('dir') { |f| f.read }.gsub(/Dir\(s\).*$/, '')
      zipfile_dir = @zip_file.file.popen('dir') { |f| f.read }.gsub(/Dir\(s\).*$/, '')
      assert_equal(system_dir, zipfile_dir)
    else
      assert_equal(::File.popen('ls') { |f| f.read },
                   @zip_file.file.popen('ls') { |f| f.read })
    end
  end

# Can be added later
#  def test_select
#    fail "implement test"
#  end

  def test_readlines
    ::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
      orig_file = ::File.readlines("test/data/file1.txt")
      zip_file = zf.file.readlines("test/data/file1.txt")

      #Ruby replaces \n with \r\n automatically on windows
      zip_file.each { |l| l.gsub!(/\r\n/, "\n") } if Zip::RUNNING_ON_WINDOWS

      assert_equal(orig_file, zip_file)
    end
  end

  def test_read
    ::Zip::File.open("test/data/generated/zipWithDir.zip") do |zf|
      orig_file = ::File.read("test/data/file1.txt")

      #Ruby replaces \n with \r\n automatically on windows
      zip_file = Zip::RUNNING_ON_WINDOWS ? \
          zf.file.read("test/data/file1.txt").gsub(/\r\n/, "\n") : zf.file.read("test/data/file1.txt")
      assert_equal(orig_file, zip_file)
    end
  end

end