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

Run specs for warning helpers in sub process #6

Merged
merged 1 commit into from
Oct 18, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rspec/support/warnings.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module RSpec

if self.const_get("Core")
if defined?(::RSpec::Core)

# @private
#
Expand Down
80 changes: 46 additions & 34 deletions spec/rspec/warnings_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down