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

Nicely handle errors encountered from processing --require. #2504

Merged
merged 1 commit into from
Jan 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Enhancements:

* Improve shell escaping used by `RSpec::Core::RakeTask` and `--bisect` so
that it works on `Pathname` objects. (Andrew Vit, #2479)
* Nicely format errors encountered while loading files specified
by `--require` option. (Myron Marston, #2504)

### 3.7.1 / 2018-01-02
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.7.0...v3.7.1)
Expand Down
8 changes: 4 additions & 4 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,7 @@ def configure_example(example, example_hooks)
def requires=(paths)
directories = ['lib', default_path].select { |p| File.directory? p }
RSpec::Core::RubyProject.add_to_load_path(*directories)
paths.each { |path| require path }
paths.each { |path| load_file_handling_errors(:require, path) }
@requires += paths
end

Expand Down Expand Up @@ -1492,7 +1492,7 @@ def load_spec_files

files_to_run.uniq.each do |f|
file = File.expand_path(f)
load_spec_file_handling_errors(file)
load_file_handling_errors(:load, file)
loaded_spec_files << file
end

Expand Down Expand Up @@ -1948,8 +1948,8 @@ def on_example_group_definition_callbacks

private

def load_spec_file_handling_errors(file)
load file
def load_file_handling_errors(method, file)
__send__(method, file)
rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex
relative_file = Metadata.relative_path(file)
reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.")
Expand Down
23 changes: 23 additions & 0 deletions spec/integration/spec_file_load_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@
end
end

it 'nicely handles load-time errors from --require files' do
write_file_formatted "helper_with_error.rb", "raise 'boom'"

run_command "--require ./helper_with_error"
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