Skip to content

Commit

Permalink
Standardize on rspec-support’s thread local data hash.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed May 16, 2015
1 parent 1f162ed commit 6a18dbd
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
11 changes: 2 additions & 9 deletions lib/rspec/core.rb
Expand Up @@ -119,20 +119,13 @@ def self.configure
# end
#
def self.current_example
thread_local_metadata[:current_example]
RSpec::Support.thread_local_data[:current_example]
end

# Set the current example being executed.
# @api private
def self.current_example=(example)
thread_local_metadata[:current_example] = example
end

# @private
# A single thread local variable so we don't excessively pollute that
# namespace.
def self.thread_local_metadata
Thread.current[:_rspec] ||= { :shared_example_group_inclusions => [] }
RSpec::Support.thread_local_data[:current_example] = example
end

# @private
Expand Down
11 changes: 8 additions & 3 deletions lib/rspec/core/example_group.rb
Expand Up @@ -231,7 +231,7 @@ def self.define_example_method(name, extra_options={})
# @see DSL#describe
def self.define_example_group_method(name, metadata={})
idempotently_define_singleton_method(name) do |*args, &example_group_block|
thread_data = RSpec.thread_local_metadata
thread_data = RSpec::Support.thread_local_data
top_level = self == ExampleGroup

if top_level
Expand Down Expand Up @@ -705,17 +705,22 @@ def description

# @private
def self.current_backtrace
RSpec.thread_local_metadata[:shared_example_group_inclusions].reverse
shared_example_group_inclusions.reverse
end

# @private
def self.with_frame(name, location)
current_stack = RSpec.thread_local_metadata[:shared_example_group_inclusions]
current_stack = shared_example_group_inclusions
current_stack << new(name, location)
yield
ensure
current_stack.pop
end

# @private
def self.shared_example_group_inclusions
RSpec::Support.thread_local_data[:shared_example_group_inclusions] ||= []
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/metadata.rb
Expand Up @@ -270,7 +270,7 @@ def self.backwards_compatibility_default_proc(&example_group_selector)
# that take a metadata hash, and MetadataFilter sets this thread
# local to silence the warning here since it would be so
# confusing.
unless RSpec.thread_local_metadata[:silence_metadata_example_group_deprecations]
unless RSpec::Support.thread_local_data[:silence_metadata_example_group_deprecations]
RSpec.deprecate("The `:example_group` key in an example group's metadata hash",
:replacement => "the example group's hash directly for the " \
"computed keys and `:parent_example_group` to access the parent " \
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/core/metadata_filter.rb
Expand Up @@ -78,10 +78,10 @@ def filters_apply?(key, value, metadata)
end

def silence_metadata_example_group_deprecations
RSpec.thread_local_metadata[:silence_metadata_example_group_deprecations] = true
RSpec::Support.thread_local_data[:silence_metadata_example_group_deprecations] = true
yield
ensure
RSpec.thread_local_metadata.delete(:silence_metadata_example_group_deprecations)
RSpec::Support.thread_local_data.delete(:silence_metadata_example_group_deprecations)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/core/shared_example_group.rb
Expand Up @@ -80,7 +80,7 @@ module SharedExampleGroup
# @see ExampleGroup.include_context
def shared_examples(name, *args, &block)
top_level = self == ExampleGroup
if top_level && RSpec.thread_local_metadata[:in_example_group]
if top_level && RSpec::Support.thread_local_data[:in_example_group]
raise "Creating isolated shared examples from within a context is " \
"not allowed. Remove `RSpec.` prefix or move this to a " \
"top-level scope."
Expand Down
8 changes: 4 additions & 4 deletions spec/rspec/core/example_group_spec.rb
Expand Up @@ -1460,15 +1460,15 @@ def extract_execution_results(group)
it "leaves RSpec's thread metadata unchanged" do
expect {
self.group.send(name, "named this")
}.to avoid_changing(RSpec, :thread_local_metadata)
}.to avoid_changing(RSpec::Support, :thread_local_data)
end

it "leaves RSpec's thread metadata unchanged, even when an error occurs during evaluation" do
expect {
self.group.send(name, "named this") do
raise "boom"
end
}.to raise_error("boom").and avoid_changing(RSpec, :thread_local_metadata)
}.to raise_error("boom").and avoid_changing(RSpec::Support, :thread_local_data)
end

it "passes parameters to the shared content" do
Expand Down Expand Up @@ -1637,7 +1637,7 @@ def foo; end
shared_examples_for("stuff") { }
it_should_behave_like "stuff"
end
}.to avoid_changing(RSpec, :thread_local_metadata)
}.to avoid_changing(RSpec::Support, :thread_local_data)
end

it "leaves RSpec's thread metadata unchanged, even when an error occurs during evaluation" do
Expand All @@ -1648,7 +1648,7 @@ def foo; end
raise "boom"
end
end
}.to raise_error("boom").and avoid_changing(RSpec, :thread_local_metadata)
}.to raise_error("boom").and avoid_changing(RSpec::Support, :thread_local_data)
end
end

Expand Down
8 changes: 8 additions & 0 deletions spec/rspec/core_spec.rb
Expand Up @@ -218,6 +218,14 @@
end
end

it 'uses only one thread local variable', :run_last do
# Trigger features that use thread locals...
aggregate_failures { }
RSpec.shared_examples_for("something") { }

expect(Thread.current.keys.map(&:to_s).grep(/rspec/i).count).to eq(1)
end

describe "::Core.path_to_executable" do
it 'returns the absolute location of the exe/rspec file' do
expect(File.exist? RSpec::Core.path_to_executable).to be_truthy
Expand Down

0 comments on commit 6a18dbd

Please sign in to comment.