Skip to content

Commit

Permalink
Single error when --require'd files fail to load (#2568)
Browse files Browse the repository at this point in the history
* Single error when `--require`'d files fail to load

When there's an error loading files specified via the `--require` flag,
typically `rails_helper.rb` or `spec_helper.rb`, it's very common that
most of the spec files won't load either since they depend on the helper
file being loaded. In these situations, one gets hundreds of errors
printed to the screen. That can be quite intimidating and contributes to
hide the real culprit, which is the load error in the helper.
  • Loading branch information
deivid-rodriguez authored and JonRowe committed Oct 3, 2018
1 parent 73e7870 commit d730c02
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rspec/core/reporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ def report(expected_example_count)
end
end

# @param exit_code [Integer] the exit_code to be return by the reporter
#
# Reports a run that exited early without having run any examples.
#
def exit_early(exit_code)
report(0) { exit_code }
end

# @private
def start(expected_example_count, time=RSpec::Core::Time.now)
@start = time
Expand Down
5 changes: 5 additions & 0 deletions lib/rspec/core/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def initialize(options, configuration=RSpec.configuration, world=RSpec.world)
# @param out [IO] output stream
def run(err, out)
setup(err, out)
return @configuration.reporter.exit_early(@configuration.failure_exit_code) if RSpec.world.wants_to_quit

run_specs(@world.ordered_example_groups).tap do
persist_example_statuses
end
Expand All @@ -95,7 +97,10 @@ def run(err, out)
# @param out [IO] output stream
def setup(err, out)
configure(err, out)
return if RSpec.world.wants_to_quit

@configuration.load_spec_files
ensure
@world.announce_filters
end

Expand Down
29 changes: 29 additions & 0 deletions spec/integration/spec_file_load_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@
EOS
end

it 'prints a single error when it happens on --require files' do
write_file_formatted "helper_with_error.rb", "raise 'boom'"

write_file_formatted "1_spec.rb", "
RSpec.describe 'A broken spec file that will raise when loaded' do
raise 'kaboom'
end
"

run_command "--require ./helper_with_error 1_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 while loading ./helper_with_error.
Failure/Error: raise 'boom'
RuntimeError:
boom
# ./helper_with_error.rb:1#{spec_line_suffix}
No examples found.
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 handles load-time errors in user spec files' do
write_file_formatted "1_spec.rb", "
boom
Expand Down
15 changes: 15 additions & 0 deletions spec/rspec/core/reporter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ module RSpec::Core
end
end

describe "#exit_early" do
it "returns the passed exit code" do
expect(reporter.exit_early(42)).to eq(42)
end

it "sends a complete cycle of notifications" do
formatter = double("formatter")
%w[seed start start_dump dump_pending dump_failures dump_summary seed close].map(&:to_sym).each do |message|
reporter.register_listener formatter, message
expect(formatter).to receive(message).ordered
end
reporter.exit_early(42)
end
end

describe "#report" do
it "supports one arg (count)" do
reporter.report(1) {}
Expand Down

0 comments on commit d730c02

Please sign in to comment.