summaryrefslogtreecommitdiff
path: root/test/ruby/test_dir.rb
diff options
context:
space:
mode:
authormame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-03 13:34:48 +0000
committermame <mame@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-06-03 13:34:48 +0000
commit9f42dfa0123a704a2144727af7a14d0b5a17b431 (patch)
treef8e99f2380f1e382febef1c4bbfdbe1e7869a8aa /test/ruby/test_dir.rb
parent87050da59fb29e098f32f5b941ea4091685c79ba (diff)
* test/ruby/test_dir.rb: add tests to achieve over 90% test coverage
of dir.c. * test/ruby/test_encoding.rb: add tests for dummy?, name_list and aliases. * test/ruby/test_marshal.rb: add some tests. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@16797 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/ruby/test_dir.rb')
-rw-r--r--test/ruby/test_dir.rb115
1 files changed, 115 insertions, 0 deletions
diff --git a/test/ruby/test_dir.rb b/test/ruby/test_dir.rb
index 10c4a04fed..e6d9e9c3ac 100644
--- a/test/ruby/test_dir.rb
+++ b/test/ruby/test_dir.rb
@@ -7,6 +7,7 @@ class TestDir < Test::Unit::TestCase
def setup
@root = Dir.mktmpdir('__test_dir__')
+ @nodir = File.join(@root, "dummy")
for i in ?a..?z
if i.ord % 2 == 0
FileUtils.touch(File.join(@root, i))
@@ -47,4 +48,118 @@ class TestDir < Test::Unit::TestCase
assert_raise(SecurityError) { b.call }
end
+ def test_nodir
+ assert_raise(Errno::ENOENT) { Dir.open(@nodir) }
+ end
+
+ def test_inspect
+ d = Dir.open(@root)
+ assert_match(/^#<Dir:#{ Regexp.quote(@root) }>$/, d.inspect)
+ assert_match(/^#<Dir:.*>$/, Dir.allocate.inspect)
+ ensure
+ d.close
+ end
+
+ def test_path
+ d = Dir.open(@root)
+ assert_equal(@root, d.path)
+ assert_nil(Dir.allocate.path)
+ ensure
+ d.close
+ end
+
+ def test_set_pos
+ d = Dir.open(@root)
+ loop do
+ i = d.pos
+ break unless x = d.read
+ d.pos = i
+ assert_equal(x, d.read)
+ end
+ ensure
+ d.close
+ end
+
+ def test_rewind
+ d = Dir.open(@root)
+ a = (0..5).map { d.read }
+ d.rewind
+ b = (0..5).map { d.read }
+ assert_equal(a, b)
+ assert_raise(SecurityError) do
+ Thread.new do
+ $SAFE = 4
+ d.rewind
+ end.join
+ end
+ ensure
+ d.close
+ end
+
+ def test_chdir
+ @pwd = Dir.pwd
+ @env_home = ENV["HOME"]
+ @env_logdir = ENV["LOGDIR"]
+ ENV.delete("HOME")
+ ENV.delete("LOGDIR")
+
+ assert_raise(Errno::ENOENT) { Dir.chdir(@nodir) }
+ assert_raise(ArgumentError) { Dir.chdir }
+ ENV["HOME"] = @pwd
+ Dir.chdir do
+ assert_equal(@pwd, Dir.pwd)
+ Dir.chdir(@root)
+ assert_equal(@root, Dir.pwd)
+ end
+
+ ensure
+ begin
+ Dir.chdir(@pwd)
+ rescue
+ abort("cannot return the original directory: #{ @pwd }")
+ end
+ if @env_home
+ ENV["HOME"] = @env_home
+ else
+ ENV.delete("HOME")
+ end
+ if @env_logdir
+ ENV["LOGDIR"] = @env_logdir
+ else
+ ENV.delete("LOGDIR")
+ end
+ end
+
+ def test_chroot_nodir
+ assert_raise(NotImplementedError, Errno::ENOENT) { Dir.chroot(File.join(@nodir, "")) }
+ end
+
+ def test_close
+ d = Dir.open(@root)
+ d.close
+ assert_raise(IOError) { d.read }
+ end
+
+ def test_glob
+ assert_equal((%w(. ..) + (?a..?z).to_a).map{|f| File.join(@root, f) },
+ Dir.glob(File.join(@root, "*"), File::FNM_DOTMATCH).sort)
+ assert_equal([@root] + (?a..?z).map {|f| File.join(@root, f) },
+ Dir.glob([@root, File.join(@root, "*")]))
+ assert_equal([@root] + (?a..?z).map {|f| File.join(@root, f) },
+ Dir.glob(@root + "\0\0\0" + File.join(@root, "*")))
+
+ assert_equal((?a..?z).step(2).map {|f| File.join(File.join(@root, f), "") },
+ Dir.glob(File.join(@root, "*/")))
+
+ FileUtils.touch(File.join(@root, "{}"))
+ assert_equal(%w({} a).map{|f| File.join(@root, f) },
+ Dir.glob(File.join(@root, '{\{\},a}')))
+ assert_equal([], Dir.glob(File.join(@root, '[')))
+ assert_equal([], Dir.glob(File.join(@root, '[a-\\')))
+ end
+
+ def test_foreach
+ assert_equal(Dir.foreach(@root).to_a.sort, %w(. ..) + (?a..?z).to_a)
+ end
+
end