ruby-changes:28348
From: akr <ko1@a...>
Date: Sun, 21 Apr 2013 08:04:05 +0900 (JST)
Subject: [ruby-changes:28348] akr:r40400 (trunk): * test/csv/test_features.rb, test/logger/test_logger.rb
akr 2013-04-21 08:03:52 +0900 (Sun, 21 Apr 2013) New Revision: 40400 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=40400 Log: * 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. Modified files: trunk/ChangeLog trunk/test/csv/test_features.rb trunk/test/logger/test_logger.rb trunk/test/mkmf/test_have_macro.rb trunk/test/net/http/test_http.rb trunk/test/openssl/test_config.rb trunk/test/psych/test_encoding.rb trunk/test/psych/test_exception.rb trunk/test/psych/test_psych.rb trunk/test/psych/test_tainted.rb trunk/test/readline/test_readline.rb trunk/test/rexml/test_contrib.rb trunk/test/ruby/test_autoload.rb trunk/test/ruby/test_beginendblock.rb trunk/test/ruby/test_exception.rb trunk/test/ruby/test_file.rb trunk/test/ruby/test_io.rb trunk/test/ruby/test_marshal.rb trunk/test/ruby/test_process.rb trunk/test/ruby/test_require.rb trunk/test/ruby/test_rubyoptions.rb trunk/test/syslog/test_syslog_logger.rb trunk/test/webrick/test_httpauth.rb trunk/test/zlib/test_zlib.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 40399) +++ ChangeLog (revision 40400) @@ -1,3 +1,18 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Sun Apr 21 08:00:55 2013 Tanaka Akira <akr@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. + Sun Apr 21 00:15:36 2013 Tanaka Akira <akr@f...> * lib/tempfile.rb (Tempfile.create): Close when the block exits. Index: test/logger/test_logger.rb =================================================================== --- test/logger/test_logger.rb (revision 40399) +++ test/logger/test_logger.rb (revision 40400) @@ -41,13 +41,12 @@ class TestLogger < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/logger/test_logger.rb#L41 end def log_raw(logger, msg_id, *arg, &block) - logdev = Tempfile.new(File.basename(__FILE__) + '.log') - logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) } - logger.__send__(msg_id, *arg, &block) - logdev.open - msg = logdev.read - logdev.close(true) - msg + Tempfile.create(File.basename(__FILE__) + '.log') {|logdev| + logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) } + logger.__send__(msg_id, *arg, &block) + logdev.rewind + logdev.read + } end def test_level Index: test/syslog/test_syslog_logger.rb =================================================================== --- test/syslog/test_syslog_logger.rb (revision 40399) +++ test/syslog/test_syslog_logger.rb (revision 40400) @@ -80,13 +80,12 @@ class TestSyslogRootLogger < Test::Unit: https://github.com/ruby/ruby/blob/trunk/test/syslog/test_syslog_logger.rb#L80 end def log_raw(msg_id, *arg, &block) - logdev = Tempfile.new(File.basename(__FILE__) + '.log') - @logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) } - assert_equal true, @logger.__send__(msg_id, *arg, &block) - logdev.open - msg = logdev.read - logdev.close(true) - msg + Tempfile.create(File.basename(__FILE__) + '.log') {|logdev| + @logger.instance_eval { @logdev = Logger::LogDevice.new(logdev) } + assert_equal true, @logger.__send__(msg_id, *arg, &block) + logdev.rewind + logdev.read + } end def test_initialize Index: test/ruby/test_require.rb =================================================================== --- test/ruby/test_require.rb (revision 40399) +++ test/ruby/test_require.rb (revision 40400) @@ -14,19 +14,19 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L14 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L111 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\)/) - - ENV["HOME"], name = File.split(t.path) - assert_in_out_err(["-S", name], "", %w(:ok), []) + Tempfile.create(["test_ruby_test_require", ".rb"]) {|t| + t.puts "p :ok" + t.close + + 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L268 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 + 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 - t.close(true) + 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 - - assert_in_out_err([], <<-INPUT, %w(:ok), []) - abs_dir = "#{ abs_dir }" - $: << abs_dir.taint - $SAFE = 1 - begin + 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 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 + + 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 << 'elsewhere'.taint + require "#{ file }" + p :ok + INPUT + } end def test_relative @@ -402,65 +400,66 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L400 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 + 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 - t1 = Thread.new do - Thread.pass until start - begin - require(path) - rescue RuntimeError + # "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 + + start = true - t1[:scratch] = t2[:scratch] = scratch - t1[:t] = t2 - t2[:t] = t1 - - start = true - - 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_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; + } ensure $".delete(path) - tmp.close(true) if tmp end def test_loaded_features_encoding @@ -642,24 +641,23 @@ class TestRequire < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_require.rb#L641 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 Index: test/ruby/test_beginendblock.rb =================================================================== --- test/ruby/test_beginendblock.rb (revision 40399) +++ test/ruby/test_beginendblock.rb (revision 40400) @@ -16,22 +16,19 @@ class TestBeginEndBlock < Test::Unit::Te https://github.com/ruby/ruby/blob/trunk/test/ruby/test_beginendblock.rb#L16 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::Te https://github.com/ruby/ruby/blob/trunk/test/ruby/test_beginendblock.rb#L53 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_beginendblock.rb#L64 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_beginendblock.rb#L129 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 Index: test/ruby/test_file.rb =================================================================== --- test/ruby/test_file.rb (revision 40399) +++ test/ruby/test_file.rb (revision 40400) @@ -29,12 +29,11 @@ class TestFile < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file.rb#L29 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 https://github.com/ruby/ruby/blob/trunk/test/ruby/test_file.rb#L41 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 (... truncated) -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/