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

Deal with circular relationship between world and config #2185

Merged
merged 2 commits into from
Mar 3, 2016
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
4 changes: 3 additions & 1 deletion lib/rspec/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ def self.clear_examples
def self.configuration
@configuration ||= RSpec::Core::Configuration.new
end
configuration.expose_dsl_globally = true

# Yields the global configuration to a block.
# @yield [Configuration] global configuration
Expand Down Expand Up @@ -178,4 +177,7 @@ def self.const_missing(name)
require MODULES_TO_AUTOLOAD.fetch(name) { return super }
::RSpec.const_get(name)
end

Core::DSL.expose_globally!
Core::SharedExampleGroup::TopLevelDSL.expose_globally!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whilst I get that this is to prevent configuration being loaded prematurely, feels a bit like duplication :/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The loading and using of configuration in this file was causing problems for me and the config.expose_dsl_globally = true doesn't set any state in config so this seemed like the simplest solution. Do you have a suggested alternative that doesn't involve reverting to config.expose_dsl_globally = true?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, and I think this is better than adding a new method.

end
11 changes: 7 additions & 4 deletions lib/rspec/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -343,12 +343,13 @@ def treat_symbols_as_metadata_keys_with_true_values=(_value)
# @private
attr_writer :files_to_run
# @private
attr_accessor :filter_manager
attr_accessor :filter_manager, :world
# @private
attr_accessor :static_config_filter_manager
# @private
attr_reader :backtrace_formatter, :ordering_manager, :loaded_spec_files

# rubocop:disable Metrics/AbcSize
def initialize
# rubocop:disable Style/GlobalVars
@start_time = $_rspec_core_load_started_at || ::RSpec::Core::Time.now
Expand Down Expand Up @@ -394,9 +395,11 @@ def initialize
@derived_metadata_blocks = FilterableItemRepository::QueryOptimized.new(:any?)
@threadsafe = true
@max_displayed_failure_line_count = 10
@world = World::Null

define_built_in_hooks
end
# rubocop:enable Metrics/AbcSize

# @private
#
Expand Down Expand Up @@ -1256,7 +1259,7 @@ def configure_group_with(group, module_list, application_method)

# @private
def configure_existing_groups(mod, meta, application_method)
RSpec.world.all_example_groups.each do |group|
world.all_example_groups.each do |group|
next unless meta.empty? || MetadataFilter.apply?(:any?, meta, group.metadata)
__send__(application_method, mod, group)
end
Expand Down Expand Up @@ -1352,7 +1355,7 @@ def load_spec_files
# in that case, the spec file was loaded by `ruby` and
# isn't loaded by us here so we only know about it because
# of an example group being registered in it.
RSpec.world.registered_example_group_files.each do |f|
world.registered_example_group_files.each do |f|
loaded_spec_files << f # the registered files are already expended absolute paths
end

Expand Down Expand Up @@ -1814,7 +1817,7 @@ def define_built_in_hooks
end

def assert_no_example_groups_defined(config_option)
return unless RSpec.world.example_groups.any?
return unless world.example_groups.any?

raise MustBeConfiguredBeforeExampleGroupsError.new(
"RSpec's #{config_option} configuration option must be configured before " \
Expand Down
19 changes: 19 additions & 0 deletions lib/rspec/core/world.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class World

def initialize(configuration=RSpec.configuration)
@configuration = configuration
configuration.world = self
@example_groups = []
@example_group_counts_by_spec_file = Hash.new(0)
@filtered_examples = Hash.new do |hash, group|
Expand Down Expand Up @@ -208,6 +209,24 @@ def fail_if_config_and_cli_options_invalid
1 # exit code
)
end

# @private
# Provides a null implementation for initial use by configuration.
module Null
def self.registered_example_group_files
[]
end

# :nocov:
def self.example_groups
[]
end

def self.all_example_groups
[]
end
# :nocov:
end
end
end
end
2 changes: 2 additions & 0 deletions spec/rspec/core/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ module RSpec::Core
let(:exclusion_filter) { config.exclusion_filter.rules }
let(:inclusion_filter) { config.inclusion_filter.rules }

before { config.world = RSpec.world }

shared_examples_for "warning of deprecated `:example_group` during filtering configuration" do |method, *args|
it "issues a deprecation warning when filtering by `:example_group`" do
args << { :example_group => { :file_location => /spec\/unit/ } }
Expand Down