Skip to content

Commit

Permalink
Print a warning when users configure RSpec after defining an example …
Browse files Browse the repository at this point in the history
…group.
  • Loading branch information
myronmarston committed Mar 7, 2011
1 parent ab861a7 commit fc70cee
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/rspec/core.rb
Expand Up @@ -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'
Expand Down
28 changes: 28 additions & 0 deletions spec/rspec/core_spec.rb
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -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
Expand Down

0 comments on commit fc70cee

Please sign in to comment.