Skip to content

Commit

Permalink
Merge pull request #2052 from rspec/configurable_warning_behaviour
Browse files Browse the repository at this point in the history
Configurable warning behaviour
  • Loading branch information
JonRowe committed Aug 25, 2015
2 parents fecfcca + 3add554 commit 796daea
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/rspec/core/configuration.rb
Expand Up @@ -1421,6 +1421,25 @@ def warnings?
$VERBOSE
end

# @private
RAISE_ERROR_WARNING_NOTIFIER = lambda { |message| raise message }

# Turns warnings into errors. This can be useful when
# you want RSpec to run in a 'strict' no warning situation.
#
# @example
#
# RSpec.configure do |rspec|
# rspec.raise_on_warning = true
# end
def raise_on_warning=(value)
if value
RSpec::Support.warning_notifier = RAISE_ERROR_WARNING_NOTIFIER
else
RSpec::Support.warning_notifier = RSpec::Support::DEFAULT_WARNING_NOTIFIER
end
end

# Exposes the current running example via the named
# helper method. RSpec 2.x exposed this via `example`,
# but in RSpec 3.0, the example is instead exposed via
Expand Down
21 changes: 21 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Expand Up @@ -1963,6 +1963,27 @@ def strategy.order(list)
end
end

describe '#raise_on_warning=(value)' do
around do |example|
original_setting = RSpec::Support.warning_notifier
example.run
RSpec::Support.warning_notifier = original_setting
end

it 'causes warnings to raise errors when true' do
config.raise_on_warning = true
expect {
RSpec.warning 'All hell breaks loose'
}.to raise_error a_string_including('WARNING: All hell breaks loose')
end

it 'causes warnings to default to warning when false' do
config.raise_on_warning = false
expect_warning_with_call_site(__FILE__, __LINE__ + 1)
RSpec.warning 'doesnt raise'
end
end

describe "#raise_errors_for_deprecations!" do
it 'causes deprecations to raise errors rather than printing to the deprecation stream' do
config.deprecation_stream = stream = StringIO.new
Expand Down

0 comments on commit 796daea

Please sign in to comment.