2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-09-26 16:00:03 -04:00
|
|
|
require 'test/unit'
|
2003-12-04 21:54:48 -05:00
|
|
|
require 'tempfile'
|
2016-11-05 04:59:43 -04:00
|
|
|
require "-test-/file"
|
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)
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("test-eof") {|f|
|
|
|
|
f << content
|
|
|
|
f.rewind
|
|
|
|
yield f
|
|
|
|
}
|
2003-12-04 21:54:48 -05:00
|
|
|
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
|
|
|
|
2012-05-23 23:07:49 -04:00
|
|
|
def test_empty_file_bom
|
|
|
|
bug6487 = '[ruby-core:45203]'
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(__method__.to_s) {|f|
|
|
|
|
assert_file.exist?(f.path)
|
|
|
|
assert_nothing_raised(bug6487) {File.read(f.path, mode: 'r:utf-8')}
|
|
|
|
assert_nothing_raised(bug6487) {File.read(f.path, mode: 'r:bom|utf-8')}
|
|
|
|
}
|
2012-05-23 23:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def assert_bom(bytes, name)
|
|
|
|
bug6487 = '[ruby-core:45203]'
|
|
|
|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create(name.to_s) {|f|
|
|
|
|
f.sync = true
|
|
|
|
expected = ""
|
|
|
|
result = nil
|
|
|
|
bytes[0...-1].each do |x|
|
|
|
|
f.write x
|
|
|
|
f.write ' '
|
|
|
|
f.pos -= 1
|
|
|
|
expected << x
|
|
|
|
assert_nothing_raised(bug6487) {result = File.read(f.path, mode: 'rb:bom|utf-8')}
|
|
|
|
assert_equal("#{expected} ".force_encoding("utf-8"), result)
|
|
|
|
end
|
|
|
|
f.write bytes[-1]
|
2012-05-23 23:07:49 -04:00
|
|
|
assert_nothing_raised(bug6487) {result = File.read(f.path, mode: 'rb:bom|utf-8')}
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_equal '', result, "valid bom"
|
|
|
|
}
|
2012-05-23 23:07:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_bom_8
|
|
|
|
assert_bom(["\xEF", "\xBB", "\xBF"], __method__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bom_16be
|
|
|
|
assert_bom(["\xFE", "\xFF"], __method__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bom_16le
|
|
|
|
assert_bom(["\xFF", "\xFE"], __method__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bom_32be
|
|
|
|
assert_bom(["\0", "\0", "\xFE", "\xFF"], __method__)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_bom_32le
|
2019-08-13 10:23:43 -04:00
|
|
|
assert_bom(["\xFF", "\xFE\0\0"], __method__)
|
2012-05-23 23:07:49 -04:00
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_truncate_wbuf
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("test-truncate") {|f|
|
|
|
|
f.print "abc"
|
|
|
|
f.truncate(0)
|
|
|
|
f.print "def"
|
|
|
|
f.flush
|
|
|
|
assert_equal("\0\0\0def", File.read(f.path), "[ruby-dev:24191]")
|
|
|
|
}
|
2004-09-07 01:32:26 -04:00
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_truncate_rbuf
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("test-truncate") {|f|
|
|
|
|
f.puts "abc"
|
|
|
|
f.puts "def"
|
|
|
|
f.rewind
|
|
|
|
assert_equal("abc\n", f.gets)
|
|
|
|
f.truncate(3)
|
|
|
|
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
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create("test-truncate") {|f|
|
|
|
|
f.print "abc"
|
|
|
|
f.truncate 10
|
|
|
|
assert_equal("\0" * 7, f.read(100), "[ruby-dev:24532]")
|
|
|
|
}
|
2005-05-12 05:04:49 -04:00
|
|
|
end
|
|
|
|
|
2013-08-15 08:38:02 -04:00
|
|
|
def test_truncate_size
|
|
|
|
Tempfile.create("test-truncate") do |f|
|
2016-08-30 02:22:30 -04:00
|
|
|
q1 = Thread::Queue.new
|
|
|
|
q2 = Thread::Queue.new
|
2013-08-15 08:38:02 -04:00
|
|
|
|
2014-05-31 11:31:46 -04:00
|
|
|
th = Thread.new do
|
2013-08-15 08:38:02 -04:00
|
|
|
data = ''
|
|
|
|
64.times do |i|
|
|
|
|
data << i.to_s
|
|
|
|
f.rewind
|
|
|
|
f.print data
|
|
|
|
f.truncate(data.bytesize)
|
|
|
|
q1.push data.bytesize
|
|
|
|
q2.pop
|
|
|
|
end
|
|
|
|
q1.push nil
|
|
|
|
end
|
|
|
|
|
|
|
|
while size = q1.pop
|
|
|
|
assert_equal size, File.size(f.path)
|
2013-08-15 22:26:28 -04:00
|
|
|
assert_equal size, f.size
|
2013-08-15 08:38:02 -04:00
|
|
|
q2.push true
|
|
|
|
end
|
2014-05-31 11:31:46 -04:00
|
|
|
th.join
|
2013-08-15 08:38:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-05-12 05:04:49 -04:00
|
|
|
def test_read_all_extended_file
|
2019-04-07 19:55:34 -04:00
|
|
|
[{}, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
Tempfile.create("test-extended-file", **mode) {|f|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_nil(f.getc)
|
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
|
|
|
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
|
2019-04-07 19:55:34 -04:00
|
|
|
[{}, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
Tempfile.create("test-extended-file", **mode) {|f|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_nil(f.getc)
|
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
|
|
|
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
|
2019-04-07 19:55:34 -04:00
|
|
|
[{}, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
Tempfile.create("test-extended-file", **mode) {|f|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_nil(f.getc)
|
|
|
|
f.print "\na"
|
|
|
|
f.rewind
|
|
|
|
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
|
2019-04-07 19:55:34 -04:00
|
|
|
[{}, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
Tempfile.create("test-extended-file", **mode) {|f|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_nil(f.getc)
|
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
|
|
|
result = []
|
|
|
|
f.each_char {|b| result << b }
|
|
|
|
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
|
2019-04-07 19:55:34 -04:00
|
|
|
[{}, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
Tempfile.create("test-extended-file", **mode) {|f|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_nil(f.getc)
|
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
|
|
|
result = []
|
|
|
|
f.each_byte {|b| result << b.chr }
|
|
|
|
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
|
2019-04-07 19:55:34 -04:00
|
|
|
[{}, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
Tempfile.create("test-extended-file", **mode) {|f|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_nil(f.getc)
|
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
|
|
|
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
|
2019-04-07 19:55:34 -04:00
|
|
|
[{}, {:textmode=>true}, {:binmode=>true}].each do |mode|
|
|
|
|
Tempfile.create("test-extended-file", **mode) {|f|
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
assert_nil(f.getc)
|
|
|
|
f.print "a"
|
|
|
|
f.rewind
|
|
|
|
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
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_nothing_raised { File.chown(-1, -1) }
|
2005-09-22 17:01:58 -04:00
|
|
|
assert_nothing_raised { File.chown nil, nil }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_chown
|
2019-12-17 23:35:58 -05:00
|
|
|
Tempfile.create("test-chown") {|f|
|
|
|
|
assert_nothing_raised {f.chown(-1, -1)}
|
|
|
|
assert_nothing_raised("[ruby-dev:27140]") {f.chown(nil, nil)}
|
2005-09-22 17:01:58 -04:00
|
|
|
}
|
|
|
|
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
|
2010-03-06 23:55:34 -05:00
|
|
|
|
|
|
|
def test_realpath
|
|
|
|
Dir.mktmpdir('rubytest-realpath') {|tmpdir|
|
|
|
|
realdir = File.realpath(tmpdir)
|
2012-05-26 03:24:59 -04:00
|
|
|
tst = realdir + (File::SEPARATOR*3 + ".")
|
2010-03-06 23:55:34 -05:00
|
|
|
assert_equal(realdir, File.realpath(tst))
|
|
|
|
assert_equal(realdir, File.realpath(".", tst))
|
2019-11-12 04:02:28 -05:00
|
|
|
assert_equal(realdir, Dir.chdir(realdir) {File.realpath(".")})
|
|
|
|
realpath = File.join(realdir, "test")
|
|
|
|
File.write(realpath, "")
|
|
|
|
assert_equal(realpath, Dir.chdir(realdir) {File.realpath("test")})
|
2010-03-14 18:39:25 -04:00
|
|
|
if File::ALT_SEPARATOR
|
|
|
|
bug2961 = '[ruby-core:28653]'
|
|
|
|
assert_equal(realdir, File.realpath(realdir.tr(File::SEPARATOR, File::ALT_SEPARATOR)), bug2961)
|
|
|
|
end
|
2010-03-06 23:55:34 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-09-02 03:58:24 -04:00
|
|
|
def test_realpath_encoding
|
|
|
|
fsenc = Encoding.find("filesystem")
|
|
|
|
nonascii = "\u{0391 0410 0531 10A0 05d0 2C00 3042}"
|
|
|
|
tst = "A"
|
|
|
|
nonascii.each_char {|c| tst << c.encode(fsenc) rescue nil}
|
|
|
|
Dir.mktmpdir('rubytest-realpath') {|tmpdir|
|
|
|
|
realdir = File.realpath(tmpdir)
|
|
|
|
open(File.join(tmpdir, tst), "w") {}
|
2015-10-28 04:01:48 -04:00
|
|
|
a = File.join(tmpdir, "x")
|
2016-01-27 07:00:07 -05:00
|
|
|
begin
|
|
|
|
File.symlink(tst, a)
|
2016-12-06 02:46:14 -05:00
|
|
|
rescue Errno::EACCES, Errno::EPERM
|
2016-01-27 07:00:07 -05:00
|
|
|
skip "need privilege"
|
|
|
|
end
|
2015-09-02 03:58:24 -04:00
|
|
|
assert_equal(File.join(realdir, tst), File.realpath(a))
|
|
|
|
File.unlink(a)
|
|
|
|
|
|
|
|
tst = "A" + nonascii
|
|
|
|
open(File.join(tmpdir, tst), "w") {}
|
|
|
|
File.symlink(tst, a)
|
|
|
|
assert_equal(File.join(realdir, tst), File.realpath(a.encode("UTF-8")))
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-02-28 00:43:53 -05:00
|
|
|
def test_realpath_special_symlink
|
|
|
|
IO.pipe do |r, w|
|
|
|
|
if File.pipe?(path = "/dev/fd/#{r.fileno}")
|
|
|
|
assert_file.identical?(File.realpath(path), path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-06 23:55:34 -05:00
|
|
|
def test_realdirpath
|
|
|
|
Dir.mktmpdir('rubytest-realdirpath') {|tmpdir|
|
|
|
|
realdir = File.realpath(tmpdir)
|
2012-05-26 03:24:59 -04:00
|
|
|
tst = realdir + (File::SEPARATOR*3 + ".")
|
2010-03-06 23:55:34 -05:00
|
|
|
assert_equal(realdir, File.realdirpath(tst))
|
|
|
|
assert_equal(realdir, File.realdirpath(".", tst))
|
|
|
|
assert_equal(File.join(realdir, "foo"), File.realdirpath("foo", tst))
|
2019-11-12 04:02:28 -05:00
|
|
|
assert_equal(realdir, Dir.chdir(realdir) {File.realdirpath(".")})
|
|
|
|
assert_equal(File.join(realdir, "foo"), Dir.chdir(realdir) {File.realdirpath("foo")})
|
2010-03-06 23:55:34 -05:00
|
|
|
}
|
2012-05-26 03:26:36 -04:00
|
|
|
begin
|
|
|
|
result = File.realdirpath("bar", "//:/foo")
|
|
|
|
rescue SystemCallError
|
|
|
|
else
|
|
|
|
if result.start_with?("//")
|
|
|
|
assert_equal("//:/foo/bar", result)
|
|
|
|
end
|
|
|
|
end
|
2010-03-06 23:55:34 -05:00
|
|
|
end
|
|
|
|
|
2015-09-08 10:25:56 -04:00
|
|
|
def test_realdirpath_junction
|
2015-09-08 09:51:04 -04:00
|
|
|
Dir.mktmpdir('rubytest-realpath') {|tmpdir|
|
|
|
|
Dir.chdir(tmpdir) do
|
|
|
|
Dir.mkdir('foo')
|
|
|
|
skip "cannot run mklink" unless system('mklink /j bar foo > nul')
|
|
|
|
assert_equal(File.realpath('foo'), File.realpath('bar'))
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
|
2011-11-09 06:08:15 -05:00
|
|
|
def test_utime_with_minus_time_segv
|
|
|
|
bug5596 = '[ruby-dev:44838]'
|
|
|
|
assert_in_out_err([], <<-EOS, [bug5596], [])
|
2011-11-09 08:15:12 -05:00
|
|
|
require "tempfile"
|
2011-11-09 06:08:15 -05:00
|
|
|
t = Time.at(-1)
|
|
|
|
begin
|
* test/csv/test_features.rb, test/logger/test_logger.rb
test/mkmf/test_have_macro.rb, test/net/http/test_http.rb,
test/openssl/test_config.rb, test/psych/test_encoding.rb,
test/psych/test_exception.rb, test/psych/test_psych.rb,
test/psych/test_tainted.rb, test/readline/test_readline.rb,
test/rexml/test_contrib.rb, test/ruby/test_autoload.rb,
test/ruby/test_beginendblock.rb, test/ruby/test_exception.rb,
test/ruby/test_file.rb, test/ruby/test_io.rb,
test/ruby/test_marshal.rb, test/ruby/test_process.rb,
test/ruby/test_require.rb, test/ruby/test_rubyoptions.rb,
test/syslog/test_syslog_logger.rb, test/webrick/test_httpauth.rb,
test/zlib/test_zlib.rb: Use Tempfile.create.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@40400 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2013-04-20 19:03:52 -04:00
|
|
|
Tempfile.create('test_utime_with_minus_time_segv') {|f|
|
|
|
|
File.utime(t, t, f)
|
|
|
|
}
|
2011-11-09 06:08:15 -05:00
|
|
|
rescue
|
|
|
|
end
|
|
|
|
puts '#{bug5596}'
|
|
|
|
EOS
|
|
|
|
end
|
2011-11-27 19:33:18 -05:00
|
|
|
|
2012-05-15 10:54:15 -04:00
|
|
|
def test_utime
|
|
|
|
bug6385 = '[ruby-core:44776]'
|
|
|
|
|
|
|
|
mod_time_contents = Time.at 1306527039
|
|
|
|
|
|
|
|
file = Tempfile.new("utime")
|
|
|
|
file.close
|
|
|
|
path = file.path
|
|
|
|
|
|
|
|
File.utime(File.atime(path), mod_time_contents, path)
|
|
|
|
stats = File.stat(path)
|
|
|
|
|
|
|
|
file.open
|
|
|
|
file_mtime = file.mtime
|
|
|
|
file.close(true)
|
|
|
|
|
|
|
|
assert_equal(mod_time_contents, file_mtime, bug6385)
|
|
|
|
assert_equal(mod_time_contents, stats.mtime, bug6385)
|
|
|
|
end
|
|
|
|
|
2014-05-22 07:49:22 -04:00
|
|
|
def test_stat
|
2014-05-23 06:34:13 -04:00
|
|
|
tb = Process.clock_gettime(Process::CLOCK_REALTIME)
|
2014-05-26 10:45:51 -04:00
|
|
|
Tempfile.create("stat") {|file|
|
|
|
|
tb = (tb + Process.clock_gettime(Process::CLOCK_REALTIME)) / 2
|
|
|
|
file.close
|
|
|
|
path = file.path
|
|
|
|
|
|
|
|
t0 = Process.clock_gettime(Process::CLOCK_REALTIME)
|
|
|
|
File.write(path, "foo")
|
|
|
|
sleep 2
|
|
|
|
File.write(path, "bar")
|
|
|
|
sleep 2
|
2016-11-04 21:07:19 -04:00
|
|
|
File.read(path)
|
2014-05-26 10:45:51 -04:00
|
|
|
File.chmod(0644, path)
|
|
|
|
sleep 2
|
|
|
|
File.read(path)
|
|
|
|
|
|
|
|
delta = 1
|
|
|
|
stat = File.stat(path)
|
|
|
|
assert_in_delta tb, stat.birthtime.to_f, delta
|
|
|
|
assert_in_delta t0+2, stat.mtime.to_f, delta
|
|
|
|
if stat.birthtime != stat.ctime
|
|
|
|
assert_in_delta t0+4, stat.ctime.to_f, delta
|
|
|
|
end
|
2016-11-05 04:59:43 -04:00
|
|
|
if /mswin|mingw/ !~ RUBY_PLATFORM && !Bug::File::Fs.noatime?(path)
|
2015-02-20 03:19:48 -05:00
|
|
|
# Windows delays updating atime
|
|
|
|
assert_in_delta t0+6, stat.atime.to_f, delta
|
|
|
|
end
|
2014-05-26 10:45:51 -04:00
|
|
|
}
|
2014-05-22 07:49:22 -04:00
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
|
2015-06-13 07:46:38 -04:00
|
|
|
def test_stat_inode
|
|
|
|
assert_not_equal 0, File.stat(__FILE__).ino
|
|
|
|
end
|
|
|
|
|
2011-11-27 19:33:18 -05:00
|
|
|
def test_chmod_m17n
|
|
|
|
bug5671 = '[ruby-dev:44898]'
|
|
|
|
Dir.mktmpdir('test-file-chmod-m17n-') do |tmpdir|
|
|
|
|
file = File.join(tmpdir, "\u3042")
|
|
|
|
File.open(file, 'w'){}
|
|
|
|
assert_equal(File.chmod(0666, file), 1, bug5671)
|
|
|
|
end
|
|
|
|
end
|
2012-04-09 10:10:32 -04:00
|
|
|
|
2012-05-10 14:03:06 -04:00
|
|
|
def test_file_open_permissions
|
|
|
|
Dir.mktmpdir(__method__.to_s) do |tmpdir|
|
2012-05-15 10:54:12 -04:00
|
|
|
tmp = File.join(tmpdir, 'x')
|
|
|
|
File.open(tmp, :mode => IO::RDWR | IO::CREAT | IO::BINARY,
|
2012-05-10 14:03:06 -04:00
|
|
|
:encoding => Encoding::ASCII_8BIT) do |x|
|
|
|
|
|
2013-12-13 04:18:05 -05:00
|
|
|
assert_predicate(x, :autoclose?)
|
2012-05-10 14:03:06 -04:00
|
|
|
assert_equal Encoding::ASCII_8BIT, x.external_encoding
|
2013-12-13 04:18:05 -05:00
|
|
|
x.write 'hello'
|
2012-05-10 14:03:06 -04:00
|
|
|
|
|
|
|
x.seek 0, IO::SEEK_SET
|
|
|
|
|
|
|
|
assert_equal 'hello', x.read
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_file_open_double_mode
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(ArgumentError, 'mode specified twice') {
|
|
|
|
File.open("a", 'w', :mode => 'rw+')
|
|
|
|
}
|
2012-05-10 14:03:06 -04:00
|
|
|
end
|
|
|
|
|
2015-06-12 08:14:06 -04:00
|
|
|
def test_file_share_delete
|
|
|
|
Dir.mktmpdir(__method__.to_s) do |tmpdir|
|
|
|
|
tmp = File.join(tmpdir, 'x')
|
|
|
|
File.open(tmp, mode: IO::WRONLY | IO::CREAT | IO::BINARY | IO::SHARE_DELETE) do |f|
|
|
|
|
assert_file.exist?(tmp)
|
|
|
|
assert_nothing_raised do
|
|
|
|
File.unlink(tmp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_file.not_exist?(tmp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-05-10 14:03:06 -04:00
|
|
|
def test_conflicting_encodings
|
|
|
|
Dir.mktmpdir(__method__.to_s) do |tmpdir|
|
2012-05-15 10:54:12 -04:00
|
|
|
tmp = File.join(tmpdir, 'x')
|
|
|
|
File.open(tmp, 'wb', :encoding => Encoding::EUC_JP) do |x|
|
2012-05-10 14:03:06 -04:00
|
|
|
assert_equal Encoding::EUC_JP, x.external_encoding
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-12 06:50:01 -05:00
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
2012-04-09 10:10:32 -04:00
|
|
|
def test_long_unc
|
|
|
|
feature3399 = '[ruby-core:30623]'
|
|
|
|
path = File.expand_path(__FILE__)
|
2012-04-09 10:14:39 -04:00
|
|
|
path.sub!(%r'\A//', 'UNC/')
|
2012-04-09 10:10:32 -04:00
|
|
|
assert_nothing_raised(Errno::ENOENT, feature3399) do
|
|
|
|
File.stat("//?/#{path}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2012-10-12 05:18:07 -04:00
|
|
|
|
|
|
|
def test_open_nul
|
|
|
|
Dir.mktmpdir(__method__.to_s) do |tmpdir|
|
|
|
|
path = File.join(tmpdir, "foo")
|
|
|
|
assert_raise(ArgumentError) do
|
|
|
|
open(path + "\0bar", "w") {}
|
|
|
|
end
|
2012-10-16 00:02:04 -04:00
|
|
|
assert_file.not_exist?(path)
|
2012-10-12 05:18:07 -04:00
|
|
|
end
|
|
|
|
end
|
2017-08-31 07:14:36 -04:00
|
|
|
|
|
|
|
def test_open_tempfile_path
|
|
|
|
Dir.mktmpdir(__method__.to_s) do |tmpdir|
|
2017-08-31 11:41:54 -04:00
|
|
|
begin
|
|
|
|
io = File.open(tmpdir, File::RDWR | File::TMPFILE)
|
|
|
|
rescue Errno::EINVAL
|
|
|
|
skip 'O_TMPFILE not supported (EINVAL)'
|
2018-12-11 03:44:00 -05:00
|
|
|
rescue Errno::EISDIR
|
|
|
|
skip 'O_TMPFILE not supported (EISDIR)'
|
2017-12-17 23:42:53 -05:00
|
|
|
rescue Errno::EOPNOTSUPP
|
|
|
|
skip 'O_TMPFILE not supported (EOPNOTSUPP)'
|
2017-08-31 07:14:36 -04:00
|
|
|
end
|
2017-08-31 11:41:54 -04:00
|
|
|
|
|
|
|
io.write "foo"
|
|
|
|
io.flush
|
|
|
|
assert_equal 3, io.size
|
|
|
|
assert_raise(IOError) { io.path }
|
|
|
|
ensure
|
|
|
|
io&.close
|
2017-08-31 07:14:36 -04:00
|
|
|
end
|
|
|
|
end if File::Constants.const_defined?(:TMPFILE)
|
|
|
|
|
2019-05-23 05:40:27 -04:00
|
|
|
def test_absolute_path?
|
|
|
|
assert_file.absolute_path?(File.absolute_path(__FILE__))
|
|
|
|
assert_file.absolute_path?("//foo/bar\\baz")
|
|
|
|
assert_file.not_absolute_path?(File.basename(__FILE__))
|
|
|
|
assert_file.not_absolute_path?("C:foo\\bar")
|
|
|
|
assert_file.not_absolute_path?("~")
|
|
|
|
assert_file.not_absolute_path?("~user")
|
|
|
|
|
2019-12-12 06:53:59 -05:00
|
|
|
if /cygwin|mswin|mingw/ =~ RUBY_PLATFORM
|
2019-05-23 05:40:27 -04:00
|
|
|
assert_file.absolute_path?("C:\\foo\\bar")
|
|
|
|
assert_file.absolute_path?("C:/foo/bar")
|
|
|
|
else
|
|
|
|
assert_file.not_absolute_path?("C:\\foo\\bar")
|
|
|
|
assert_file.not_absolute_path?("C:/foo/bar")
|
2019-12-12 06:53:59 -05:00
|
|
|
end
|
|
|
|
if /mswin|mingw/ =~ RUBY_PLATFORM
|
|
|
|
assert_file.not_absolute_path?("/foo/bar\\baz")
|
|
|
|
else
|
2019-05-23 05:40:27 -04:00
|
|
|
assert_file.absolute_path?("/foo/bar\\baz")
|
|
|
|
end
|
|
|
|
end
|
2003-09-26 16:00:03 -04:00
|
|
|
end
|