-
-
Notifications
You must be signed in to change notification settings - Fork 753
Handle errors during spec file load time in a nicer manner. #2323
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,12 @@ module ConsoleCodes | |
module_function | ||
|
||
# @private | ||
CONFIG_COLORS_TO_METHODS = Configuration.instance_methods.grep(/_color\z/).inject({}) do |hash, method| | ||
hash[method.to_s.sub(/_color\z/, '').to_sym] = method | ||
hash | ||
def config_colors_to_methods | ||
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. Thanks for the explanation in the commit message! |
||
@config_colors_to_methods ||= | ||
Configuration.instance_methods.grep(/_color\z/).inject({}) do |hash, method| | ||
hash[method.to_s.sub(/_color\z/, '').to_sym] = method | ||
hash | ||
end | ||
end | ||
|
||
# Fetches the correct code for the supplied symbol, or checks | ||
|
@@ -34,7 +37,7 @@ module ConsoleCodes | |
# @param code_or_symbol [Symbol, Fixnum] Symbol or code to check | ||
# @return [Fixnum] a console code | ||
def console_code_for(code_or_symbol) | ||
if (config_method = CONFIG_COLORS_TO_METHODS[code_or_symbol]) | ||
if (config_method = config_colors_to_methods[code_or_symbol]) | ||
console_code_for RSpec.configuration.__send__(config_method) | ||
elsif VT100_CODE_VALUES.key?(code_or_symbol) | ||
code_or_symbol | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
require 'support/aruba_support' | ||
require 'support/formatter_support' | ||
|
||
RSpec.describe 'Spec file load errors' do | ||
include_context "aruba support" | ||
include FormatterSupport | ||
|
||
let(:failure_exit_code) { rand(97) + 2 } # 2..99 | ||
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. Why a random code? 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. Because I want the test to force the implementation to return the configured 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. Fair enough :) |
||
|
||
if RSpec::Support::Ruby.jruby_9000? | ||
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. Oh rubies... |
||
let(:spec_line_suffix) { ":in `<top>'" } | ||
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 `<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 | ||
|
||
it 'nicely handles load-time errors in user spec files' do | ||
write_file_formatted "1_spec.rb", " | ||
boom | ||
|
||
RSpec.describe 'Calling boom' do | ||
it 'will not run this example' do | ||
expect(1).to eq 1 | ||
end | ||
end | ||
" | ||
|
||
write_file_formatted "2_spec.rb", " | ||
RSpec.describe 'No Error' do | ||
it 'will not run this example, either' do | ||
expect(1).to eq 1 | ||
end | ||
end | ||
" | ||
|
||
write_file_formatted "3_spec.rb", " | ||
boom | ||
|
||
RSpec.describe 'Calling boom again' do | ||
it 'will not run this example, either' do | ||
expect(1).to eq 1 | ||
end | ||
end | ||
" | ||
|
||
run_command "1_spec.rb 2_spec.rb 3_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 ./1_spec.rb. | ||
Failure/Error: boom | ||
|
||
NameError: | ||
undefined local variable or method `boom' for main:Object | ||
# ./1_spec.rb:1#{spec_line_suffix} | ||
|
||
An error occurred while loading ./3_spec.rb. | ||
Failure/Error: boom | ||
|
||
NameError: | ||
undefined local variable or method `boom' for main:Object | ||
# ./3_spec.rb:1#{spec_line_suffix} | ||
|
||
|
||
Finished in n.nnnn seconds (files took n.nnnn seconds to load) | ||
0 examples, 0 failures | ||
EOS | ||
end | ||
end |
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.
Can't believe we didn't think of this before :)