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

ruby-changes:33190

From: ktsj <ko1@a...>
Date: Wed, 5 Mar 2014 00:44:59 +0900 (JST)
Subject: [ruby-changes:33190] ktsj:r45269 (trunk): * ext/pathname/lib/pathname.rb (Pathname#find): add "ignore_error"

ktsj	2014-03-05 00:44:53 +0900 (Wed, 05 Mar 2014)

  New Revision: 45269

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

  Log:
    * ext/pathname/lib/pathname.rb (Pathname#find): add "ignore_error"
      keyword argument defaulted to true as well as Find#find.

  Modified files:
    trunk/ChangeLog
    trunk/ext/pathname/lib/pathname.rb
    trunk/test/pathname/test_pathname.rb
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 45268)
+++ ChangeLog	(revision 45269)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Wed Mar  5 00:42:00 2014  Kazuki Tsujimoto  <kazuki@c...>
+
+	* ext/pathname/lib/pathname.rb (Pathname#find): add "ignore_error"
+	  keyword argument defaulted to true as well as Find#find.
+
 Tue Mar  4 23:00:18 2014  NAKAMURA Usaku  <usa@r...>
 
 	* test/ruby/test_eval.rb (TestEval#make_test_binding): renamed.
Index: ext/pathname/lib/pathname.rb
===================================================================
--- ext/pathname/lib/pathname.rb	(revision 45268)
+++ ext/pathname/lib/pathname.rb	(revision 45269)
@@ -534,13 +534,13 @@ class Pathname    # * Find * https://github.com/ruby/ruby/blob/trunk/ext/pathname/lib/pathname.rb#L534
   #
   # See Find.find
   #
-  def find # :yield: pathname
-    return to_enum(__method__) unless block_given?
+  def find(ignore_error: true) # :yield: pathname
+    return to_enum(__method__, ignore_error: ignore_error) unless block_given?
     require 'find'
     if @path == '.'
-      Find.find(@path) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
+      Find.find(@path, ignore_error: ignore_error) {|f| yield self.class.new(f.sub(%r{\A\./}, '')) }
     else
-      Find.find(@path) {|f| yield self.class.new(f) }
+      Find.find(@path, ignore_error: ignore_error) {|f| yield self.class.new(f) }
     end
   end
 end
Index: test/pathname/test_pathname.rb
===================================================================
--- test/pathname/test_pathname.rb	(revision 45268)
+++ test/pathname/test_pathname.rb	(revision 45269)
@@ -1253,6 +1253,31 @@ class TestPathname < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/pathname/test_pathname.rb#L1253
       assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
       a = Pathname("d").find.sort
       assert_equal([Pathname("d"), Pathname("d/x"), Pathname("d/y")], a)
+
+      begin
+        File.unlink("d/y")
+        File.chmod(0600, "d")
+        a = []; Pathname(".").find(ignore_error: true) {|v| a << v }; a.sort!
+        assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x")], a)
+        a = []; Pathname("d").find(ignore_error: true) {|v| a << v }; a.sort!
+        assert_equal([Pathname("d"), Pathname("d/x")], a)
+
+        skip "no meaning test on Windows" if /mswin|mingw/ =~ RUBY_PLATFORM
+        a = [];
+        assert_raise_with_message(Errno::EACCES, %r{d/x}) do
+          Pathname(".").find(ignore_error: false) {|v| a << v }
+        end
+        a.sort!
+        assert_equal([Pathname("."), Pathname("a"), Pathname("b"), Pathname("d"), Pathname("d/x")], a)
+        a = [];
+        assert_raise_with_message(Errno::EACCES, %r{d/x}) do
+          Pathname("d").find(ignore_error: false) {|v| a << v }
+        end
+        a.sort!
+        assert_equal([Pathname("d"), Pathname("d/x")], a)
+      ensure
+        File.chmod(0700, "d")
+      end
     }
   end
 

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

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