提交 8b16ff64 编写于 作者: X Xavier Noria

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.
上级 79e880f7
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 @@ def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
# 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 @@ def self.atomic_write(file_name, temp_dir = Dir.tmpdir)
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 @@ def test_atomic_write_preserves_default_file_permissions
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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册