Skip to content

Commit a79c727

Browse files
committed
Ignore empty environment variables
Fixes #17
1 parent de14ce0 commit a79c727

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/tmpdir.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ class Dir
1919
# Returns the operating system's temporary file path.
2020

2121
def self.tmpdir
22-
['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].find do |name, dir = ENV[name]|
23-
next if !dir
22+
['TMPDIR', 'TMP', 'TEMP', ['system temporary path', @@systmpdir], ['/tmp']*2, ['.']*2].find do |name, dir|
23+
unless dir
24+
next if !(dir = ENV[name]) or dir.empty?
25+
end
2426
dir = File.expand_path(dir)
2527
stat = File.stat(dir) rescue next
2628
case

test/test_tmpdir.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ def test_world_writable
4747
end
4848
end
4949

50+
def test_tmpdir_not_empty_parent
51+
Dir.mktmpdir do |tmpdir|
52+
envs = %w[TMPDIR TMP TEMP]
53+
oldenv = envs.each_with_object({}) {|v, h| h[v] = ENV.delete(v)}
54+
ENV[envs[0]] = ""
55+
ENV[envs[1]] = tmpdir
56+
assert_equal(tmpdir, Dir.tmpdir)
57+
ensure
58+
ENV.update(oldenv)
59+
end
60+
end
61+
5062
def test_no_homedir
5163
bug7547 = '[ruby-core:50793]'
5264
home, ENV["HOME"] = ENV["HOME"], nil

0 commit comments

Comments
 (0)