2017-01-23 18:47:32 +03:00
|
|
|
# frozen_string_literal: true
|
2012-03-12 02:19:06 +04:00
|
|
|
require 'test/unit'
|
|
|
|
require 'tmpdir'
|
|
|
|
|
|
|
|
class TestTmpdir < Test::Unit::TestCase
|
2015-10-06 15:40:41 +03:00
|
|
|
def test_tmpdir_modifiable
|
|
|
|
tmpdir = Dir.tmpdir
|
2020-07-13 13:21:01 +03:00
|
|
|
assert_not_predicate(tmpdir, :frozen?)
|
2015-10-06 15:40:41 +03:00
|
|
|
tmpdir_org = tmpdir.dup
|
|
|
|
tmpdir << "foo"
|
|
|
|
assert_equal(tmpdir_org, Dir.tmpdir)
|
|
|
|
end
|
|
|
|
|
2012-03-12 02:19:06 +04:00
|
|
|
def test_world_writable
|
2012-03-14 11:42:30 +04:00
|
|
|
skip "no meaning on this platform" if /mswin|mingw/ =~ RUBY_PLATFORM
|
2012-03-12 02:19:06 +04:00
|
|
|
Dir.mktmpdir do |tmpdir|
|
|
|
|
# ToDo: fix for parallel test
|
2020-07-13 13:44:52 +03:00
|
|
|
envs = %w[TMPDIR TMP TEMP]
|
|
|
|
oldenv = envs.each_with_object({}) {|v, h| h[v] = ENV.delete(v)}
|
2012-03-12 02:19:06 +04:00
|
|
|
begin
|
2020-07-13 13:44:52 +03:00
|
|
|
envs.each do |e|
|
2020-07-13 15:26:06 +03:00
|
|
|
tmpdirx = File.join(tmpdir, e)
|
|
|
|
ENV[e] = tmpdirx
|
2020-07-16 11:45:08 +03:00
|
|
|
assert_not_equal(tmpdirx, assert_warn('') {Dir.tmpdir})
|
2020-07-13 15:26:06 +03:00
|
|
|
File.write(tmpdirx, "")
|
2020-07-16 11:45:08 +03:00
|
|
|
assert_not_equal(tmpdirx, assert_warn(/not a directory/) {Dir.tmpdir})
|
2020-07-13 15:26:06 +03:00
|
|
|
File.unlink(tmpdirx)
|
2020-07-13 13:44:52 +03:00
|
|
|
ENV[e] = tmpdir
|
|
|
|
assert_equal(tmpdir, Dir.tmpdir)
|
2020-07-13 15:39:32 +03:00
|
|
|
File.chmod(0555, tmpdir)
|
2020-07-16 11:45:08 +03:00
|
|
|
assert_not_equal(tmpdir, assert_warn(/not writable/) {Dir.tmpdir})
|
2020-07-13 13:44:52 +03:00
|
|
|
File.chmod(0777, tmpdir)
|
2020-07-16 11:45:08 +03:00
|
|
|
assert_not_equal(tmpdir, assert_warn(/world-writable/) {Dir.tmpdir})
|
2020-07-13 13:44:52 +03:00
|
|
|
newdir = Dir.mktmpdir("d", tmpdir) do |dir|
|
|
|
|
assert_file.directory? dir
|
|
|
|
assert_equal(tmpdir, File.dirname(dir))
|
|
|
|
dir
|
|
|
|
end
|
|
|
|
assert_file.not_exist?(newdir)
|
|
|
|
File.chmod(01777, tmpdir)
|
|
|
|
assert_equal(tmpdir, Dir.tmpdir)
|
|
|
|
ENV[e] = nil
|
2019-01-23 09:06:47 +03:00
|
|
|
end
|
2012-03-12 02:19:06 +04:00
|
|
|
ensure
|
2020-07-13 13:44:52 +03:00
|
|
|
ENV.update(oldenv)
|
2012-03-12 02:19:06 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-12-12 16:40:51 +04: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 18:35:04 +04:00
|
|
|
Dir.rmdir(dir) if dir
|
2012-12-12 16:40:51 +04:00
|
|
|
end
|
2014-12-20 11:58:30 +03:00
|
|
|
|
|
|
|
def test_mktmpdir_nil
|
|
|
|
Dir.mktmpdir(nil) {|d|
|
|
|
|
assert_kind_of(String, d)
|
|
|
|
}
|
|
|
|
end
|
2018-03-28 13:03:03 +03:00
|
|
|
|
2020-05-29 05:49:10 +03:00
|
|
|
def test_mktmpdir_mutate
|
|
|
|
bug16918 = '[ruby-core:98563]'
|
|
|
|
assert_nothing_raised(bug16918) do
|
|
|
|
assert_mktmpdir_traversal do |traversal_path|
|
|
|
|
Dir.mktmpdir(traversal_path + 'foo') do |actual|
|
|
|
|
actual << "foo"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-28 13:03:03 +03:00
|
|
|
def test_mktmpdir_traversal
|
2019-10-29 16:39:30 +03:00
|
|
|
assert_mktmpdir_traversal do |traversal_path|
|
|
|
|
Dir.mktmpdir(traversal_path + 'foo') do |actual|
|
|
|
|
actual
|
|
|
|
end
|
2018-07-11 11:33:37 +03:00
|
|
|
end
|
2018-03-28 13:03:03 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_mktmpdir_traversal_array
|
2019-10-29 16:39:30 +03:00
|
|
|
assert_mktmpdir_traversal do |traversal_path|
|
|
|
|
Dir.mktmpdir([traversal_path, 'foo']) do |actual|
|
|
|
|
actual
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_mktmpdir_traversal
|
|
|
|
Dir.mktmpdir do |target|
|
|
|
|
target = target.chomp('/') + '/'
|
|
|
|
traversal_path = target.sub(/\A\w:/, '') # for DOSISH
|
|
|
|
traversal_path = Array.new(target.count('/')-2, '..').join('/') + traversal_path
|
2021-03-31 19:17:45 +03:00
|
|
|
[File::SEPARATOR, File::ALT_SEPARATOR].compact.each do |separator|
|
|
|
|
actual = yield traversal_path.tr('/', separator)
|
|
|
|
assert_not_send([File.absolute_path(actual), :start_with?, target])
|
|
|
|
end
|
2018-07-11 11:33:37 +03:00
|
|
|
end
|
2018-03-28 13:03:03 +03:00
|
|
|
end
|
2012-03-12 02:19:06 +04:00
|
|
|
end
|