Skip to content
Permalink
Browse files

Error out on the problematic configs rather than warning when a group

has been defined.
  • Loading branch information...
myronmarston committed Jul 18, 2011
1 parent 48d667a commit f46e00aff4c9aa028991ff57e71d54f3dcd5c307
Showing with 37 additions and 70 deletions.
  1. +0 −21 lib/rspec/core.rb
  2. +13 −0 lib/rspec/core/configuration.rb
  3. +24 −0 spec/rspec/core/configuration_spec.rb
  4. +0 −28 spec/rspec/core_spec.rb
  5. +0 −21 spec/spec_helper.rb
@@ -75,34 +75,13 @@ def self.configuration
# config.format = 'documentation'
# end
def self.configure
warn_about_deprecated_configure if RSpec.world.example_groups.any?
yield configuration if block_given?
end

# Used internally to clear remaining groups when fail_fast is set
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'
@@ -6,6 +6,8 @@ module Core
class Configuration
include RSpec::Core::Hooks

class MustBeConfiguredBeforeExampleGroupsError < StandardError; end

def self.add_setting(name, opts={})
if opts[:alias]
alias_method name, opts[:alias]
@@ -160,6 +162,7 @@ def mock_with(framework)
# teardown_mocks_for_rspec
# - called after verify_mocks_for_rspec (even if there are errors)
def mock_framework=(framework)
assert_no_example_groups_defined(:mock_framework)
case framework
when Module
settings[:mock_framework] = framework
@@ -200,6 +203,7 @@ def expectation_framework=(framework)
# Given :stdlib, configures test/unit/assertions
# Given both, configures both
def expect_with(*frameworks)
assert_no_example_groups_defined(:expect_with)
settings[:expectation_frameworks] = []
frameworks.each do |framework|
case framework
@@ -455,6 +459,15 @@ def load_spec_files

private

def assert_no_example_groups_defined(config_option)
if RSpec.world.example_groups.any?
raise MustBeConfiguredBeforeExampleGroupsError.new(
"RSpec's #{config_option} configuration option must be configured before " +
"any example groups are defined, but you have already defined a group."
)
end
end

def raise_if_rspec_1_is_loaded
if defined?(Spec) && defined?(Spec::VERSION::MAJOR) && Spec::VERSION::MAJOR == 1
raise <<-MESSAGE
@@ -71,6 +71,18 @@ module RSpec::Core
config.should_receive(:require).with('rspec/core/mocking/with_absolutely_nothing')
config.mock_framework = :crazy_new_mocking_framework_ive_not_yet_heard_of
end

context 'when there are already some example groups defined' do
before(:each) do
RSpec.world.stub(:example_groups).and_return([double.as_null_object])
end

it 'raises an error since this setting must be applied before any groups are defined' do
expect {
config.mock_framework = :rspec
}.to raise_error(/must be configured before any example groups are defined/)
end
end
end

describe "#mock_with" do
@@ -109,6 +121,18 @@ module RSpec::Core
config.expect_with :not_supported
end.to raise_error(ArgumentError)
end

context 'when there are already some example groups defined' do
before(:each) do
RSpec.world.stub(:example_groups).and_return([double.as_null_object])
end

it 'raises an error since this setting must be applied before any groups are defined' do
expect {
config.expect_with :rspec
}.to raise_error(/must be configured before any example groups are defined/)
end
end
end

describe "#expecting_with_rspec?" do
@@ -11,41 +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|
config.should eq(RSpec::configuration)
end
end

context "when an example group has already been defined" do
before(:each) do
RSpec.world.stub(:example_groups).and_return([double.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
@@ -49,27 +49,6 @@ 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 f46e00a

Please sign in to comment.
You can’t perform that action at this time.