From 0a4a9dbaea37782ffe47210b26f2462f42ebff16 Mon Sep 17 00:00:00 2001 From: aamine Date: Sat, 1 Jan 2005 07:22:04 +0000 Subject: * lib/fileutils.rb (copy_stream): use read/write instead of sysread/syswrite, which allows duck typing. [ruby-dev:25369] * lib/fileutils.rb (copy_stream): does NOT support nonblocking IO. [ruby-dev:25370] * test/fileutils/test_fileutils.rb: test copy_entry, copy_file, copy_stream. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@7699 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 11 +++ lib/fileutils.rb | 20 +++--- test/fileutils/test_fileutils.rb | 147 ++++++++++++++++++++++++++++++--------- 3 files changed, 136 insertions(+), 42 deletions(-) diff --git a/ChangeLog b/ChangeLog index b88fe2b5f7..9d0b7807cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +Sat Jan 1 16:21:29 2005 Minero Aoki + + * lib/fileutils.rb (copy_stream): use read/write instead of + sysread/syswrite, which allows duck typing. [ruby-dev:25369] + + * lib/fileutils.rb (copy_stream): does NOT support nonblocking IO. + [ruby-dev:25370] + + * test/fileutils/test_fileutils.rb: test copy_entry, copy_file, + copy_stream. + Sat Jan 1 04:20:23 2005 GOTOU Yuuzou * ext/openssl/ossl_ns_spki.c (ossl_spki_set_challenge): should call diff --git a/lib/fileutils.rb b/lib/fileutils.rb index 38478cea71..18823e0cfe 100644 --- a/lib/fileutils.rb +++ b/lib/fileutils.rb @@ -47,12 +47,14 @@ # # There are some `low level' methods, which does not accept any option: # -# uptodate?(file, cmp_list) # copy_entry(src, dest, preserve = false, dereference = false) # copy_file(src, dest, preserve = false, dereference = true) # copy_stream(srcstream, deststream) +# remove_file(path, force = false) +# remove_dir(path, force = false) # compare_file(path_a, path_b) # compare_stream(stream_a, stream_b) +# uptodate?(file, cmp_list) # # == module FileUtils::Verbose # @@ -434,18 +436,16 @@ module FileUtils # # Copies stream +src+ to +dest+. - # Both of +src+ and +dest+ must be a IO. + # +src+ must be respond to #read(n) and + # +dest+ must be respond to #write(str). # def copy_stream(src, dest) fu_copy_stream0 src, dest, fu_stream_blksize(src, dest) end def fu_copy_stream0(src, dest, blksize) #:nodoc: - begin - while true - dest.syswrite src.sysread(blksize) - end - rescue EOFError + while s = src.read(blksize) + dest.write s end end private :fu_copy_stream0 @@ -530,7 +530,11 @@ module FileUtils end def stat(path) - @stat ||= ::File.stat(path) + if @dereference + @stat ||= ::File.stat(path) + else + @stat ||= ::File.lstat(path) + end end def chmod(mode, path) diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb index a34da88421..18944e913e 100644 --- a/test/fileutils/test_fileutils.rb +++ b/test/fileutils/test_fileutils.rb @@ -141,6 +141,16 @@ class TestFileUtils File.utime t-4, t-4, 'data/newer' end + def each_sample_file + TARGETS.each do |srcpath| + yield srcpath, "tmp/#{File.basename(srcpath)}" + end + end + + # + # Test Cases + # + def test_pwd assert_equal Dir.pwd, pwd() @@ -176,24 +186,19 @@ end end def test_cp - TARGETS.each do |fname| - cp fname, 'tmp/cp' - assert_same_file fname, 'tmp/cp' + each_sample_file do |srcpath, destpath| + cp srcpath, destpath + assert_same_file srcpath, destpath - cp fname, 'tmp' - assert_same_file fname, 'tmp/' + File.basename(fname) + cp srcpath, File.dirname(destpath) + assert_same_file srcpath, destpath - cp fname, 'tmp/' - assert_same_file fname, 'tmp/' + File.basename(fname) + cp srcpath, File.dirname(destpath) + '/' + assert_same_file srcpath, destpath - cp fname, 'tmp/preserve', :preserve => true - assert_same_file fname, 'tmp/preserve' - a = File.stat(fname) - b = File.stat('tmp/preserve') - assert_equal a.mode, b.mode - assert_equal a.mtime, b.mtime - assert_equal a.uid, b.uid - assert_equal a.gid, b.gid + cp srcpath, destpath, :preserve => true + assert_same_file srcpath, destpath + assert_same_entry srcpath, destpath end # src==dest (1) same path @@ -673,25 +678,6 @@ end } end - def test_uptodate? - Dir.chdir('data') { - assert( uptodate?('newest', %w(old newer notexist)) ) - assert( ! uptodate?('newer', %w(old newest notexist)) ) - assert( ! uptodate?('notexist', %w(old newest newer)) ) - } - - # pathname - touch 'tmp/a' - touch 'tmp/b' - touch 'tmp/c' - assert_nothing_raised { - uptodate? Pathname.new('tmp/a'), ['tmp/b', 'tmp/c'] - uptodate? 'tmp/a', [Pathname.new('tmp/b'), 'tmp/c'] - uptodate? 'tmp/a', ['tmp/b', Pathname.new('tmp/c')] - uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')] - } - end - def test_install File.open('tmp/aaa', 'w') {|f| f.puts 'aaa' } File.open('tmp/bbb', 'w') {|f| f.puts 'bbb' } @@ -761,4 +747,97 @@ end # FIXME end + def test_copy_entry + each_sample_file do |srcpath, destpath| + copy_entry srcpath, destpath + assert_same_file srcpath, destpath + assert_same_entry srcpath, destpath + end +if have_symlink? + File.symlink 'somewhere', 'tmp/symsrc' + copy_entry 'tmp/symsrc', 'tmp/symdest' + assert_equal File.lstat('tmp/symsrc').mode, File.lstat('tmp/symdest').mode +end + end + + def test_copy_file + each_sample_file do |srcpath, destpath| + copy_file srcpath, destpath + assert_same_file srcpath, destpath + end + end + + def test_copy_stream + # IO + each_sample_file do |srcpath, destpath| + File.open(srcpath) {|src| + File.open(destpath, 'w') {|dest| + copy_stream src, dest + } + } + assert_same_file srcpath, destpath + end + + # duck typing test [ruby-dev:25369] + rm_rf 'tmp' + Dir.mkdir 'tmp' + each_sample_file do |srcpath, destpath| + File.open(srcpath) {|src| + File.open(destpath, 'w') {|dest| + copy_stream Stream.new(src), Stream.new(dest) + } + } + assert_same_file srcpath, destpath + end + end + + def test_remove_file + # FIXME + end + + def test_remove_dir + # FIXME + end + + def test_compare_file + # FIXME + end + + def test_compare_stream + # FIXME + end + + class Stream + def initialize(f) + @f = f + end + + def read(n) + @f.read(n) + end + + def write(str) + @f.write str + end + end + + def test_uptodate? + Dir.chdir('data') { + assert( uptodate?('newest', %w(old newer notexist)) ) + assert( ! uptodate?('newer', %w(old newest notexist)) ) + assert( ! uptodate?('notexist', %w(old newest newer)) ) + } + + # pathname + touch 'tmp/a' + touch 'tmp/b' + touch 'tmp/c' + assert_nothing_raised { + uptodate? Pathname.new('tmp/a'), ['tmp/b', 'tmp/c'] + uptodate? 'tmp/a', [Pathname.new('tmp/b'), 'tmp/c'] + uptodate? 'tmp/a', ['tmp/b', Pathname.new('tmp/c')] + uptodate? Pathname.new('tmp/a'), [Pathname.new('tmp/b'), Pathname.new('tmp/c')] + } + end + end -- cgit v1.2.3