Fix remove_entry error when path encoding is not compatible UTF-8

This commit is contained in:
Kazuhiro NISHIYAMA 2020-06-22 17:07:49 +09:00
parent 48d7ebe6fc
commit d231b8f95b
No known key found for this signature in database
GPG Key ID: 262ED8DBB4222F7A
2 changed files with 22 additions and 1 deletions

View File

@ -1287,7 +1287,11 @@ module FileUtils
def entries
opts = {}
opts[:encoding] = fu_windows? ? ::Encoding::UTF_8 : path.encoding
if fu_windows? && ::Encoding.compatible?(::Encoding::UTF_8, path.encoding)
opts[:encoding] = ::Encoding::UTF_8
else
opts[:encoding] = path.encoding
end
files = if Dir.respond_to?(:children)
Dir.children(path, **opts)

View File

@ -756,6 +756,23 @@ class TestFileUtils < Test::Unit::TestCase
assert_file_not_exist dir
end
def test_remove_entry_multibyte_path
c = "\u00a7"
begin
c = c.encode('filesystem')
rescue EncodingError
c = c.b
end
dir = "tmpdir#{c}"
my_rm_rf dir
Dir.mkdir dir
File.write("#{dir}/#{c}.txt", "test_remove_entry_multibyte_path")
remove_entry dir
assert_file_not_exist dir
end
def test_remove_entry_secure
check_singleton :remove_entry_secure