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

* test/zlib/test_zlib.rb (*): should close files associated with zlib.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@30201 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
usa 2010-12-14 03:57:26 +00:00
parent 842110e6a6
commit b33cb3c017
2 changed files with 155 additions and 129 deletions

View file

@ -1,3 +1,7 @@
Tue Dec 14 12:55:46 2010 NAKAMURA Usaku <usa@ruby-lang.org>
* test/zlib/test_zlib.rb (*): should close files associated with zlib.
Tue Dec 14 11:30:17 2010 NAKAMURA Usaku <usa@ruby-lang.org> Tue Dec 14 11:30:17 2010 NAKAMURA Usaku <usa@ruby-lang.org>
* test/ruby/test_argf.rb (test_inplace_rename_impossible): unlink * test/ruby/test_argf.rb (test_inplace_rename_impossible): unlink

View file

@ -233,7 +233,7 @@ if defined? Zlib
class TestZlibGzipFile < Test::Unit::TestCase class TestZlibGzipFile < Test::Unit::TestCase
def test_to_io def test_to_io
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_to_io")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
@ -242,19 +242,20 @@ if defined? Zlib
end end
def test_crc def test_crc
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_crc")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
f.read f.read
assert_equal(0x8c736521, f.crc) assert_equal(0x8c736521, f.crc)
end
end end
def test_mtime def test_mtime
tim = Time.now tim = Time.now
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_mtime")
t.close t.close
Zlib::GzipWriter.open(t.path) do |gz| Zlib::GzipWriter.open(t.path) do |gz|
gz.mtime = -1 gz.mtime = -1
@ -264,30 +265,33 @@ if defined? Zlib
assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now } assert_raise(Zlib::GzipFile::Error) { gz.mtime = Time.now }
end end
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal(tim.to_i, f.mtime.to_i) assert_equal(tim.to_i, f.mtime.to_i)
end
end end
def test_level def test_level
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_level")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal(Zlib::DEFAULT_COMPRESSION, f.level) assert_equal(Zlib::DEFAULT_COMPRESSION, f.level)
end
end end
def test_os_code def test_os_code
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_os_code")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal(Zlib::OS_CODE, f.os_code) assert_equal(Zlib::OS_CODE, f.os_code)
end
end end
def test_orig_name def test_orig_name
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_orig_name")
t.close t.close
Zlib::GzipWriter.open(t.path) do |gz| Zlib::GzipWriter.open(t.path) do |gz|
gz.orig_name = "foobarbazqux\0quux" gz.orig_name = "foobarbazqux\0quux"
@ -296,12 +300,13 @@ if defined? Zlib
assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" } assert_raise(Zlib::GzipFile::Error) { gz.orig_name = "quux" }
end end
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal("foobarbazqux", f.orig_name) assert_equal("foobarbazqux", f.orig_name)
end
end end
def test_comment def test_comment
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_comment")
t.close t.close
Zlib::GzipWriter.open(t.path) do |gz| Zlib::GzipWriter.open(t.path) do |gz|
gz.comment = "foobarbazqux\0quux" gz.comment = "foobarbazqux\0quux"
@ -310,52 +315,55 @@ if defined? Zlib
assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" } assert_raise(Zlib::GzipFile::Error) { gz.comment = "quux" }
end end
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal("foobarbazqux", f.comment) assert_equal("foobarbazqux", f.comment)
end
end end
def test_lineno def test_lineno
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_lineno")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\nqux\n") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal([0, "foo\n"], [f.lineno, f.gets]) assert_equal([0, "foo\n"], [f.lineno, f.gets])
assert_equal([1, "bar\n"], [f.lineno, f.gets]) assert_equal([1, "bar\n"], [f.lineno, f.gets])
f.lineno = 1000 f.lineno = 1000
assert_equal([1000, "baz\n"], [f.lineno, f.gets]) assert_equal([1000, "baz\n"], [f.lineno, f.gets])
assert_equal([1001, "qux\n"], [f.lineno, f.gets]) assert_equal([1001, "qux\n"], [f.lineno, f.gets])
end
end end
def test_closed_p def test_closed_p
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_closed_p")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal(false, f.closed?) assert_equal(false, f.closed?)
f.read f.read
assert_equal(false, f.closed?) assert_equal(false, f.closed?)
f.close f.close
assert_equal(true, f.closed?) assert_equal(true, f.closed?)
end
end end
def test_sync def test_sync
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_sync")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
f.sync = true f.sync = true
assert_equal(true, f.sync) assert_equal(true, f.sync)
f.read f.read
f.sync = false f.sync = false
assert_equal(false, f.sync) assert_equal(false, f.sync)
f.close end
end end
def test_pos def test_pos
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_pos")
t.close t.close
Zlib::GzipWriter.open(t.path) do |gz| Zlib::GzipWriter.open(t.path) do |gz|
gz.print("foo") gz.print("foo")
@ -365,7 +373,7 @@ if defined? Zlib
end end
def test_path def test_path
t = Tempfile.new("test_zlib_gzip_file") t = Tempfile.new("test_zlib_gzip_file_path")
t.close t.close
gz = Zlib::GzipWriter.open(t.path) gz = Zlib::GzipWriter.open(t.path)
@ -374,10 +382,11 @@ if defined? Zlib
gz.close gz.close
assert_equal(t.path, gz.path) assert_equal(t.path, gz.path)
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal(t.path, f.path) assert_equal(t.path, f.path)
f.close f.close
assert_equal(t.path, f.path) assert_equal(t.path, f.path)
end
s = "" s = ""
sio = StringIO.new(s) sio = StringIO.new(s)
@ -387,9 +396,9 @@ if defined? Zlib
gz.close gz.close
sio = StringIO.new(s) sio = StringIO.new(s)
f = Zlib::GzipReader.new(sio) Zlib::GzipReader.new(sio) do |f|
assert_raise(NoMethodError) { f.path } assert_raise(NoMethodError) { f.path }
f.close end
end end
end end
@ -429,7 +438,7 @@ if defined? Zlib
end end
def test_open def test_open
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_open")
t.close t.close
e = assert_raise(Zlib::GzipFile::Error) { e = assert_raise(Zlib::GzipFile::Error) {
Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path)
@ -456,156 +465,166 @@ if defined? Zlib
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
f = Zlib::GzipReader.open(t.path) f = Zlib::GzipReader.open(t.path)
assert_equal("foo", f.read) begin
f.close assert_equal("foo", f.read)
ensure
f.close
end
end end
def test_rewind def test_rewind
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_rewind")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo", f.read) assert_equal("foo", f.read)
f.rewind f.rewind
assert_equal("foo", f.read) assert_equal("foo", f.read)
f.close end
end end
def test_unused def test_unused
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_unused")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo", f.read(3)) assert_equal("foo", f.read(3))
f.unused f.unused
assert_equal("bar", f.read) assert_equal("bar", f.read)
f.unused f.unused
f.close end
end end
def test_read def test_read
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_read")
t.close t.close
str = "\u3042\u3044\u3046" str = "\u3042\u3044\u3046"
Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) } Zlib::GzipWriter.open(t.path) {|gz| gz.print(str) }
f = Zlib::GzipReader.open(t.path, encoding: "UTF-8") Zlib::GzipReader.open(t.path, encoding: "UTF-8") do |f|
assert_raise(ArgumentError) { f.read(-1) } assert_raise(ArgumentError) { f.read(-1) }
assert_equal(str, f.read) assert_equal(str, f.read)
end
end end
def test_readpartial def test_readpartial
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_readpartial")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert("foo".start_with?(f.readpartial(3))) assert("foo".start_with?(f.readpartial(3)))
end
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
s = "" s = ""
f.readpartial(3, s) f.readpartial(3, s)
assert("foo".start_with?(s)) assert("foo".start_with?(s))
assert_raise(ArgumentError) { f.readpartial(-1) } assert_raise(ArgumentError) { f.readpartial(-1) }
end
end end
def test_getc def test_getc
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_getc")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
"foobar".each_char {|c| assert_equal(c, f.getc) } "foobar".each_char {|c| assert_equal(c, f.getc) }
assert_nil(f.getc) assert_nil(f.getc)
end
end end
def test_getbyte def test_getbyte
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_getbyte")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
"foobar".each_byte {|c| assert_equal(c, f.getbyte) } "foobar".each_byte {|c| assert_equal(c, f.getbyte) }
assert_nil(f.getbyte) assert_nil(f.getbyte)
end
end end
def test_readchar def test_readchar
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_readchar")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
"foobar".each_byte {|c| assert_equal(c, f.readchar.ord) } "foobar".each_byte {|c| assert_equal(c, f.readchar.ord) }
assert_raise(EOFError) { f.readchar } assert_raise(EOFError) { f.readchar }
end
end end
def test_each_byte def test_each_byte
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_each_byte")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foobar") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
a = [] a = []
f.each_byte {|c| a << c } f.each_byte {|c| a << c }
assert_equal("foobar".each_byte.to_a, a) assert_equal("foobar".each_byte.to_a, a)
end
end end
def test_gets2 def test_gets2
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_gets2")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo\n", f.gets) assert_equal("foo\n", f.gets)
assert_equal("bar\n", f.gets) assert_equal("bar\n", f.gets)
assert_equal("baz\n", f.gets) assert_equal("baz\n", f.gets)
assert_nil(f.gets) assert_nil(f.gets)
f.close end
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo\nbar\nbaz\n", f.gets(nil)) assert_equal("foo\nbar\nbaz\n", f.gets(nil))
f.close end
end end
def test_gets def test_gets
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_gets")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal("foo\n", f.readline) assert_equal("foo\n", f.readline)
assert_equal("bar\n", f.readline) assert_equal("bar\n", f.readline)
assert_equal("baz\n", f.readline) assert_equal("baz\n", f.readline)
assert_raise(EOFError) { f.readline } assert_raise(EOFError) { f.readline }
f.close end
end end
def test_each def test_each
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_each")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
a = ["foo\n", "bar\n", "baz\n"] a = ["foo\n", "bar\n", "baz\n"]
f.each {|l| assert_equal(a.shift, l) } f.each {|l| assert_equal(a.shift, l) }
f.close end
end end
def test_readlines def test_readlines
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_readlines")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo\nbar\nbaz\n") }
f = Zlib::GzipReader.open(t.path) Zlib::GzipReader.open(t.path) do |f|
assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines) assert_equal(["foo\n", "bar\n", "baz\n"], f.readlines)
f.close end
end end
def test_reader_wrap def test_reader_wrap
t = Tempfile.new("test_zlib_gzip_reader") t = Tempfile.new("test_zlib_gzip_reader_wrap")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
f = open(t.path) f = open(t.path)
@ -625,21 +644,24 @@ if defined? Zlib
def test_open def test_open
assert_raise(ArgumentError) { Zlib::GzipWriter.open } assert_raise(ArgumentError) { Zlib::GzipWriter.open }
t = Tempfile.new("test_zlib_gzip_writer") t = Tempfile.new("test_zlib_gzip_writer_open")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
f = Zlib::GzipWriter.open(t.path) f = Zlib::GzipWriter.open(t.path)
f.print("bar") begin
f.close f.print("bar")
ensure
f.close
end
assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read }) assert_equal("bar", Zlib::GzipReader.open(t.path) {|gz| gz.read })
assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) } assert_raise(Zlib::StreamError) { Zlib::GzipWriter.open(t.path, 10000) }
end end
def test_write def test_write
t = Tempfile.new("test_zlib_gzip_writer") t = Tempfile.new("test_zlib_gzip_writer_write")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") } Zlib::GzipWriter.open(t.path) {|gz| gz.print("foo") }
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })
@ -651,7 +673,7 @@ if defined? Zlib
end end
def test_putc def test_putc
t = Tempfile.new("test_zlib_gzip_writer") t = Tempfile.new("test_zlib_gzip_writer_putc")
t.close t.close
Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) } Zlib::GzipWriter.open(t.path) {|gz| gz.putc(?x) }
assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read }) assert_equal("x", Zlib::GzipReader.open(t.path) {|gz| gz.read })
@ -660,7 +682,7 @@ if defined? Zlib
end end
def test_writer_wrap def test_writer_wrap
t = Tempfile.new("test_zlib_gzip_writer") t = Tempfile.new("test_zlib_gzip_writer_wrap")
Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") } Zlib::GzipWriter.wrap(t) {|gz| gz.print("foo") }
t.close t.close
assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read }) assert_equal("foo", Zlib::GzipReader.open(t.path) {|gz| gz.read })