1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Return value of yielded block in File.atomic_write

Staying true to Ruby convention, we now return the value of the yielded
block from `File.atomic_write {...}`. This mimics the behavior of MRI's
`File.open {...}`.
This commit is contained in:
Ian Ker-Seymer 2015-01-28 00:33:57 -07:00
parent 36082a6204
commit b3c23f1176
2 changed files with 14 additions and 1 deletions

View file

@ -20,7 +20,7 @@ class File
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 @@ class File
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

View file

@ -57,6 +57,16 @@ class AtomicWriteTest < ActiveSupport::TestCase
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"