diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-16 11:59:47 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-09-16 11:59:47 +0000 |
commit | 0e9c2c4dac28bacc851665de94d34b21b1c1cac8 (patch) | |
tree | 7f717315b6ae9f7380e64d62fdc209b82e6f7faf | |
parent | 2ca3c92b99d8d58c5394f6f3b83544047e6f9ebf (diff) | |
download | ruby-0e9c2c4dac28bacc851665de94d34b21b1c1cac8.tar.gz |
fileutils.rb: error at rmdir
* lib/fileutils.rb (rmdir): should not ignore errors first, except
for parent directories. [ruby-dev:50236] [Bug #13889]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59934 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | lib/fileutils.rb | 8 | ||||
-rw-r--r-- | test/fileutils/test_fileutils.rb | 9 |
2 files changed, 13 insertions, 4 deletions
diff --git a/lib/fileutils.rb b/lib/fileutils.rb index ad4a21e26e..37945b4096 100644 --- a/lib/fileutils.rb +++ b/lib/fileutils.rb @@ -245,15 +245,15 @@ module FileUtils fu_output_message "rmdir #{parents ? '-p ' : ''}#{list.join ' '}" if verbose return if noop list.each do |dir| - begin - Dir.rmdir(dir = remove_trailing_slash(dir)) - if parents + Dir.rmdir(dir = remove_trailing_slash(dir)) + if parents + begin until (parent = File.dirname(dir)) == '.' or parent == dir dir = parent Dir.rmdir(dir) end + rescue Errno::ENOTEMPTY, Errno::EEXIST, Errno::ENOENT end - rescue Errno::ENOTEMPTY, Errno::EEXIST, Errno::ENOENT end end end diff --git a/test/fileutils/test_fileutils.rb b/test/fileutils/test_fileutils.rb index 923468d2b9..0c79bc4c1f 100644 --- a/test/fileutils/test_fileutils.rb +++ b/test/fileutils/test_fileutils.rb @@ -1612,6 +1612,15 @@ class TestFileUtils < Test::Unit::TestCase subdir = 'data/sub/dir' mkdir_p(subdir) + File.write("#{subdir}/file", '') + msg = "should fail to remove non-empty directory" + assert_raise(Errno::ENOTEMPTY, Errno::EEXIST, msg) { + rmdir(subdir) + } + assert_raise(Errno::ENOTEMPTY, Errno::EEXIST, msg) { + rmdir(subdir, parents: true) + } + File.unlink("#{subdir}/file") assert_nothing_raised(Errno::ENOENT) { rmdir(subdir, parents: true) } |