提交 71a84206 编写于 作者: Y Yves Senn

Merge pull request #18709 from ianks/atomic-write

Return value of yielded block in File.atomic_write
* Return value of yielded block in `File.atomic_write`.
*Ian Ker-Seymer*
* Duplicate frozen array when assigning it to a HashWithIndifferentAccess so
that it doesn't raise a `RuntimeError` when calling `map!` on it in `convert_value`.
......
......@@ -20,7 +20,7 @@ def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
temp_file = Tempfile.new(basename(file_name), temp_dir)
temp_file.binmode
yield temp_file
return_val = yield temp_file
temp_file.close
if File.exist?(file_name)
......@@ -40,6 +40,9 @@ def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
chown(old_stat.uid, old_stat.gid, file_name)
# This operation will affect filesystem ACL's
chmod(old_stat.mode, file_name)
# Make sure we return the result of the yielded block
return_val
rescue Errno::EPERM, Errno::EACCES
# Changing file ownership failed, moving on.
end
......
......@@ -57,6 +57,16 @@ def test_atomic_write_preserves_default_file_permissions
File.unlink(file_name) rescue nil
end
def test_atomic_write_returns_result_from_yielded_block
block_return_value = File.atomic_write(file_name, Dir.pwd) do |file|
"Hello world!"
end
assert_equal "Hello world!", block_return_value
ensure
File.unlink(file_name) rescue nil
end
private
def file_name
"atomic.file"
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册