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

Commit d53df3a

Browse files
committed
Deal with circular dependency between world and config.
`RSpec::World#initialize` references `RSpec.configuration` and `RSpec::Configuration` references `RSpec.world` in a few places. This circular dependency is not ideal and has triggered infinite recursion during some recent changes I've tried to make. It would be nice to get rid of it entirely, but for now, this is the simplest change I can make it solve my immediate problems.
1 parent 3ada308 commit d53df3a

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

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: 17 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,22 @@ 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+
def self.example_groups
221+
[]
222+
end
223+
224+
def self.all_example_groups
225+
[]
226+
end
227+
end
211228
end
212229
end
213230
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)