mirror of https://github.com/rails/rails
revises a brittle test in Active Support
The revised test assumed that the default permissions of a file matched the umask of the process, but in the general case that depends also on the file system. This test was failing in the /vagrant shared folder of Rails development boxes.
This commit is contained in:
parent
79e880f7f4
commit
8b16ff64f2
|
@ -1,3 +1,5 @@
|
|||
require 'fileutils'
|
||||
|
||||
class File
|
||||
# Write to a file atomically. Useful for situations where you don't
|
||||
# want other processes or threads to see half-written files.
|
||||
|
@ -25,17 +27,9 @@ class File
|
|||
# Get original file permissions
|
||||
old_stat = stat(file_name)
|
||||
rescue Errno::ENOENT
|
||||
# No old permissions, write a temp file to determine the defaults
|
||||
temp_file_name = [
|
||||
'.permissions_check',
|
||||
Thread.current.object_id,
|
||||
Process.pid,
|
||||
rand(1000000)
|
||||
].join('.')
|
||||
check_name = join(dirname(file_name), temp_file_name)
|
||||
open(check_name, 'w') { }
|
||||
old_stat = stat(check_name)
|
||||
unlink(check_name)
|
||||
# If not possible, probe which are the default permissions in the
|
||||
# destination directory.
|
||||
old_stat = probe_stat_in(dirname(file_name))
|
||||
end
|
||||
|
||||
# Overwrite original file with temp file
|
||||
|
@ -45,4 +39,20 @@ class File
|
|||
chown(old_stat.uid, old_stat.gid, file_name)
|
||||
chmod(old_stat.mode, file_name)
|
||||
end
|
||||
|
||||
# Private utility method.
|
||||
def self.probe_stat_in(dir) #:nodoc:
|
||||
basename = [
|
||||
'.permissions_check',
|
||||
Thread.current.object_id,
|
||||
Process.pid,
|
||||
rand(1000000)
|
||||
].join('.')
|
||||
|
||||
file_name = join(dir, basename)
|
||||
FileUtils.touch(file_name)
|
||||
stat(file_name)
|
||||
ensure
|
||||
FileUtils.rm_f(file_name) if file_name
|
||||
end
|
||||
end
|
||||
|
|
|
@ -51,7 +51,7 @@ class AtomicWriteTest < ActiveSupport::TestCase
|
|||
assert !File.exist?(file_name)
|
||||
end
|
||||
assert File.exist?(file_name)
|
||||
assert_equal 0100666 & ~File.umask, file_mode
|
||||
assert_equal File.probe_stat_in(Dir.pwd).mode, file_mode
|
||||
assert_equal contents, File.read(file_name)
|
||||
ensure
|
||||
File.unlink(file_name) rescue nil
|
||||
|
|
Loading…
Reference in New Issue