diff options
Diffstat (limited to 'test/ruby')
-rw-r--r-- | test/ruby/test_autoload.rb | 162 | ||||
-rw-r--r-- | test/ruby/test_beginendblock.rb | 92 | ||||
-rw-r--r-- | test/ruby/test_exception.rb | 39 | ||||
-rw-r--r-- | test/ruby/test_file.rb | 192 | ||||
-rw-r--r-- | test/ruby/test_io.rb | 44 | ||||
-rw-r--r-- | test/ruby/test_marshal.rb | 7 | ||||
-rw-r--r-- | test/ruby/test_process.rb | 3 | ||||
-rw-r--r-- | test/ruby/test_require.rb | 310 | ||||
-rw-r--r-- | test/ruby/test_rubyoptions.rb | 193 |
9 files changed, 506 insertions, 536 deletions
diff --git a/test/ruby/test_autoload.rb b/test/ruby/test_autoload.rb index 8ca2187970..1931df45c5 100644 --- a/test/ruby/test_autoload.rb +++ b/test/ruby/test_autoload.rb @@ -57,109 +57,103 @@ p Foo::Bar end def test_require_explicit - file = Tempfile.open(['autoload', '.rb']) - file.puts 'class Object; AutoloadTest = 1; end' - file.close - add_autoload(file.path) - begin - assert_nothing_raised do - assert(require file.path) - assert_equal(1, ::AutoloadTest) + Tempfile.create(['autoload', '.rb']) {|file| + file.puts 'class Object; AutoloadTest = 1; end' + file.close + add_autoload(file.path) + begin + assert_nothing_raised do + assert(require file.path) + assert_equal(1, ::AutoloadTest) + end + ensure + remove_autoload_constant end - ensure - remove_autoload_constant - end - ensure - file.unlink + } end def test_threaded_accessing_constant - file = Tempfile.open(['autoload', '.rb']) - file.puts 'sleep 0.5; class AutoloadTest; X = 1; end' - file.close - add_autoload(file.path) - begin - assert_nothing_raised do - t1 = Thread.new { ::AutoloadTest::X } - t2 = Thread.new { ::AutoloadTest::X } - [t1, t2].each(&:join) + Tempfile.create(['autoload', '.rb']) {|file| + file.puts 'sleep 0.5; class AutoloadTest; X = 1; end' + file.close + add_autoload(file.path) + begin + assert_nothing_raised do + t1 = Thread.new { ::AutoloadTest::X } + t2 = Thread.new { ::AutoloadTest::X } + [t1, t2].each(&:join) + end + ensure + remove_autoload_constant end - ensure - remove_autoload_constant - end - ensure - file.unlink + } end def test_threaded_accessing_inner_constant - file = Tempfile.open(['autoload', '.rb']) - file.puts 'class AutoloadTest; sleep 0.5; X = 1; end' - file.close - add_autoload(file.path) - begin - assert_nothing_raised do - t1 = Thread.new { ::AutoloadTest::X } - t2 = Thread.new { ::AutoloadTest::X } - [t1, t2].each(&:join) + Tempfile.create(['autoload', '.rb']) {|file| + file.puts 'class AutoloadTest; sleep 0.5; X = 1; end' + file.close + add_autoload(file.path) + begin + assert_nothing_raised do + t1 = Thread.new { ::AutoloadTest::X } + t2 = Thread.new { ::AutoloadTest::X } + [t1, t2].each(&:join) + end + ensure + remove_autoload_constant end - ensure - remove_autoload_constant - end - ensure - file.unlink + } end def test_nameerror_when_autoload_did_not_define_the_constant - file = Tempfile.open(['autoload', '.rb']) - file.puts '' - file.close - add_autoload(file.path) - begin - assert_raise(NameError) do - AutoloadTest + Tempfile.create(['autoload', '.rb']) {|file| + file.puts '' + file.close + add_autoload(file.path) + begin + assert_raise(NameError) do + AutoloadTest + end + ensure + remove_autoload_constant end - ensure - remove_autoload_constant - end - ensure - file.unlink + } end def test_override_autoload - file = Tempfile.open(['autoload', '.rb']) - file.puts '' - file.close - add_autoload(file.path) - begin - eval %q(class AutoloadTest; end) - assert_equal(Class, AutoloadTest.class) - ensure - remove_autoload_constant - end - ensure - file.unlink + Tempfile.create(['autoload', '.rb']) {|file| + file.puts '' + file.close + add_autoload(file.path) + begin + eval %q(class AutoloadTest; end) + assert_equal(Class, AutoloadTest.class) + ensure + remove_autoload_constant + end + } end def test_override_while_autoloading - file = Tempfile.open(['autoload', '.rb']) - file.puts 'class AutoloadTest; sleep 0.5; end' - file.close - add_autoload(file.path) - begin - # while autoloading... - t = Thread.new { AutoloadTest } - sleep 0.1 - # override it - EnvUtil.suppress_warning { - eval %q(AutoloadTest = 1) - } - t.join - assert_equal(1, AutoloadTest) - ensure - remove_autoload_constant - end - ensure - file.unlink + Tempfile.create(['autoload', '.rb']) {|file| + file.puts 'class AutoloadTest; sleep 0.5; end' + file.close + add_autoload(file.path) + begin + # while autoloading... + t = Thread.new { AutoloadTest } + sleep 0.1 + # override it + EnvUtil.suppress_warning { + eval %q(AutoloadTest = 1) + } + t.join + assert_equal(1, AutoloadTest) + ensure + remove_autoload_constant + end + } end def add_autoload(path) diff --git a/test/ruby/test_beginendblock.rb b/test/ruby/test_beginendblock.rb index beb934b1ac..85b2afb139 100644 --- a/test/ruby/test_beginendblock.rb +++ b/test/ruby/test_beginendblock.rb @@ -16,22 +16,19 @@ class TestBeginEndBlock < Test::Unit::TestCase result = IO.popen([ruby, target]){|io|io.read} assert_equal(%w(b1 b2-1 b2 main b3-1 b3 b4 e1 e1-1 e4 e4-2 e4-1 e4-1-1 e3 e2), result.split) - input = Tempfile.new(self.class.name) - inputpath = input.path - input.close - result = IO.popen([ruby, "-n", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} - assert_equal(%w(:begin), result.split) - result = IO.popen([ruby, "-p", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} - assert_equal(%w(:begin), result.split) - input.open - input.puts "foo\nbar" - input.close - result = IO.popen([ruby, "-n", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} - assert_equal(%w(:begin :end), result.split) - result = IO.popen([ruby, "-p", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} - assert_equal(%w(:begin foo bar :end), result.split) - ensure - input.unlink + Tempfile.create(self.class.name) {|input| + inputpath = input.path + result = IO.popen([ruby, "-n", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} + assert_equal(%w(:begin), result.split) + result = IO.popen([ruby, "-p", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} + assert_equal(%w(:begin), result.split) + input.puts "foo\nbar" + input.close + result = IO.popen([ruby, "-n", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} + assert_equal(%w(:begin :end), result.split) + result = IO.popen([ruby, "-p", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} + assert_equal(%w(:begin foo bar :end), result.split) + } end def test_begininmethod @@ -56,10 +53,10 @@ class TestBeginEndBlock < Test::Unit::TestCase def test_endblockwarn ruby = EnvUtil.rubybin # Use Tempfile to create temporary file path. - launcher = Tempfile.new(self.class.name) - errout = Tempfile.new(self.class.name) + Tempfile.create(self.class.name) {|launcher| + Tempfile.create(self.class.name) {|errout| - launcher << <<EOF + launcher << <<EOF # -*- coding: #{ruby.encoding.name} -*- errout = ARGV.shift STDERR.reopen(File.open(errout, "w")) @@ -67,20 +64,18 @@ STDERR.sync = true Dir.chdir(#{q(DIR)}) system("#{ruby}", "endblockwarn_rb") EOF - launcher.close - launcherpath = launcher.path - errout.close - erroutpath = errout.path - system(ruby, launcherpath, erroutpath) - expected = <<EOW + launcher.close + launcherpath = launcher.path + errout.close + erroutpath = errout.path + system(ruby, launcherpath, erroutpath) + expected = <<EOW endblockwarn_rb:2: warning: END in method; use at_exit (eval):2: warning: END in method; use at_exit EOW - assert_equal(expected, File.read(erroutpath)) - # expecting Tempfile to unlink launcher and errout file. - ensure - launcher.unlink - errout.unlink + assert_equal(expected, File.read(erroutpath)) + } + } end def test_raise_in_at_exit @@ -134,25 +129,24 @@ EOW end def test_nested_at_exit - t = Tempfile.new(["test_nested_at_exit_", ".rb"]) - t.puts "at_exit { puts :outer0 }" - t.puts "at_exit { puts :outer1_begin; at_exit { puts :inner1 }; puts :outer1_end }" - t.puts "at_exit { puts :outer2_begin; at_exit { puts :inner2 }; puts :outer2_end }" - t.puts "at_exit { puts :outer3 }" - t.flush - - expected = [ "outer3", - "outer2_begin", - "outer2_end", - "inner2", - "outer1_begin", - "outer1_end", - "inner1", - "outer0" ] - - assert_in_out_err(t.path, "", expected, [], "[ruby-core:35237]") - ensure - t.close(true) + Tempfile.create(["test_nested_at_exit_", ".rb"]) {|t| + t.puts "at_exit { puts :outer0 }" + t.puts "at_exit { puts :outer1_begin; at_exit { puts :inner1 }; puts :outer1_end }" + t.puts "at_exit { puts :outer2_begin; at_exit { puts :inner2 }; puts :outer2_end }" + t.puts "at_exit { puts :outer3 }" + t.flush + + expected = [ "outer3", + "outer2_begin", + "outer2_end", + "inner2", + "outer1_begin", + "outer1_end", + "inner1", + "outer0" ] + + assert_in_out_err(t.path, "", expected, [], "[ruby-core:35237]") + } end def test_rescue_at_exit diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb index a44a52e928..c829a179d5 100644 --- a/test/ruby/test_exception.rb +++ b/test/ruby/test_exception.rb @@ -108,12 +108,11 @@ class TestException < Test::Unit::TestCase def test_catch_throw_in_require bug7185 = '[ruby-dev:46234]' - t = Tempfile.open(["dep", ".rb"]) - t.puts("throw :extdep, 42") - t.close - assert_equal(42, catch(:extdep) {require t.path}, bug7185) - ensure - t.close! if t + Tempfile.create(["dep", ".rb"]) {|t| + t.puts("throw :extdep, 42") + t.close + assert_equal(42, catch(:extdep) {require t.path}, bug7185) + } end def test_else_no_exception @@ -398,9 +397,7 @@ end.join def test_exception_in_name_error_to_str bug5575 = '[ruby-core:41612]' - t = nil - Tempfile.open(["test_exception_in_name_error_to_str", ".rb"]) do |f| - t = f + Tempfile.create(["test_exception_in_name_error_to_str", ".rb"]) do |t| t.puts <<-EOC begin BasicObject.new.inspect @@ -408,12 +405,11 @@ end.join $!.inspect end EOC + t.close + assert_nothing_raised(NameError, bug5575) do + load(t.path) + end end - assert_nothing_raised(NameError, bug5575) do - load(t.path) - end - ensure - t.close(true) if t end def test_equal @@ -424,21 +420,18 @@ end.join def test_exception_in_exception_equal bug5865 = '[ruby-core:41979]' - t = nil - Tempfile.open(["test_exception_in_exception_equal", ".rb"]) do |f| - t = f + Tempfile.create(["test_exception_in_exception_equal", ".rb"]) do |t| t.puts <<-EOC o = Object.new def o.exception(arg) end _ = RuntimeError.new("a") == o - EOC - end - assert_nothing_raised(ArgumentError, bug5865) do - load(t.path) + EOC + t.close + assert_nothing_raised(ArgumentError, bug5865) do + load(t.path) + end end - ensure - t.close(true) if t end Bug4438 = '[ruby-core:35364]' diff --git a/test/ruby/test_file.rb b/test/ruby/test_file.rb index 9c93f16576..528193dabb 100644 --- a/test/ruby/test_file.rb +++ b/test/ruby/test_file.rb @@ -29,12 +29,11 @@ class TestFile < Test::Unit::TestCase include TestEOF def open_file(content) - f = Tempfile.new("test-eof") - f << content - f.rewind - yield f - ensure - f.close(true) + Tempfile.create("test-eof") {|f| + f << content + f.rewind + yield f + } end alias open_file_rw open_file @@ -42,33 +41,32 @@ class TestFile < Test::Unit::TestCase def test_empty_file_bom bug6487 = '[ruby-core:45203]' - f = Tempfile.new(__method__.to_s) - f.close - 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')} - f.close(true) + 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')} + } end def assert_bom(bytes, name) bug6487 = '[ruby-core:45203]' - f = Tempfile.new(name.to_s) - f.sync = true - expected = "" - result = nil - bytes[0...-1].each do |x| - f.write x - f.write ' ' - f.pos -= 1 - expected << x + 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] 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] - assert_nothing_raised(bug6487) {result = File.read(f.path, mode: 'rb:bom|utf-8')} - assert_equal '', result, "valid bom" - f.close(true) + assert_equal '', result, "valid bom" + } end def test_bom_8 @@ -92,113 +90,112 @@ class TestFile < Test::Unit::TestCase end def test_truncate_wbuf - f = Tempfile.new("test-truncate") - f.print "abc" - f.truncate(0) - f.print "def" - f.flush - assert_equal("\0\0\0def", File.read(f.path), "[ruby-dev:24191]") - f.close(true) + 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]") + } end def test_truncate_rbuf - f = Tempfile.new("test-truncate") - f.puts "abc" - f.puts "def" - f.close - f.open - assert_equal("abc\n", f.gets) - f.truncate(3) - assert_equal(nil, f.gets, "[ruby-dev:24197]") - f.close(true) + 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]") + } end 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]") - f.close(true) + Tempfile.create("test-truncate") {|f| + f.print "abc" + f.truncate 10 + assert_equal("\0" * 7, f.read(100), "[ruby-dev:24532]") + } end def test_read_all_extended_file [nil, {:textmode=>true}, {:binmode=>true}].each do |mode| - f = Tempfile.new("test-extended-file", mode) - assert_nil(f.getc) - f.print "a" - f.rewind - assert_equal("a", f.read, "mode = <#{mode}>") - f.close(true) + Tempfile.create("test-extended-file", mode) {|f| + assert_nil(f.getc) + f.print "a" + f.rewind + assert_equal("a", f.read, "mode = <#{mode}>") + } end end def test_gets_extended_file [nil, {:textmode=>true}, {:binmode=>true}].each do |mode| - f = Tempfile.new("test-extended-file", mode) - assert_nil(f.getc) - f.print "a" - f.rewind - assert_equal("a", f.gets("a"), "mode = <#{mode}>") - f.close(true) + Tempfile.create("test-extended-file", mode) {|f| + assert_nil(f.getc) + f.print "a" + f.rewind + assert_equal("a", f.gets("a"), "mode = <#{mode}>") + } end end def test_gets_para_extended_file [nil, {:textmode=>true}, {:binmode=>true}].each do |mode| - f = Tempfile.new("test-extended-file", mode) - assert_nil(f.getc) - f.print "\na" - f.rewind - assert_equal("a", f.gets(""), "mode = <#{mode}>") - f.close(true) + Tempfile.create("test-extended-file", mode) {|f| + assert_nil(f.getc) + f.print "\na" + f.rewind + assert_equal("a", f.gets(""), "mode = <#{mode}>") + } end end def test_each_char_extended_file [nil, {:textmode=>true}, {:binmode=>true}].each do |mode| - f = Tempfile.new("test-extended-file", mode) - assert_nil(f.getc) - f.print "a" - f.rewind - result = [] - f.each_char {|b| result << b } - assert_equal([?a], result, "mode = <#{mode}>") - f.close(true) + Tempfile.create("test-extended-file", mode) {|f| + assert_nil(f.getc) + f.print "a" + f.rewind + result = [] + f.each_char {|b| result << b } + assert_equal([?a], result, "mode = <#{mode}>") + } end end def test_each_byte_extended_file [nil, {:textmode=>true}, {:binmode=>true}].each do |mode| - f = Tempfile.new("test-extended-file", mode) - assert_nil(f.getc) - f.print "a" - f.rewind - result = [] - f.each_byte {|b| result << b.chr } - assert_equal([?a], result, "mode = <#{mode}>") - f.close(true) + Tempfile.create("test-extended-file", mode) {|f| + assert_nil(f.getc) + f.print "a" + f.rewind + result = [] + f.each_byte {|b| result << b.chr } + assert_equal([?a], result, "mode = <#{mode}>") + } end end def test_getc_extended_file [nil, {:textmode=>true}, {:binmode=>true}].each do |mode| - f = Tempfile.new("test-extended-file", mode) - assert_nil(f.getc) - f.print "a" - f.rewind - assert_equal(?a, f.getc, "mode = <#{mode}>") - f.close(true) + Tempfile.create("test-extended-file", mode) {|f| + assert_nil(f.getc) + f.print "a" + f.rewind + assert_equal(?a, f.getc, "mode = <#{mode}>") + } end end def test_getbyte_extended_file [nil, {:textmode=>true}, {:binmode=>true}].each do |mode| - f = Tempfile.new("test-extended-file", mode) - assert_nil(f.getc) - f.print "a" - f.rewind - assert_equal(?a, f.getbyte.chr, "mode = <#{mode}>") - f.close(true) + Tempfile.create("test-extended-file", mode) {|f| + assert_nil(f.getc) + f.print "a" + f.rewind + assert_equal(?a, f.getbyte.chr, "mode = <#{mode}>") + } end end @@ -258,11 +255,10 @@ class TestFile < Test::Unit::TestCase require "tempfile" t = Time.at(-1) begin - f = Tempfile.new('test_utime_with_minus_time_segv') - File.utime(t, t, f) + Tempfile.create('test_utime_with_minus_time_segv') {|f| + File.utime(t, t, f) + } rescue - ensure - f.close(true) end puts '#{bug5596}' EOS diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index b968bb6fcd..58cae7e291 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -2204,7 +2204,7 @@ End return if /x86_64-linux/ !~ RUBY_PLATFORM # A binary form of struct flock depend on platform pad=0 - Tempfile.open(self.class.name) do |f| + Tempfile.create(self.class.name) do |f| r, w = IO.pipe pid = fork do r.close @@ -2230,7 +2230,6 @@ End Process.kill :TERM, pid Process.waitpid2(pid) - f.close(true) end end @@ -2240,7 +2239,7 @@ End start = 12 len = 34 sysid = 0 - Tempfile.open(self.class.name) do |f| + Tempfile.create(self.class.name) do |f| r, w = IO.pipe pid = fork do r.close @@ -2270,14 +2269,13 @@ End end def test_fcntl_dupfd - Tempfile.open(self.class.name) do |f| + Tempfile.create(self.class.name) do |f| fd = f.fcntl(Fcntl::F_DUPFD, 63) begin assert_operator(fd, :>=, 63) ensure IO.for_fd(fd).close end - f.unlink end end @@ -2396,23 +2394,25 @@ End end def test_race_between_read - file = Tempfile.new("test") - path = file.path - file.close - write_file = File.open(path, "wt") - read_file = File.open(path, "rt") - - threads = [] - 10.times do |i| - threads << Thread.new {write_file.print(i)} - threads << Thread.new {read_file.read} - end - threads.each {|t| t.join} - assert(true, "[ruby-core:37197]") - ensure - read_file.close - write_file.close - file.close! + Tempfile.create("test") {|file| + begin + path = file.path + file.close + write_file = File.open(path, "wt") + read_file = File.open(path, "rt") + + threads = [] + 10.times do |i| + threads << Thread.new {write_file.print(i)} + threads << Thread.new {read_file.read} + end + threads.each {|t| t.join} + assert(true, "[ruby-core:37197]") + ensure + read_file.close + write_file.close + end + } end def test_warn diff --git a/test/ruby/test_marshal.rb b/test/ruby/test_marshal.rb index 6d610b3a28..a642385a8e 100644 --- a/test/ruby/test_marshal.rb +++ b/test/ruby/test_marshal.rb @@ -333,11 +333,10 @@ class TestMarshal < Test::Unit::TestCase assert_equal(c, Marshal.load(Marshal.dump(c)), bug2109) assert_nothing_raised(ArgumentError, '[ruby-dev:40386]') do - re = Tempfile.open("marshal_regexp") do |f| + re = Tempfile.create("marshal_regexp") do |f| f.binmode.write("\x04\bI/\x00\x00\x06:\rencoding\"\rUS-ASCII") - f.close - re2 = Marshal.load(f.open.binmode) - f.close(true) + f.rewind + re2 = Marshal.load(f) re2 end assert_equal(//, re) diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb index a70dca692e..99723f625f 100644 --- a/test/ruby/test_process.rb +++ b/test/ruby/test_process.rb @@ -808,14 +808,13 @@ class TestProcess < Test::Unit::TestCase def test_execopts_redirect_tempfile bug6269 = '[ruby-core:44181]' - Tempfile.open("execopts") do |tmp| + Tempfile.create("execopts") do |tmp| pid = assert_nothing_raised(ArgumentError, bug6269) do break spawn(RUBY, "-e", "print $$", out: tmp) end Process.wait(pid) tmp.rewind assert_equal(pid.to_s, tmp.read) - tmp.close(true) end end diff --git a/test/ruby/test_require.rb b/test/ruby/test_require.rb index c03876d223..663c125708 100644 --- a/test/ruby/test_require.rb +++ b/test/ruby/test_require.rb @@ -14,19 +14,19 @@ class TestRequire < Test::Unit::TestCase end def test_require_invalid_shared_object - t = Tempfile.new(["test_ruby_test_require", ".so"]) - t.puts "dummy" - t.close + Tempfile.create(["test_ruby_test_require", ".so"]) {|t| + t.puts "dummy" + t.close - assert_in_out_err([], <<-INPUT, %w(:ok), []) - $:.replace([IO::NULL]) - begin - require \"#{ t.path }\" - rescue LoadError - p :ok - end - INPUT - t.close(true) + assert_in_out_err([], <<-INPUT, %w(:ok), []) + $:.replace([IO::NULL]) + begin + require \"#{ t.path }\" + rescue LoadError + p :ok + end + INPUT + } end def test_require_too_long_filename @@ -111,21 +111,20 @@ class TestRequire < Test::Unit::TestCase def test_require_path_home_3 env_rubypath, env_home = ENV["RUBYPATH"], ENV["HOME"] - t = Tempfile.new(["test_ruby_test_require", ".rb"]) - t.puts "p :ok" - t.close - - ENV["RUBYPATH"] = "~" - ENV["HOME"] = t.path - assert_in_out_err(%w(-S test_ruby_test_require), "", [], /\(LoadError\)/) + Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| + t.puts "p :ok" + t.close - ENV["HOME"], name = File.split(t.path) - assert_in_out_err(["-S", name], "", %w(:ok), []) + ENV["RUBYPATH"] = "~" + ENV["HOME"] = t.path + assert_in_out_err(%w(-S test_ruby_test_require), "", [], /\(LoadError\)/) + ENV["HOME"], name = File.split(t.path) + assert_in_out_err(["-S", name], "", %w(:ok), []) + } ensure env_rubypath ? ENV["RUBYPATH"] = env_rubypath : ENV.delete("RUBYPATH") env_home ? ENV["HOME"] = env_home : ENV.delete("HOME") - t.close(true) end def test_require_with_unc @@ -269,86 +268,85 @@ class TestRequire < Test::Unit::TestCase end def test_load - t = Tempfile.new(["test_ruby_test_require", ".rb"]) - t.puts "module Foo; end" - t.puts "at_exit { p :wrap_end }" - t.puts "at_exit { raise 'error in at_exit test' }" - t.puts "p :ok" - t.close - - assert_in_out_err([], <<-INPUT, %w(:ok :end :wrap_end), /error in at_exit test/) - load(#{ t.path.dump }, true) - GC.start - p :end - INPUT + Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| + t.puts "module Foo; end" + t.puts "at_exit { p :wrap_end }" + t.puts "at_exit { raise 'error in at_exit test' }" + t.puts "p :ok" + t.close + + assert_in_out_err([], <<-INPUT, %w(:ok :end :wrap_end), /error in at_exit test/) + load(#{ t.path.dump }, true) + GC.start + p :end + INPUT - assert_raise(ArgumentError) { at_exit } - t.close(true) + assert_raise(ArgumentError) { at_exit } + } end def test_load2 # [ruby-core:25039] - t = Tempfile.new(["test_ruby_test_require", ".rb"]) - t.puts "Hello = 'hello'" - t.puts "class Foo" - t.puts " p Hello" - t.puts "end" - t.close - - assert_in_out_err([], <<-INPUT, %w("hello"), []) - load(#{ t.path.dump }, true) - INPUT - t.close(true) + Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| + t.puts "Hello = 'hello'" + t.puts "class Foo" + t.puts " p Hello" + t.puts "end" + t.close + + assert_in_out_err([], <<-INPUT, %w("hello"), []) + load(#{ t.path.dump }, true) + INPUT + } end def test_tainted_loadpath - t = Tempfile.new(["test_ruby_test_require", ".rb"]) - abs_dir, file = File.split(t.path) - abs_dir = File.expand_path(abs_dir).untaint - - assert_in_out_err([], <<-INPUT, %w(:ok), []) - abs_dir = "#{ abs_dir }" - $: << abs_dir - require "#{ file }" - p :ok - INPUT - - assert_in_out_err([], <<-INPUT, %w(:ok), []) - abs_dir = "#{ abs_dir }" - $: << abs_dir.taint - require "#{ file }" - p :ok - INPUT + Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| + abs_dir, file = File.split(t.path) + abs_dir = File.expand_path(abs_dir).untaint - assert_in_out_err([], <<-INPUT, %w(:ok), []) - abs_dir = "#{ abs_dir }" - $: << abs_dir.taint - $SAFE = 1 - begin + assert_in_out_err([], <<-INPUT, %w(:ok), []) + abs_dir = "#{ abs_dir }" + $: << abs_dir require "#{ file }" - rescue SecurityError p :ok - end - INPUT + INPUT - assert_in_out_err([], <<-INPUT, %w(:ok), []) - abs_dir = "#{ abs_dir }" - $: << abs_dir.taint - $SAFE = 1 - begin + assert_in_out_err([], <<-INPUT, %w(:ok), []) + abs_dir = "#{ abs_dir }" + $: << abs_dir.taint require "#{ file }" - rescue SecurityError p :ok - end - INPUT + INPUT - assert_in_out_err([], <<-INPUT, %w(:ok), []) - abs_dir = "#{ abs_dir }" - $: << abs_dir << 'elsewhere'.taint - require "#{ file }" - p :ok - INPUT + assert_in_out_err([], <<-INPUT, %w(:ok), []) + abs_dir = "#{ abs_dir }" + $: << abs_dir.taint + $SAFE = 1 + begin + require "#{ file }" + rescue SecurityError + p :ok + end + INPUT - t.close(true) + assert_in_out_err([], <<-INPUT, %w(:ok), []) + abs_dir = "#{ abs_dir }" + $: << abs_dir.taint + $SAFE = 1 + begin + require "#{ file }" + rescue SecurityError + p :ok + end + INPUT + + assert_in_out_err([], <<-INPUT, %w(:ok), []) + abs_dir = "#{ abs_dir }" + $: << abs_dir << 'elsewhere'.taint + require "#{ file }" + p :ok + INPUT + } end def test_relative @@ -402,65 +400,66 @@ class TestRequire < Test::Unit::TestCase def test_race_exception bug5754 = '[ruby-core:41618]' - tmp = Tempfile.new(%w"bug5754 .rb") - path = tmp.path - tmp.print %{\ - th = Thread.current - t = th[:t] - scratch = th[:scratch] - - if scratch.empty? - scratch << :pre - Thread.pass until t.stop? - raise RuntimeError - else - scratch << :post - end - } - tmp.close - - # "circular require" warnings to $stderr, but backtraces to stderr - # in C-level. And redirecting stderr to a pipe seems to change - # some blocking timings and causes a deadlock, so run in a - # separated process for the time being. - assert_separately(["-w", "-", path, bug5754], <<-'end;', ignore_stderr: true) - path, bug5754 = *ARGV - start = false - - scratch = [] - t1_res = nil - t2_res = nil - - t1 = Thread.new do - Thread.pass until start - begin - require(path) - rescue RuntimeError - end + path = nil + Tempfile.create(%w"bug5754 .rb") {|tmp| + path = tmp.path + tmp.print %{\ + th = Thread.current + t = th[:t] + scratch = th[:scratch] + + if scratch.empty? + scratch << :pre + Thread.pass until t.stop? + raise RuntimeError + else + scratch << :post + end + } + tmp.close + + # "circular require" warnings to $stderr, but backtraces to stderr + # in C-level. And redirecting stderr to a pipe seems to change + # some blocking timings and causes a deadlock, so run in a + # separated process for the time being. + assert_separately(["-w", "-", path, bug5754], <<-'end;', ignore_stderr: true) + path, bug5754 = *ARGV + start = false + + scratch = [] + t1_res = nil + t2_res = nil + + t1 = Thread.new do + Thread.pass until start + begin + require(path) + rescue RuntimeError + end - t1_res = require(path) - end + t1_res = require(path) + end - t2 = Thread.new do - Thread.pass until scratch[0] - t2_res = require(path) - end + t2 = Thread.new do + Thread.pass until scratch[0] + t2_res = require(path) + end - t1[:scratch] = t2[:scratch] = scratch - t1[:t] = t2 - t2[:t] = t1 + t1[:scratch] = t2[:scratch] = scratch + t1[:t] = t2 + t2[:t] = t1 - start = true + start = true - assert_nothing_raised(ThreadError, bug5754) {t1.join} - assert_nothing_raised(ThreadError, bug5754) {t2.join} + assert_nothing_raised(ThreadError, bug5754) {t1.join} + assert_nothing_raised(ThreadError, bug5754) {t2.join} - assert_equal(true, (t1_res ^ t2_res), bug5754 + " t1:#{t1_res} t2:#{t2_res}") - assert_equal([:pre, :post], scratch, bug5754) - end; + assert_equal(true, (t1_res ^ t2_res), bug5754 + " t1:#{t1_res} t2:#{t2_res}") + assert_equal([:pre, :post], scratch, bug5754) + end; + } ensure $".delete(path) - tmp.close(true) if tmp end def test_loaded_features_encoding @@ -642,24 +641,23 @@ class TestRequire < Test::Unit::TestCase def test_require_with_loaded_features_pop bug7530 = '[ruby-core:50645]' - script = Tempfile.new(%w'bug-7530- .rb') - script.close - assert_in_out_err([{"RUBYOPT" => nil}, "-", script.path], <<-INPUT, %w(:ok), [], bug7530) - PATH = ARGV.shift - THREADS = 2 - ITERATIONS_PER_THREAD = 1000 - - THREADS.times.map { - Thread.new do - ITERATIONS_PER_THREAD.times do - require PATH - $".pop + Tempfile.create(%w'bug-7530- .rb') {|script| + script.close + assert_in_out_err([{"RUBYOPT" => nil}, "-", script.path], <<-INPUT, %w(:ok), [], bug7530) + PATH = ARGV.shift + THREADS = 2 + ITERATIONS_PER_THREAD = 1000 + + THREADS.times.map { + Thread.new do + ITERATIONS_PER_THREAD.times do + require PATH + $".pop + end end - end - }.each(&:join) - p :ok - INPUT - ensure - script.close(true) if script + }.each(&:join) + p :ok + INPUT + } end end diff --git a/test/ruby/test_rubyoptions.rb b/test/ruby/test_rubyoptions.rb index cd100de3bd..fbc18f3ef0 100644 --- a/test/ruby/test_rubyoptions.rb +++ b/test/ruby/test_rubyoptions.rb @@ -271,20 +271,21 @@ class TestRubyOptions < Test::Unit::TestCase rubypath_orig = ENV['RUBYPATH'] path_orig = ENV['PATH'] - t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"]) - t.puts "p 1" - t.close + Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t| + t.puts "p 1" + t.close - @verbose = $VERBOSE - $VERBOSE = nil + @verbose = $VERBOSE + $VERBOSE = nil - ENV['PATH'] = File.dirname(t.path) + ENV['PATH'] = File.dirname(t.path) - assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), []) + assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), []) - ENV['RUBYPATH'] = File.dirname(t.path) + ENV['RUBYPATH'] = File.dirname(t.path) - assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), []) + assert_in_out_err(%w(-S) + [File.basename(t.path)], "", %w(1), []) + } ensure if rubypath_orig @@ -297,7 +298,6 @@ class TestRubyOptions < Test::Unit::TestCase else ENV.delete('PATH') end - t.close(true) if t $VERBOSE = @verbose end @@ -333,81 +333,79 @@ class TestRubyOptions < Test::Unit::TestCase end def test_assignment_in_conditional - t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"]) - t.puts "if a = 1" - t.puts "end" - t.puts "0.times do" - t.puts " if b = 2" - t.puts " a += b" - t.puts " end" - t.puts "end" - t.close - warning = ' warning: found = in conditional, should be ==' - err = ["#{t.path}:1:#{warning}", - "#{t.path}:4:#{warning}", - ] - bug2136 = '[ruby-dev:39363]' - assert_in_out_err(["-w", t.path], "", [], err, bug2136) - assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, bug2136) - - t.open - t.truncate(0) - t.puts "if a = ''; end" - t.puts "if a = []; end" - t.puts "if a = [1]; end" - t.puts "if a = [a]; end" - t.puts "if a = {}; end" - t.puts "if a = {1=>2}; end" - t.puts "if a = {3=>a}; end" - t.close - err = ["#{t.path}:1:#{warning}", - "#{t.path}:2:#{warning}", - "#{t.path}:3:#{warning}", - "#{t.path}:5:#{warning}", - "#{t.path}:6:#{warning}", - ] - feature4299 = '[ruby-dev:43083]' - assert_in_out_err(["-w", t.path], "", [], err, feature4299) - assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, feature4299) - ensure - t.close(true) if t + Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t| + t.puts "if a = 1" + t.puts "end" + t.puts "0.times do" + t.puts " if b = 2" + t.puts " a += b" + t.puts " end" + t.puts "end" + t.flush + warning = ' warning: found = in conditional, should be ==' + err = ["#{t.path}:1:#{warning}", + "#{t.path}:4:#{warning}", + ] + bug2136 = '[ruby-dev:39363]' + assert_in_out_err(["-w", t.path], "", [], err, bug2136) + assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, bug2136) + + t.rewind + t.truncate(0) + t.puts "if a = ''; end" + t.puts "if a = []; end" + t.puts "if a = [1]; end" + t.puts "if a = [a]; end" + t.puts "if a = {}; end" + t.puts "if a = {1=>2}; end" + t.puts "if a = {3=>a}; end" + t.flush + err = ["#{t.path}:1:#{warning}", + "#{t.path}:2:#{warning}", + "#{t.path}:3:#{warning}", + "#{t.path}:5:#{warning}", + "#{t.path}:6:#{warning}", + ] + feature4299 = '[ruby-dev:43083]' + assert_in_out_err(["-w", t.path], "", [], err, feature4299) + assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err, feature4299) + } end def test_indentation_check - t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"]) - t.puts "begin" - t.puts " end" - t.close - err = ["#{t.path}:2: warning: mismatched indentations at 'end' with 'begin' at 1"] - assert_in_out_err(["-w", t.path], "", [], err) - assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err) - - t.open - t.puts "# -*- warn-indent: false -*-" - t.puts "begin" - t.puts " end" - t.close - assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]') - - err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 3"] - t.open - t.puts "# -*- warn-indent: false -*-" - t.puts "# -*- warn-indent: true -*-" - t.puts "begin" - t.puts " end" - t.close - assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]') - - err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 2"] - t.open - t.puts "# -*- warn-indent: true -*-" - t.puts "begin" - t.puts "# -*- warn-indent: false -*-" - t.puts " end" - t.close - assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]') - ensure - t.close(true) if t + Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t| + t.puts "begin" + t.puts " end" + t.flush + err = ["#{t.path}:2: warning: mismatched indentations at 'end' with 'begin' at 1"] + assert_in_out_err(["-w", t.path], "", [], err) + assert_in_out_err(["-wr", t.path, "-e", ""], "", [], err) + + t.rewind + t.puts "# -*- warn-indent: false -*-" + t.puts "begin" + t.puts " end" + t.flush + assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]') + + err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 3"] + t.rewind + t.puts "# -*- warn-indent: false -*-" + t.puts "# -*- warn-indent: true -*-" + t.puts "begin" + t.puts " end" + t.flush + assert_in_out_err(["-w", t.path], "", [], err, '[ruby-core:25442]') + + err = ["#{t.path}:4: warning: mismatched indentations at 'end' with 'begin' at 2"] + t.rewind + t.puts "# -*- warn-indent: true -*-" + t.puts "begin" + t.puts "# -*- warn-indent: false -*-" + t.puts " end" + t.flush + assert_in_out_err(["-w", t.path], "", [], [], '[ruby-core:25442]') + } end def test_notfound @@ -523,25 +521,24 @@ class TestRubyOptions < Test::Unit::TestCase assert_not_predicate(status, :success?, "segv but success #{bug7402}") bug7597 = '[ruby-dev:46786]' - t = Tempfile.new(["test_ruby_test_bug7597", ".rb"]) - t.write "f" * 100 - t.flush - assert_in_out_err(["-e", "$0=ARGV[0]; Process.kill :SEGV, $$", t.path], - "", [], expected_stderr, bug7597, opts) - t.close(true) + Tempfile.create(["test_ruby_test_bug7597", ".rb"]) {|t| + t.write "f" * 100 + t.flush + assert_in_out_err(["-e", "$0=ARGV[0]; Process.kill :SEGV, $$", t.path], + "", [], expected_stderr, bug7597, opts) + } end def test_DATA - t = Tempfile.new(["test_ruby_test_rubyoption", ".rb"]) - t.puts "puts DATA.read.inspect" - t.puts "__END__" - t.puts "foo" - t.puts "bar" - t.puts "baz" - t.close - assert_in_out_err([t.path], "", %w("foo\\nbar\\nbaz\\n"), []) - ensure - t.close(true) if t + Tempfile.create(["test_ruby_test_rubyoption", ".rb"]) {|t| + t.puts "puts DATA.read.inspect" + t.puts "__END__" + t.puts "foo" + t.puts "bar" + t.puts "baz" + t.flush + assert_in_out_err([t.path], "", %w("foo\\nbar\\nbaz\\n"), []) + } end def test_unused_variable |