2003-10-04 22:56:42 -04:00
|
|
|
require 'test/unit'
|
2003-10-07 02:27:11 -04:00
|
|
|
require 'tempfile'
|
2010-02-21 12:17:41 -05:00
|
|
|
require 'timeout'
|
2008-02-18 03:09:05 -05:00
|
|
|
require_relative 'envutil'
|
2003-10-04 22:56:42 -04:00
|
|
|
|
|
|
|
class TestBeginEndBlock < Test::Unit::TestCase
|
|
|
|
DIR = File.dirname(File.expand_path(__FILE__))
|
|
|
|
|
2003-10-07 02:27:11 -04:00
|
|
|
def q(content)
|
|
|
|
"\"#{content}\""
|
|
|
|
end
|
|
|
|
|
2003-10-04 22:56:42 -04:00
|
|
|
def test_beginendblock
|
|
|
|
ruby = EnvUtil.rubybin
|
2003-10-07 02:27:11 -04:00
|
|
|
target = File.join(DIR, 'beginmainend.rb')
|
2007-06-10 00:46:17 -04:00
|
|
|
result = IO.popen([ruby, target]){|io|io.read}
|
2011-02-16 06:42:03 -05:00
|
|
|
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)
|
2008-07-20 09:50:14 -04:00
|
|
|
|
* 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(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)
|
|
|
|
}
|
2003-10-05 00:51:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_begininmethod
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
|
2003-10-05 00:51:05 -04:00
|
|
|
eval("def foo; BEGIN {}; end")
|
|
|
|
end
|
|
|
|
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
|
2003-10-07 02:27:11 -04:00
|
|
|
eval('eval("def foo; BEGIN {}; end")')
|
2003-10-05 00:51:05 -04:00
|
|
|
end
|
2003-10-07 02:27:11 -04:00
|
|
|
end
|
|
|
|
|
2010-01-18 11:43:29 -05:00
|
|
|
def test_begininclass
|
2013-10-09 04:43:12 -04:00
|
|
|
assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
|
2010-01-18 11:43:29 -05:00
|
|
|
eval("class TestBeginEndBlock; BEGIN {}; end")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-10-07 02:27:11 -04:00
|
|
|
def test_endblockwarn
|
|
|
|
ruby = EnvUtil.rubybin
|
|
|
|
# Use Tempfile to create temporary file path.
|
* 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(self.class.name) {|launcher|
|
|
|
|
Tempfile.create(self.class.name) {|errout|
|
2003-10-07 02:27:11 -04:00
|
|
|
|
* 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
|
|
|
launcher << <<EOF
|
2010-05-30 15:03:47 -04:00
|
|
|
# -*- coding: #{ruby.encoding.name} -*-
|
2003-10-07 02:27:11 -04:00
|
|
|
errout = ARGV.shift
|
|
|
|
STDERR.reopen(File.open(errout, "w"))
|
|
|
|
STDERR.sync = true
|
|
|
|
Dir.chdir(#{q(DIR)})
|
2007-12-23 10:48:39 -05:00
|
|
|
system("#{ruby}", "endblockwarn_rb")
|
2003-10-07 02:27:11 -04:00
|
|
|
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
|
|
|
launcher.close
|
|
|
|
launcherpath = launcher.path
|
|
|
|
errout.close
|
|
|
|
erroutpath = errout.path
|
|
|
|
system(ruby, launcherpath, erroutpath)
|
|
|
|
expected = <<EOW
|
2007-12-23 10:48:39 -05:00
|
|
|
endblockwarn_rb:2: warning: END in method; use at_exit
|
2007-05-15 03:38:59 -04:00
|
|
|
(eval):2: warning: END in method; use at_exit
|
2003-10-07 02:27:11 -04:00
|
|
|
EOW
|
* 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(expected, File.read(erroutpath))
|
|
|
|
}
|
|
|
|
}
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|
2006-12-31 10:02:22 -05:00
|
|
|
|
2007-05-25 11:04:05 -04:00
|
|
|
def test_raise_in_at_exit
|
|
|
|
ruby = EnvUtil.rubybin
|
2007-06-10 00:46:17 -04:00
|
|
|
out = IO.popen([ruby, '-e', 'STDERR.reopen(STDOUT)',
|
2007-06-10 03:10:56 -04:00
|
|
|
'-e', 'at_exit{raise %[SomethingBad]}',
|
|
|
|
'-e', 'raise %[SomethingElse]']) {|f|
|
2007-05-25 11:04:05 -04:00
|
|
|
f.read
|
|
|
|
}
|
2013-06-10 02:35:37 -04:00
|
|
|
status = $?
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/SomethingBad/, out, "[ruby-core:9675]")
|
|
|
|
assert_match(/SomethingElse/, out, "[ruby-core:9675]")
|
2013-06-10 02:35:37 -04:00
|
|
|
assert_not_predicate(status, :success?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_exitcode_in_at_exit
|
|
|
|
bug8501 = '[ruby-core:55365] [Bug #8501]'
|
2013-06-10 09:28:58 -04:00
|
|
|
ruby = EnvUtil.rubybin
|
2013-06-10 02:35:37 -04:00
|
|
|
out = IO.popen([ruby, '-e', 'STDERR.reopen(STDOUT)',
|
|
|
|
'-e', 'o = Object.new; def o.inspect; raise "[Bug #8501]"; end',
|
|
|
|
'-e', 'at_exit{o.nope}']) {|f|
|
|
|
|
f.read
|
|
|
|
}
|
|
|
|
status = $?
|
|
|
|
assert_match(/undefined method `nope'/, out, bug8501)
|
|
|
|
assert_not_predicate(status, :success?, bug8501)
|
2007-05-25 11:04:05 -04:00
|
|
|
end
|
|
|
|
|
2013-06-10 02:33:58 -04:00
|
|
|
def test_propagate_exit_code
|
2007-05-25 11:04:05 -04:00
|
|
|
ruby = EnvUtil.rubybin
|
2007-06-10 00:46:17 -04:00
|
|
|
assert_equal false, system(ruby, '-e', 'at_exit{exit 2}')
|
2007-05-25 11:04:05 -04:00
|
|
|
assert_equal 2, $?.exitstatus
|
|
|
|
assert_nil $?.termsig
|
|
|
|
end
|
|
|
|
|
2013-06-10 02:33:58 -04:00
|
|
|
def test_propagate_signaled
|
2007-05-25 11:04:05 -04:00
|
|
|
ruby = EnvUtil.rubybin
|
2007-11-26 21:28:28 -05:00
|
|
|
out = IO.popen(
|
|
|
|
[ruby,
|
|
|
|
'-e', 'STDERR.reopen(STDOUT)',
|
2010-02-22 07:34:54 -05:00
|
|
|
'-e', 'at_exit{Process.kill(:INT, $$); sleep 5 }']) {|f|
|
|
|
|
timeout(10) {
|
2010-02-21 12:17:41 -05:00
|
|
|
f.read
|
|
|
|
}
|
2007-05-25 11:04:05 -04:00
|
|
|
}
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/Interrupt$/, out)
|
2007-05-25 11:04:05 -04:00
|
|
|
Process.kill(0, 0) rescue return # check if signal works
|
|
|
|
assert_nil $?.exitstatus
|
|
|
|
assert_equal Signal.list["INT"], $?.termsig
|
|
|
|
end
|
2010-06-21 10:43:58 -04:00
|
|
|
|
|
|
|
def test_endblock_raise
|
|
|
|
ruby = EnvUtil.rubybin
|
|
|
|
out = IO.popen(
|
|
|
|
[ruby,
|
|
|
|
'-e', 'class C; def write(x); puts x; STDOUT.flush; sleep 0.01; end; end',
|
|
|
|
'-e', '$stderr = C.new',
|
|
|
|
'-e', 'END {raise "e1"}; END {puts "e2"}',
|
|
|
|
'-e', 'END {raise "e3"}; END {puts "e4"}',
|
|
|
|
'-e', 'END {raise "e5"}; END {puts "e6"}']) {|f|
|
|
|
|
Thread.new {sleep 5; Process.kill :KILL, f.pid}
|
|
|
|
f.read
|
|
|
|
}
|
|
|
|
assert_match(/e1/, out)
|
|
|
|
assert_match(/e6/, out)
|
|
|
|
end
|
2011-02-16 06:42:03 -05:00
|
|
|
|
|
|
|
def test_nested_at_exit
|
* 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_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]")
|
|
|
|
}
|
2011-02-16 06:42:03 -05:00
|
|
|
end
|
2012-03-11 21:13:40 -04:00
|
|
|
|
|
|
|
def test_rescue_at_exit
|
|
|
|
bug5218 = '[ruby-core:43173][Bug #5218]'
|
|
|
|
cmd = [
|
|
|
|
"raise 'X' rescue nil",
|
|
|
|
"nil",
|
|
|
|
"exit(42)",
|
|
|
|
]
|
|
|
|
%w[at_exit END].each do |ex|
|
|
|
|
out, err, status = EnvUtil.invoke_ruby(cmd.map {|s|["-e", "#{ex} {#{s}}"]}.flatten, "", true, true)
|
|
|
|
assert_equal(["", "", 42], [out, err, status.exitstatus], "#{bug5218}: #{ex}")
|
|
|
|
end
|
|
|
|
end
|
2013-11-15 10:29:23 -05:00
|
|
|
|
|
|
|
def test_callcc_at_exit
|
|
|
|
bug9110 = '[ruby-core:58329][Bug #9110]'
|
|
|
|
script = <<EOS
|
|
|
|
require "continuation"
|
|
|
|
c = nil
|
|
|
|
at_exit { c.call }
|
|
|
|
at_exit { callcc {|_c| c = _c } }
|
|
|
|
EOS
|
2013-11-15 15:54:16 -05:00
|
|
|
assert_normal_exit(script, bug9110)
|
2013-11-15 10:29:23 -05:00
|
|
|
end
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|