1 # frozen_string_literal: true
5 class TestTmpdir < Test::Unit::TestCase
6 def test_tmpdir_modifiable
8 assert_not_predicate(tmpdir, :frozen?)
9 tmpdir_org = tmpdir.dup
11 assert_equal(tmpdir_org, Dir.tmpdir)
14 def test_world_writable
15 omit "no meaning on this platform" if /mswin|mingw/ =~ RUBY_PLATFORM
16 Dir.mktmpdir do |tmpdir|
17 # ToDo: fix for parallel test
18 envs = %w[TMPDIR TMP TEMP]
19 oldenv = envs.each_with_object({}) {|v, h| h[v] = ENV.delete(v)}
22 tmpdirx = File.join(tmpdir, e)
24 assert_not_equal(tmpdirx, assert_warn('') {Dir.tmpdir})
25 File.write(tmpdirx, "")
26 assert_not_equal(tmpdirx, assert_warn(/not a directory/) {Dir.tmpdir})
29 assert_equal(tmpdir, Dir.tmpdir)
30 File.chmod(0555, tmpdir)
31 assert_not_equal(tmpdir, assert_warn(/not writable/) {Dir.tmpdir})
32 File.chmod(0777, tmpdir)
33 assert_not_equal(tmpdir, assert_warn(/world-writable/) {Dir.tmpdir})
34 newdir = Dir.mktmpdir("d", tmpdir) do |dir|
35 assert_file.directory? dir
36 assert_equal(tmpdir, File.dirname(dir))
39 assert_file.not_exist?(newdir)
40 File.chmod(01777, tmpdir)
41 assert_equal(tmpdir, Dir.tmpdir)
51 bug7547 = '[ruby-core:50793]'
52 home, ENV["HOME"] = ENV["HOME"], nil
53 dir = assert_nothing_raised(bug7547) do
54 break Dir.mktmpdir("~")
56 assert_match(/\A~/, File.basename(dir), bug7547)
63 Dir.mktmpdir(nil) {|d|
64 assert_kind_of(String, d)
68 def test_mktmpdir_mutate
69 bug16918 = '[ruby-core:98563]'
70 assert_nothing_raised(bug16918) do
71 assert_mktmpdir_traversal do |traversal_path|
72 Dir.mktmpdir(traversal_path + 'foo') do |actual|
79 def test_mktmpdir_traversal
80 assert_mktmpdir_traversal do |traversal_path|
81 Dir.mktmpdir(traversal_path + 'foo') do |actual|
87 def test_mktmpdir_traversal_array
88 assert_mktmpdir_traversal do |traversal_path|
89 Dir.mktmpdir([traversal_path, 'foo']) do |actual|
95 def assert_mktmpdir_traversal
96 Dir.mktmpdir do |target|
97 target = target.chomp('/') + '/'
98 traversal_path = target.sub(/\A\w:/, '') # for DOSISH
99 traversal_path = Array.new(target.count('/')-2, '..').join('/') + traversal_path
100 [File::SEPARATOR, File::ALT_SEPARATOR].compact.each do |separator|
101 actual = yield traversal_path.tr('/', separator)
102 assert_not_send([File.absolute_path(actual), :start_with?, target])