test_beginendblock.rb: assert_in_out_err

* test/ruby/test_beginendblock.rb (TestBeginEndBlock): simplify
  with assert_in_out_err.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@49902 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-03-09 05:47:05 +00:00
parent dbbccb3cff
commit 34beb98464
2 changed files with 51 additions and 121 deletions

View File

@ -1,12 +0,0 @@
def end1
END {}
end
end1
eval <<EOE
def end2
END {}
end
EOE

View File

@ -1,33 +1,16 @@
require 'test/unit' require 'test/unit'
require 'tempfile'
require 'timeout'
class TestBeginEndBlock < Test::Unit::TestCase class TestBeginEndBlock < Test::Unit::TestCase
DIR = File.dirname(File.expand_path(__FILE__)) DIR = File.dirname(File.expand_path(__FILE__))
def q(content)
"\"#{content}\""
end
def test_beginendblock def test_beginendblock
ruby = EnvUtil.rubybin
target = File.join(DIR, 'beginmainend.rb') target = File.join(DIR, 'beginmainend.rb')
result = IO.popen([ruby, target]){|io|io.read} 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_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)
Tempfile.create(self.class.name) {|input| assert_in_out_err(["-n", "-eBEGIN{p :begin}", "-eEND{p :end}"], '', %w(:begin))
inputpath = input.path assert_in_out_err(["-p", "-eBEGIN{p :begin}", "-eEND{p :end}"], '', %w(:begin))
result = IO.popen([ruby, "-n", "-eBEGIN{p :begin}", "-eEND{p :end}", inputpath]){|io|io.read} assert_in_out_err(["-n", "-eBEGIN{p :begin}", "-eEND{p :end}"], "foo\nbar\n", %w(:begin :end))
assert_equal(%w(:begin), result.split) assert_in_out_err(["-p", "-eBEGIN{p :begin}", "-eEND{p :end}"], "foo\nbar\n", %w(:begin foo bar :end))
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 end
def test_begininmethod def test_begininmethod
@ -47,56 +30,36 @@ class TestBeginEndBlock < Test::Unit::TestCase
end end
def test_endblockwarn def test_endblockwarn
ruby = EnvUtil.rubybin assert_in_out_err([], <<-'end;', [], ['-:2: warning: END in method; use at_exit'])
# Use Tempfile to create temporary file path. def end1
Tempfile.create(self.class.name) {|launcher| END {}
Tempfile.create(self.class.name) {|errout| end
end;
end
launcher << <<EOF def test_endblockwarn_in_eval
# -*- coding: #{ruby.encoding.name} -*- assert_in_out_err([], <<-'end;', [], ['(eval):2: warning: END in method; use at_exit'])
errout = ARGV.shift eval <<-EOE
STDERR.reopen(File.open(errout, "w")) def end2
STDERR.sync = true END {}
system("#{ruby}", File.join(#{q(DIR)}, "endblockwarn_rb")) end
EOF EOE
launcher.close end;
launcherpath = launcher.path
errout.close
erroutpath = errout.path
system(ruby, launcherpath, erroutpath)
path = File.join(DIR, 'endblockwarn_rb')
expected = <<EOW
#{path}:2: warning: END in method; use at_exit
(eval):2: warning: END in method; use at_exit
EOW
assert_equal(expected, File.read(erroutpath))
}
}
end end
def test_raise_in_at_exit def test_raise_in_at_exit
ruby = EnvUtil.rubybin args = ['-e', 'at_exit{raise %[SomethingBad]}',
out = IO.popen([ruby, '-e', 'STDERR.reopen(STDOUT)', '-e', 'raise %[SomethingElse]']
'-e', 'at_exit{raise %[SomethingBad]}', expected = [:*, /SomethingBad/, :*, /SomethingElse/, :*]
'-e', 'raise %[SomethingElse]']) {|f| status = assert_in_out_err(args, '', [], expected, "[ruby-core:9675]")
f.read
}
status = $?
assert_match(/SomethingBad/, out, "[ruby-core:9675]")
assert_match(/SomethingElse/, out, "[ruby-core:9675]")
assert_not_predicate(status, :success?) assert_not_predicate(status, :success?)
end end
def test_exitcode_in_at_exit def test_exitcode_in_at_exit
bug8501 = '[ruby-core:55365] [Bug #8501]' bug8501 = '[ruby-core:55365] [Bug #8501]'
ruby = EnvUtil.rubybin args = ['-e', 'o = Object.new; def o.inspect; raise "[Bug #8501]"; end',
out = IO.popen([ruby, '-e', 'STDERR.reopen(STDOUT)', '-e', 'at_exit{o.nope}']
'-e', 'o = Object.new; def o.inspect; raise "[Bug #8501]"; end', status = assert_in_out_err(args, '', [], /undefined method `nope'/, bug8501)
'-e', 'at_exit{o.nope}']) {|f|
f.read
}
status = $?
assert_match(/undefined method `nope'/, out, bug8501)
assert_not_predicate(status, :success?, bug8501) assert_not_predicate(status, :success?, bug8501)
end end
@ -108,60 +71,39 @@ EOW
end end
def test_propagate_signaled def test_propagate_signaled
ruby = EnvUtil.rubybin status = assert_in_out_err([], <<-'end;', [], /Interrupt$/)
out = IO.popen( trap(:INT, "DEFAULT")
[ruby, at_exit{Process.kill(:INT, $$)}
'-e', 'trap(:INT, "DEFAULT")', end;
'-e', 'STDERR.reopen(STDOUT)',
'-e', 'at_exit{Process.kill(:INT, $$); sleep 5 }']) {|f|
timeout(10) {
f.read
}
}
assert_match(/Interrupt$/, out)
Process.kill(0, 0) rescue return # check if signal works Process.kill(0, 0) rescue return # check if signal works
assert_nil $?.exitstatus assert_nil status.exitstatus
assert_equal Signal.list["INT"], $?.termsig assert_equal Signal.list["INT"], status.termsig
end end
def test_endblock_raise def test_endblock_raise
ruby = EnvUtil.rubybin assert_in_out_err([], <<-'end;', %w(e6 e4 e2), [:*, /e5/, :*, /e3/, :*, /e1/, :*])
th = nil END {raise "e1"}; END {puts "e2"}
out = IO.popen( END {raise "e3"}; END {puts "e4"}
[ruby, END {raise "e5"}; END {puts "e6"}
'-e', 'class C; def write(x); puts x; STDOUT.flush; sleep 0.01; end; 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|
th = Thread.new {sleep 5; Process.kill :KILL, f.pid}
f.read
}
assert_match(/e1/, out)
assert_match(/e6/, out)
ensure
th.kill.join if th.alive?
end end
def test_nested_at_exit def test_nested_at_exit
Tempfile.create(["test_nested_at_exit_", ".rb"]) {|t| expected = [ "outer3",
t.puts "at_exit { puts :outer0 }" "outer2_begin",
t.puts "at_exit { puts :outer1_begin; at_exit { puts :inner1 }; puts :outer1_end }" "outer2_end",
t.puts "at_exit { puts :outer2_begin; at_exit { puts :inner2 }; puts :outer2_end }" "inner2",
t.puts "at_exit { puts :outer3 }" "outer1_begin",
t.flush "outer1_end",
"inner1",
"outer0" ]
expected = [ "outer3", assert_in_out_err([], <<-'end;', expected, [], "[ruby-core:35237]")
"outer2_begin", at_exit { puts :outer0 }
"outer2_end", at_exit { puts :outer1_begin; at_exit { puts :inner1 }; puts :outer1_end }
"inner2", at_exit { puts :outer2_begin; at_exit { puts :inner2 }; puts :outer2_end }
"outer1_begin", at_exit { puts :outer3 }
"outer1_end", end;
"inner1",
"outer0" ]
assert_in_out_err(t.path, "", expected, [], "[ruby-core:35237]")
}
end end
def test_rescue_at_exit def test_rescue_at_exit