Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit bef9d61

Browse files
committed
Address code review feedback.
- s/exception/example/ in a comment - Do not run later `before(:suite)` hooks when an earlier one has failed. This aligns with `before(:example)` and `before(:context)` hooks. However, `after(:suite)` hooks all get run even if one fails, which again aligns with `after(:example)` and `after(:suite)` hooks.
1 parent 77f8b01 commit bef9d61

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

lib/rspec/core/configuration.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,12 @@ def run_suite_hooks(hook_description, hooks)
18871887
hook.run(context)
18881888
rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex
18891889
context.set_exception(ex)
1890+
1891+
# Do not run subsequent `before` hooks if one fails.
1892+
# But for `after` hooks, we run them all so that all
1893+
# cleanup bits get a chance to complete, minimizing the
1894+
# chance that resources get left behind.
1895+
break if hooks.equal?(@before_suite_hooks)
18901896
end
18911897
end
18921898
end

lib/rspec/core/reporter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def deprecation(hash)
153153

154154
# @private
155155
# Provides a way to notify of an exception that is not tied to any
156-
# particular exception (such as an exception encountered in a :suite hook).
156+
# particular example (such as an exception encountered in a :suite hook).
157157
# Exceptions will be formatted the same way they normally are.
158158
def notify_non_example_exception(exception, context_description)
159159
@configuration.world.non_example_failure = true

spec/integration/suite_hooks_errors_spec.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,6 @@ def run_spec_expecting_non_zero(before_or_after)
110110
before 1
111111
# ./the_spec.rb:3#{spec_line_suffix}
112112
113-
An error occurred in a `before(:suite)` hook.
114-
Failure/Error: c.before(:suite) { raise 'before 2' }
115-
116-
RuntimeError:
117-
before 2
118-
# ./the_spec.rb:4#{spec_line_suffix}
119-
120113
An error occurred in an `after(:suite)` hook.
121114
Failure/Error: c.after(:suite) { raise 'after 2' }
122115

spec/rspec/core/hooks_spec.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,64 @@ def hook_collection_for(position, scope)
1818
end
1919
end
2020

21+
[:example, :context, :suite].each do |scope|
22+
describe "#before(#{scope})" do
23+
it "stops running subsequent hooks of the same type when an error is encountered" do
24+
sequence = []
25+
26+
RSpec.configure do |c|
27+
c.output_stream = StringIO.new
28+
29+
c.before(scope) do
30+
sequence << :hook_1
31+
raise "boom"
32+
end
33+
34+
c.before(scope) do
35+
sequence << :hook_2
36+
raise "boom"
37+
end
38+
end
39+
40+
RSpec.configuration.with_suite_hooks do
41+
RSpec.describe do
42+
example { sequence << :example }
43+
end.run
44+
end
45+
46+
expect(sequence).to eq [:hook_1]
47+
end
48+
end
49+
50+
describe "#after(#{scope})" do
51+
it "runs subsequent hooks of the same type when an error is encountered so all cleanup can complete" do
52+
sequence = []
53+
54+
RSpec.configure do |c|
55+
c.output_stream = StringIO.new
56+
57+
c.after(scope) do
58+
sequence << :hook_2
59+
raise "boom"
60+
end
61+
62+
c.after(scope) do
63+
sequence << :hook_1
64+
raise "boom"
65+
end
66+
end
67+
68+
RSpec.configuration.with_suite_hooks do
69+
RSpec.describe do
70+
example { sequence << :example }
71+
end.run
72+
end
73+
74+
expect(sequence).to eq [:example, :hook_1, :hook_2]
75+
end
76+
end
77+
end
78+
2179
[:before, :after, :around].each do |type|
2280
[:example, :context].each do |scope|
2381
next if type == :around && scope == :context

0 commit comments

Comments
 (0)