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:
parent
228728325e
commit
a9c2a18cc7
8 changed files with 106 additions and 87 deletions
|
@ -1,4 +1,5 @@
|
|||
require 'test/unit'
|
||||
require 'tmpdir'
|
||||
|
||||
begin
|
||||
require 'dbm'
|
||||
|
@ -27,17 +28,19 @@ if defined? DBM
|
|||
SYSTEM = uname_s
|
||||
|
||||
def setup
|
||||
@path = "tmptest_dbm_"
|
||||
@tmpdir = Dir.tmpdir
|
||||
@prefix = "tmptest_dbm_#{$$}"
|
||||
@path = "#{@tmpdir}/#{@prefix}_"
|
||||
assert_instance_of(DBM, @dbm = DBM.new(@path))
|
||||
|
||||
# prepare to make readonly DBM file
|
||||
DBM.open("tmptest_dbm_rdonly") {|dbm|
|
||||
DBM.open("#{@tmpdir}/#{@prefix}_rdonly") {|dbm|
|
||||
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
|
||||
def teardown
|
||||
assert_nil(@dbm.close)
|
||||
|
@ -45,8 +48,8 @@ if defined? DBM
|
|||
ObjectSpace.each_object(DBM) do |obj|
|
||||
obj.close unless obj.closed?
|
||||
end
|
||||
File.delete *Dir.glob("tmptest_dbm*").to_a
|
||||
p Dir.glob("tmptest_dbm*") if $DEBUG
|
||||
File.delete *Dir.glob("#{@tmpdir}/#{@prefix}*").to_a
|
||||
p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
|
||||
end
|
||||
|
||||
def check_size(expect, dbm=@dbm)
|
||||
|
@ -73,21 +76,21 @@ if defined? DBM
|
|||
def test_s_new_has_no_block
|
||||
# DBM.new ignore the block
|
||||
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_nil(dbm.close)
|
||||
end
|
||||
def test_s_open_no_create
|
||||
assert_nil(dbm = DBM.open("tmptest_dbm", nil))
|
||||
assert_nil(dbm = DBM.open("#{@tmpdir}/#{@prefix}", nil))
|
||||
ensure
|
||||
dbm.close if dbm
|
||||
end
|
||||
def test_s_open_with_block
|
||||
assert_equal(DBM.open("tmptest_dbm") { :foo }, :foo)
|
||||
assert_equal(DBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
# closed DBM file
|
||||
|
@ -498,7 +501,7 @@ if defined? DBM
|
|||
TMPROOT = "#{Dir.tmpdir}/ruby-dbm.#{$$}"
|
||||
|
||||
def setup
|
||||
Dir.mkdir TMPROOT
|
||||
Dir.mkdir TMPROOT, 0755
|
||||
end
|
||||
|
||||
def teardown
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'test/unit'
|
||||
require 'tmpdir'
|
||||
|
||||
begin
|
||||
require 'gdbm'
|
||||
|
@ -27,14 +28,16 @@ if defined? GDBM
|
|||
SYSTEM = uname_s
|
||||
|
||||
def setup
|
||||
@path = "tmptest_gdbm_"
|
||||
@tmpdir = Dir.tmpdir
|
||||
@prefix = "tmptest_gdbm_#{$$}"
|
||||
@path = "#{@tmpdir}/#{@prefix}_"
|
||||
assert_instance_of(GDBM, @gdbm = GDBM.new(@path))
|
||||
|
||||
# prepare to make readonly GDBM file
|
||||
GDBM.open("tmptest_gdbm_rdonly", 0400) {|gdbm|
|
||||
GDBM.open("#{@tmpdir}/#{@prefix}_rdonly", 0400) {|gdbm|
|
||||
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
|
||||
def teardown
|
||||
assert_nil(@gdbm.close)
|
||||
|
@ -42,8 +45,8 @@ if defined? GDBM
|
|||
ObjectSpace.each_object(GDBM) do |obj|
|
||||
obj.close unless obj.closed?
|
||||
end
|
||||
File.delete *Dir.glob("tmptest_gdbm*").to_a
|
||||
p Dir.glob("tmptest_gdbm*") if $DEBUG
|
||||
File.delete *Dir.glob("#{@tmpdir}/#{@prefix}*").to_a
|
||||
p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
|
||||
end
|
||||
|
||||
def check_size(expect, gdbm=@gdbm)
|
||||
|
@ -70,7 +73,7 @@ if defined? GDBM
|
|||
def test_s_new_has_no_block
|
||||
# GDBM.new ignore the block
|
||||
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_nil(gdbm.close)
|
||||
end
|
||||
|
@ -79,54 +82,54 @@ if defined? GDBM
|
|||
|
||||
save_mask = File.umask(0)
|
||||
begin
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm"))
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}"))
|
||||
gdbm.close
|
||||
assert_equal(File.stat("tmptest_gdbm").mode & 0777, 0666)
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm2", 0644))
|
||||
assert_equal(File.stat("#{@tmpdir}/#{@prefix}").mode & 0777, 0666)
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}2", 0644))
|
||||
gdbm.close
|
||||
assert_equal(File.stat("tmptest_gdbm2").mode & 0777, 0644)
|
||||
assert_equal(File.stat("#{@tmpdir}/#{@prefix}2").mode & 0777, 0644)
|
||||
ensure
|
||||
File.umask save_mask
|
||||
end
|
||||
end
|
||||
def test_s_open_no_create
|
||||
# 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
|
||||
gdbm.close if gdbm
|
||||
end
|
||||
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.close
|
||||
|
||||
# gdbm 1.8.0 specific
|
||||
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.close
|
||||
end
|
||||
# gdbm 1.8.0 specific
|
||||
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.close
|
||||
end
|
||||
end
|
||||
def test_s_open_with_block
|
||||
assert_equal(GDBM.open("tmptest_gdbm") { :foo }, :foo)
|
||||
assert_equal(GDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
|
||||
end
|
||||
def test_s_open_lock
|
||||
return unless have_fork? # snip this test
|
||||
pid = fork() {
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644))
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
|
||||
sleep 2
|
||||
}
|
||||
begin
|
||||
sleep 1
|
||||
assert_raise(Errno::EWOULDBLOCK) {
|
||||
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
|
||||
raise Errno::EWOULDBLOCK
|
||||
end
|
||||
|
@ -140,10 +143,10 @@ if defined? GDBM
|
|||
# Is it guaranteed on many OS?
|
||||
def test_s_open_lock_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) {
|
||||
begin
|
||||
GDBM.open("tmptest_gdbm", 0644)
|
||||
GDBM.open("#{@tmpdir}/#{@prefix}", 0644)
|
||||
rescue Errno::EAGAIN
|
||||
raise Errno::EWOULDBLOCK
|
||||
end
|
||||
|
@ -159,7 +162,7 @@ if defined? GDBM
|
|||
return unless have_fork? # snip this test
|
||||
|
||||
pid = fork() {
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644,
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644,
|
||||
GDBM::NOLOCK))
|
||||
sleep 2
|
||||
}
|
||||
|
@ -167,17 +170,17 @@ if defined? GDBM
|
|||
begin
|
||||
gdbm2 = nil
|
||||
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
|
||||
Process.wait pid
|
||||
gdbm2.close if gdbm2
|
||||
end
|
||||
|
||||
p Dir.glob("tmptest_gdbm*") if $DEBUG
|
||||
p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
|
||||
|
||||
pid = fork() {
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("tmptest_gdbm", 0644))
|
||||
assert_instance_of(GDBM, gdbm = GDBM.open("#{@tmpdir}/#{@prefix}", 0644))
|
||||
sleep 2
|
||||
}
|
||||
begin
|
||||
|
@ -185,7 +188,7 @@ if defined? GDBM
|
|||
gdbm2 = nil
|
||||
assert_nothing_raised(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
|
||||
# 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))
|
||||
}
|
||||
ensure
|
||||
|
@ -195,15 +198,15 @@ if defined? GDBM
|
|||
end
|
||||
|
||||
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) {
|
||||
GDBM.open("tmptest_gdbm", 0)
|
||||
GDBM.open("#{@tmpdir}/#{@prefix}", 0)
|
||||
}
|
||||
gdbm.close
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
# closed GDBM file
|
||||
|
@ -592,10 +595,10 @@ if defined? GDBM
|
|||
end
|
||||
|
||||
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)
|
||||
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)
|
||||
gdbm.close
|
||||
end
|
||||
|
@ -653,7 +656,7 @@ if defined? GDBM
|
|||
TMPROOT = "#{Dir.tmpdir}/ruby-gdbm.#{$$}"
|
||||
|
||||
def setup
|
||||
Dir.mkdir TMPROOT
|
||||
Dir.mkdir TMPROOT, 0755
|
||||
end
|
||||
|
||||
def teardown
|
||||
|
|
|
@ -7,7 +7,7 @@ class TestFile < Test::Unit::TestCase
|
|||
# I don't know Ruby's spec about "unlink-before-close" exactly.
|
||||
# This test asserts current behaviour.
|
||||
def test_unlink_before_close
|
||||
filename = File.basename(__FILE__) + ".#{$$}"
|
||||
filename = Dir.tmpdir + '/' + File.basename(__FILE__) + ".#{$$}"
|
||||
w = File.open(filename, "w")
|
||||
w << "foo"
|
||||
w.close
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
require 'test/unit'
|
||||
require 'envutil'
|
||||
require 'tmpdir'
|
||||
|
||||
class TestSystem < Test::Unit::TestCase
|
||||
def valid_syntax?(code, fname)
|
||||
|
@ -14,21 +15,23 @@ class TestSystem < Test::Unit::TestCase
|
|||
assert_equal("foobar\n", `echo 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.close
|
||||
|
||||
assert_equal('true', `#{ruby} -s script_tmp -zzz`)
|
||||
assert_equal('555', `#{ruby} -s script_tmp -zzz=555`)
|
||||
assert_equal('true', `#{ruby} -s #{tmpfilename} -zzz`)
|
||||
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 "print $zzz\n";
|
||||
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 "#! /usr/local/bin/ruby -s\n";
|
||||
tmp.print "print $zzz\n";
|
||||
|
@ -36,24 +39,24 @@ class TestSystem < Test::Unit::TestCase
|
|||
tmp.print "this is a trailing junk\n";
|
||||
tmp.close
|
||||
|
||||
assert_equal('', `#{ruby} -x script_tmp`)
|
||||
assert_equal('555', `#{ruby} -x script_tmp -zzz=555`)
|
||||
assert_equal('', `#{ruby} -x #{tmpfilename}`)
|
||||
assert_equal('555', `#{ruby} -x #{tmpfilename} -zzz=555`)
|
||||
|
||||
tmp = open("script_tmp", "w")
|
||||
tmp = open(tmpfilename, "w")
|
||||
for i in 1..5
|
||||
tmp.print i, "\n"
|
||||
end
|
||||
tmp.close
|
||||
|
||||
`#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' script_tmp`
|
||||
tmp = open("script_tmp", "r")
|
||||
`#{ruby} -i.bak -pe '$_.sub!(/^[0-9]+$/){$&.to_i * 5}' #{tmpfilename}`
|
||||
tmp = open(tmpfilename, "r")
|
||||
while tmp.gets
|
||||
assert_equal(0, $_.to_i % 5)
|
||||
end
|
||||
tmp.close
|
||||
|
||||
File.unlink "script_tmp" or `/bin/rm -f "script_tmp"`
|
||||
File.unlink "script_tmp.bak" or `/bin/rm -f "script_tmp.bak"`
|
||||
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
|
||||
File.unlink "#{tmpfilename}.bak" or `/bin/rm -f "#{tmpfilename}.bak"`
|
||||
end
|
||||
|
||||
def test_syntax
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
require 'test/unit'
|
||||
require 'tmpdir'
|
||||
|
||||
class TestWhileuntil < Test::Unit::TestCase
|
||||
def test_while
|
||||
tmp = open("while_tmp", "w")
|
||||
tmpfilename = "#{Dir.tmpdir}/ruby_while_tmp.#{$$}"
|
||||
|
||||
tmp = open(tmpfilename, "w")
|
||||
tmp.print "tvi925\n";
|
||||
tmp.print "tvi920\n";
|
||||
tmp.print "vt100\n";
|
||||
|
@ -10,7 +13,7 @@ class TestWhileuntil < Test::Unit::TestCase
|
|||
tmp.print "paper\n";
|
||||
tmp.close
|
||||
|
||||
tmp = open("while_tmp", "r")
|
||||
tmp = open(tmpfilename, "r")
|
||||
assert_instance_of(File, tmp)
|
||||
|
||||
while line = tmp.gets()
|
||||
|
@ -21,7 +24,7 @@ class TestWhileuntil < Test::Unit::TestCase
|
|||
assert_match(/vt100/, line)
|
||||
tmp.close
|
||||
|
||||
tmp = open("while_tmp", "r")
|
||||
tmp = open(tmpfilename, "r")
|
||||
while line = tmp.gets()
|
||||
next if /vt100/ =~ line
|
||||
assert_no_match(/vt100/, line)
|
||||
|
@ -30,7 +33,7 @@ class TestWhileuntil < Test::Unit::TestCase
|
|||
assert_no_match(/vt100/, line)
|
||||
tmp.close
|
||||
|
||||
tmp = open("while_tmp", "r")
|
||||
tmp = open(tmpfilename, "r")
|
||||
while line = tmp.gets()
|
||||
lastline = line
|
||||
line = line.gsub(/vt100/, 'VT100')
|
||||
|
@ -54,7 +57,7 @@ class TestWhileuntil < Test::Unit::TestCase
|
|||
end
|
||||
assert_equal(220, sum)
|
||||
|
||||
tmp = open("while_tmp", "r")
|
||||
tmp = open(tmpfilename, "r")
|
||||
while line = tmp.gets()
|
||||
break if 3
|
||||
assert_no_match(/vt100/, line)
|
||||
|
@ -63,8 +66,8 @@ class TestWhileuntil < Test::Unit::TestCase
|
|||
end
|
||||
tmp.close
|
||||
|
||||
File.unlink "while_tmp" or `/bin/rm -f "while_tmp"`
|
||||
assert(!File.exist?("while_tmp"))
|
||||
File.unlink tmpfilename or `/bin/rm -f "#{tmpfilename}"`
|
||||
assert(!File.exist?(tmpfilename))
|
||||
end
|
||||
|
||||
def test_until
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
require 'scanf.rb'
|
||||
require 'test/unit'
|
||||
require 'tmpdir'
|
||||
|
||||
# Comment out either of these lines to skip those tests.
|
||||
|
||||
|
@ -295,15 +296,17 @@ end
|
|||
class TestIOScanf
|
||||
include Scanf
|
||||
extend ScanfTests
|
||||
|
||||
tmpfilename = "#{Dir.tmpdir}/iotest.dat"
|
||||
|
||||
i = 1
|
||||
self.tests.each do |test|
|
||||
define_method("test_#{i}") do ||
|
||||
File.open("iotest.dat", "w") {|fh| fh.print test[1]}
|
||||
File.open("iotest.dat", "r") { |fh|
|
||||
File.open(tmpfilename, "w") {|fh| fh.print test[1]}
|
||||
File.open(tmpfilename, "r") { |fh|
|
||||
assert_equal(test[2], fh.scanf(test[0]))
|
||||
}
|
||||
File.delete("iotest.dat")
|
||||
File.delete(tmpfilename)
|
||||
end
|
||||
i += 1
|
||||
end
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
require 'test/unit'
|
||||
require 'scanf'
|
||||
require 'tmpdir'
|
||||
|
||||
class TestScanfBlock < Test::Unit::TestCase
|
||||
|
||||
|
@ -49,8 +50,8 @@ alias set_up setup
|
|||
end
|
||||
|
||||
def test_io1
|
||||
File.open("iotest.dat", "w") { |fh| fh.puts(@str) }
|
||||
fh = File.open("iotest.dat", "rb")
|
||||
File.open("#{Dir.tmpdir}/iotest.dat", "w") { |fh| fh.puts(@str) }
|
||||
fh = File.open("#{Dir.tmpdir}/iotest.dat", "rb")
|
||||
res = fh.scanf("%s%d") { |name, year| "#{name} was born in #{year}." }
|
||||
|
||||
assert_equal(
|
||||
|
@ -61,18 +62,18 @@ alias set_up setup
|
|||
"Brahms was born in 1833." ],res)
|
||||
fh.close
|
||||
ensure
|
||||
File.delete("iotest.dat")
|
||||
File.delete("#{Dir.tmpdir}/iotest.dat")
|
||||
end
|
||||
|
||||
def test_io2
|
||||
File.open("iotest.dat", "w").close
|
||||
fh = File.open("iotest.dat","rb")
|
||||
File.open("#{Dir.tmpdir}/iotest.dat", "w").close
|
||||
fh = File.open("#{Dir.tmpdir}/iotest.dat","rb")
|
||||
assert_equal(fh.scanf("") {}, [])
|
||||
fh.seek(0)
|
||||
assert_equal(fh.scanf("%d%f%s") {}, [])
|
||||
fh.close
|
||||
ensure
|
||||
File.delete("iotest.dat")
|
||||
File.delete("#{Dir.tmpdir}/iotest.dat")
|
||||
end
|
||||
|
||||
end
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
require 'test/unit'
|
||||
require 'tmpdir'
|
||||
|
||||
begin
|
||||
require 'sdbm'
|
||||
|
@ -7,7 +8,9 @@ end
|
|||
|
||||
class TestSDBM < Test::Unit::TestCase
|
||||
def setup
|
||||
@path = "tmptest_sdbm_"
|
||||
@tmpdir = Dir.tmpdir
|
||||
@prefix = "tmptest_sdbm_#{$$}"
|
||||
@path = "#{@tmpdir}/#{@prefix}_"
|
||||
assert_instance_of(SDBM, @sdbm = SDBM.new(@path))
|
||||
end
|
||||
def teardown
|
||||
|
@ -15,8 +18,8 @@ class TestSDBM < Test::Unit::TestCase
|
|||
ObjectSpace.each_object(SDBM) do |obj|
|
||||
obj.close unless obj.closed?
|
||||
end
|
||||
File.delete *Dir.glob("tmptest_sdbm*").to_a
|
||||
p Dir.glob("tmptest_sdbm*") if $DEBUG
|
||||
File.delete *Dir.glob("#{@tmpdir}/#{@prefix}*").to_a
|
||||
p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
|
||||
end
|
||||
|
||||
def check_size(expect, sdbm=@sdbm)
|
||||
|
@ -47,26 +50,26 @@ class TestSDBM < Test::Unit::TestCase
|
|||
def test_s_new_has_no_block
|
||||
# SDBM.new ignore the block
|
||||
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_nil(sdbm.close)
|
||||
end
|
||||
def test_s_open_no_create
|
||||
assert_nil(sdbm = SDBM.open("tmptest_sdbm", nil))
|
||||
assert_nil(sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", nil))
|
||||
ensure
|
||||
sdbm.close if sdbm
|
||||
end
|
||||
def test_s_open_with_block
|
||||
assert_equal(SDBM.open("tmptest_sdbm") { :foo }, :foo)
|
||||
assert_equal(SDBM.open("#{@tmpdir}/#{@prefix}") { :foo }, :foo)
|
||||
end
|
||||
=begin
|
||||
# Is it guaranteed on many OS?
|
||||
def test_s_open_lock_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) {
|
||||
begin
|
||||
SDBM.open("tmptest_sdbm", 0644)
|
||||
SDBM.open("#{@tmpdir}/#{@prefix}", 0644)
|
||||
rescue Errno::EAGAIN
|
||||
raise Errno::EWOULDBLOCK
|
||||
end
|
||||
|
@ -82,7 +85,7 @@ class TestSDBM < Test::Unit::TestCase
|
|||
return unless have_fork? # snip this test
|
||||
|
||||
pid = fork() {
|
||||
assert_instance_of(SDBM, sdbm = SDBM.open("tmptest_sdbm", 0644,
|
||||
assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644,
|
||||
SDBM::NOLOCK))
|
||||
sleep 2
|
||||
}
|
||||
|
@ -90,17 +93,17 @@ class TestSDBM < Test::Unit::TestCase
|
|||
begin
|
||||
sdbm2 = nil
|
||||
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
|
||||
Process.wait pid
|
||||
sdbm2.close if sdbm2
|
||||
end
|
||||
|
||||
p Dir.glob("tmptest_sdbm*") if $DEBUG
|
||||
p Dir.glob("#{@tmpdir}/#{@prefix}*") if $DEBUG
|
||||
|
||||
pid = fork() {
|
||||
assert_instance_of(SDBM, sdbm = SDBM.open("tmptest_sdbm", 0644))
|
||||
assert_instance_of(SDBM, sdbm = SDBM.open("#{@tmpdir}/#{@prefix}", 0644))
|
||||
sleep 2
|
||||
}
|
||||
begin
|
||||
|
@ -108,7 +111,7 @@ class TestSDBM < Test::Unit::TestCase
|
|||
sdbm2 = nil
|
||||
assert_no_exception(Errno::EWOULDBLOCK, Errno::EAGAIN, Errno::EACCES) {
|
||||
# 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))
|
||||
}
|
||||
ensure
|
||||
|
@ -118,15 +121,15 @@ class TestSDBM < Test::Unit::TestCase
|
|||
end
|
||||
|
||||
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) {
|
||||
SDBM.open("tmptest_sdbm", 0)
|
||||
SDBM.open("#{@tmpdir}/#{@prefix}", 0)
|
||||
}
|
||||
sdbm.close
|
||||
end
|
||||
|
||||
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)
|
||||
|
||||
# closed SDBM file
|
||||
|
|
Loading…
Reference in a new issue