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

Commit 6b8257d

Browse files
committed
Merge pull request #2185 from rspec/world-configuration-circular
Deal with circular relationship between world and config
2 parents 83cba5d + fc0e6b5 commit 6b8257d

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

lib/rspec/core.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ def self.clear_examples
8383
def self.configuration
8484
@configuration ||= RSpec::Core::Configuration.new
8585
end
86-
configuration.expose_dsl_globally = true
8786

8887
# Yields the global configuration to a block.
8988
# @yield [Configuration] global configuration
@@ -178,4 +177,7 @@ def self.const_missing(name)
178177
require MODULES_TO_AUTOLOAD.fetch(name) { return super }
179178
::RSpec.const_get(name)
180179
end
180+
181+
Core::DSL.expose_globally!
182+
Core::SharedExampleGroup::TopLevelDSL.expose_globally!
181183
end

lib/rspec/core/configuration.rb

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,13 @@ def treat_symbols_as_metadata_keys_with_true_values=(_value)
343343
# @private
344344
attr_writer :files_to_run
345345
# @private
346-
attr_accessor :filter_manager
346+
attr_accessor :filter_manager, :world
347347
# @private
348348
attr_accessor :static_config_filter_manager
349349
# @private
350350
attr_reader :backtrace_formatter, :ordering_manager, :loaded_spec_files
351351

352+
# rubocop:disable Metrics/AbcSize
352353
def initialize
353354
# rubocop:disable Style/GlobalVars
354355
@start_time = $_rspec_core_load_started_at || ::RSpec::Core::Time.now
@@ -394,9 +395,11 @@ def initialize
394395
@derived_metadata_blocks = FilterableItemRepository::QueryOptimized.new(:any?)
395396
@threadsafe = true
396397
@max_displayed_failure_line_count = 10
398+
@world = World::Null
397399

398400
define_built_in_hooks
399401
end
402+
# rubocop:enable Metrics/AbcSize
400403

401404
# @private
402405
#
@@ -1256,7 +1259,7 @@ def configure_group_with(group, module_list, application_method)
12561259

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

@@ -1814,7 +1817,7 @@ def define_built_in_hooks
18141817
end
18151818

18161819
def assert_no_example_groups_defined(config_option)
1817-
return unless RSpec.world.example_groups.any?
1820+
return unless world.example_groups.any?
18181821

18191822
raise MustBeConfiguredBeforeExampleGroupsError.new(
18201823
"RSpec's #{config_option} configuration option must be configured before " \

lib/rspec/core/world.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class World
1212

1313
def initialize(configuration=RSpec.configuration)
1414
@configuration = configuration
15+
configuration.world = self
1516
@example_groups = []
1617
@example_group_counts_by_spec_file = Hash.new(0)
1718
@filtered_examples = Hash.new do |hash, group|
@@ -208,6 +209,24 @@ def fail_if_config_and_cli_options_invalid
208209
1 # exit code
209210
)
210211
end
212+
213+
# @private
214+
# Provides a null implementation for initial use by configuration.
215+
module Null
216+
def self.registered_example_group_files
217+
[]
218+
end
219+
220+
# :nocov:
221+
def self.example_groups
222+
[]
223+
end
224+
225+
def self.all_example_groups
226+
[]
227+
end
228+
# :nocov:
229+
end
211230
end
212231
end
213232
end

spec/rspec/core/configuration_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ module RSpec::Core
1010
let(:exclusion_filter) { config.exclusion_filter.rules }
1111
let(:inclusion_filter) { config.inclusion_filter.rules }
1212

13+
before { config.world = RSpec.world }
14+
1315
shared_examples_for "warning of deprecated `:example_group` during filtering configuration" do |method, *args|
1416
it "issues a deprecation warning when filtering by `:example_group`" do
1517
args << { :example_group => { :file_location => /spec\/unit/ } }

0 commit comments

Comments
 (0)