2003-09-26 16:00:03 -04:00
|
|
|
require 'test/unit'
|
2003-12-04 21:54:48 -05:00
|
|
|
require 'tempfile'
|
2008-02-18 03:09:05 -05:00
|
|
|
require_relative 'ut_eof'
|
2003-09-26 16:00:03 -04:00
|
|
|
|
|
|
|
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
|
2007-10-24 03:11:35 -04:00
|
|
|
Dir.mktmpdir('rubytest-file') {|tmpdir|
|
2007-10-24 01:55:26 -04:00
|
|
|
filename = tmpdir + '/' + File.basename(__FILE__) + ".#{$$}"
|
|
|
|
w = File.open(filename, "w")
|
|
|
|
w << "foo"
|
|
|
|
w.close
|
|
|
|
r = File.open(filename, "r")
|
|
|
|
begin
|
|
|
|
if /(mswin|bccwin|mingw|emx)/ =~ RUBY_PLATFORM
|
|
|
|
assert_raise(Errno::EACCES) {File.unlink(filename)}
|
|
|
|
else
|
|
|
|
assert_nothing_raised {File.unlink(filename)}
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
r.close
|
|
|
|
File.unlink(filename) if File.exist?(filename)
|
2003-09-26 16:00:03 -04:00
|
|
|
end
|
2007-10-24 01:55:26 -04:00
|
|
|
}
|
2003-09-26 16:00:03 -04:00
|
|
|
end
|
2003-12-04 21:54:48 -05:00
|
|
|
|
|
|
|
include TestEOF
|
|
|
|
def open_file(content)
|
|
|
|
f = Tempfile.new("test-eof")
|
|
|
|
f << content
|
|
|
|
f.rewind
|
|
|
|
yield f
|
|
|
|
end
|
2003-12-09 00:33:32 -05:00
|
|
|
alias open_file_rw open_file
|
2003-12-10 03:16:14 -05:00
|
|
|
|
|
|
|
include TestEOF::Seek
|
2004-02-08 01:52:45 -05:00
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_truncate_wbuf
|
2004-09-07 01:32:26 -04:00
|
|
|
f = Tempfile.new("test-truncate")
|
2004-11-11 02:56:38 -05:00
|
|
|
f.print "abc"
|
2004-09-07 01:32:26 -04:00
|
|
|
f.truncate(0)
|
2004-11-11 02:56:38 -05:00
|
|
|
f.print "def"
|
2008-10-29 12:40:05 -04:00
|
|
|
f.flush
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal("\0\0\0def", File.read(f.path), "[ruby-dev:24191]")
|
2008-10-29 12:40:05 -04:00
|
|
|
f.close
|
2004-09-07 01:32:26 -04:00
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_truncate_rbuf
|
2004-09-07 01:32:26 -04:00
|
|
|
f = Tempfile.new("test-truncate")
|
|
|
|
f.puts "abc"
|
|
|
|
f.puts "def"
|
|
|
|
f.close
|
|
|
|
f.open
|
|
|
|
assert_equal("abc\n", f.gets)
|
|
|
|
f.truncate(3)
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_equal(nil, f.gets, "[ruby-dev:24197]")
|
2004-09-07 01:32:26 -04:00
|
|
|
end
|
2005-05-12 05:04:49 -04:00
|
|
|
|
|
|
|
def test_truncate_beyond_eof
|
|
|
|
f = Tempfile.new("test-truncate")
|
|
|
|
f.print "abc"
|
|
|
|
f.truncate 10
|
|
|
|
assert_equal("\0" * 7, f.read(100), "[ruby-dev:24532]")
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_read_all_extended_file
|
2008-10-28 01:20:24 -04:00
|
|
|
[nil, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
f = Tempfile.new("test-extended-file", mode)
|
|
|
|
assert_nil(f.getc)
|
2008-10-29 12:40:05 -04:00
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
2008-10-28 05:34:37 -04:00
|
|
|
assert_equal("a", f.read, "mode = <#{mode}>")
|
2008-10-28 01:20:24 -04:00
|
|
|
end
|
2005-05-12 05:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_gets_extended_file
|
2008-10-28 01:20:24 -04:00
|
|
|
[nil, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
f = Tempfile.new("test-extended-file", mode)
|
|
|
|
assert_nil(f.getc)
|
2008-10-29 12:40:05 -04:00
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
2008-10-28 05:34:37 -04:00
|
|
|
assert_equal("a", f.gets("a"), "mode = <#{mode}>")
|
2008-10-28 01:20:24 -04:00
|
|
|
end
|
2005-05-12 05:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_gets_para_extended_file
|
2008-10-28 01:20:24 -04:00
|
|
|
[nil, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
2008-10-28 05:34:37 -04:00
|
|
|
f = Tempfile.new("test-extended-file", mode)
|
2008-10-28 01:20:24 -04:00
|
|
|
assert_nil(f.getc)
|
2008-10-29 12:40:05 -04:00
|
|
|
f.print "\na"
|
|
|
|
f.rewind
|
2008-10-28 05:34:37 -04:00
|
|
|
assert_equal("a", f.gets(""), "mode = <#{mode}>")
|
2008-10-28 01:20:24 -04:00
|
|
|
end
|
2005-05-12 05:04:49 -04:00
|
|
|
end
|
|
|
|
|
2008-10-27 23:52:43 -04:00
|
|
|
def test_each_char_extended_file
|
2008-10-28 01:20:24 -04:00
|
|
|
[nil, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
2008-10-28 05:34:37 -04:00
|
|
|
f = Tempfile.new("test-extended-file", mode)
|
2008-10-28 01:20:24 -04:00
|
|
|
assert_nil(f.getc)
|
2008-10-29 12:40:05 -04:00
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
2008-10-28 01:20:24 -04:00
|
|
|
result = []
|
|
|
|
f.each_char {|b| result << b }
|
2008-10-28 05:34:37 -04:00
|
|
|
assert_equal([?a], result, "mode = <#{mode}>")
|
2008-10-28 01:20:24 -04:00
|
|
|
end
|
2008-10-27 23:52:43 -04:00
|
|
|
end
|
|
|
|
|
2005-05-12 05:04:49 -04:00
|
|
|
def test_each_byte_extended_file
|
2008-10-28 01:20:24 -04:00
|
|
|
[nil, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
2008-10-28 05:34:37 -04:00
|
|
|
f = Tempfile.new("test-extended-file", mode)
|
2008-10-28 01:20:24 -04:00
|
|
|
assert_nil(f.getc)
|
2008-10-29 12:40:05 -04:00
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
2008-10-28 01:20:24 -04:00
|
|
|
result = []
|
|
|
|
f.each_byte {|b| result << b.chr }
|
2008-10-28 05:34:37 -04:00
|
|
|
assert_equal([?a], result, "mode = <#{mode}>")
|
2008-10-28 01:20:24 -04:00
|
|
|
end
|
2005-05-12 05:04:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_getc_extended_file
|
2008-10-28 01:20:24 -04:00
|
|
|
[nil, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
2008-10-28 05:34:37 -04:00
|
|
|
f = Tempfile.new("test-extended-file", mode)
|
2008-10-28 01:20:24 -04:00
|
|
|
assert_nil(f.getc)
|
2008-10-29 12:40:05 -04:00
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
2008-10-28 05:34:37 -04:00
|
|
|
assert_equal(?a, f.getc, "mode = <#{mode}>")
|
2008-10-28 01:20:24 -04:00
|
|
|
end
|
2005-05-12 05:04:49 -04:00
|
|
|
end
|
|
|
|
|
2008-10-27 23:52:43 -04:00
|
|
|
def test_getbyte_extended_file
|
2008-10-28 01:20:24 -04:00
|
|
|
[nil, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
2008-10-28 05:34:37 -04:00
|
|
|
f = Tempfile.new("test-extended-file", mode)
|
2008-10-28 01:20:24 -04:00
|
|
|
assert_nil(f.getc)
|
2008-10-29 12:40:05 -04:00
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
2008-10-28 05:34:37 -04:00
|
|
|
assert_equal(?a, f.getbyte.chr, "mode = <#{mode}>")
|
2008-10-28 01:20:24 -04:00
|
|
|
end
|
2008-10-27 23:52:43 -04:00
|
|
|
end
|
|
|
|
|
2005-09-22 17:01:58 -04:00
|
|
|
def test_s_chown
|
|
|
|
assert_nothing_raised { File.chown -1, -1 }
|
|
|
|
assert_nothing_raised { File.chown nil, nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chown
|
|
|
|
assert_nothing_raised {
|
|
|
|
File.open(__FILE__) {|f| f.chown -1, -1 }
|
|
|
|
}
|
2007-11-18 02:18:56 -05:00
|
|
|
assert_nothing_raised("[ruby-dev:27140]") {
|
2005-09-22 17:01:58 -04:00
|
|
|
File.open(__FILE__) {|f| f.chown nil, nil }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2008-06-02 08:57:18 -04:00
|
|
|
def test_uninitialized
|
|
|
|
assert_raise(TypeError) { File::Stat.allocate.readable? }
|
2008-08-21 06:20:55 -04:00
|
|
|
assert_nothing_raised { File::Stat.allocate.inspect }
|
2008-06-02 08:57:18 -04:00
|
|
|
end
|
2003-09-26 16:00:03 -04:00
|
|
|
end
|