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

[ruby/tmpdir] Warn when environment variables skipped (fixes #2)

https://github.com/ruby/tmpdir/commit/af7b020a89
This commit is contained in:
Nobuyoshi Nakada 2020-07-16 17:45:08 +09:00 committed by Hiroshi SHIBATA
parent df1c035d03
commit edb5c67195
2 changed files with 15 additions and 8 deletions

View file

@ -20,14 +20,21 @@ class Dir
def self.tmpdir def self.tmpdir
tmp = nil tmp = nil
[ENV['TMPDIR'], ENV['TMP'], ENV['TEMP'], @@systmpdir, '/tmp', '.'].each do |dir| ['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].each do |name, dir = ENV[name]|
next if !dir next if !dir
dir = File.expand_path(dir) dir = File.expand_path(dir)
if stat = File.stat(dir) and stat.directory? and stat.writable? and stat = File.stat(dir) rescue next
(!stat.world_writable? or stat.sticky?) case
when !stat.directory?
warn "#{name} is not a directory: #{dir}"
when !stat.writable?
warn "#{name} is not writable: #{dir}"
when stat.world_writable? && !stat.sticky?
warn "#{name} is world-writable: #{dir}"
else
tmp = dir tmp = dir
break break
end rescue nil end
end end
raise ArgumentError, "could not find a temporary directory" unless tmp raise ArgumentError, "could not find a temporary directory" unless tmp
tmp tmp

View file

@ -21,16 +21,16 @@ class TestTmpdir < Test::Unit::TestCase
envs.each do |e| envs.each do |e|
tmpdirx = File.join(tmpdir, e) tmpdirx = File.join(tmpdir, e)
ENV[e] = tmpdirx ENV[e] = tmpdirx
assert_not_equal(tmpdirx, Dir.tmpdir) assert_not_equal(tmpdirx, assert_warn('') {Dir.tmpdir})
File.write(tmpdirx, "") File.write(tmpdirx, "")
assert_not_equal(tmpdirx, Dir.tmpdir) assert_not_equal(tmpdirx, assert_warn(/not a directory/) {Dir.tmpdir})
File.unlink(tmpdirx) File.unlink(tmpdirx)
ENV[e] = tmpdir ENV[e] = tmpdir
assert_equal(tmpdir, Dir.tmpdir) assert_equal(tmpdir, Dir.tmpdir)
File.chmod(0555, tmpdir) File.chmod(0555, tmpdir)
assert_not_equal(tmpdir, Dir.tmpdir) assert_not_equal(tmpdir, assert_warn(/not writable/) {Dir.tmpdir})
File.chmod(0777, tmpdir) File.chmod(0777, tmpdir)
assert_not_equal(tmpdir, Dir.tmpdir) assert_not_equal(tmpdir, assert_warn(/world-writable/) {Dir.tmpdir})
newdir = Dir.mktmpdir("d", tmpdir) do |dir| newdir = Dir.mktmpdir("d", tmpdir) do |dir|
assert_file.directory? dir assert_file.directory? dir
assert_equal(tmpdir, File.dirname(dir)) assert_equal(tmpdir, File.dirname(dir))