9 class TestFileUtils < Test::Unit::TestCase
10 TMPROOT = "#{Dir.tmpdir}/fileutils.rb.#{$$}"
14 tmproot = TestFileUtils::TMPROOT
15 Dir.mkdir tmproot unless File.directory?(tmproot)
18 def have_drive_letter?
19 /djgpp|mswin(?!ce)|mingw|bcc|emx/ =~ RUBY_PLATFORM
23 /djgpp|mswin|mingw|bcc|wince|emx/ !~ RUBY_PLATFORM
26 $fileutils_rb_have_symlink = nil
29 if $fileutils_rb_have_symlink == nil
30 $fileutils_rb_have_symlink = check_have_symlink?
32 $fileutils_rb_have_symlink
35 def check_have_symlink?
37 rescue NotImplementedError
43 $fileutils_rb_have_hardlink = nil
46 if $fileutils_rb_have_hardlink == nil
47 $fileutils_rb_have_hardlink = check_have_hardlink?
49 $fileutils_rb_have_hardlink
52 def check_have_hardlink?
54 rescue NotImplementedError
63 def lf_in_path_allowed?
67 def lf_in_path_allowed?
79 def check_singleton(name)
80 assert_equal true, ::FileUtils.public_methods.include?(name.to_sym)
84 if File.exist?('/bin/rm')
85 system %Q[/bin/rm -rf "#{path}"]
93 File.chown nil, Process.gid, path if have_file_perm?
99 mymkdir tmproot unless File.directory?(tmproot)
101 my_rm_rf 'data'; mymkdir 'data'
102 my_rm_rf 'tmp'; mymkdir 'tmp'
113 TARGETS = %w( data/a data/all data/random data/zero )
115 def prepare_data_file
116 File.open('data/a', 'w') {|f|
122 all_chars = (0..255).map {|n| n.chr }.join('')
123 File.open('data/all', 'w') {|f|
129 random_chars = (0...50).map { rand(256).chr }.join('')
130 File.open('data/random', 'w') {|f|
136 File.open('data/zero', 'w') {|f|
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"
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' }
156 File.utime t-8, t-8, 'data/old'
157 File.utime t-4, t-4, 'data/newer'
161 TARGETS.each do |path|
162 yield path, "tmp/#{File.basename(path)}"
173 assert_equal Dir.pwd, pwd()
176 if have_drive_letter?
178 assert_equal 'C:/', pwd()
180 assert_equal cwd, pwd()
183 assert_equal '/', pwd()
185 assert_equal cwd, pwd()
192 TARGETS.each do |fname|
193 assert cmp(fname, fname), 'not same?'
195 assert_raises(ArgumentError) {
196 cmp TARGETS[0], TARGETS[0], :undefinedoption => true
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')
211 each_srcdest do |srcpath, destpath|
213 assert_same_file srcpath, destpath
215 cp srcpath, File.dirname(destpath)
216 assert_same_file srcpath, destpath
218 cp srcpath, File.dirname(destpath) + '/'
219 assert_same_file srcpath, destpath
221 cp srcpath, destpath, :preserve => true
222 assert_same_file srcpath, destpath
223 assert_same_entry srcpath, destpath
226 # src==dest (1) same path
228 assert_raises(ArgumentError) {
229 cp 'tmp/cptmp', 'tmp/cptmp'
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'
237 assert_raises(ArgumentError) {
238 cp 'tmp/cptmp_symlink', 'tmp/cptmp'
240 # src==dest (3) looped symlink
241 File.symlink 'symlink', 'tmp/symlink'
242 assert_raises(Errno::ELOOP) {
243 cp 'tmp/symlink', 'tmp/symlink'
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')
253 cp ['tmp/cptmp', 'tmp/tmpdest'], Pathname.new('tmp/tmpdir')
258 check_singleton :cp_r
261 TARGETS.each do |fname|
262 assert_same_file fname, "tmp/#{fname}"
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)}"
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'
287 # symlink in a directory
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')
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 assert_symlink 'tmp/cpr_dest2' # 2005-05-26: feature change
300 assert_symlink 'tmp/cpr_dest2/symlink'
301 assert_equal 'SLdest', File.readlink('tmp/cpr_dest2/symlink')
306 assert_nothing_raised {
307 cp_r Pathname.new('tmp/cprtmp'), 'tmp/tmpdest'
308 cp_r 'tmp/cprtmp', Pathname.new('tmp/tmpdest')
309 cp_r Pathname.new('tmp/cprtmp'), Pathname.new('tmp/tmpdest')
317 TARGETS.each do |fname|
318 cp fname, 'tmp/mvsrc'
319 mv 'tmp/mvsrc', 'tmp/mvdest'
320 assert_same_file fname, 'tmp/mvdest'
322 mv 'tmp/mvdest', 'tmp/dest/'
323 assert_same_file fname, 'tmp/dest/mvdest'
325 mv 'tmp/dest/mvdest', 'tmp'
326 assert_same_file fname, 'tmp/mvdest'
330 mkdir_p 'tmp/dest2/tmpdir'
331 assert_raises(Errno::EEXIST) {
332 mv 'tmp/tmpdir', 'tmp/dest2'
334 mkdir 'tmp/dest2/tmpdir/junk'
335 assert_raises(Errno::EEXIST, "[ruby-talk:124368]") {
336 mv 'tmp/tmpdir', 'tmp/dest2'
339 # src==dest (1) same path
341 assert_raises(ArgumentError) {
342 mv 'tmp/cptmp', 'tmp/cptmp'
345 # src==dest (2) symlink and its target
346 File.symlink 'cptmp', 'tmp/cptmp_symlink'
347 assert_raises(ArgumentError) {
348 mv 'tmp/cptmp', 'tmp/cptmp_symlink'
350 assert_raises(ArgumentError) {
351 mv 'tmp/cptmp_symlink', 'tmp/cptmp'
353 # src==dest (3) looped symlink
354 File.symlink 'symlink', 'tmp/symlink'
355 assert_raises(Errno::ELOOP) {
356 mv 'tmp/symlink', 'tmp/symlink'
361 assert_nothing_raised {
363 mv Pathname.new('tmp/mvtmpsrc'), 'tmp/mvtmpdest'
365 mv 'tmp/mvtmpsrc', Pathname.new('tmp/mvtmpdest')
367 mv Pathname.new('tmp/mvtmpsrc'), Pathname.new('tmp/mvtmpdest')
374 TARGETS.each do |fname|
375 cp fname, 'tmp/rmsrc'
377 assert_file_not_exist 'tmp/rmsrc'
384 assert_nothing_raised {
385 rm Pathname.new('tmp/rmtmp1')
386 rm [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
388 assert_file_not_exist 'tmp/rmtmp1'
389 assert_file_not_exist 'tmp/rmtmp2'
390 assert_file_not_exist 'tmp/rmtmp3'
394 check_singleton :rm_f
396 TARGETS.each do |fname|
397 cp fname, 'tmp/rmsrc'
399 assert_file_not_exist 'tmp/rmsrc'
403 File.open('tmp/lnf_symlink_src', 'w') {|f| f.puts 'dummy' }
404 File.symlink 'tmp/lnf_symlink_src', 'tmp/lnf_symlink_dest'
405 rm_f 'tmp/lnf_symlink_dest'
406 assert_file_not_exist 'tmp/lnf_symlink_dest'
407 assert_file_exist 'tmp/lnf_symlink_src'
410 rm_f 'notexistdatafile'
411 rm_f 'tmp/notexistdatafile'
412 my_rm_rf 'tmpdatadir'
413 Dir.mkdir 'tmpdatadir'
415 Dir.rmdir 'tmpdatadir'
417 Dir.mkdir 'tmp/tmpdir'
418 File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
419 File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
420 rm_f ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c']
421 assert_file_not_exist 'tmp/tmpdir/a'
422 assert_file_not_exist 'tmp/tmpdir/c'
423 Dir.rmdir 'tmp/tmpdir'
430 assert_nothing_raised {
431 rm_f Pathname.new('tmp/rmtmp1')
432 rm_f [Pathname.new('tmp/rmtmp2'), Pathname.new('tmp/rmtmp3')]
434 assert_file_not_exist 'tmp/rmtmp1'
435 assert_file_not_exist 'tmp/rmtmp2'
436 assert_file_not_exist 'tmp/rmtmp3'
437 assert_file_exist 'tmp/rmtmp4'
441 check_singleton :rm_r
443 my_rm_rf 'tmpdatadir'
445 Dir.mkdir 'tmpdatadir'
447 assert_file_not_exist 'tmpdatadir'
449 Dir.mkdir 'tmpdatadir'
451 assert_file_not_exist 'tmpdatadir'
453 Dir.mkdir 'tmp/tmpdir'
455 assert_file_not_exist 'tmp/tmpdir'
456 assert_file_exist 'tmp'
458 Dir.mkdir 'tmp/tmpdir'
460 assert_file_not_exist 'tmp/tmpdir'
461 assert_file_exist 'tmp'
463 Dir.mkdir 'tmp/tmpdir'
464 File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
465 File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
466 File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
468 assert_file_not_exist 'tmp/tmpdir'
469 assert_file_exist 'tmp'
471 Dir.mkdir 'tmp/tmpdir'
472 File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
473 File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
474 rm_r ['tmp/tmpdir/a', 'tmp/tmpdir/b', 'tmp/tmpdir/c'], :force => true
475 assert_file_not_exist 'tmp/tmpdir/a'
476 assert_file_not_exist 'tmp/tmpdir/c'
477 Dir.rmdir 'tmp/tmpdir'
480 # [ruby-talk:94635] a symlink to the directory
481 Dir.mkdir 'tmp/tmpdir'
482 File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
484 assert_file_not_exist 'tmp/tmpdir'
485 assert_file_exist 'tmp'
489 Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
490 Dir.mkdir 'tmp/tmpdir2'; touch 'tmp/tmpdir2/tmp'
491 Dir.mkdir 'tmp/tmpdir3'; touch 'tmp/tmpdir3/tmp'
492 assert_nothing_raised {
493 rm_r Pathname.new('tmp/tmpdir1')
494 rm_r [Pathname.new('tmp/tmpdir2'), Pathname.new('tmp/tmpdir3')]
496 assert_file_not_exist 'tmp/tmpdir1'
497 assert_file_not_exist 'tmp/tmpdir2'
498 assert_file_not_exist 'tmp/tmpdir3'
501 def test_remove_entry_secure
502 check_singleton :remove_entry_secure
504 my_rm_rf 'tmpdatadir'
506 Dir.mkdir 'tmpdatadir'
507 remove_entry_secure 'tmpdatadir'
508 assert_file_not_exist 'tmpdatadir'
510 Dir.mkdir 'tmpdatadir'
511 remove_entry_secure 'tmpdatadir/'
512 assert_file_not_exist 'tmpdatadir'
514 Dir.mkdir 'tmp/tmpdir'
515 remove_entry_secure 'tmp/tmpdir/'
516 assert_file_not_exist 'tmp/tmpdir'
517 assert_file_exist 'tmp'
519 Dir.mkdir 'tmp/tmpdir'
520 remove_entry_secure 'tmp/tmpdir'
521 assert_file_not_exist 'tmp/tmpdir'
522 assert_file_exist 'tmp'
524 Dir.mkdir 'tmp/tmpdir'
525 File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
526 File.open('tmp/tmpdir/b', 'w') {|f| f.puts 'dummy' }
527 File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
528 remove_entry_secure 'tmp/tmpdir'
529 assert_file_not_exist 'tmp/tmpdir'
530 assert_file_exist 'tmp'
532 Dir.mkdir 'tmp/tmpdir'
533 File.open('tmp/tmpdir/a', 'w') {|f| f.puts 'dummy' }
534 File.open('tmp/tmpdir/c', 'w') {|f| f.puts 'dummy' }
535 remove_entry_secure 'tmp/tmpdir/a', true
536 remove_entry_secure 'tmp/tmpdir/b', true
537 remove_entry_secure 'tmp/tmpdir/c', true
538 assert_file_not_exist 'tmp/tmpdir/a'
539 assert_file_not_exist 'tmp/tmpdir/c'
540 Dir.rmdir 'tmp/tmpdir'
543 # [ruby-talk:94635] a symlink to the directory
544 Dir.mkdir 'tmp/tmpdir'
545 File.symlink '..', 'tmp/tmpdir/symlink_to_dir'
546 remove_entry_secure 'tmp/tmpdir'
547 assert_file_not_exist 'tmp/tmpdir'
548 assert_file_exist 'tmp'
552 Dir.mkdir 'tmp/tmpdir1'; touch 'tmp/tmpdir1/tmp'
553 assert_nothing_raised {
554 remove_entry_secure Pathname.new('tmp/tmpdir1')
556 assert_file_not_exist 'tmp/tmpdir1'
559 def test_with_big_file
562 cp BIGFILE, 'tmp/cpdest'
563 assert_same_file BIGFILE, 'tmp/cpdest'
564 assert cmp(BIGFILE, 'tmp/cpdest'), 'orig != copied'
566 mv 'tmp/cpdest', 'tmp/mvdest'
567 assert_same_file BIGFILE, 'tmp/mvdest'
568 assert_file_not_exist 'tmp/cpdest'
571 assert_file_not_exist 'tmp/mvdest'
576 TARGETS.each do |fname|
577 ln fname, 'tmp/lndest'
578 assert_same_file fname, 'tmp/lndest'
579 File.unlink 'tmp/lndest'
583 TARGETS.each do |fname|
584 assert_same_file fname, 'tmp/' + File.basename(fname)
586 TARGETS.each do |fname|
587 File.unlink 'tmp/' + File.basename(fname)
590 # src==dest (1) same path
592 assert_raises(Errno::EEXIST) {
593 ln 'tmp/cptmp', 'tmp/cptmp'
596 # src==dest (2) symlink and its target
597 File.symlink 'cptmp', 'tmp/symlink'
598 assert_raises(Errno::EEXIST) {
599 ln 'tmp/cptmp', 'tmp/symlink' # normal file -> symlink
601 assert_raises(Errno::EEXIST) {
602 ln 'tmp/symlink', 'tmp/cptmp' # symlink -> normal file
604 # src==dest (3) looped symlink
605 File.symlink 'cptmp_symlink', 'tmp/cptmp_symlink'
607 ln 'tmp/cptmp_symlink', 'tmp/cptmp_symlink'
609 assert_kind_of SystemCallError, err
615 assert_nothing_raised {
616 ln Pathname.new('tmp/lntmp'), 'tmp/lndesttmp1'
617 ln 'tmp/lntmp', Pathname.new('tmp/lndesttmp2')
618 ln Pathname.new('tmp/lntmp'), Pathname.new('tmp/lndesttmp3')
625 check_singleton :ln_s
627 TARGETS.each do |fname|
628 ln_s fname, 'tmp/lnsdest'
629 assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
630 assert_equal fname, File.readlink('tmp/lnsdest')
633 assert_nothing_raised {
634 ln_s 'symlink', 'tmp/symlink'
636 assert_symlink 'tmp/symlink'
640 assert_nothing_raised {
641 ln_s Pathname.new('lnsdest'), 'tmp/symlink_tmp1'
642 ln_s 'lnsdest', Pathname.new('tmp/symlink_tmp2')
643 ln_s Pathname.new('lnsdest'), Pathname.new('tmp/symlink_tmp3')
650 check_singleton :ln_sf
652 TARGETS.each do |fname|
653 ln_sf fname, 'tmp/lnsdest'
654 assert FileTest.symlink?('tmp/lnsdest'), 'not symlink'
655 assert_equal fname, File.readlink('tmp/lnsdest')
656 ln_sf fname, 'tmp/lnsdest'
657 ln_sf fname, 'tmp/lnsdest'
659 assert_nothing_raised {
660 ln_sf 'symlink', 'tmp/symlink'
665 assert_nothing_raised {
666 ln_sf Pathname.new('lns_dest'), 'tmp/symlink_tmp1'
667 ln_sf 'lns_dest', Pathname.new('tmp/symlink_tmp2')
668 ln_sf Pathname.new('lns_dest'), Pathname.new('tmp/symlink_tmp3')
674 check_singleton :mkdir
676 my_rm_rf 'tmpdatadir'
678 assert_directory 'tmpdatadir'
679 Dir.rmdir 'tmpdatadir'
682 assert_directory 'tmpdatadir'
683 Dir.rmdir 'tmpdatadir'
685 mkdir 'tmp/mkdirdest'
686 assert_directory 'tmp/mkdirdest'
687 Dir.rmdir 'tmp/mkdirdest'
689 mkdir 'tmp/tmp', :mode => 0700
690 assert_directory 'tmp/tmp'
691 assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
695 mkdir 'tmp/tmp', :mode => 07777
696 assert_directory 'tmp/tmp'
697 assert_equal 07777, (File.stat('tmp/tmp').mode & 07777)
701 if lf_in_path_allowed?
702 mkdir "tmp-first-line\ntmp-second-line"
703 assert_directory "tmp-first-line\ntmp-second-line"
704 Dir.rmdir "tmp-first-line\ntmp-second-line"
708 assert_nothing_raised {
709 mkdir Pathname.new('tmp/tmpdirtmp')
710 mkdir [Pathname.new('tmp/tmpdirtmp2'), Pathname.new('tmp/tmpdirtmp3')]
715 check_singleton :mkdir_p
720 tmpdir/dir/./.././dir/
727 tmpdir/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a/a
734 assert_file_not_exist "#{d}/a"
735 assert_file_not_exist "#{d}/b"
736 assert_file_not_exist "#{d}/c"
745 mkdir_p "#{Dir.pwd}/#{d}"
750 mkdir_p 'tmp/tmp/tmp', :mode => 0700
751 assert_directory 'tmp/tmp'
752 assert_directory 'tmp/tmp/tmp'
753 assert_equal 0700, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
754 assert_equal 0700, (File.stat('tmp/tmp/tmp').mode & 0777) if have_file_perm?
757 mkdir_p 'tmp/tmp', :mode => 0
758 assert_directory 'tmp/tmp'
759 assert_equal 0, (File.stat('tmp/tmp').mode & 0777) if have_file_perm?
760 # DO NOT USE rm_rf here.
761 # (rm(1) try to chdir to parent directory, it fails to remove directory.)
766 mkdir_p 'tmp/tmp/tmp', :mode => 07777
767 assert_directory 'tmp/tmp/tmp'
768 assert_equal 07777, (File.stat('tmp/tmp/tmp').mode & 07777)
769 Dir.rmdir 'tmp/tmp/tmp'
774 assert_nothing_raised {
775 mkdir_p Pathname.new('tmp/tmp/tmp')
780 check_singleton :install
782 File.open('tmp/aaa', 'w') {|f| f.puts 'aaa' }
783 File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' }
784 install 'tmp/aaa', 'tmp/bbb', :mode => 0600
785 assert_equal "aaa\n", File.read('tmp/bbb')
786 assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
788 t = File.mtime('tmp/bbb')
789 install 'tmp/aaa', 'tmp/bbb'
790 assert_equal "aaa\n", File.read('tmp/bbb')
791 assert_equal 0600, (File.stat('tmp/bbb').mode & 0777) if have_file_perm?
792 assert_equal_time t, File.mtime('tmp/bbb')
794 File.unlink 'tmp/aaa'
795 File.unlink 'tmp/bbb'
797 # src==dest (1) same path
799 assert_raises(ArgumentError) {
800 install 'tmp/cptmp', 'tmp/cptmp'
803 # src==dest (2) symlink and its target
804 File.symlink 'cptmp', 'tmp/cptmp_symlink'
805 assert_raises(ArgumentError) {
806 install 'tmp/cptmp', 'tmp/cptmp_symlink'
808 assert_raises(ArgumentError) {
809 install 'tmp/cptmp_symlink', 'tmp/cptmp'
811 # src==dest (3) looped symlink
812 File.symlink 'symlink', 'tmp/symlink'
813 assert_raises(Errno::ELOOP) {
814 # File#install invokes open(2), always ELOOP must be raised
815 install 'tmp/symlink', 'tmp/symlink'
820 assert_nothing_raised {
821 rm_f 'tmp/a'; touch 'tmp/a'
822 install 'tmp/a', Pathname.new('tmp/b')
823 rm_f 'tmp/a'; touch 'tmp/a'
824 install Pathname.new('tmp/a'), 'tmp/b'
825 rm_f 'tmp/a'; touch 'tmp/a'
826 install Pathname.new('tmp/a'), Pathname.new('tmp/b')
831 install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], 'tmp/dest'
834 install [Pathname.new('tmp/a'), Pathname.new('tmp/b')], Pathname.new('tmp/dest')
840 check_singleton :chmod
844 assert_equal 0700, File.stat('tmp/a').mode & 0777
846 assert_equal 0500, File.stat('tmp/a').mode & 0777
850 check_singleton :chmod_R
852 mkdir_p 'tmp/dir/dir'
853 touch %w( tmp/dir/file tmp/dir/dir/file )
854 chmod_R 0700, 'tmp/dir'
855 assert_equal 0700, File.stat('tmp/dir').mode & 0777
856 assert_equal 0700, File.stat('tmp/dir/file').mode & 0777
857 assert_equal 0700, File.stat('tmp/dir/dir').mode & 0777
858 assert_equal 0700, File.stat('tmp/dir/dir/file').mode & 0777
859 chmod_R 0500, 'tmp/dir'
860 assert_equal 0500, File.stat('tmp/dir').mode & 0777
861 assert_equal 0500, File.stat('tmp/dir/file').mode & 0777
862 assert_equal 0500, File.stat('tmp/dir/dir').mode & 0777
863 assert_equal 0500, File.stat('tmp/dir/dir/file').mode & 0777
864 chmod_R 0700, 'tmp/dir' # to remove
867 # FIXME: How can I test this method?
869 check_singleton :chown
872 # FIXME: How can I test this method?
874 check_singleton :chown_R
879 check_singleton :copy_entry
881 each_srcdest do |srcpath, destpath|
882 copy_entry srcpath, destpath
883 assert_same_file srcpath, destpath
884 assert_equal File.stat(srcpath).ftype, File.stat(destpath).ftype
888 File.symlink 'somewhere', 'tmp/symsrc'
889 copy_entry 'tmp/symsrc', 'tmp/symdest'
890 assert_symlink 'tmp/symdest'
891 assert_equal 'somewhere', File.readlink('tmp/symdest')
893 # content is a symlink
895 File.symlink 'somewhere', 'tmp/dir/sym'
896 copy_entry 'tmp/dir', 'tmp/dirdest'
897 assert_directory 'tmp/dirdest'
898 assert_not_symlink 'tmp/dirdest'
899 assert_symlink 'tmp/dirdest/sym'
900 assert_equal 'somewhere', File.readlink('tmp/dirdest/sym')
905 check_singleton :copy_file
907 each_srcdest do |srcpath, destpath|
908 copy_file srcpath, destpath
909 assert_same_file srcpath, destpath
914 check_singleton :copy_stream
916 each_srcdest do |srcpath, destpath|
917 File.open(srcpath) {|src|
918 File.open(destpath, 'w') {|dest|
919 copy_stream src, dest
922 assert_same_file srcpath, destpath
925 # duck typing test [ruby-dev:25369]
928 each_srcdest do |srcpath, destpath|
929 File.open(srcpath) {|src|
930 File.open(destpath, 'w') {|dest|
931 copy_stream Stream.new(src), Stream.new(dest)
934 assert_same_file srcpath, destpath
939 check_singleton :remove_file
940 File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
941 remove_file 'data/tmp'
942 assert_file_not_exist 'data/tmp'
944 File.open('data/tmp', 'w') {|f| f.puts 'dummy' }
945 File.chmod 0, 'data/tmp'
946 remove_file 'data/tmp'
947 assert_file_not_exist 'data/tmp'
952 check_singleton :remove_dir
953 Dir.mkdir 'data/tmpdir'
954 File.open('data/tmpdir/a', 'w') {|f| f.puts 'dummy' }
955 remove_dir 'data/tmpdir'
956 assert_file_not_exist 'data/tmpdir'
958 Dir.mkdir 'data/tmpdir'
959 File.chmod 0555, 'data/tmpdir'
960 remove_dir 'data/tmpdir'
961 assert_file_not_exist 'data/tmpdir'
965 def test_compare_file
966 check_singleton :compare_file
970 def test_compare_stream
971 check_singleton :compare_stream
990 check_singleton :uptodate?
993 assert( uptodate?('newest', %w(old newer notexist)) )
994 assert( ! uptodate?('newer', %w(old newest notexist)) )
995 assert( ! uptodate?('notexist', %w(old newest newer)) )
1002 assert_nothing_raised {
1003 uptodate? Pathname.new('tmp/a'), ['tmp/b', 'tmp/c']
1004 uptodate? 'tmp/a', [Pathname.new('tmp/b'), 'tmp/c']
1005 uptodate? 'tmp/a', ['tmp/b', Pathname.new('tmp/c')]
1006 uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')]
1015 check_singleton :chdir
1019 check_singleton :getwd
1023 check_singleton :identical?
1027 check_singleton :link
1031 check_singleton :makedirs
1035 check_singleton :mkpath
1039 check_singleton :move
1043 check_singleton :rm_rf
1047 check_singleton :rmdir
1051 check_singleton :rmtree
1054 def test_safe_unlink
1055 check_singleton :safe_unlink
1059 check_singleton :symlink
1063 check_singleton :touch
1066 def test_collect_methods
1072 def test_have_option?