summaryrefslogtreecommitdiff
path: root/test/ruby/test_beginendblock.rb
blob: 0c88e9eb230d5ab13fa09f06a67b87b343c5e28e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# frozen_string_literal: false
require 'test/unit'

class TestBeginEndBlock < Test::Unit::TestCase
  DIR = File.dirname(File.expand_path(__FILE__))

  def test_beginendblock
    target = File.join(DIR, 'beginmainend.rb')
    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))
  end

  def test_begininmethod
    assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
      eval("def foo; BEGIN {}; end")
    end

    assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
      eval('eval("def foo; BEGIN {}; end")')
    end
  end

  def test_begininclass
    assert_raise_with_message(SyntaxError, /BEGIN is permitted only at toplevel/) do
      eval("class TestBeginEndBlock; BEGIN {}; end")
    end
  end

  def test_endblockwarn
    assert_in_out_err([], <<-'end;', [], ['-:2: warning: END in method; use at_exit'])
      def end1
        END {}
      end
    end;
  end

  def test_endblockwarn_in_eval
    assert_in_out_err([], <<-'end;', [], ['(eval):2: warning: END in method; use at_exit'])
      eval <<-EOE
        def end2
          END {}
        end
      EOE
    end;
  end

  def test_raise_in_at_exit
    args = ['-e', 'at_exit{raise %[SomethingBad]}',
            '-e', 'raise %[SomethingElse]']
    expected = [:*, /SomethingBad/, :*, /SomethingElse/, :*]
    status = assert_in_out_err(args, '', [], expected, "[ruby-core:9675]")
    assert_not_predicate(status, :success?)
  end

  def test_exitcode_in_at_exit
    bug8501 = '[ruby-core:55365] [Bug #8501]'
    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)
    assert_not_predicate(status, :success?, bug8501)
  end

  def test_propagate_exit_code
    ruby = EnvUtil.rubybin
    assert_equal false, system(ruby, '-e', 'at_exit{exit 2}')
    assert_equal 2, $?.exitstatus
    assert_nil $?.termsig
  end

  def test_propagate_signaled
    status = assert_in_out_err([], <<-'end;', [], /Interrupt$/)
      trap(:INT, "DEFAULT")
      at_exit{Process.kill(:INT, $$)}
    end;
    Process.kill(0, 0) rescue return # check if signal works
    assert_nil status.exitstatus
    assert_equal Signal.list["INT"], status.termsig
  end

  def test_endblock_raise
    assert_in_out_err([], <<-'end;', %w(e6 e4 e2), [:*, /e5/, :*, /e3/, :*, /e1/, :*])
      END {raise "e1"}; END {puts "e2"}
      END {raise "e3"}; END {puts "e4"}
      END {raise "e5"}; END {puts "e6"}
    end;
  end

  def test_nested_at_exit
    expected = [ "outer3",
                 "outer2_begin",
                 "outer2_end",
                 "inner2",
                 "outer1_begin",
                 "outer1_end",
                 "inner1",
                 "outer0" ]

    assert_in_out_err([], <<-'end;', expected, [], "[ruby-core:35237]")
      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;
  end

  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

  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
    assert_normal_exit(script, bug9110)
  end
end