From aad895181ee6852c39ac68ec9c98b74f33292eed Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 2 Mar 2014 02:15:21 +0000 Subject: [PATCH] find.rb: add ignore_error * lib/find.rb (Find#find): add "ignore_error" keyword argument defaulted to true. [ruby-core:51025] [Feature #7596] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45241 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ lib/find.rb | 4 +++- test/test_find.rb | 21 +++++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 0627f85b8b..2d85d1fb44 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Mar 2 11:15:10 2014 Nobuyoshi Nakada + + * 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 * ext/readline/extconf.rb (rl_hook_func_t): define as Function for diff --git a/lib/find.rb b/lib/find.rb index 6f3e4282ed..c5fd35b1d7 100644 --- a/lib/find.rb +++ b/lib/find.rb @@ -34,7 +34,7 @@ module Find # # 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 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! diff --git a/test/test_find.rb b/test/test_find.rb index b9246510ed..8bd67829d3 100644 --- a/test/test_find.rb +++ b/test/test_find.rb @@ -100,6 +100,16 @@ class TestFind < Test::Unit::TestCase 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 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