Skip to content

Commit

Permalink
Stash methods that get and set thread local variables to allow the us…
Browse files Browse the repository at this point in the history
…er to mock them

Fixes rspec#605.
  • Loading branch information
manueljacob committed Jul 12, 2024
1 parent 42b18c4 commit c699dec
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions lib/rspec/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,18 @@ def self.class_of(object)
singleton_class.ancestors.find { |ancestor| !ancestor.equal?(singleton_class) }
end

# A single thread local variable so we don't excessively pollute that namespace.
# Stash original methods to allow the user to mock them.
if RUBY_VERSION.to_f >= 2
def self.thread_local_data
Thread.current.thread_variable_get(:__rspec) || Thread.current.thread_variable_set(:__rspec, {})
end
@thread_variable_get = Thread.instance_method(:thread_variable_get)
@thread_variable_set = Thread.instance_method(:thread_variable_set)
else
def self.thread_local_data
Thread.current[:__rspec] ||= {}
end
@thread_variable_get = Thread.instance_method(:[])
@thread_variable_set = Thread.instance_method(:[]=)
end

# A single thread local variable so we don't excessively pollute that namespace.
def self.thread_local_data
@thread_variable_get.bind(Thread.current).call(:__rspec) || @thread_variable_set.bind(Thread.current).call(:__rspec, {})
end

# @api private
Expand Down

0 comments on commit c699dec

Please sign in to comment.