This repository was archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 753
Handle errors in :suite hooks. #2316
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a1936ff
Make `ExceptionPresenter#fully_formatted` support a `nil` arg.
myronmarston de49f81
Remove leading space from full description when no parent description.
myronmarston ebbea9f
Refactor: support `FailedExampleNotification.new(example)`.
myronmarston fd4e761
Refactor spec to be resilient in the face of changes to :suite error …
myronmarston 5ae27f2
Add `reporter#notify_non_example_exception`.
myronmarston 77f8b01
Handle errors in :suite hooks.
myronmarston bef9d61
Address code review feedback.
myronmarston File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,14 +109,17 @@ def setup(err, out) | |
# failed. | ||
def run_specs(example_groups) | ||
examples_count = @world.example_count(example_groups) | ||
@configuration.reporter.report(examples_count) do |reporter| | ||
success = @configuration.reporter.report(examples_count) do |reporter| | ||
@configuration.with_suite_hooks do | ||
if examples_count == 0 && @configuration.fail_if_no_examples | ||
return @configuration.failure_exit_code | ||
end | ||
example_groups.map { |g| g.run(reporter) }.all? ? 0 : @configuration.failure_exit_code | ||
|
||
example_groups.map { |g| g.run(reporter) }.all? | ||
end | ||
end | ||
end && [email protected]_example_failure | ||
|
||
success ? 0 : @configuration.failure_exit_code | ||
end | ||
|
||
private | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
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 | ||
|
||
if 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 | ||
# get out of `aruba` sub-dir so that `filter_gems_from_backtrace 'aruba'` | ||
# below does not filter out our spec file. | ||
expect(dirs.pop).to eq "aruba" | ||
|
||
clean_current_dir | ||
|
||
RSpec.configure do |c| | ||
c.filter_gems_from_backtrace "aruba" | ||
c.backtrace_exclusion_patterns << %r{/rspec-core/spec/} << %r{rspec_with_simplecov} | ||
c.failure_exit_code = failure_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(failure_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 | ||
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 | ||
EOS | ||
end | ||
|
||
it 'nicely formats errors from multiple :suite hooks of both types and exits with non-zero' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh this is interesting: we keep running hooks even if one fails. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was a good point. In the follow-up commit I just pushed I addressed this, and made it behave the same as |
||
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 | ||
" | ||
|
||
run_command "the_spec.rb" | ||
expect(last_cmd_exit_status).to eq(failure_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} | ||
|
||
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} | ||
|
||
|
||
Finished in n.nnnn seconds (files took n.nnnn seconds to load) | ||
0 examples, 0 failures | ||
EOS | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just double checking that
presenter
here isn't an extension point for plugins or anything like that? Passingnil
is ok?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ExceptionPresenter
is tagged as# @private
so it's not part of our public API. I passnil
here because usually we pass an integer (e.g.7
if it's the 7th example that has failed) but there isn't a good way to get a number here so the error just doesn't have the7)
prefix. a1936ff was written to specifically allow this.