6 class TestTryopen < Test::Unit::TestCase
8 def test_tryopen_success
9 tmp = Kgio::File.tryopen(__FILE__)
11 tmp.respond_to?(:close_on_exec?) and
12 assert_equal(RUBY_VERSION.to_f >= 2.0, tmp.close_on_exec?)
14 assert_kind_of File, tmp
15 assert_equal File.read(__FILE__), tmp.read
16 assert_equal __FILE__, tmp.path
17 assert_equal __FILE__, tmp.to_path
21 def test_tryopen_ENOENT
22 tmp = Tempfile.new "tryopen"
25 tmp = Kgio::File.tryopen(path)
26 assert_equal :ENOENT, tmp
29 def test_tryopen_EACCES
30 tmp = Tempfile.new "tryopen"
31 File.chmod 0000, tmp.path
32 tmp = Kgio::File.tryopen(tmp.path)
34 assert_kind_of Kgio::File, tmp
35 warn "cannot test EACCES when euid == 0"
37 assert_equal(:EACCES, tmp)
41 def test_tryopen_readwrite
42 tmp = Tempfile.new "tryopen"
43 file = Kgio::File.tryopen(tmp.path, IO::RDWR)
45 assert_equal "FOO", tmp.sysread(3)
48 def test_tryopen_try_readwrite
49 tmp = Tempfile.new "tryopen"
50 file = Kgio::File.tryopen(tmp.path, IO::RDWR)
51 assert_nil file.kgio_trywrite("FOO")
53 assert_equal "FOO", file.kgio_tryread(3)
57 tmp = Tempfile.new "tryopen"
60 file = Kgio::File.tryopen(path, IO::RDWR|IO::CREAT, 0000)
61 assert_equal 0100000, File.stat(path).mode
70 tmp = Tempfile.new('tryopen')
73 x.report("tryopen (OK)") do
74 nr.times { Kgio::File.tryopen(file).close }
76 x.report("open (OK)") do
77 nr.times { File.readable?(file) && File.open(file).close }
81 assert_equal :ENOENT, Kgio::File.tryopen(file)
83 x.report("tryopen (ENOENT)") do
84 nr.times { Kgio::File.tryopen(file) }
86 x.report("open (ENOENT)") do
87 nr.times { File.readable?(file) && File.open(file) }
90 end if ENV["BENCHMARK"]