From fc70cee014689a947bfdea1ab04d91191327946f Mon Sep 17 00:00:00 2001 From: Myron Marston Date: Sun, 6 Mar 2011 21:12:54 -0800 Subject: [PATCH] Print a warning when users configure RSpec after defining an example group. --- lib/rspec/core.rb | 21 +++++++++++++++++++++ spec/rspec/core_spec.rb | 28 ++++++++++++++++++++++++++++ spec/spec_helper.rb | 21 +++++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/lib/rspec/core.rb b/lib/rspec/core.rb index b747e3ab77..d3bfe461e5 100644 --- a/lib/rspec/core.rb +++ b/lib/rspec/core.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' diff --git a/spec/rspec/core_spec.rb b/spec/rspec/core_spec.rb index b685b28271..b9437d462b 100644 --- a/spec/rspec/core_spec.rb +++ b/spec/rspec/core_spec.rb @@ -11,6 +11,13 @@ 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| @@ -18,6 +25,27 @@ 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fbcc2d1f98..739374eda1 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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