diff --git a/lib/rspec/support/warnings.rb b/lib/rspec/support/warnings.rb index 2acd27920..d21da16ba 100644 --- a/lib/rspec/support/warnings.rb +++ b/lib/rspec/support/warnings.rb @@ -1,6 +1,6 @@ module RSpec - if self.const_get("Core") + if defined?(::RSpec::Core) # @private # diff --git a/spec/rspec/warnings_spec.rb b/spec/rspec/warnings_spec.rb index a31b932c3..b3fa2739d 100644 --- a/spec/rspec/warnings_spec.rb +++ b/spec/rspec/warnings_spec.rb @@ -1,4 +1,5 @@ require "spec_helper" +require 'rspec/support/spec/in_sub_process' # Make sure we're using the `rspec-support` CallerFilter # can be removed at a later date @@ -27,61 +28,66 @@ class << self end # end warnings cleanup # -require 'rspec/support/warnings' - describe "rspec warnings and deprecations" do + include RSpec::Support::InSubProcess - def reset_and_load_warnings - RSpec.module_eval do - class << self - undef deprecate if defined?(RSpec.deprecate) - undef warn_deprecation if defined?(RSpec.warn_deprecation) - undef warning if defined?(RSpec.warning) - undef warn_with if defined?(RSpec.warn_with) - end + def run_with_rspec_core + in_sub_process do + load 'rspec/support/warnings.rb' + yield end - load 'rspec/support/warnings.rb' end - context "when rspec-core is available" do - before do - reset_and_load_warnings + def run_without_rspec_core + in_sub_process do + hide_const "::RSpec::Core" + load 'rspec/support/warnings.rb' + yield end + end + + context "when rspec-core is available" do describe "#deprecate" do it "passes the hash to the reporter" do - expect(RSpec.configuration.reporter).to receive(:deprecation).with(hash_including :deprecated => "deprecated_method", :replacement => "replacement") - RSpec.deprecate("deprecated_method", :replacement => "replacement") + run_with_rspec_core do + expect(RSpec.configuration.reporter).to receive(:deprecation).with(hash_including :deprecated => "deprecated_method", :replacement => "replacement") + RSpec.deprecate("deprecated_method", :replacement => "replacement") + end end it "adds the call site" do - expect_deprecation_with_call_site(__FILE__, __LINE__ + 1) - RSpec.deprecate("deprecated_method") + run_with_rspec_core do + expect_deprecation_with_call_site(__FILE__, __LINE__ + 1) + RSpec.deprecate("deprecated_method") + end end it "doesn't override a passed call site" do - expect_deprecation_with_call_site("some_file.rb", 17) - RSpec.deprecate("deprecated_method", :call_site => "/some_file.rb:17") + run_with_rspec_core do + expect_deprecation_with_call_site("some_file.rb", 17) + RSpec.deprecate("deprecated_method", :call_site => "/some_file.rb:17") + end end end describe "#warn_deprecation" do it "puts message in a hash" do - expect(RSpec.configuration.reporter).to receive(:deprecation).with(hash_including :message => "this is the message") - RSpec.warn_deprecation("this is the message") + run_with_rspec_core do + expect(RSpec.configuration.reporter).to receive(:deprecation).with(hash_including :message => "this is the message") + RSpec.warn_deprecation("this is the message") + end end end end context "when rspec-core is not available" do - before do - allow(RSpec).to receive(:const_get).with("Core") - reset_and_load_warnings - end shared_examples_for "falls back to warn for" do |method| it 'falls back to warning with a plain message' do - expect(::Kernel).to receive(:warn).with /message/ - RSpec.send(method,'message') + run_without_rspec_core do + expect(::Kernel).to receive(:warn).with /message/ + RSpec.send(method,'message') + end end end @@ -91,20 +97,26 @@ class << self shared_examples_for "warning helper" do |helper| it 'warns with the message text' do - expect(::Kernel).to receive(:warn).with(/Message/) - RSpec.send(helper, 'Message') + run_without_rspec_core do + expect(::Kernel).to receive(:warn).with(/Message/) + RSpec.send(helper, 'Message') + end end it 'sets the calling line' do - expect(::Kernel).to receive(:warn).with(/#{__FILE__}:#{__LINE__+1}/) - RSpec.send(helper, 'Message') + run_without_rspec_core do + expect(::Kernel).to receive(:warn).with(/#{__FILE__}:#{__LINE__+1}/) + RSpec.send(helper, 'Message') + end end end describe "#warning" do it 'prepends WARNING:' do - expect(::Kernel).to receive(:warn).with(/WARNING: Message\./) - RSpec.warning 'Message' + run_without_rspec_core do + expect(::Kernel).to receive(:warn).with(/WARNING: Message\./) + RSpec.warning 'Message' + end end it_should_behave_like 'warning helper', :warning end