aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorYves Senn <yves.senn@gmail.com>2015-01-28 08:56:40 +0100
committerYves Senn <yves.senn@gmail.com>2015-01-28 08:58:16 +0100
commit71a84206ab4d3488ac0d522a7375efc67301aae5 (patch)
tree9dea31850c699f74aa2c9f794f30a5f53c6dc7f2 /activesupport
parent36082a62048248c27cdd513c943ee94b716bd7f8 (diff)
parentb3c23f117695b3089c03b7c0d78781c176b2c418 (diff)
downloadrails-71a84206ab4d3488ac0d522a7375efc67301aae5.tar.gz
rails-71a84206ab4d3488ac0d522a7375efc67301aae5.tar.bz2
rails-71a84206ab4d3488ac0d522a7375efc67301aae5.zip
Merge pull request #18709 from ianks/atomic-write
Return value of yielded block in File.atomic_write
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG.md4
-rw-r--r--activesupport/lib/active_support/core_ext/file/atomic.rb5
-rw-r--r--activesupport/test/core_ext/file_test.rb10
3 files changed, 18 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md
index 2caf0b27ee..f597bc9104 100644
--- a/activesupport/CHANGELOG.md
+++ b/activesupport/CHANGELOG.md
@@ -1,3 +1,7 @@
+* 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`.
diff --git a/activesupport/lib/active_support/core_ext/file/atomic.rb b/activesupport/lib/active_support/core_ext/file/atomic.rb
index 38374af388..fad6fa8d9d 100644
--- a/activesupport/lib/active_support/core_ext/file/atomic.rb
+++ b/activesupport/lib/active_support/core_ext/file/atomic.rb
@@ -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
diff --git a/activesupport/test/core_ext/file_test.rb b/activesupport/test/core_ext/file_test.rb
index 2c04e9687c..cde0132b97 100644
--- a/activesupport/test/core_ext/file_test.rb
+++ b/activesupport/test/core_ext/file_test.rb
@@ -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"