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

Commit 88bd0c9

Browse files
authored
Merge pull request #2504 from rspec/myron/handle-require-errors
Nicely handle errors encountered from processing `--require`.
2 parents a1813f0 + e7da350 commit 88bd0c9

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Enhancements:
55

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

911
### 3.7.1 / 2018-01-02
1012
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.7.0...v3.7.1)

lib/rspec/core/configuration.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,7 +1467,7 @@ def configure_example(example, example_hooks)
14671467
def requires=(paths)
14681468
directories = ['lib', default_path].select { |p| File.directory? p }
14691469
RSpec::Core::RubyProject.add_to_load_path(*directories)
1470-
paths.each { |path| require path }
1470+
paths.each { |path| load_file_handling_errors(:require, path) }
14711471
@requires += paths
14721472
end
14731473

@@ -1508,7 +1508,7 @@ def load_spec_files
15081508

15091509
files_to_run.uniq.each do |f|
15101510
file = File.expand_path(f)
1511-
load_spec_file_handling_errors(file)
1511+
load_file_handling_errors(:load, file)
15121512
loaded_spec_files << file
15131513
end
15141514

@@ -1964,8 +1964,8 @@ def on_example_group_definition_callbacks
19641964

19651965
private
19661966

1967-
def load_spec_file_handling_errors(file)
1968-
load file
1967+
def load_file_handling_errors(method, file)
1968+
__send__(method, file)
19691969
rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex
19701970
relative_file = Metadata.relative_path(file)
19711971
reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.")

spec/integration/spec_file_load_errors_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,29 @@
3131
end
3232
end
3333

34+
it 'nicely handles load-time errors from --require files' do
35+
write_file_formatted "helper_with_error.rb", "raise 'boom'"
36+
37+
run_command "--require ./helper_with_error"
38+
expect(last_cmd_exit_status).to eq(failure_exit_code)
39+
output = normalize_durations(last_cmd_stdout)
40+
expect(output).to eq unindent(<<-EOS)
41+
42+
An error occurred while loading ./helper_with_error.
43+
Failure/Error: raise 'boom'
44+
45+
RuntimeError:
46+
boom
47+
# ./helper_with_error.rb:1#{spec_line_suffix}
48+
No examples found.
49+
50+
51+
Finished in n.nnnn seconds (files took n.nnnn seconds to load)
52+
0 examples, 0 failures, 1 error occurred outside of examples
53+
54+
EOS
55+
end
56+
3457
it 'nicely handles load-time errors in user spec files' do
3558
write_file_formatted "1_spec.rb", "
3659
boom

0 commit comments

Comments
 (0)