diff options
author | Jeremy Evans <code@jeremyevans.net> | 2023-02-09 12:45:52 -0800 |
---|---|---|
committer | Jeremy Evans <code@jeremyevans.net> | 2023-03-24 11:18:57 -0700 |
commit | 3be65f63c79492908e898d8d7281035445a2b9a1 (patch) | |
tree | d26c29069be936a0f8de84b1105f96b5da3479ea /test | |
parent | 466ca7ae205126c7cac83735db887d69e293f816 (diff) | |
download | ruby-3be65f63c79492908e898d8d7281035445a2b9a1.tar.gz |
Add Dir#chdir
This uses Dir.fchdir if supported, or Dir.chdir otherwise.
Implements [Feature #19347]
Diffstat (limited to 'test')
-rw-r--r-- | test/ruby/test_dir.rb | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb index 63ef99f159..36480023fb 100644 --- a/test/ruby/test_dir.rb +++ b/test/ruby/test_dir.rb @@ -96,7 +96,7 @@ class TestDir < Test::Unit::TestCase d.close end - def test_chdir + def test_class_chdir pwd = Dir.pwd setup_envs @@ -134,6 +134,45 @@ class TestDir < Test::Unit::TestCase end end + def test_instance_chdir + pwd = Dir.pwd + dir = Dir.new(pwd) + root_dir = Dir.new(@root) + setup_envs + + ENV["HOME"] = pwd + root_dir.chdir do + assert_warning(/conflicting chdir during another chdir block/) { dir.chdir } + assert_warning(/conflicting chdir during another chdir block/) { root_dir.chdir } + + assert_equal(@root, Dir.pwd) + + assert_raise(RuntimeError) { Thread.new { Thread.current.report_on_exception = false; dir.chdir }.join } + assert_raise(RuntimeError) { Thread.new { Thread.current.report_on_exception = false; dir.chdir{} }.join } + + assert_warning(/conflicting chdir during another chdir block/) { dir.chdir } + assert_equal(pwd, Dir.pwd) + + assert_warning(/conflicting chdir during another chdir block/) { root_dir.chdir } + assert_equal(@root, Dir.pwd) + + assert_warning(/conflicting chdir during another chdir block/) { dir.chdir } + + root_dir.chdir do + assert_equal(@root, Dir.pwd) + end + assert_equal(pwd, Dir.pwd) + end + ensure + begin + dir.chdir + rescue + abort("cannot return the original directory: #{ pwd }") + end + dir.close + root_dir.close + end + def test_chdir_conflict pwd = Dir.pwd q = Thread::Queue.new |