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}
|
2007-05-25 11:04:05 -04:00
|
|
|
assert_equal(%w(b1 b2-1 b2 main b3-1 b3 b4 e1 e4 e3 e2 e4-2 e4-1 e1-1 e4-1-1), result.split)
|
2008-07-20 09:50:14 -04:00
|
|
|
|
|
|
|
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)
|
2003-10-05 00:51:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_begininmethod
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(SyntaxError) do
|
2003-10-05 00:51:05 -04:00
|
|
|
eval("def foo; BEGIN {}; end")
|
|
|
|
end
|
|
|
|
|
2008-09-24 13:44:39 -04:00
|
|
|
assert_raise(SyntaxError) 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
|
|
|
|
assert_raise(SyntaxError) do
|
|
|
|
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.
|
|
|
|
launcher = Tempfile.new(self.class.name)
|
|
|
|
errout = Tempfile.new(self.class.name)
|
|
|
|
|
|
|
|
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
|
|
|
|
launcher.close
|
|
|
|
launcherpath = launcher.path
|
|
|
|
errout.close
|
|
|
|
erroutpath = errout.path
|
2007-06-10 00:46:17 -04:00
|
|
|
system(ruby, launcherpath, erroutpath)
|
2003-10-07 02:27:11 -04:00
|
|
|
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
|
|
|
|
assert_equal(expected, File.read(erroutpath))
|
|
|
|
# expecting Tempfile to unlink launcher and errout file.
|
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
|
|
|
|
}
|
2010-01-25 18:12:50 -05:00
|
|
|
assert_match(/SomethingBad/, out, "[ruby-core:9675]")
|
|
|
|
assert_match(/SomethingElse/, out, "[ruby-core:9675]")
|
2007-05-25 11:04:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_propagate_exit_code
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_should_propagate_signaled
|
|
|
|
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
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|