~ubuntu-branches/ubuntu/intrepid/ruby1.8/intrepid-updates

« back to all changes in this revision

Viewing changes to test/fileutils/test_fileutils.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# $Id: test_fileutils.rb 11708 2007-02-12 23:01:19Z shyouhei $
 
2
 
 
3
require 'fileutils'
 
4
require 'fileasserts'
 
5
require 'pathname'
 
6
require 'tmpdir'
 
7
require 'test/unit'
 
8
 
 
9
class TestFileUtils < Test::Unit::TestCase
 
10
  TMPROOT = "#{Dir.tmpdir}/fileutils.rb.#{$$}"
 
11
end
 
12
 
 
13
prevdir = Dir.pwd
 
14
tmproot = TestFileUtils::TMPROOT
 
15
Dir.mkdir tmproot unless File.directory?(tmproot)
 
16
Dir.chdir tmproot
 
17
 
 
18
def have_drive_letter?
 
19
  /djgpp|mswin(?!ce)|mingw|bcc|emx/ =~ RUBY_PLATFORM
 
20
end
 
21
 
 
22
def have_file_perm?
 
23
  /djgpp|mswin|mingw|bcc|wince|emx/ !~ RUBY_PLATFORM
 
24
end
 
25
 
 
26
$fileutils_rb_have_symlink = nil
 
27
 
 
28
def have_symlink?
 
29
  if $fileutils_rb_have_symlink == nil
 
30
    $fileutils_rb_have_symlink = check_have_symlink?
 
31
  end
 
32
  $fileutils_rb_have_symlink
 
33
end
 
34
 
 
35
def check_have_symlink?
 
36
  File.symlink nil, nil
 
37
rescue NotImplementedError
 
38
  return false
 
39
rescue
 
40
  return true
 
41
end
 
42
 
 
43
$fileutils_rb_have_hardlink = nil
 
44
 
 
45
def have_hardlink?
 
46
  if $fileutils_rb_have_hardlink == nil
 
47
    $fileutils_rb_have_hardlink = check_have_hardlink?
 
48
  end
 
49
  $fileutils_rb_have_hardlink
 
50
end
 
51
 
 
52
def check_have_hardlink?
 
53
  File.link nil, nil
 
54
rescue NotImplementedError
 
55
  return false
 
56
rescue
 
57
  return true
 
58
end
 
59
 
 
60
begin
 
61
  Dir.mkdir("\n")
 
62
  Dir.rmdir("\n")
 
63
  def lf_in_path_allowed?
 
64
    true
 
65
  end
 
66
rescue
 
67
  def lf_in_path_allowed?
 
68
    false
 
69
  end
 
70
end
 
71
 
 
72
Dir.chdir prevdir
 
73
Dir.rmdir tmproot
 
74
 
 
75
class TestFileUtils
 
76
 
 
77
  include FileUtils
 
78
 
 
79
  def check_singleton(name)
 
80
    assert_equal true, ::FileUtils.public_methods.include?(name.to_s)
 
81
  end
 
82
 
 
83
  def my_rm_rf(path)
 
84
    if File.exist?('/bin/rm')
 
85
      system %Q[/bin/rm -rf "#{path}"]
 
86
    else
 
87
      FileUtils.rm_rf path
 
88
    end
 
89
  end
 
90
 
 
91
  def mymkdir(path)
 
92
    Dir.mkdir path
 
93
    File.chown nil, Process.gid, path if have_file_perm?
 
94
  end
 
95
 
 
96
  def setup
 
97
    @prevdir = Dir.pwd
 
98
    tmproot = TMPROOT
 
99
    mymkdir tmproot unless File.directory?(tmproot)
 
100
    Dir.chdir tmproot
 
101
    my_rm_rf 'data'; mymkdir 'data'
 
102
    my_rm_rf 'tmp';  mymkdir 'tmp'
 
103
    prepare_data_file
 
104
  end
 
105
 
 
106
  def teardown
 
107
    tmproot = Dir.pwd
 
108
    Dir.chdir @prevdir
 
109
    my_rm_rf tmproot
 
110
  end
 
111
 
 
112
 
 
113
  TARGETS = %w( data/a data/all data/random data/zero )
 
114
 
 
115
  def prepare_data_file
 
116
    File.open('data/a', 'w') {|f|
 
117
      32.times do
 
118
        f.puts 'a' * 50
 
119
      end
 
120
    }
 
121
 
 
122
    all_chars = (0..255).map {|n| n.chr }.join('')
 
123
    File.open('data/all', 'w') {|f|
 
124
      32.times do
 
125
        f.puts all_chars
 
126
      end
 
127
    }
 
128
 
 
129
    random_chars = (0...50).map { rand(256).chr }.join('')
 
130
    File.open('data/random', 'w') {|f|
 
131
      32.times do
 
132
        f.puts random_chars
 
133
      end
 
134
    }
 
135
 
 
136
    File.open('data/zero', 'w') {|f|
 
137
      ;
 
138
    }
 
139
  end
 
140
 
 
141
  BIGFILE = 'data/big'
 
142
 
 
143
  def prepare_big_file
 
144
    File.open('data/big', 'w') {|f|
 
145
      (4 * 1024 * 1024 / 256).times do   # 4MB
 
146
        f.print "aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa aaaa\n"
 
147
      end
 
148
    }
 
149
  end
 
150
 
 
151
  def prepare_time_data
 
152
    File.open('data/old',    'w') {|f| f.puts 'dummy' }
 
153
    File.open('data/newer',  'w') {|f| f.puts 'dummy' }
 
154
    File.open('data/newest', 'w') {|f| f.puts 'dummy' }
 
155
    t = Time.now
 
156
    File.utime t-8, t-8, 'data/old'
 
157
    File.utime t-4, t-4, 'data/newer'
 
158
  end
 
159
 
 
160
  def each_srcdest
 
161
    TARGETS.each do |path|
 
162
      yield path, "tmp/#{File.basename(path)}"
 
163
    end
 
164
  end
 
165
 
 
166
  #
 
167
  # Test Cases
 
168
  #
 
169
 
 
170
  def test_pwd
 
171
    check_singleton :pwd
 
172
 
 
173
    assert_equal Dir.pwd, pwd()
 
174
 
 
175
    cwd = Dir.pwd
 
176
if have_drive_letter?
 
177
    cd('C:/') {
 
178
      assert_equal 'C:/', pwd()
 
179
    }
 
180
    assert_equal cwd, pwd()
 
181
else
 
182
    cd('/') {
 
183
      assert_equal '/', pwd()
 
184
    }
 
185
    assert_equal cwd, pwd()
 
186
end
 
187
  end
 
188
 
 
189
  def test_cmp
 
190
    check_singleton :cmp
 
191
 
 
192
    TARGETS.each do |fname|
 
193
      assert cmp(fname, fname), 'not same?'
 
194
    end
 
195
    assert_raises(ArgumentError) {
 
196
      cmp TARGETS[0], TARGETS[0], :undefinedoption => true
 
197
    }
 
198
 
 
199
    # pathname
 
200
    touch 'tmp/cmptmp'
 
201
    assert_nothing_raised {
 
202
      cmp Pathname.new('tmp/cmptmp'), 'tmp/cmptmp'
 
203
      cmp 'tmp/cmptmp', Pathname.new('tmp/cmptmp')
 
204
      cmp Pathname.new('tmp/cmptmp'), Pathname.new('tmp/cmptmp')
 
205
    }
 
206
  end
 
207
 
 
208
  def test_cp
 
209
    check_singleton :cp
 
210
 
 
211
    each_srcdest do |srcpath, destpath|
 
212
      cp srcpath, destpath
 
213
      assert_same_file srcpath, destpath
 
214
 
 
215
      cp srcpath, File.dirname(destpath)
 
216
      assert_same_file srcpath, destpath
 
217
 
 
218
      cp srcpath, File.dirname(destpath) + '/'
 
219
      assert_same_file srcpath, destpath
 
220
 
 
221
      cp srcpath, destpath, :preserve => true
 
222
      assert_same_file srcpath, destpath
 
223
      assert_same_entry srcpath, destpath
 
224
    end
 
225
 
 
226
    # src==dest (1) same path
 
227
    touch 'tmp/cptmp'
 
228
    assert_raises(ArgumentError) {
 
229
      cp 'tmp/cptmp', 'tmp/cptmp'
 
230
    }
 
231
if have_symlink?
 
232
    # src==dest (2) symlink and its target
 
233
    File.symlink 'cptmp', 'tmp/cptmp_symlink'
 
234
    assert_raises(ArgumentError) {
 
235
      cp 'tmp/cptmp', 'tmp/cptmp_symlink'
 
236
    }
 
237
    assert_raises(ArgumentError) {
 
238
      cp 'tmp/cptmp_symlink', 'tmp/cptmp'
 
239
    }
 
240
    # src==dest (3) looped symlink
 
241
    File.symlink 'symlink', 'tmp/symlink'
 
242
    assert_raises(Errno::ELOOP) {
 
243
      cp 'tmp/symlink', 'tmp/symlink'
 
244
    }
 
245
end
 
246
 
 
247
    # pathname
 
248
    assert_nothing_raised {
 
249
      cp 'tmp/cptmp', Pathname.new('tmp/tmpdest')
 
250
      cp Pathname.new('tmp/cptmp'), 'tmp/tmpdest'
 
251
      cp Pathname.new('tmp/cptmp'), Pathname.new('tmp/tmpdest')
 
252
      mkdir 'tmp/tmpdir'
 
253
      cp ['tmp/cptmp', 'tmp/tmpdest'], Pathname.new('tmp/tmpdir')
 
254
    }
 
255
  end
 
256
 
 
257
  def test_cp_r
 
258
    check_singleton :cp_r
 
259
 
 
260
    cp_r 'data', 'tmp'
 
261
    TARGETS.each do |fname|
 
262
      assert_same_file fname, "tmp/#{fname}"
 
263
    end
 
264
 
 
265
    cp_r 'data', 'tmp2', :preserve => true
 
266
    TARGETS.each do |fname|
 
267
      assert_same_entry fname, "tmp2/#{File.basename(fname)}"
 
268
      assert_same_file fname, "tmp2/#{File.basename(fname)}"
 
269
    end
 
270
 
 
271
    # a/* -> b/*
 
272
    mkdir 'tmp/cpr_src'
 
273
    mkdir 'tmp/cpr_dest'
 
274
    File.open('tmp/cpr_src/a', 'w') {|f| f.puts 'a' }
 
275
    File.open('tmp/cpr_src/b', 'w') {|f| f.puts 'b' }
 
276
    File.open('tmp/cpr_src/c', 'w') {|f| f.puts 'c' }
 
277
    mkdir 'tmp/cpr_src/d'
 
278
    cp_r 'tmp/cpr_src/.', 'tmp/cpr_dest'
 
279
    assert_same_file 'tmp/cpr_src/a', 'tmp/cpr_dest/a'
 
280
    assert_same_file 'tmp/cpr_src/b', 'tmp/cpr_dest/b'
 
281
    assert_same_file 'tmp/cpr_src/c', 'tmp/cpr_dest/c'
 
282
    assert_directory 'tmp/cpr_dest/d'
 
283
    my_rm_rf 'tmp/cpr_src'
 
284
    my_rm_rf 'tmp/cpr_dest'
 
285
 
 
286
if have_symlink?
 
287
    # symlink in a directory
 
288
    mkdir 'tmp/cpr_src'
 
289
    ln_s 'SLdest', 'tmp/cpr_src/symlink'
 
290
    cp_r 'tmp/cpr_src', 'tmp/cpr_dest'
 
291
    assert_symlink 'tmp/cpr_dest/symlink'
 
292
    assert_equal 'SLdest', File.readlink('tmp/cpr_dest/symlink')
 
293
 
 
294
    # root is a symlink
 
295
    ln_s 'cpr_src', 'tmp/cpr_src2'
 
296
    cp_r 'tmp/cpr_src2', 'tmp/cpr_dest2'
 
297
    assert_directory 'tmp/cpr_dest2'
 
298
    #assert_not_symlink 'tmp/cpr_dest2'
 
299
    # * 2005-05-26: feature change on trunk
 
300
    #assert_symlink 'tmp/cpr_dest2'
 
301
    # * 2005-09-19: revert for 1.8 (:dereference_root => true by default)
 
302
    assert_not_symlink 'tmp/cpr_dest2'
 
303
    assert_symlink 'tmp/cpr_dest2/symlink'
 
304
    assert_equal 'SLdest', File.readlink('tmp/cpr_dest2/symlink')
 
305
end
 
306
 
 
307
    # pathname
 
308
    touch 'tmp/cprtmp'
 
309
    assert_nothing_raised {
 
310
      cp_r Pathname.new('tmp/cprtmp'), 'tmp/tmpdest'
 
311
      cp_r 'tmp/cprtmp', Pathname.new('tmp/tmpdest')
 
312
      cp_r Pathname.new('tmp/cprtmp'), Pathname.new('tmp/tmpdest')
 
313
    }
 
314
  end
 
315
 
 
316
  def test_mv
 
317
    check_singleton :mv
 
318
 
 
319
    mkdir 'tmp/dest'
 
320
    TARGETS.each do |fname|
 
321
      cp fname, 'tmp/mvsrc'
 
322
      mv 'tmp/mvsrc', 'tmp/mvdest'
 
323
      assert_same_file fname, 'tmp/mvdest'
 
324
 
 
325
      mv 'tmp/mvdest', 'tmp/dest/'
 
326
      assert_same_file fname, 'tmp/dest/mvdest'
 
327
 
 
328
      mv 'tmp/dest/mvdest', 'tmp'
 
329
      assert_same_file fname, 'tmp/mvdest'
 
330
    end
 
331
 
 
332
    # [ruby-talk:124368]
 
333
    mkdir 'tmp/tmpdir'
 
334
    mkdir_p 'tmp/dest2/tmpdir'
 
335
    assert_raises(Errno::EEXIST) {
 
336
      mv 'tmp/tmpdir', 'tmp/dest2'
 
337
    }
 
338
    mkdir 'tmp/dest2/tmpdir/junk'
 
339
    assert_raises(Errno::EEXIST) {
 
340
      mv 'tmp/tmpdir', 'tmp/dest2'
 
341
    }
 
342
 
 
343
    # src==dest (1) same path
 
344
    touch 'tmp/cptmp'
 
345
    assert_raises(ArgumentError) {
 
346
      mv 'tmp/cptmp', 'tmp/cptmp'
 
347
    }
 
348
if have_symlink?
 
349
    # src==dest (2) symlink and its target
 
350
    File.symlink 'cptmp', 'tmp/cptmp_symlink'
 
351
    assert_raises(ArgumentError) {
 
352
      mv 'tmp/cptmp', 'tmp/cptmp_symlink'
 
353
    }
 
354
    assert_raises(ArgumentError) {
 
355
      mv 'tmp/cptmp_symlink', 'tmp/cptmp'
 
356
    }
 
357
    # src==dest (3) looped symlink
 
358
    File.symlink 'symlink', 'tmp/symlink'
 
359
    assert_raises(Errno::ELOOP) {
 
360
      mv 'tmp/symlink', 'tmp/symlink'
 
361
    }
 
362
end
 
363
 
 
364
    # pathname
 
365
    assert_nothing_raised {
 
366
      touch 'tmp/mvtmpsrc'
 
367
      mv Pathname.new('tmp/mvtmpsrc'), 'tmp/mvtmpdest'
 
368
      touch 'tmp/mvtmpsrc'
 
369
      mv 'tmp/mvtmpsrc', Pathname.new('tmp/mvtmpdest')
 
370
      touch 'tmp/mvtmpsrc'
 
371
      mv Pathname.new('tmp/mvtmpsrc'), Pathname.new('tmp/mvtmpdest')
 
372
    }
 
373
  end
 
374
 
 
375
  def test_rm
 
376
    check_singleton :rm
 
377
 
 
378
    TARGETS.each do |fname|
 
379
      cp fname, 'tmp/rmsrc'
 
380
      rm 'tmp/rmsrc'
 
381
      assert_file_not_exist 'tmp/rmsrc'
 
382
    end
 
383
 
 
384
    # pathname
 
385
    touch 'tmp/rmtmp1'
 
386
    touch 'tmp/rmtmp2'
 
387
    touch 'tmp/rmtmp3'
 
388
    assert_nothing_raised {
 
389
      rm Pathname.new('tmp/rmtmp1')
 
390
      rm [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
 
391
    }
 
392
    assert_file_not_exist 'tmp/rmtmp1'
 
393
    assert_file_not_exist 'tmp/rmtmp2'
 
394
    assert_file_not_exist 'tmp/rmtmp3'
 
395
  end
 
396
 
 
397
  def test_rm_f
 
398
    check_singleton :rm_f
 
399
 
 
400
    TARGETS.each do |fname|
 
401
      cp fname, 'tmp/rmsrc'
 
402
      rm_f 'tmp/rmsrc'
 
403
      assert_file_not_exist 'tmp/rmsrc'
 
404
    end
 
405
 
 
406
if have_symlink?
 
407
    File.open('tmp/lnf_symlink_src', 'w') {|f| f.puts 'dummy' }
 
408
    File.symlink 'tmp/lnf_symlink_src', 'tmp/lnf_symlink_dest'
 
409
    rm_f 'tmp/lnf_symlink_dest'
 
410
    assert_file_not_exist 'tmp/lnf_symlink_dest'
 
411
    assert_file_exist     'tmp/lnf_symlink_src'
 
412
end
 
413
 
 
414
    rm_f 'notexistdatafile'
 
415
    rm_f 'tmp/notexistdatafile'
 
416
    my_rm_rf 'tmpdatadir'
 
417
    Dir.mkdir 'tmpdatadir'
 
418
    # rm_f 'tmpdatadir'
 
419
    Dir.rmdir 'tmpdatadir'
 
420
 
 
421
    Dir.mkdir 'tmp/tmpdir'
 
422
    File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
 
423
    File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
 
424
    rm_f ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c']
 
425
    assert_file_not_exist 'tmp/tmpdir/a'
 
426
    assert_file_not_exist 'tmp/tmpdir/c'
 
427
    Dir.rmdir 'tmp/tmpdir'
 
428
 
 
429
    # pathname
 
430
    touch 'tmp/rmtmp1'
 
431
    touch 'tmp/rmtmp2'
 
432
    touch 'tmp/rmtmp3'
 
433
    touch 'tmp/rmtmp4'
 
434
    assert_nothing_raised {
 
435
      rm_f Pathname.new('tmp/rmtmp1')
 
436
      rm_f [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
 
437
    }
 
438
    assert_file_not_exist 'tmp/rmtmp1'
 
439
    assert_file_not_exist 'tmp/rmtmp2'
 
440
    assert_file_not_exist 'tmp/rmtmp3'
 
441
    assert_file_exist 'tmp/rmtmp4'
 
442
  end
 
443
 
 
444
  def test_rm_r
 
445
    check_singleton :rm_r
 
446
 
 
447
    my_rm_rf 'tmpdatadir'
 
448
 
 
449
    Dir.mkdir 'tmpdatadir'
 
450
    rm_r 'tmpdatadir'
 
451
    assert_file_not_exist 'tmpdatadir'
 
452
 
 
453
    Dir.mkdir 'tmpdatadir'
 
454
    rm_r 'tmpdatadir/'
 
455
    assert_file_not_exist 'tmpdatadir'
 
456
 
 
457
    Dir.mkdir 'tmp/tmpdir'
 
458
    rm_r 'tmp/tmpdir/'
 
459
    assert_file_not_exist 'tmp/tmpdir'
 
460
    assert_file_exist     'tmp'
 
461
 
 
462
    Dir.mkdir 'tmp/tmpdir'
 
463
    rm_r 'tmp/tmpdir'
 
464
    assert_file_not_exist 'tmp/tmpdir'
 
465
    assert_file_exist     'tmp'
 
466
 
 
467
    Dir.mkdir 'tmp/tmpdir'
 
468
    File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
 
469
    File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
 
470
    File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
 
471
    rm_r 'tmp/tmpdir'
 
472
    assert_file_not_exist 'tmp/tmpdir'
 
473
    assert_file_exist     'tmp'
 
474
 
 
475
    Dir.mkdir 'tmp/tmpdir'
 
476
    File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
 
477
    File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
 
478
    rm_r ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c'], :force => true
 
479
    assert_file_not_exist 'tmp/tmpdir/a'
 
480
    assert_file_not_exist 'tmp/tmpdir/c'
 
481
    Dir.rmdir 'tmp/tmpdir'
 
482
 
 
483
if have_symlink?
 
484
    # [ruby-talk:94635] a symlink to the directory
 
485
    Dir.mkdir 'tmp/tmpdir'
 
486
    File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
 
487
    rm_r 'tmp/tmpdir'
 
488
    assert_file_not_exist 'tmp/tmpdir'
 
489
    assert_file_exist     'tmp'
 
490
end
 
491
 
 
492
    # pathname
 
493
    Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
 
494
    Dir.mkdir 'tmp/tmpdir2'; touch 'tmp/tmpdir2/tmp'
 
495
    Dir.mkdir 'tmp/tmpdir3'; touch 'tmp/tmpdir3/tmp'
 
496
    assert_nothing_raised {
 
497
      rm_r Pathname.new('tmp/tmpdir1')
 
498
      rm_r [Pathname.new('tmp/tmpdir2'), Pathname.new('tmp/tmpdir3')]
 
499
    }
 
500
    assert_file_not_exist 'tmp/tmpdir1'
 
501
    assert_file_not_exist 'tmp/tmpdir2'
 
502
    assert_file_not_exist 'tmp/tmpdir3'
 
503
  end
 
504
 
 
505
  def test_remove_entry_secure
 
506
    check_singleton :remove_entry_secure
 
507
 
 
508
    my_rm_rf 'tmpdatadir'
 
509
 
 
510
    Dir.mkdir 'tmpdatadir'
 
511
    remove_entry_secure 'tmpdatadir'
 
512
    assert_file_not_exist 'tmpdatadir'
 
513
 
 
514
    Dir.mkdir 'tmpdatadir'
 
515
    remove_entry_secure 'tmpdatadir/'
 
516
    assert_file_not_exist 'tmpdatadir'
 
517
 
 
518
    Dir.mkdir 'tmp/tmpdir'
 
519
    remove_entry_secure 'tmp/tmpdir/'
 
520
    assert_file_not_exist 'tmp/tmpdir'
 
521
    assert_file_exist     'tmp'
 
522
 
 
523
    Dir.mkdir 'tmp/tmpdir'
 
524
    remove_entry_secure 'tmp/tmpdir'
 
525
    assert_file_not_exist 'tmp/tmpdir'
 
526
    assert_file_exist     'tmp'
 
527
 
 
528
    Dir.mkdir 'tmp/tmpdir'
 
529
    File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
 
530
    File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
 
531
    File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
 
532
    remove_entry_secure 'tmp/tmpdir'
 
533
    assert_file_not_exist 'tmp/tmpdir'
 
534
    assert_file_exist     'tmp'
 
535
 
 
536
    Dir.mkdir 'tmp/tmpdir'
 
537
    File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
 
538
    File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
 
539
    remove_entry_secure 'tmp/tmpdir/a', true
 
540
    remove_entry_secure 'tmp/tmpdir/b', true
 
541
    remove_entry_secure 'tmp/tmpdir/c', true
 
542
    assert_file_not_exist 'tmp/tmpdir/a'
 
543
    assert_file_not_exist 'tmp/tmpdir/c'
 
544
    Dir.rmdir 'tmp/tmpdir'
 
545
 
 
546
if have_symlink?
 
547
    # [ruby-talk:94635] a symlink to the directory
 
548
    Dir.mkdir 'tmp/tmpdir'
 
549
    File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
 
550
    remove_entry_secure 'tmp/tmpdir'
 
551
    assert_file_not_exist 'tmp/tmpdir'
 
552
    assert_file_exist     'tmp'
 
553
end
 
554
 
 
555
    # pathname
 
556
    Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
 
557
    assert_nothing_raised {
 
558
      remove_entry_secure Pathname.new('tmp/tmpdir1')
 
559
    }
 
560
    assert_file_not_exist 'tmp/tmpdir1'
 
561
  end
 
562
 
 
563
  def test_with_big_file
 
564
    prepare_big_file
 
565
 
 
566
    cp BIGFILE, 'tmp/cpdest'
 
567
    assert_same_file BIGFILE, 'tmp/cpdest'
 
568
    assert cmp(BIGFILE, 'tmp/cpdest'), 'orig != copied'
 
569
 
 
570
    mv 'tmp/cpdest', 'tmp/mvdest'
 
571
    assert_same_file BIGFILE, 'tmp/mvdest'
 
572
    assert_file_not_exist 'tmp/cpdest'
 
573
 
 
574
    rm 'tmp/mvdest'
 
575
    assert_file_not_exist 'tmp/mvdest'
 
576
  end
 
577
 
 
578
if have_hardlink?
 
579
  def test_ln
 
580
    TARGETS.each do |fname|
 
581
      ln fname, 'tmp/lndest'
 
582
      assert_same_file fname, 'tmp/lndest'
 
583
      File.unlink 'tmp/lndest'
 
584
    end
 
585
 
 
586
    ln TARGETS, 'tmp'
 
587
    TARGETS.each do |fname|
 
588
      assert_same_file fname, 'tmp/' + File.basename(fname)
 
589
    end
 
590
    TARGETS.each do |fname|
 
591
      File.unlink 'tmp/' + File.basename(fname)
 
592
    end
 
593
 
 
594
    # src==dest (1) same path
 
595
    touch 'tmp/cptmp'
 
596
    assert_raises(Errno::EEXIST) {
 
597
      ln 'tmp/cptmp', 'tmp/cptmp'
 
598
    }
 
599
if have_symlink?
 
600
    # src==dest (2) symlink and its target
 
601
    File.symlink 'cptmp', 'tmp/symlink'
 
602
    assert_raises(Errno::EEXIST) {
 
603
      ln 'tmp/cptmp', 'tmp/symlink'   # normal file -> symlink
 
604
    }
 
605
    assert_raises(Errno::EEXIST) {
 
606
      ln 'tmp/symlink', 'tmp/cptmp'   # symlink -> normal file
 
607
    }
 
608
    # src==dest (3) looped symlink
 
609
    File.symlink 'cptmp_symlink', 'tmp/cptmp_symlink'
 
610
    begin
 
611
      ln 'tmp/cptmp_symlink', 'tmp/cptmp_symlink'
 
612
    rescue => err
 
613
      assert_kind_of SystemCallError, err
 
614
    end
 
615
end
 
616
 
 
617
    # pathname
 
618
    touch 'tmp/lntmp'
 
619
    assert_nothing_raised {
 
620
      ln Pathname.new('tmp/lntmp'), 'tmp/lndesttmp1'
 
621
      ln 'tmp/lntmp', Pathname.new('tmp/lndesttmp2')
 
622
      ln Pathname.new('tmp/lntmp'), Pathname.new('tmp/lndesttmp3')
 
623
    }
 
624
  end
 
625
end
 
626
 
 
627
if have_symlink?
 
628
  def test_ln_s
 
629
    check_singleton :ln_s
 
630
 
 
631
    TARGETS.each do |fname|
 
632
      ln_s fname, 'tmp/lnsdest'
 
633
      assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
 
634
      assert_equal fname, File.readlink('tmp/lnsdest')
 
635
      rm_f 'tmp/lnsdest'
 
636
    end
 
637
    assert_nothing_raised {
 
638
      ln_s 'symlink', 'tmp/symlink'
 
639
    }
 
640
    assert_symlink 'tmp/symlink'
 
641
 
 
642
    # pathname
 
643
    touch 'tmp/lnsdest'
 
644
    assert_nothing_raised {
 
645
      ln_s Pathname.new('lnsdest'), 'tmp/symlink_tmp1'
 
646
      ln_s 'lnsdest', Pathname.new('tmp/symlink_tmp2')
 
647
      ln_s Pathname.new('lnsdest'), Pathname.new('tmp/symlink_tmp3')
 
648
    }
 
649
  end
 
650
end
 
651
 
 
652
if have_symlink?
 
653
  def test_ln_sf
 
654
    check_singleton :ln_sf
 
655
 
 
656
    TARGETS.each do |fname|
 
657
      ln_sf fname, 'tmp/lnsdest'
 
658
      assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
 
659
      assert_equal fname, File.readlink('tmp/lnsdest')
 
660
      ln_sf fname, 'tmp/lnsdest'
 
661
      ln_sf fname, 'tmp/lnsdest'
 
662
    end
 
663
    assert_nothing_raised {
 
664
      ln_sf 'symlink', 'tmp/symlink'
 
665
    }
 
666
 
 
667
    # pathname
 
668
    touch 'tmp/lns_dest'
 
669
    assert_nothing_raised {
 
670
      ln_sf Pathname.new('lns_dest'), 'tmp/symlink_tmp1'
 
671
      ln_sf 'lns_dest', Pathname.new('tmp/symlink_tmp2')
 
672
      ln_sf Pathname.new('lns_dest'), Pathname.new('tmp/symlink_tmp3')
 
673
    }
 
674
  end
 
675
end
 
676
 
 
677
  def test_mkdir
 
678
    check_singleton :mkdir
 
679
 
 
680
    my_rm_rf 'tmpdatadir'
 
681
    mkdir 'tmpdatadir'
 
682
    assert_directory 'tmpdatadir'
 
683
    Dir.rmdir 'tmpdatadir'
 
684
 
 
685
    mkdir 'tmpdatadir/'
 
686
    assert_directory 'tmpdatadir'
 
687
    Dir.rmdir 'tmpdatadir'
 
688
 
 
689
    mkdir 'tmp/mkdirdest'
 
690
    assert_directory 'tmp/mkdirdest'
 
691
    Dir.rmdir 'tmp/mkdirdest'
 
692
 
 
693
    mkdir 'tmp/tmp', :mode => 0700
 
694
    assert_directory 'tmp/tmp'
 
695
    assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
 
696
    Dir.rmdir 'tmp/tmp'
 
697
 
 
698
if have_file_perm?
 
699
    mkdir 'tmp/tmp', :mode => 07777
 
700
    assert_directory 'tmp/tmp'
 
701
    assert_equal 07777, (File.stat('tmp/tmp').mode & 07777)
 
702
    Dir.rmdir 'tmp/tmp'
 
703
end
 
704
 
 
705
if lf_in_path_allowed?
 
706
    mkdir "tmp-first-line\ntmp-second-line"
 
707
    assert_directory "tmp-first-line\ntmp-second-line"
 
708
    Dir.rmdir "tmp-first-line\ntmp-second-line"
 
709
end
 
710
 
 
711
    # pathname
 
712
    assert_nothing_raised {
 
713
      mkdir Pathname.new('tmp/tmpdirtmp')
 
714
      mkdir [Pathname.new('tmp/tmpdirtmp2'), Pathname.new('tmp/tmpdirtmp3')]
 
715
    }
 
716
  end
 
717
 
 
718
  def test_mkdir_p
 
719
    check_singleton :mkdir_p
 
720
 
 
721
    dirs = %w(
 
722
      tmpdir/dir/
 
723
      tmpdir/dir/./
 
724
      tmpdir/dir/./.././dir/
 
725
      tmpdir/a
 
726
      tmpdir/a/
 
727
      tmpdir/a/b
 
728
      tmpdir/a/b/
 
729
      tmpdir/a/b/c/
 
730
      tmpdir/a/b/c
 
731
      tmpdir/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a
 
732
      tmpdir/a/a
 
733
    )
 
734
    my_rm_rf 'tmpdir'
 
735
    dirs.each do |d|
 
736
      mkdir_p d
 
737
      assert_directory d
 
738
      assert_file_not_exist "#{d}/a"
 
739
      assert_file_not_exist "#{d}/b"
 
740
      assert_file_not_exist "#{d}/c"
 
741
      my_rm_rf 'tmpdir'
 
742
    end
 
743
    dirs.each do |d|
 
744
      mkdir_p d
 
745
      assert_directory d
 
746
    end
 
747
    rm_rf 'tmpdir'
 
748
    dirs.each do |d|
 
749
      mkdir_p "#{Dir.pwd}/#{d}"
 
750
      assert_directory d
 
751
    end
 
752
    rm_rf 'tmpdir'
 
753
 
 
754
    mkdir_p 'tmp/tmp/tmp', :mode => 0700
 
755
    assert_directory 'tmp/tmp'
 
756
    assert_directory 'tmp/tmp/tmp'
 
757
    assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
 
758
    assert_equal 0700, (File.stat('tmp/tmp/tmp').mode & 0777) if have_file_perm?
 
759
    rm_rf 'tmp/tmp'
 
760
 
 
761
    mkdir_p 'tmp/tmp', :mode => 0
 
762
    assert_directory 'tmp/tmp'
 
763
    assert_equal 0, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
 
764
    # DO NOT USE rm_rf here.
 
765
    # (rm(1) try to chdir to parent directory, it fails to remove directory.)
 
766
    Dir.rmdir 'tmp/tmp'
 
767
    Dir.rmdir 'tmp'
 
768
 
 
769
if have_file_perm?
 
770
    mkdir_p 'tmp/tmp/tmp', :mode => 07777
 
771
    assert_directory 'tmp/tmp/tmp'
 
772
    assert_equal 07777, (File.stat('tmp/tmp/tmp').mode & 07777)
 
773
    Dir.rmdir 'tmp/tmp/tmp'
 
774
    Dir.rmdir 'tmp/tmp'
 
775
end
 
776
 
 
777
    # pathname
 
778
    assert_nothing_raised {
 
779
      mkdir_p Pathname.new('tmp/tmp/tmp')
 
780
    }
 
781
  end
 
782
 
 
783
  def test_install
 
784
    check_singleton :install
 
785
 
 
786
    File.open('tmp/aaa', 'w') {|f| f.puts 'aaa' }
 
787
    File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' }
 
788
    install 'tmp/aaa', 'tmp/bbb', :mode => 0600
 
789
    assert_equal "aaa\n", File.read('tmp/bbb')
 
790
    assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
 
791
 
 
792
    t = File.mtime('tmp/bbb')
 
793
    install 'tmp/aaa', 'tmp/bbb'
 
794
    assert_equal "aaa\n", File.read('tmp/bbb')
 
795
    assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
 
796
    assert_equal t, File.mtime('tmp/bbb')
 
797
 
 
798
    File.unlink 'tmp/aaa'
 
799
    File.unlink 'tmp/bbb'
 
800
 
 
801
    # src==dest (1) same path
 
802
    touch 'tmp/cptmp'
 
803
    assert_raises(ArgumentError) {
 
804
      install 'tmp/cptmp', 'tmp/cptmp'
 
805
    }
 
806
if have_symlink?
 
807
    # src==dest (2) symlink and its target
 
808
    File.symlink 'cptmp', 'tmp/cptmp_symlink'
 
809
    assert_raises(ArgumentError) {
 
810
      install 'tmp/cptmp', 'tmp/cptmp_symlink'
 
811
    }
 
812
    assert_raises(ArgumentError) {
 
813
      install 'tmp/cptmp_symlink', 'tmp/cptmp'
 
814
    }
 
815
    # src==dest (3) looped symlink
 
816
    File.symlink 'symlink', 'tmp/symlink'
 
817
    assert_raises(Errno::ELOOP) {
 
818
      # File#install invokes open(2), always ELOOP must be raised
 
819
      install 'tmp/symlink', 'tmp/symlink'
 
820
    }
 
821
end
 
822
 
 
823
    # pathname
 
824
    assert_nothing_raised {
 
825
      rm_f 'tmp/a'; touch 'tmp/a'
 
826
      install 'tmp/a', Pathname.new('tmp/b')
 
827
      rm_f 'tmp/a'; touch 'tmp/a'
 
828
      install Pathname.new('tmp/a'), 'tmp/b'
 
829
      rm_f 'tmp/a'; touch 'tmp/a'
 
830
      install Pathname.new('tmp/a'), Pathname.new('tmp/b')
 
831
      rm_f 'tmp/a'
 
832
      touch 'tmp/a'
 
833
      touch 'tmp/b'
 
834
      mkdir 'tmp/dest'
 
835
      install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], 'tmp/dest'
 
836
      my_rm_rf 'tmp/dest'
 
837
      mkdir 'tmp/dest'
 
838
      install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], Pathname.new('tmp/dest')
 
839
    }
 
840
  end
 
841
 
 
842
if have_file_perm?
 
843
  def test_chmod
 
844
    check_singleton :chmod
 
845
 
 
846
    touch 'tmp/a'
 
847
    chmod 0700, 'tmp/a'
 
848
    assert_equal 0700, File.stat('tmp/a').mode & 0777
 
849
    chmod 0500, 'tmp/a'
 
850
    assert_equal 0500, File.stat('tmp/a').mode & 0777
 
851
  end
 
852
 
 
853
  def test_chmod_R
 
854
    check_singleton :chmod_R
 
855
 
 
856
    mkdir_p 'tmp/dir/dir'
 
857
    touch %w( tmp/dir/file tmp/dir/dir/file )
 
858
    chmod_R 0700, 'tmp/dir'
 
859
    assert_equal 0700, File.stat('tmp/dir').mode & 0777
 
860
    assert_equal 0700, File.stat('tmp/dir/file').mode & 0777
 
861
    assert_equal 0700, File.stat('tmp/dir/dir').mode & 0777
 
862
    assert_equal 0700, File.stat('tmp/dir/dir/file').mode & 0777
 
863
    chmod_R 0500, 'tmp/dir'
 
864
    assert_equal 0500, File.stat('tmp/dir').mode & 0777
 
865
    assert_equal 0500, File.stat('tmp/dir/file').mode & 0777
 
866
    assert_equal 0500, File.stat('tmp/dir/dir').mode & 0777
 
867
    assert_equal 0500, File.stat('tmp/dir/dir/file').mode & 0777
 
868
    chmod_R 0700, 'tmp/dir'   # to remove
 
869
  end
 
870
 
 
871
  # FIXME: How can I test this method?
 
872
  def test_chown
 
873
    check_singleton :chown
 
874
  end
 
875
 
 
876
  # FIXME: How can I test this method?
 
877
  def test_chown_R
 
878
    check_singleton :chown_R
 
879
  end
 
880
end
 
881
 
 
882
  def test_copy_entry
 
883
    check_singleton :copy_entry
 
884
 
 
885
    each_srcdest do |srcpath, destpath|
 
886
      copy_entry srcpath, destpath
 
887
      assert_same_file srcpath, destpath
 
888
      assert_equal File.stat(srcpath).ftype, File.stat(destpath).ftype
 
889
    end
 
890
if have_symlink?
 
891
    # root is a symlink
 
892
    File.symlink 'somewhere', 'tmp/symsrc'
 
893
    copy_entry 'tmp/symsrc', 'tmp/symdest'
 
894
    assert_symlink 'tmp/symdest'
 
895
    assert_equal 'somewhere', File.readlink('tmp/symdest')
 
896
 
 
897
    # content is a symlink
 
898
    mkdir 'tmp/dir'
 
899
    File.symlink 'somewhere', 'tmp/dir/sym'
 
900
    copy_entry 'tmp/dir', 'tmp/dirdest'
 
901
    assert_directory 'tmp/dirdest'
 
902
    assert_not_symlink 'tmp/dirdest'
 
903
    assert_symlink 'tmp/dirdest/sym'
 
904
    assert_equal 'somewhere', File.readlink('tmp/dirdest/sym')
 
905
end
 
906
  end
 
907
 
 
908
  def test_copy_file
 
909
    check_singleton :copy_file
 
910
 
 
911
    each_srcdest do |srcpath, destpath|
 
912
      copy_file srcpath, destpath
 
913
      assert_same_file srcpath, destpath
 
914
    end
 
915
  end
 
916
 
 
917
  def test_copy_stream
 
918
    check_singleton :copy_stream
 
919
    # IO
 
920
    each_srcdest do |srcpath, destpath|
 
921
      File.open(srcpath) {|src|
 
922
        File.open(destpath, 'w') {|dest|
 
923
          copy_stream src, dest
 
924
        }
 
925
      }
 
926
      assert_same_file srcpath, destpath
 
927
    end
 
928
 
 
929
    # duck typing test  [ruby-dev:25369]
 
930
    my_rm_rf 'tmp'
 
931
    Dir.mkdir 'tmp'
 
932
    each_srcdest do |srcpath, destpath|
 
933
      File.open(srcpath) {|src|
 
934
        File.open(destpath, 'w') {|dest|
 
935
          copy_stream Stream.new(src), Stream.new(dest)
 
936
        }
 
937
      }
 
938
      assert_same_file srcpath, destpath
 
939
    end
 
940
  end
 
941
 
 
942
  def test_remove_file
 
943
    check_singleton :remove_file
 
944
    File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
 
945
    remove_file 'data/tmp'
 
946
    assert_file_not_exist 'data/tmp'
 
947
if have_file_perm?
 
948
    File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
 
949
    File.chmod 0, 'data/tmp'
 
950
    remove_file 'data/tmp'
 
951
    assert_file_not_exist 'data/tmp'
 
952
end
 
953
  end
 
954
 
 
955
  def test_remove_dir
 
956
    check_singleton :remove_dir
 
957
    Dir.mkdir 'data/tmpdir'
 
958
    File.open('data/tmpdir/a', 'w') {|f| f.puts 'dummy' }
 
959
    remove_dir 'data/tmpdir'
 
960
    assert_file_not_exist 'data/tmpdir'
 
961
if have_file_perm?
 
962
    Dir.mkdir 'data/tmpdir'
 
963
    File.chmod 0555, 'data/tmpdir'
 
964
    remove_dir 'data/tmpdir'
 
965
    assert_file_not_exist 'data/tmpdir'
 
966
end
 
967
  end
 
968
 
 
969
  def test_compare_file
 
970
    check_singleton :compare_file
 
971
    # FIXME
 
972
  end
 
973
 
 
974
  def test_compare_stream
 
975
    check_singleton :compare_stream
 
976
    # FIXME
 
977
  end
 
978
 
 
979
  class Stream
 
980
    def initialize(f)
 
981
      @f = f
 
982
    end
 
983
 
 
984
    def read(n)
 
985
      @f.read(n)
 
986
    end
 
987
 
 
988
    def write(str)
 
989
      @f.write str
 
990
    end
 
991
  end
 
992
 
 
993
  def test_uptodate?
 
994
    check_singleton :uptodate?
 
995
    prepare_time_data
 
996
    Dir.chdir('data') {
 
997
      assert(   uptodate?('newest', %w(old newer notexist)) )
 
998
      assert( ! uptodate?('newer', %w(old newest notexist)) )
 
999
      assert( ! uptodate?('notexist', %w(old newest newer)) )
 
1000
    }
 
1001
 
 
1002
    # pathname
 
1003
    touch 'tmp/a'
 
1004
    touch 'tmp/b'
 
1005
    touch 'tmp/c'
 
1006
    assert_nothing_raised {
 
1007
      uptodate? Pathname.new('tmp/a'), ['tmp/b', 'tmp/c']
 
1008
      uptodate? 'tmp/a', [Pathname.new('tmp/b'), 'tmp/c']
 
1009
      uptodate? 'tmp/a', ['tmp/b', Pathname.new('tmp/c')]
 
1010
      uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')]
 
1011
    }
 
1012
  end
 
1013
 
 
1014
  def test_cd
 
1015
    check_singleton :cd
 
1016
  end
 
1017
 
 
1018
  def test_chdir
 
1019
    check_singleton :chdir
 
1020
  end
 
1021
 
 
1022
  def test_getwd
 
1023
    check_singleton :getwd
 
1024
  end
 
1025
 
 
1026
  def test_identical?
 
1027
    check_singleton :identical?
 
1028
  end
 
1029
 
 
1030
  def test_link
 
1031
    check_singleton :link
 
1032
  end
 
1033
 
 
1034
  def test_makedirs
 
1035
    check_singleton :makedirs
 
1036
  end
 
1037
 
 
1038
  def test_mkpath
 
1039
    check_singleton :mkpath
 
1040
  end
 
1041
 
 
1042
  def test_move
 
1043
    check_singleton :move
 
1044
  end
 
1045
 
 
1046
  def test_rm_rf
 
1047
    check_singleton :rm_rf
 
1048
  end
 
1049
 
 
1050
  def test_rmdir
 
1051
    check_singleton :rmdir
 
1052
  end
 
1053
 
 
1054
  def test_rmtree
 
1055
    check_singleton :rmtree
 
1056
  end
 
1057
 
 
1058
  def test_safe_unlink
 
1059
    check_singleton :safe_unlink
 
1060
  end
 
1061
 
 
1062
  def test_symlink
 
1063
    check_singleton :symlink
 
1064
  end
 
1065
 
 
1066
  def test_touch
 
1067
    check_singleton :touch
 
1068
  end
 
1069
 
 
1070
  def test_collect_methods
 
1071
  end
 
1072
 
 
1073
  def test_commands
 
1074
  end
 
1075
 
 
1076
  def test_have_option?
 
1077
  end
 
1078
 
 
1079
  def test_options
 
1080
  end
 
1081
 
 
1082
  def test_options_of
 
1083
  end
 
1084
 
 
1085
end