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

Commit 0c54868

Browse files
committed
Handle errors during spec file load time in a nicer manner.
- Format them like other errors. - Allow other files to be loaded so you can see all the load-time errors. - Abort the test suite before running any examples.
1 parent 6a4376e commit 0c54868

File tree

3 files changed

+99
-2
lines changed

3 files changed

+99
-2
lines changed

Changelog.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Enhancements:
55

6-
* Warn when duplicate shared exmaples definitions are loaded due to being
6+
* Warn when duplicate shared examples definitions are loaded due to being
77
defined in files matching the spec pattern (e.g. `_spec.rb`) (#2278, Devon Estes)
88
* Improve metadata filtering so that it can match against any object
99
that implements `===` instead of treating regular expressions as
@@ -12,6 +12,8 @@ Enhancements:
1212
RSpec to prevent confusion. (Myron Marston, #2304)
1313
* Add `config.fail_if_no_examples` option which causes RSpec to fail if
1414
no examples are found. (Ewa Czechowska, #2302)
15+
* Nicely format errors encountered while loading spec files.
16+
(Myron Marston, #2323)
1517

1618
### 3.5.3 / 2016-09-02
1719
[Full Changelog](http://github.com/rspec/rspec-core/compare/v3.5.2...v3.5.3)

lib/rspec/core/configuration.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1437,7 +1437,7 @@ def load_spec_files
14371437

14381438
files_to_run.uniq.each do |f|
14391439
file = File.expand_path(f)
1440-
load file
1440+
load_spec_file_handling_errors(file)
14411441
loaded_spec_files << file
14421442
end
14431443

@@ -1864,6 +1864,14 @@ def on_example_group_definition_callbacks
18641864

18651865
private
18661866

1867+
def load_spec_file_handling_errors(file)
1868+
load file
1869+
rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex
1870+
relative_file = Metadata.relative_path(file)
1871+
reporter.notify_non_example_exception(ex, "An error occurred while loading #{relative_file}.")
1872+
RSpec.world.wants_to_quit = true
1873+
end
1874+
18671875
def handle_suite_hook(scope, meta)
18681876
return nil unless scope == :suite
18691877

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
require 'support/aruba_support'
2+
require 'support/formatter_support'
3+
4+
RSpec.describe 'Spec file load errors' do
5+
include_context "aruba support"
6+
include FormatterSupport
7+
8+
let(:failure_exit_code) { rand(97) + 2 } # 2..99
9+
10+
if RSpec::Support::Ruby.jruby_9000?
11+
let(:spec_line_suffix) { ":in `block in (root)'" }
12+
elsif RSpec::Support::Ruby.jruby?
13+
let(:spec_line_suffix) { ":in `(root)'" }
14+
elsif RUBY_VERSION == "1.8.7"
15+
let(:spec_line_suffix) { "" }
16+
else
17+
let(:spec_line_suffix) { ":in `<top (required)>'" }
18+
end
19+
20+
before do
21+
# get out of `aruba` sub-dir so that `filter_gems_from_backtrace 'aruba'`
22+
# below does not filter out our spec file.
23+
expect(dirs.pop).to eq "aruba"
24+
25+
clean_current_dir
26+
27+
RSpec.configure do |c|
28+
c.filter_gems_from_backtrace "aruba"
29+
c.backtrace_exclusion_patterns << %r{/rspec-core/spec/} << %r{rspec_with_simplecov}
30+
c.failure_exit_code = failure_exit_code
31+
end
32+
end
33+
34+
it 'nicely handles load-time errors in user spec files' do
35+
write_file_formatted "1_spec.rb", "
36+
boom
37+
38+
RSpec.describe 'Calling boom' do
39+
it 'will not run this example' do
40+
expect(1).to eq 1
41+
end
42+
end
43+
"
44+
45+
write_file_formatted "2_spec.rb", "
46+
RSpec.describe 'No Error' do
47+
it 'will not run this example, either' do
48+
expect(1).to eq 1
49+
end
50+
end
51+
"
52+
53+
write_file_formatted "3_spec.rb", "
54+
boom
55+
56+
RSpec.describe 'Calling boom again' do
57+
it 'will not run this example, either' do
58+
expect(1).to eq 1
59+
end
60+
end
61+
"
62+
63+
run_command "1_spec.rb 2_spec.rb 3_spec.rb"
64+
expect(last_cmd_exit_status).to eq(failure_exit_code)
65+
output = normalize_durations(last_cmd_stdout)
66+
expect(output).to eq unindent(<<-EOS)
67+
68+
An error occurred while loading ./1_spec.rb.
69+
Failure/Error: boom
70+
71+
NameError:
72+
undefined local variable or method `boom' for main:Object
73+
# ./1_spec.rb:1#{spec_line_suffix}
74+
75+
An error occurred while loading ./3_spec.rb.
76+
Failure/Error: boom
77+
78+
NameError:
79+
undefined local variable or method `boom' for main:Object
80+
# ./3_spec.rb:1#{spec_line_suffix}
81+
82+
83+
Finished in n.nnnn seconds (files took n.nnnn seconds to load)
84+
0 examples, 0 failures
85+
EOS
86+
end
87+
end

0 commit comments

Comments
 (0)