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

Commit

Permalink
Protect against mocking Thread.current
Browse files Browse the repository at this point in the history
  • Loading branch information
JonRowe committed Jul 17, 2024
1 parent 1a69d22 commit d5745fb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/rspec/support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

class Thread
attr_accessor :__rspec_local_data
class << self
alias __current_for_rspec current
end
end

module RSpec
Expand Down Expand Up @@ -96,7 +99,7 @@ def self.class_of(object)

# A single thread local variable so we don't excessively pollute that namespace.
def self.thread_local_data
Thread.current.__rspec_local_data ||= {}
Thread.__current_for_rspec.__rspec_local_data ||= {}
end

# @api private
Expand Down
7 changes: 7 additions & 0 deletions spec/rspec/support_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,13 @@ def object.some_method
end
end

it "works when Thread.current is mocked" do
expect(Thread).to_not receive(:current)

RSpec::Support.thread_local_data[:__for_test] = :oh_hai
expect(RSpec::Support.thread_local_data[:__for_test]).to eq :oh_hai
end

it "works when Thread#thread_variable_get and Thread#thread_variable_set are mocked" do
expect(Thread.current).to receive(:thread_variable_set).with(:test, true).once.and_return(true)
expect(Thread.current).to receive(:thread_variable_get).with(:test).once.and_return(true)
Expand Down

0 comments on commit d5745fb

Please sign in to comment.