[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 коммит произвёл Hiroshi SHIBATA
Родитель df1c035d03
Коммит edb5c67195
2 изменённых файлов: 15 добавлений и 8 удалений

Просмотреть файл

@ -20,14 +20,21 @@ class Dir
def self.tmpdir
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
dir = File.expand_path(dir)
if stat = File.stat(dir) and stat.directory? and stat.writable? and
(!stat.world_writable? or stat.sticky?)
stat = File.stat(dir) rescue next
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
break
end rescue nil
end
end
raise ArgumentError, "could not find a temporary directory" unless tmp
tmp

Просмотреть файл

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