[前][次][番号順一覧][スレッド一覧]

ruby-changes:62941

From: Nobuyoshi <ko1@a...>
Date: Mon, 14 Sep 2020 18:28:39 +0900 (JST)
Subject: [ruby-changes:62941] edb5c67195 (master): [ruby/tmpdir] Warn when environment variables skipped (fixes #2)

https://git.ruby-lang.org/ruby.git/commit/?id=edb5c67195

From edb5c67195129e1d10f329edb55e486e1874b20e Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Thu, 16 Jul 2020 17:45:08 +0900
Subject: [ruby/tmpdir] Warn when environment variables skipped (fixes #2)

https://github.com/ruby/tmpdir/commit/af7b020a89

diff --git a/lib/tmpdir.rb b/lib/tmpdir.rb
index e6cb327..0b1f00a 100644
--- a/lib/tmpdir.rb
+++ b/lib/tmpdir.rb
@@ -20,14 +20,21 @@ class Dir https://github.com/ruby/ruby/blob/trunk/lib/tmpdir.rb#L20
 
   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
diff --git a/test/test_tmpdir.rb b/test/test_tmpdir.rb
index 7cee2e1..c56fd5f 100644
--- a/test/test_tmpdir.rb
+++ b/test/test_tmpdir.rb
@@ -21,16 +21,16 @@ class TestTmpdir < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/test_tmpdir.rb#L21
         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))
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]