Permalink
Browse files

Print a warning when users configure RSpec after defining an example …

…group.
  • Loading branch information...
myronmarston committed Mar 7, 2011
1 parent ab861a7 commit fc70cee014689a947bfdea1ab04d91191327946f
Showing with 70 additions and 0 deletions.
  1. +21 −0 lib/rspec/core.rb
  2. +28 −0 spec/rspec/core_spec.rb
  3. +21 −0 spec/spec_helper.rb
@@ -54,12 +54,33 @@ def self.configuration
end

def self.configure
warn_about_deprecated_configure if RSpec.world.example_groups.any?
yield configuration if block_given?
end

def self.clear_remaining_example_groups
world.example_groups.clear
end

private

def self.warn_about_deprecated_configure
warn <<-NOTICE
*****************************************************************
DEPRECATION WARNING: you are using deprecated behaviour that will
be removed from RSpec 3.
You have set some configuration options after an example group has
already been defined. In RSpec 3, this will not be allowed. All
configuration should happen before the first example group is
defined. The configuration is happening at:
#{caller[1]}
*****************************************************************
NOTICE
end
end

require 'rspec/core/backward_compatibility'
@@ -11,13 +11,41 @@
end

describe "#configure" do
around(:each) do |example|
RSpec.allowing_configure_warning(&example)
end

before(:each) do
RSpec.stub(:warn)
end

it "yields the current configuration" do
RSpec.configure do |config|
config.should == RSpec::configuration
end
end

context "when an example group has already been defined" do
before(:each) do
RSpec.world.stub(:example_groups).and_return([stub.as_null_object])
end

it "prints a deprecation warning" do
RSpec.should_receive(:warn).with(/configuration should happen before the first example group/)
RSpec.configure { |c| }
end
end

context "when no examples have been defined yet" do
before(:each) do
RSpec.world.stub(:example_groups).and_return([])
end

it "does not print a deprecation warning" do
RSpec.should_not_receive(:warn)
RSpec.configure { |c| }
end
end
end

describe "#world" do
@@ -50,6 +50,27 @@ def in_editor?
ENV.has_key?('TM_MODE') || ENV.has_key?('EMACS') || ENV.has_key?('VIM')
end

class << RSpec
alias_method :original_warn_about_deprecated_configure, :warn_about_deprecated_configure

def warn_about_deprecated_configure
# no-op: in our specs we don't want to see the warning.
end

alias_method :null_warn_about_deprecated_configure, :warn_about_deprecated_configure

def allowing_configure_warning
(class << self; self; end).class_eval do
alias_method :warn_about_deprecated_configure, :original_warn_about_deprecated_configure
begin
yield
ensure
alias_method :warn_about_deprecated_configure, :null_warn_about_deprecated_configure
end
end
end
end

RSpec.configure do |c|
c.color_enabled = !in_editor?
c.filter_run :focus => true

0 comments on commit fc70cee

Please sign in to comment.