2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2003-10-04 22:56:42 -04:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestBeginEndBlock < Test::Unit::TestCase
|
|
|
|
DIR = File.dirname(File.expand_path(__FILE__))
|
|
|
|
|
|
|
|
def test_beginendblock
|
2003-10-07 02:27:11 -04:00
|
|
|
target = File.join(DIR, 'beginmainend.rb')
|
2015-03-09 01:47:05 -04:00
|
|
|
assert_in_out_err([target], '', %w(b1 b2-1 b2 main b3-1 b3 b4 e1 e1-1 e4 e4-2 e4-1 e4-1-1 e3 e2))
|
|
|
|
|
|
|
|
assert_in_out_err(["-n", "-eBEGIN{p :begin}", "-eEND{p :end}"], '', %w(:begin))
|
|
|
|
assert_in_out_err(["-p", "-eBEGIN{p :begin}", "-eEND{p :end}"], '', %w(:begin))
|
|
|
|
assert_in_out_err(["-n", "-eBEGIN{p :begin}", "-eEND{p :end}"], "foo\nbar\n", %w(:begin :end))
|
|
|
|
assert_in_out_err(["-p", "-eBEGIN{p :begin}", "-eEND{p :end}"], "foo\nbar\n", %w(:begin foo bar :end))
|
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
|
2017-08-29 03:11:52 -04:00
|
|
|
assert_in_out_err([], "#{<<~"begin;"}#{<<~'end;'}", [], ['-:2: warning: END in method; use at_exit'])
|
|
|
|
begin;
|
2015-03-09 01:47:05 -04:00
|
|
|
def end1
|
|
|
|
END {}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_endblockwarn_in_eval
|
2017-08-29 03:11:52 -04:00
|
|
|
assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", [], ['(eval):2: warning: END in method; use at_exit'])
|
|
|
|
begin;
|
2015-03-09 01:47:05 -04:00
|
|
|
eval <<-EOE
|
|
|
|
def end2
|
|
|
|
END {}
|
|
|
|
end
|
|
|
|
EOE
|
|
|
|
end;
|
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
|
2015-03-09 01:47:05 -04:00
|
|
|
args = ['-e', 'at_exit{raise %[SomethingBad]}',
|
|
|
|
'-e', 'raise %[SomethingElse]']
|
|
|
|
expected = [:*, /SomethingBad/, :*, /SomethingElse/, :*]
|
|
|
|
status = assert_in_out_err(args, '', [], expected, "[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]'
|
2015-03-09 01:47:05 -04:00
|
|
|
args = ['-e', 'o = Object.new; def o.inspect; raise "[Bug #8501]"; end',
|
|
|
|
'-e', 'at_exit{o.nope}']
|
|
|
|
status = assert_in_out_err(args, '', [], /undefined method `nope'/, bug8501)
|
2013-06-10 02:35:37 -04:00
|
|
|
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
|
2017-08-29 03:11:52 -04:00
|
|
|
status = assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", [], /Interrupt$/)
|
|
|
|
begin;
|
2015-03-09 01:47:05 -04:00
|
|
|
trap(:INT, "DEFAULT")
|
|
|
|
at_exit{Process.kill(:INT, $$)}
|
|
|
|
end;
|
2007-05-25 11:04:05 -04:00
|
|
|
Process.kill(0, 0) rescue return # check if signal works
|
2015-03-09 01:47:05 -04:00
|
|
|
assert_nil status.exitstatus
|
|
|
|
assert_equal Signal.list["INT"], status.termsig
|
2007-05-25 11:04:05 -04:00
|
|
|
end
|
2010-06-21 10:43:58 -04:00
|
|
|
|
|
|
|
def test_endblock_raise
|
2017-08-29 03:11:52 -04:00
|
|
|
assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", %w(e6 e4 e2), [:*, /e5/, :*, /e3/, :*, /e1/, :*])
|
|
|
|
begin;
|
2015-03-09 01:47:05 -04:00
|
|
|
END {raise "e1"}; END {puts "e2"}
|
|
|
|
END {raise "e3"}; END {puts "e4"}
|
|
|
|
END {raise "e5"}; END {puts "e6"}
|
|
|
|
end;
|
2010-06-21 10:43:58 -04:00
|
|
|
end
|
2011-02-16 06:42:03 -05:00
|
|
|
|
|
|
|
def test_nested_at_exit
|
2015-03-09 01:47:05 -04:00
|
|
|
expected = [ "outer3",
|
|
|
|
"outer2_begin",
|
|
|
|
"outer2_end",
|
|
|
|
"inner2",
|
|
|
|
"outer1_begin",
|
|
|
|
"outer1_end",
|
|
|
|
"inner1",
|
|
|
|
"outer0" ]
|
|
|
|
|
2017-08-29 03:11:52 -04:00
|
|
|
assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", expected, [], "[ruby-core:35237]")
|
|
|
|
begin;
|
2015-03-09 01:47:05 -04:00
|
|
|
at_exit { puts :outer0 }
|
|
|
|
at_exit { puts :outer1_begin; at_exit { puts :inner1 }; puts :outer1_end }
|
|
|
|
at_exit { puts :outer2_begin; at_exit { puts :inner2 }; puts :outer2_end }
|
|
|
|
at_exit { puts :outer3 }
|
|
|
|
end;
|
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]'
|
2017-08-29 03:11:52 -04:00
|
|
|
assert_ruby_status([], "#{<<~"begin;"}\n#{<<~'end;'}", bug9110)
|
|
|
|
begin;
|
|
|
|
require "continuation"
|
|
|
|
c = nil
|
|
|
|
at_exit { c.call }
|
|
|
|
at_exit { callcc {|_c| c = _c } }
|
|
|
|
end;
|
2013-11-15 10:29:23 -05:00
|
|
|
end
|
2016-04-22 02:09:29 -04:00
|
|
|
|
|
|
|
def test_errinfo_at_exit
|
|
|
|
bug12302 = '[ruby-core:75038] [Bug #12302]'
|
2017-08-29 03:11:52 -04:00
|
|
|
assert_in_out_err([], "#{<<~"begin;"}\n#{<<~'end;'}", %w[2:exit 1:exit], [], bug12302)
|
|
|
|
begin;
|
2016-04-22 02:09:29 -04:00
|
|
|
at_exit do
|
|
|
|
puts "1:#{$!}"
|
|
|
|
end
|
|
|
|
|
|
|
|
at_exit do
|
|
|
|
puts "2:#{$!}"
|
|
|
|
raise 'x' rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
at_exit do
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
end
|
2003-10-04 22:56:42 -04:00
|
|
|
end
|