1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

don't generate temporary files under current directory.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13553 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2007-09-28 16:51:41 +00:00
parent 228728325e
commit a9c2a18cc7
8 changed files with 106 additions and 87 deletions

View file

@ -1,4 +1,5 @@
require 'test/unit' require 'test/unit'
require 'tmpdir'
begin begin
require 'dbm' require 'dbm'
@ -27,17 +28,19 @@ if defined? DBM
SYSTEM = uname_s SYSTEM = uname_s
def setup def setup
@path = "tmptest_dbm_" @tmpdir = Dir.tmpdir
@prefix = "tmptest_dbm_#{$$}"
@path = "#{@tmpdir}/#{@prefix}_"
assert_instance_of(DBM, @dbm = DBM.new(@path)) assert_instance_of(DBM, @dbm = DBM.new(@path))
# prepare to make readonly DBM file # prepare to make readonly DBM file
DBM.open("tmptest_dbm_rdonly") {|dbm| DBM.open("#{@tmpdir}/#{@prefix}_rdonly") {|dbm|
dbm['foo'] = 'FOO' dbm['foo'] = 'FOO'
} }
File.chmod(0400, *Dir.glob("tmptest_dbm_rdonly.*")) File.chmod(0400, *Dir.glob("#{@tmpdir}/#{@prefix}_rdonly.*"))
assert_instance_of(DBM, @dbm_rdonly = DBM.new("tmptest_dbm_rdonly", nil)) assert_instance_of(DBM, @dbm_rdonly = DBM.new("#{@tmpdir}/#{@prefix}_rdonly", nil))
end end
def teardown def teardown
assert_nil(@dbm.close) assert_nil(@dbm.close)
@ -45,8 +48,8 @@ if defined? DBM
ObjectSpace.each_object(DBM) do |obj| ObjectSpace.each_object(DBM) do |obj|
obj.close unless obj.closed? obj.close unless obj.closed?
end end
File.delete *Dir.glob("tmptest_dbm*").to_a File.delete *Dir.glob("#{@tmpdir}/#{@prefix}*").to_a
p Dir.glob("tmptest_dbm*") if $DEBUG p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
end end
def check_size(expect, dbm=@dbm) def check_size(expect, dbm=@dbm)
@ -73,21 +76,21 @@ if defined? DBM
def test_s_new_has_no_block def test_s_new_has_no_block
# DBM.new ignore the block # DBM.new ignore the block
foo = true foo = true
assert_instance_of(DBM, dbm = DBM.new("tmptest_dbm") { foo = false }) assert_instance_of(DBM, dbm = DBM.new("#{@tmpdir}/#{@prefix}") { foo = false })
assert_equal(foo, true) assert_equal(foo, true)
assert_nil(dbm.close) assert_nil(dbm.close)
end end
def test_s_open_no_create def test_s_open_no_create
assert_nil(dbm = DBM.open("tmptest_dbm", nil)) assert_nil(dbm = DBM.open("#{@tmpdir}/#{@prefix}", nil))
ensure ensure
dbm.close if dbm dbm.close if dbm
end end
def test_s_open_with_block def test_s_open_with_block
assert_equal(DBM.open("tmptest_dbm") { :foo }, :foo) assert_equal(DBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
end end
def test_close def test_close
assert_instance_of(DBM, dbm = DBM.open("tmptest_dbm")) assert_instance_of(DBM, dbm = DBM.open("#{@tmpdir}/#{@prefix}"))
assert_nil(dbm.close) assert_nil(dbm.close)
# closed DBM file # closed DBM file
@ -498,7 +501,7 @@ if defined? DBM
TMPROOT = "#{Dir.tmpdir}/ruby-dbm.#{$$}" TMPROOT = "#{Dir.tmpdir}/ruby-dbm.#{$$}"
def setup def setup
Dir.mkdir TMPROOT Dir.mkdir TMPROOT, 0755
end end
def teardown def teardown

View file

@ -1,4 +1,5 @@
require 'test/unit' require 'test/unit'
require 'tmpdir'
begin begin
require 'gdbm' require 'gdbm'
@ -27,14 +28,16 @@ if defined? GDBM
SYSTEM = uname_s SYSTEM = uname_s
def setup def setup
@path = "tmptest_gdbm_" @tmpdir = Dir.tmpdir
@prefix = "tmptest_gdbm_#{$$}"
@path = "#{@tmpdir}/#{@prefix}_"
assert_instance_of(GDBM, @gdbm = GDBM.new(@path)) assert_instance_of(GDBM, @gdbm = GDBM.new(@path))
# prepare to make readonly GDBM file # prepare to make readonly GDBM file
GDBM.open("tmptest_gdbm_rdonly", 0400) {|gdbm| GDBM.open("#{@tmpdir}/#{@prefix}_rdonly", 0400) {|gdbm|
gdbm['foo'] = 'FOO' gdbm['foo'] = 'FOO'
} }
assert_instance_of(GDBM, @gdbm_rdonly = GDBM.new("tmptest_gdbm_rdonly", nil)) assert_instance_of(GDBM, @gdbm_rdonly = GDBM.new("#{@tmpdir}/#{@prefix}_rdonly", nil))
end end
def teardown def teardown
assert_nil(@gdbm.close) assert_nil(@gdbm.close)
@ -42,8 +45,8 @@ if defined? GDBM
ObjectSpace.each_object(GDBM) do |obj| ObjectSpace.each_object(GDBM) do |obj|
obj.close unless obj.closed? obj.close unless obj.closed?
end end
File.delete *Dir.glob("tmptest_gdbm*").to_a File.delete *Dir.glob("#{@tmpdir}/#{@prefix}*").to_a
p Dir.glob("tmptest_gdbm*") if $DEBUG p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
end end
def check_size(expect, gdbm=@gdbm) def check_size(expect, gdbm=@gdbm)
@ -70,7 +73,7 @@ if defined? GDBM
def test_s_new_has_no_block def test_s_new_has_no_block
# GDBM.new ignore the block # GDBM.new ignore the block
foo = true foo = true
assert_instance_of(GDBM, gdbm = GDBM.new("tmptest_gdbm") { foo = false }) assert_instance_of(GDBM, gdbm = GDBM.new("#{@tmpdir}/#{@prefix}") { foo = false })
assert_equal(foo, true) assert_equal(foo, true)
assert_nil(gdbm.close) assert_nil(gdbm.close)
end end
@ -79,54 +82,54 @@ if defined? GDBM
save_mask = File.umask(0) save_mask = File.umask(0)
begin begin
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm")) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}"))
gdbm.close gdbm.close
assert_equal(File.stat("tmptest_gdbm").mode & 0777, 0666) assert_equal(File.stat("#{@tmpdir}/#{@prefix}").mode & 0777, 0666)
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm2", 0644)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}2", 0644))
gdbm.close gdbm.close
assert_equal(File.stat("tmptest_gdbm2").mode & 0777, 0644) assert_equal(File.stat("#{@tmpdir}/#{@prefix}2").mode & 0777, 0644)
ensure ensure
File.umask save_mask File.umask save_mask
end end
end end
def test_s_open_no_create def test_s_open_no_create
# this test is failed on libgdbm 1.8.0 # this test is failed on libgdbm 1.8.0
assert_nil(gdbm = GDBM.open("tmptest_gdbm", nil)) assert_nil(gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", nil))
ensure ensure
gdbm.close if gdbm gdbm.close if gdbm
end end
def test_s_open_3rd_arg def test_s_open_3rd_arg
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
GDBM::FAST)) GDBM::FAST))
gdbm.close gdbm.close
# gdbm 1.8.0 specific # gdbm 1.8.0 specific
if defined? GDBM::SYNC if defined? GDBM::SYNC
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
GDBM::SYNC)) GDBM::SYNC))
gdbm.close gdbm.close
end end
# gdbm 1.8.0 specific # gdbm 1.8.0 specific
if defined? GDBM::NOLOCK if defined? GDBM::NOLOCK
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
GDBM::NOLOCK)) GDBM::NOLOCK))
gdbm.close gdbm.close
end end
end end
def test_s_open_with_block def test_s_open_with_block
assert_equal(GDBM.open("tmptest_gdbm") { :foo }, :foo) assert_equal(GDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
end end
def test_s_open_lock def test_s_open_lock
return unless have_fork? # snip this test return unless have_fork? # snip this test
pid = fork() { pid = fork() {
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
sleep 2 sleep 2
} }
begin begin
sleep 1 sleep 1
assert_raise(Errno::EWOULDBLOCK) { assert_raise(Errno::EWOULDBLOCK) {
begin begin
assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644)) assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
rescue Errno::EAGAIN, Errno::EACCES rescue Errno::EAGAIN, Errno::EACCES
raise Errno::EWOULDBLOCK raise Errno::EWOULDBLOCK
end end
@ -140,10 +143,10 @@ if defined? GDBM
# Is it guaranteed on many OS? # Is it guaranteed on many OS?
def test_s_open_lock_one_process def test_s_open_lock_one_process
# locking on one process # locking on one process
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
assert_raise(Errno::EWOULDBLOCK) { assert_raise(Errno::EWOULDBLOCK) {
begin begin
GDBM.open("tmptest_gdbm", 0644) GDBM.open("#{@tmpdir}/#{@prefix}", 0644)
rescue Errno::EAGAIN rescue Errno::EAGAIN
raise Errno::EWOULDBLOCK raise Errno::EWOULDBLOCK
end end
@ -159,7 +162,7 @@ if defined? GDBM
return unless have_fork? # snip this test return unless have_fork? # snip this test
pid = fork() { pid = fork() {
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644, assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
GDBM::NOLOCK)) GDBM::NOLOCK))
sleep 2 sleep 2
} }
@ -167,17 +170,17 @@ if defined? GDBM
begin begin
gdbm2 = nil gdbm2 = nil
assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644)) assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
} }
ensure ensure
Process.wait pid Process.wait pid
gdbm2.close if gdbm2 gdbm2.close if gdbm2
end end
p Dir.glob("tmptest_gdbm*") if $DEBUG p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
pid = fork() { pid = fork() {
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
sleep 2 sleep 2
} }
begin begin
@ -185,7 +188,7 @@ if defined? GDBM
gdbm2 = nil gdbm2 = nil
assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
# this test is failed on Cygwin98 (???) # this test is failed on Cygwin98 (???)
assert_instance_of(GDBM, gdbm2 = GDBM.open("tmptest_gdbm", 0644, assert_instance_of(GDBM, gdbm2 = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
GDBM::NOLOCK)) GDBM::NOLOCK))
} }
ensure ensure
@ -195,15 +198,15 @@ if defined? GDBM
end end
def test_s_open_error def test_s_open_error
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0))
assert_raise(Errno::EACCES) { assert_raise(Errno::EACCES) {
GDBM.open("tmptest_gdbm", 0) GDBM.open("#{@tmpdir}/#{@prefix}", 0)
} }
gdbm.close gdbm.close
end end
def test_close def test_close
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm")) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}"))
assert_nil(gdbm.close) assert_nil(gdbm.close)
# closed GDBM file # closed GDBM file
@ -592,10 +595,10 @@ if defined? GDBM
end end
def test_sync def test_sync
assert_instance_of(GDBM, gdbm = GDBM.open('tmptest_gdbm', 0666, GDBM::FAST)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0666, GDBM::FAST))
assert_equal(gdbm.sync, gdbm) assert_equal(gdbm.sync, gdbm)
gdbm.close gdbm.close
assert_instance_of(GDBM, gdbm = GDBM.open('tmptest_gdbm', 0666)) assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0666))
assert_equal(gdbm.sync, gdbm) assert_equal(gdbm.sync, gdbm)
gdbm.close gdbm.close
end end
@ -653,7 +656,7 @@ if defined? GDBM
TMPROOT = "#{Dir.tmpdir}/ruby-gdbm.#{$$}" TMPROOT = "#{Dir.tmpdir}/ruby-gdbm.#{$$}"
def setup def setup
Dir.mkdir TMPROOT Dir.mkdir TMPROOT, 0755
end end
def teardown def teardown

View file

@ -7,7 +7,7 @@ class TestFile < Test::Unit::TestCase
# I don't know Ruby's spec about "unlink-before-close" exactly. # I don't know Ruby's spec about "unlink-before-close" exactly.
# This test asserts current behaviour. # This test asserts current behaviour.
def test_unlink_before_close def test_unlink_before_close
filename = File.basename(__FILE__) + ".#{$$}" filename = Dir.tmpdir + '/' + File.basename(__FILE__) + ".#{$$}"
w = File.open(filename, "w") w = File.open(filename, "w")
w << "foo" w << "foo"
w.close w.close

View file

@ -1,5 +1,6 @@
require 'test/unit' require 'test/unit'
require 'envutil' require 'envutil'
require 'tmpdir'
class TestSystem < Test::Unit::TestCase class TestSystem < Test::Unit::TestCase
def valid_syntax?(code, fname) def valid_syntax?(code, fname)
@ -14,21 +15,23 @@ class TestSystem < Test::Unit::TestCase
assert_equal("foobar\n", `echo foobar`) assert_equal("foobar\n", `echo foobar`)
assert_equal('foobar', `#{ruby} -e 'print "foobar"'`) assert_equal('foobar', `#{ruby} -e 'print "foobar"'`)
tmp = open("script_tmp", "w") tmpfilename = "#{Dir.tmpdir}/ruby_script_tmp.#{$$}"
tmp = open(tmpfilename, "w")
tmp.print "print $zzz\n"; tmp.print "print $zzz\n";
tmp.close tmp.close
assert_equal('true', `#{ruby} -s script_tmp -zzz`) assert_equal('true', `#{ruby} -s #{tmpfilename} -zzz`)
assert_equal('555', `#{ruby} -s script_tmp -zzz=555`) assert_equal('555', `#{ruby} -s #{tmpfilename} -zzz=555`)
tmp = open("script_tmp", "w") tmp = open(tmpfilename, "w")
tmp.print "#! /usr/local/bin/ruby -s\n"; tmp.print "#! /usr/local/bin/ruby -s\n";
tmp.print "print $zzz\n"; tmp.print "print $zzz\n";
tmp.close tmp.close
assert_equal('678', `#{ruby} script_tmp -zzz=678`) assert_equal('678', `#{ruby} #{tmpfilename} -zzz=678`)
tmp = open("script_tmp", "w") tmp = open(tmpfilename, "w")
tmp.print "this is a leading junk\n"; tmp.print "this is a leading junk\n";
tmp.print "#! /usr/local/bin/ruby -s\n"; tmp.print "#! /usr/local/bin/ruby -s\n";
tmp.print "print $zzz\n"; tmp.print "print $zzz\n";
@ -36,24 +39,24 @@ class TestSystem < Test::Unit::TestCase
tmp.print "this is a trailing junk\n"; tmp.print "this is a trailing junk\n";
tmp.close tmp.close
assert_equal('', `#{ruby} -x script_tmp`) assert_equal('', `#{ruby} -x #{tmpfilename}`)
assert_equal('555', `#{ruby} -x script_tmp -zzz=555`) assert_equal('555', `#{ruby} -x #{tmpfilename} -zzz=555`)
tmp = open("script_tmp", "w") tmp = open(tmpfilename, "w")
for i in 1..5 for i in 1..5
tmp.print i, "\n" tmp.print i, "\n"
end end
tmp.close tmp.close
`#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' script_tmp` `#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' #{tmpfilename}`
tmp = open("script_tmp", "r") tmp = open(tmpfilename, "r")
while tmp.gets while tmp.gets
assert_equal(0, $_.to_i % 5) assert_equal(0, $_.to_i % 5)
end end
tmp.close tmp.close
File.unlink "script_tmp" or `/bin/rm -f "script_tmp"` File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"` File.unlink "#{tmpfilename}.bak" or `/bin/rm -f "#{tmpfilename}.bak"`
end end
def test_syntax def test_syntax

View file

@ -1,8 +1,11 @@
require 'test/unit' require 'test/unit'
require 'tmpdir'
class TestWhileuntil < Test::Unit::TestCase class TestWhileuntil < Test::Unit::TestCase
def test_while def test_while
tmp = open("while_tmp", "w") tmpfilename = "#{Dir.tmpdir}/ruby_while_tmp.#{$$}"
tmp = open(tmpfilename, "w")
tmp.print "tvi925\n"; tmp.print "tvi925\n";
tmp.print "tvi920\n"; tmp.print "tvi920\n";
tmp.print "vt100\n"; tmp.print "vt100\n";
@ -10,7 +13,7 @@ class TestWhileuntil < Test::Unit::TestCase
tmp.print "paper\n"; tmp.print "paper\n";
tmp.close tmp.close
tmp = open("while_tmp", "r") tmp = open(tmpfilename, "r")
assert_instance_of(File, tmp) assert_instance_of(File, tmp)
while line = tmp.gets() while line = tmp.gets()
@ -21,7 +24,7 @@ class TestWhileuntil < Test::Unit::TestCase
assert_match(/vt100/, line) assert_match(/vt100/, line)
tmp.close tmp.close
tmp = open("while_tmp", "r") tmp = open(tmpfilename, "r")
while line = tmp.gets() while line = tmp.gets()
next if /vt100/ =~ line next if /vt100/ =~ line
assert_no_match(/vt100/, line) assert_no_match(/vt100/, line)
@ -30,7 +33,7 @@ class TestWhileuntil < Test::Unit::TestCase
assert_no_match(/vt100/, line) assert_no_match(/vt100/, line)
tmp.close tmp.close
tmp = open("while_tmp", "r") tmp = open(tmpfilename, "r")
while line = tmp.gets() while line = tmp.gets()
lastline = line lastline = line
line = line.gsub(/vt100/, 'VT100') line = line.gsub(/vt100/, 'VT100')
@ -54,7 +57,7 @@ class TestWhileuntil < Test::Unit::TestCase
end end
assert_equal(220, sum) assert_equal(220, sum)
tmp = open("while_tmp", "r") tmp = open(tmpfilename, "r")
while line = tmp.gets() while line = tmp.gets()
break if 3 break if 3
assert_no_match(/vt100/, line) assert_no_match(/vt100/, line)
@ -63,8 +66,8 @@ class TestWhileuntil < Test::Unit::TestCase
end end
tmp.close tmp.close
File.unlink "while_tmp" or `/bin/rm -f "while_tmp"` File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
assert(!File.exist?("while_tmp")) assert(!File.exist?(tmpfilename))
end end
def test_until def test_until

View file

@ -7,6 +7,7 @@
require 'scanf.rb' require 'scanf.rb'
require 'test/unit' require 'test/unit'
require 'tmpdir'
# Comment out either of these lines to skip those tests. # Comment out either of these lines to skip those tests.
@ -295,15 +296,17 @@ end
class TestIOScanf class TestIOScanf
include Scanf include Scanf
extend ScanfTests extend ScanfTests
tmpfilename = "#{Dir.tmpdir}/iotest.dat"
i = 1 i = 1
self.tests.each do |test| self.tests.each do |test|
define_method("test_#{i}") do || define_method("test_#{i}") do ||
File.open("iotest.dat", "w") {|fh| fh.print test[1]} File.open(tmpfilename, "w") {|fh| fh.print test[1]}
File.open("iotest.dat", "r") { |fh| File.open(tmpfilename, "r") { |fh|
assert_equal(test[2], fh.scanf(test[0])) assert_equal(test[2], fh.scanf(test[0]))
} }
File.delete("iotest.dat") File.delete(tmpfilename)
end end
i += 1 i += 1
end end

View file

@ -7,6 +7,7 @@
require 'test/unit' require 'test/unit'
require 'scanf' require 'scanf'
require 'tmpdir'
class TestScanfBlock < Test::Unit::TestCase class TestScanfBlock < Test::Unit::TestCase
@ -49,8 +50,8 @@ alias set_up setup
end end
def test_io1 def test_io1
File.open("iotest.dat", "w") { |fh| fh.puts(@str) } File.open("#{Dir.tmpdir}/iotest.dat", "w") { |fh| fh.puts(@str) }
fh = File.open("iotest.dat", "rb") fh = File.open("#{Dir.tmpdir}/iotest.dat", "rb")
res = fh.scanf("%s%d") { |name, year| "#{name} was born in #{year}." } res = fh.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }
assert_equal( assert_equal(
@ -61,18 +62,18 @@ alias set_up setup
"Brahms was born in 1833." ],res) "Brahms was born in 1833." ],res)
fh.close fh.close
ensure ensure
File.delete("iotest.dat") File.delete("#{Dir.tmpdir}/iotest.dat")
end end
def test_io2 def test_io2
File.open("iotest.dat", "w").close File.open("#{Dir.tmpdir}/iotest.dat", "w").close
fh = File.open("iotest.dat","rb") fh = File.open("#{Dir.tmpdir}/iotest.dat","rb")
assert_equal(fh.scanf("") {}, []) assert_equal(fh.scanf("") {}, [])
fh.seek(0) fh.seek(0)
assert_equal(fh.scanf("%d%f%s") {}, []) assert_equal(fh.scanf("%d%f%s") {}, [])
fh.close fh.close
ensure ensure
File.delete("iotest.dat") File.delete("#{Dir.tmpdir}/iotest.dat")
end end
end end

View file

@ -1,4 +1,5 @@
require 'test/unit' require 'test/unit'
require 'tmpdir'
begin begin
require 'sdbm' require 'sdbm'
@ -7,7 +8,9 @@ end
class TestSDBM < Test::Unit::TestCase class TestSDBM < Test::Unit::TestCase
def setup def setup
@path = "tmptest_sdbm_" @tmpdir = Dir.tmpdir
@prefix = "tmptest_sdbm_#{$$}"
@path = "#{@tmpdir}/#{@prefix}_"
assert_instance_of(SDBM, @sdbm = SDBM.new(@path)) assert_instance_of(SDBM, @sdbm = SDBM.new(@path))
end end
def teardown def teardown
@ -15,8 +18,8 @@ class TestSDBM < Test::Unit::TestCase
ObjectSpace.each_object(SDBM) do |obj| ObjectSpace.each_object(SDBM) do |obj|
obj.close unless obj.closed? obj.close unless obj.closed?
end end
File.delete *Dir.glob("tmptest_sdbm*").to_a File.delete *Dir.glob("#{@tmpdir}/#{@prefix}*").to_a
p Dir.glob("tmptest_sdbm*") if $DEBUG p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
end end
def check_size(expect, sdbm=@sdbm) def check_size(expect, sdbm=@sdbm)
@ -47,26 +50,26 @@ class TestSDBM < Test::Unit::TestCase
def test_s_new_has_no_block def test_s_new_has_no_block
# SDBM.new ignore the block # SDBM.new ignore the block
foo = true foo = true
assert_instance_of(SDBM, sdbm = SDBM.new("tmptest_sdbm") { foo = false }) assert_instance_of(SDBM, sdbm = SDBM.new("#{@tmpdir}/#{@prefix}") { foo = false })
assert_equal(foo, true) assert_equal(foo, true)
assert_nil(sdbm.close) assert_nil(sdbm.close)
end end
def test_s_open_no_create def test_s_open_no_create
assert_nil(sdbm = SDBM.open("tmptest_sdbm", nil)) assert_nil(sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", nil))
ensure ensure
sdbm.close if sdbm sdbm.close if sdbm
end end
def test_s_open_with_block def test_s_open_with_block
assert_equal(SDBM.open("tmptest_sdbm") { :foo }, :foo) assert_equal(SDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
end end
=begin =begin
# Is it guaranteed on many OS? # Is it guaranteed on many OS?
def test_s_open_lock_one_process def test_s_open_lock_one_process
# locking on one process # locking on one process
assert_instance_of(SDBM, sdbm = SDBM.open("tmptest_sdbm", 0644)) assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644))
assert_raise(Errno::EWOULDBLOCK) { assert_raise(Errno::EWOULDBLOCK) {
begin begin
SDBM.open("tmptest_sdbm", 0644) SDBM.open("#{@tmpdir}/#{@prefix}", 0644)
rescue Errno::EAGAIN rescue Errno::EAGAIN
raise Errno::EWOULDBLOCK raise Errno::EWOULDBLOCK
end end
@ -82,7 +85,7 @@ class TestSDBM < Test::Unit::TestCase
return unless have_fork? # snip this test return unless have_fork? # snip this test
pid = fork() { pid = fork() {
assert_instance_of(SDBM, sdbm = SDBM.open("tmptest_sdbm", 0644, assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644,
SDBM::NOLOCK)) SDBM::NOLOCK))
sleep 2 sleep 2
} }
@ -90,17 +93,17 @@ class TestSDBM < Test::Unit::TestCase
begin begin
sdbm2 = nil sdbm2 = nil
assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
assert_instance_of(SDBM, sdbm2 = SDBM.open("tmptest_sdbm", 0644)) assert_instance_of(SDBM, sdbm2 = SDBM.open("#{@tmpdir}/#{@prefix}", 0644))
} }
ensure ensure
Process.wait pid Process.wait pid
sdbm2.close if sdbm2 sdbm2.close if sdbm2
end end
p Dir.glob("tmptest_sdbm*") if $DEBUG p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
pid = fork() { pid = fork() {
assert_instance_of(SDBM, sdbm = SDBM.open("tmptest_sdbm", 0644)) assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644))
sleep 2 sleep 2
} }
begin begin
@ -108,7 +111,7 @@ class TestSDBM < Test::Unit::TestCase
sdbm2 = nil sdbm2 = nil
assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) { assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
# this test is failed on Cygwin98 (???) # this test is failed on Cygwin98 (???)
assert_instance_of(SDBM, sdbm2 = SDBM.open("tmptest_sdbm", 0644, assert_instance_of(SDBM, sdbm2 = SDBM.open("#{@tmpdir}/#{@prefix}", 0644,
SDBM::NOLOCK)) SDBM::NOLOCK))
} }
ensure ensure
@ -118,15 +121,15 @@ class TestSDBM < Test::Unit::TestCase
end end
def test_s_open_error def test_s_open_error
assert_instance_of(SDBM, sdbm = SDBM.open("tmptest_sdbm", 0)) assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0))
assert_raise(Errno::EACCES) { assert_raise(Errno::EACCES) {
SDBM.open("tmptest_sdbm", 0) SDBM.open("#{@tmpdir}/#{@prefix}", 0)
} }
sdbm.close sdbm.close
end end
def test_close def test_close
assert_instance_of(SDBM, sdbm = SDBM.open("tmptest_sdbm")) assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}"))
assert_nil(sdbm.close) assert_nil(sdbm.close)
# closed SDBM file # closed SDBM file