summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tool/lib/_tmpdir.rb26
1 files changed, 18 insertions, 8 deletions
diff --git a/tool/lib/_tmpdir.rb b/tool/lib/_tmpdir.rb
index cb2a9f69f1..52baa6e00b 100644
--- a/tool/lib/_tmpdir.rb
+++ b/tool/lib/_tmpdir.rb
@@ -1,14 +1,23 @@
-require "tmpdir"
-require "fileutils"
-
template = "rubytest."
-if (tmpdir = Dir.mktmpdir(template)).size > 50 and File.directory?("/tmp")
+
+# This path is only for tests.
+# Assume the directory by these environment variables are safe.
+base = [ENV["TMPDIR"], ENV["TMP"], "/tmp"].find do |tmp|
+ next unless tmp and tmp.size <= 50 and File.directory?(tmp)
# On macOS, the default TMPDIR is very long, inspite of UNIX socket
# path length is limited.
- # Assume "/tmp" always exists on UNIX-like systems where UNIX socket
- # is available, otherwise no need to shorten TMPDIR.
- Dir.rmdir(tmpdir)
- tmpdir = Dir.mktmpdir(template, "/tmp")
+ #
+ # Also Rubygems creates its own temporary directory per tests, and
+ # some tests copy the full path of gemhome there. In that caes, the
+ # path contains both temporary names twice, and can exceed path name
+ # limit very easily.
+ tmp
+end
+begin
+ tmpdir = File.join(base, template + Random.new_seed.to_s(36)[-6..-1])
+ Dir.mkdir(tmpdir, 0o700)
+rescue Errno::EEXIST
+ retry
end
# warn "tmpdir(#{tmpdir.size}) = #{tmpdir}"
@@ -80,6 +89,7 @@ END {
File.unlink(path)
end
end
+ require "fileutils"
FileUtils.rm_rf(tmpdir)
end
end