2017-01-23 10:47:32 -05:00
|
|
|
# frozen_string_literal: true
|
2012-03-11 18:19:06 -04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'tmpdir'
|
|
|
|
|
|
|
|
class TestTmpdir < Test::Unit::TestCase
|
2015-10-06 08:40:41 -04:00
|
|
|
def test_tmpdir_modifiable
|
|
|
|
tmpdir = Dir.tmpdir
|
|
|
|
assert_equal(false, tmpdir.frozen?)
|
|
|
|
tmpdir_org = tmpdir.dup
|
|
|
|
tmpdir << "foo"
|
|
|
|
assert_equal(tmpdir_org, Dir.tmpdir)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tmpdir_modifiable_safe
|
|
|
|
Thread.new {
|
|
|
|
$SAFE = 1
|
|
|
|
tmpdir = Dir.tmpdir
|
|
|
|
assert_equal(false, tmpdir.frozen?)
|
|
|
|
tmpdir_org = tmpdir.dup
|
|
|
|
tmpdir << "foo"
|
|
|
|
assert_equal(tmpdir_org, Dir.tmpdir)
|
|
|
|
}.join
|
2017-12-28 15:09:24 -05:00
|
|
|
ensure
|
|
|
|
$SAFE = 0
|
2015-10-06 08:40:41 -04:00
|
|
|
end
|
|
|
|
|
2012-03-11 18:19:06 -04:00
|
|
|
def test_world_writable
|
2012-03-14 03:42:30 -04:00
|
|
|
skip "no meaning on this platform" if /mswin|mingw/ =~ RUBY_PLATFORM
|
2012-03-11 18:19:06 -04:00
|
|
|
Dir.mktmpdir do |tmpdir|
|
|
|
|
# ToDo: fix for parallel test
|
|
|
|
olddir, ENV["TMPDIR"] = ENV["TMPDIR"], tmpdir
|
|
|
|
begin
|
|
|
|
assert_equal(tmpdir, Dir.tmpdir)
|
|
|
|
File.chmod(0777, tmpdir)
|
|
|
|
assert_not_equal(tmpdir, Dir.tmpdir)
|
|
|
|
File.chmod(01777, tmpdir)
|
|
|
|
assert_equal(tmpdir, Dir.tmpdir)
|
|
|
|
ensure
|
|
|
|
ENV["TMPDIR"] = olddir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-12 07:40:51 -05:00
|
|
|
|
|
|
|
def test_no_homedir
|
|
|
|
bug7547 = '[ruby-core:50793]'
|
|
|
|
home, ENV["HOME"] = ENV["HOME"], nil
|
|
|
|
dir = assert_nothing_raised(bug7547) do
|
|
|
|
break Dir.mktmpdir("~")
|
|
|
|
end
|
|
|
|
assert_match(/\A~/, File.basename(dir), bug7547)
|
|
|
|
ensure
|
|
|
|
ENV["HOME"] = home
|
2013-01-01 09:35:04 -05:00
|
|
|
Dir.rmdir(dir) if dir
|
2012-12-12 07:40:51 -05:00
|
|
|
end
|
2014-12-20 03:58:30 -05:00
|
|
|
|
|
|
|
def test_mktmpdir_nil
|
|
|
|
Dir.mktmpdir(nil) {|d|
|
|
|
|
assert_kind_of(String, d)
|
|
|
|
}
|
|
|
|
end
|
2018-03-28 06:03:03 -04:00
|
|
|
|
|
|
|
TRAVERSAL_PATH = Array.new(Dir.pwd.split('/').count, '..').join('/') + Dir.pwd + '/'
|
|
|
|
TRAVERSAL_PATH.delete!(':') if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
|
|
|
|
def test_mktmpdir_traversal
|
|
|
|
expect = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
Dir.mktmpdir(TRAVERSAL_PATH + 'foo')
|
|
|
|
actual = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
assert_equal expect, actual
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_mktmpdir_traversal_array
|
|
|
|
expect = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
Dir.mktmpdir([TRAVERSAL_PATH, 'foo'])
|
|
|
|
actual = Dir.glob(TRAVERSAL_PATH + '*').count
|
|
|
|
assert_equal expect, actual
|
|
|
|
end
|
2012-03-11 18:19:06 -04:00
|
|
|
end
|