File: suite_hooks_errors_spec.rb

package info (click to toggle)
ruby-rspec 3.12.0c0e1m1s0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,752 kB
  • sloc: ruby: 69,818; sh: 1,861; makefile: 99
file content (149 lines) | stat: -rw-r--r-- 4,163 bytes parent folder | download | duplicates (2)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
require 'support/aruba_support'
require 'support/formatter_support'

RSpec.describe 'Suite hook errors' do
  include_context "aruba support"
  include FormatterSupport

  let(:failure_exit_code) { rand(97) + 2 } # 2..99
  let(:error_exit_code) { failure_exit_code + 2 } # 4..101

  if RSpec::Support::Ruby.jruby_9000? && RSpec::Support::Ruby.jruby_version > '9.2.0.0'
    let(:spec_line_suffix) { ":in `block in <main>'" }
  elsif RSpec::Support::Ruby.jruby_9000?
    let(:spec_line_suffix) { ":in `block in (root)'" }
  elsif RSpec::Support::Ruby.jruby?
    let(:spec_line_suffix) { ":in `(root)'" }
  elsif RUBY_VERSION == "1.8.7"
    let(:spec_line_suffix) { "" }
  else
    let(:spec_line_suffix) { ":in `block (2 levels) in <top (required)>'" }
  end

  before do
    setup_aruba

    RSpec.configure do |c|
      c.filter_gems_from_backtrace "gems/aruba"
      c.filter_gems_from_backtrace "gems/bundler"
      c.backtrace_exclusion_patterns << %r{/rspec-core/spec/} << %r{rspec_with_simplecov}
      c.failure_exit_code = failure_exit_code
      c.error_exit_code = error_exit_code
    end
  end

  def run_spec_expecting_non_zero(before_or_after)
    write_file "the_spec.rb", "
      RSpec.configure do |c|
        c.#{before_or_after}(:suite) do
          raise 'boom'
        end
      end

      RSpec.describe do
        it { }
      end
    "

    run_command "the_spec.rb"
    expect(last_cmd_exit_status).to eq(error_exit_code)
    normalize_durations(last_cmd_stdout)
  end

  it 'nicely formats errors in `before(:suite)` hooks and exits with non-zero' do
    output = run_spec_expecting_non_zero(:before)
    expect(output).to eq unindent(<<-EOS)

      An error occurred in a `before(:suite)` hook.
      Failure/Error: raise 'boom'

      RuntimeError:
        boom
      # ./the_spec.rb:4#{spec_line_suffix}


      Finished in n.nnnn seconds (files took n.nnnn seconds to load)
      0 examples, 0 failures, 1 error occurred outside of examples

    EOS
  end

  it 'nicely formats errors in `after(:suite)` hooks and exits with non-zero' do
    output = run_spec_expecting_non_zero(:after)
    expect(output).to eq unindent(<<-EOS)
      .
      An error occurred in an `after(:suite)` hook.
      Failure/Error: raise 'boom'

      RuntimeError:
        boom
      # ./the_spec.rb:4#{spec_line_suffix}


      Finished in n.nnnn seconds (files took n.nnnn seconds to load)
      1 example, 0 failures, 1 error occurred outside of examples

    EOS
  end

  it 'nicely formats errors from multiple :suite hooks of both types and exits with non-zero' do
    write_file "the_spec.rb", "
      RSpec.configure do |c|
        c.before(:suite) { raise 'before 1' }
        c.before(:suite) { raise 'before 2' }
        c.after(:suite) { raise 'after 1' }
        c.after(:suite) { raise 'after 2' }
      end

      RSpec.describe do
        it { }
      end
    "

    cause =
      if RSpec::Support::Ruby.jruby_9000? && RSpec::Support::Ruby.jruby_version > '9.2.0.0'
        unindent(<<-EOS)
          # ------------------
          # --- Caused by: ---
          # RuntimeError:
          #   before 1
          #   ./the_spec.rb:3:in `block in <main>'
        EOS
      else
        ""
      end

    run_command "the_spec.rb"
    expect(last_cmd_exit_status).to eq(error_exit_code)
    output = normalize_durations(last_cmd_stdout)

    expect(output).to eq unindent(<<-EOS)

      An error occurred in a `before(:suite)` hook.
      Failure/Error: c.before(:suite) { raise 'before 1' }

      RuntimeError:
        before 1
      # ./the_spec.rb:3#{spec_line_suffix}

      An error occurred in an `after(:suite)` hook.
      Failure/Error: c.after(:suite) { raise 'after 2' }

      RuntimeError:
        after 2
      # ./the_spec.rb:6#{spec_line_suffix}
      #{ cause }
      An error occurred in an `after(:suite)` hook.
      Failure/Error: c.after(:suite) { raise 'after 1' }

      RuntimeError:
        after 1
      # ./the_spec.rb:5#{spec_line_suffix}
      #{ cause }

      Finished in n.nnnn seconds (files took n.nnnn seconds to load)
      0 examples, 0 failures, 3 errors occurred outside of examples

    EOS
  end
end