[前][次][番号順一覧][スレッド一覧]

ruby-changes:33162

From: nobu <ko1@a...>
Date: Sun, 2 Mar 2014 11:15:27 +0900 (JST)
Subject: [ruby-changes:33162] nobu:r45241 (trunk): find.rb: add ignore_error

nobu	2014-03-02 11:15:21 +0900 (Sun, 02 Mar 2014)

  New Revision: 45241

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45241

  Log:
    find.rb: add ignore_error
    
    * lib/find.rb (Find#find): add "ignore_error" keyword argument
      defaulted to true.  [ruby-core:51025] [Feature #7596]

  Modified files:
    trunk/ChangeLog
    trunk/lib/find.rb
    trunk/test/test_find.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45240)
+++ ChangeLog	(revision 45241)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Mar  2 11:15:10 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* lib/find.rb (Find#find): add "ignore_error" keyword argument
+	  defaulted to true.  [ruby-core:51025] [Feature #7596]
+
 Sun Mar  2 11:13:30 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* ext/readline/extconf.rb (rl_hook_func_t): define as Function for
Index: lib/find.rb
===================================================================
--- lib/find.rb	(revision 45240)
+++ lib/find.rb	(revision 45241)
@@ -34,7 +34,7 @@ module Find https://github.com/ruby/ruby/blob/trunk/lib/find.rb#L34
   #
   # See the +Find+ module documentation for an example.
   #
-  def find(*paths) # :yield: path
+  def find(*paths, ignore_error: true) # :yield: path
     block_given? or return enum_for(__method__, *paths)
 
     fs_encoding = Encoding.find("filesystem")
@@ -48,12 +48,14 @@ module Find https://github.com/ruby/ruby/blob/trunk/lib/find.rb#L48
           begin
             s = File.lstat(file)
           rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
+            raise unless ignore_error
             next
           end
           if s.directory? then
             begin
               fs = Dir.entries(file, encoding: enc)
             rescue Errno::ENOENT, Errno::EACCES, Errno::ENOTDIR, Errno::ELOOP, Errno::ENAMETOOLONG
+              raise unless ignore_error
               next
             end
             fs.sort!
Index: test/test_find.rb
===================================================================
--- test/test_find.rb	(revision 45240)
+++ test/test_find.rb	(revision 45241)
@@ -100,6 +100,16 @@ class TestFind < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_find.rb#L100
         a = []
         Find.find(d) {|f| a << f }
         assert_equal([d, dir], a)
+
+        a = []
+        Find.find(d, ignore_error: true) {|f| a << f }
+        assert_equal([d, dir], a)
+
+        a = []
+        assert_raise_with_message(Errno::EACCES, /#{Regexp.quote(dir)}/) do
+          Find.find(d, ignore_error: false) {|f| a << f }
+        end
+        assert_equal([d, dir], a)
       ensure
         File.chmod(0700, dir)
       end
@@ -115,6 +125,17 @@ class TestFind < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_find.rb#L125
         a = []
         Find.find(d) {|f| a << f }
         assert_equal([d, dir, file], a)
+
+        a = []
+        Find.find(d, ignore_error: true) {|f| a << f }
+        assert_equal([d, dir, file], a)
+
+        a = []
+        assert_raise_with_message(Errno::EACCES, /#{Regexp.quote(file)}/) do
+          Find.find(d, ignore_error: false) {|f| a << f }
+        end
+        assert_equal([d, dir, file], a)
+
         skip "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
         assert_raise(Errno::EACCES) { File.lstat(file) }
       ensure

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]