Skip to content

Commit

Permalink
Refactor monkey patch management so that they are turned off by defau…
Browse files Browse the repository at this point in the history
…lt and only enabled for individual specs.
  • Loading branch information
myronmarston committed Aug 16, 2011
1 parent 1eafe63 commit 9377c2b
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 47 deletions.
64 changes: 40 additions & 24 deletions spec/monkey_patches.rb
Expand Up @@ -12,25 +12,28 @@ module MonkeyPatches

def enable!(scope)
case scope
when :all
MONKEY_PATCHES.each do |mp|
realias mp.first, mp.last, :with_monkeypatches
when :fakeweb
realias_all :with_fakeweb
enable!(:vcr) # fakeweb adapter relies upon VCR's Net::HTTP monkey patch
when :webmock
::WebMock::HttpLibAdapters::NetHttpAdapter.enable!
::WebMock::HttpLibAdapters::TyphoeusAdapter.enable!
$original_webmock_callbacks.each do |cb|
::WebMock::CallbackRegistry.add_callback(cb[:options], cb[:block])
end
when :vcr
realias Net::HTTP, :request, :with_vcr
else raise ArgumentError.new("Unexpected scope: #{scope}")
end
end

def disable!(scope)
case scope
when :all
MONKEY_PATCHES.each do |mp|
realias mp.first, mp.last, :without_monkeypatches
end
when :vcr
realias Net::HTTP, :request, :without_vcr
else raise ArgumentError.new("Unexpected scope: #{scope}")
def disable_all!
realias_all :without_monkeypatches

if defined?(::WebMock)
::WebMock::HttpLibAdapters::NetHttpAdapter.disable!
::WebMock::HttpLibAdapters::TyphoeusAdapter.disable!
::WebMock::CallbackRegistry.reset
end
end

Expand All @@ -45,13 +48,7 @@ def init

def capture_method_definition(klass, method, original)
klass.class_eval do
monkeypatch_methods = [
:with_vcr, :without_vcr,
:with_fakeweb, :without_fakeweb,
:with_webmock, :without_webmock
].select do |m|
method_defined?(:"#{method}_#{m}")
end
monkeypatch_methods = [:vcr, :fakeweb].select { |m| method_defined?(:"#{method}_with_#{m}") }

if original
if monkeypatch_methods.size > 0
Expand All @@ -78,6 +75,12 @@ def capture_method_definition(klass, method, original)
def realias(klass, method, alias_extension)
klass.class_eval { alias_method method, :"#{method}_#{alias_extension}" }
end

def realias_all(alias_extension)
MONKEY_PATCHES.each do |mp|
realias mp.first, mp.last, alias_extension
end
end
end

# Require all the HTTP libraries--these must be required before WebMock
Expand All @@ -91,13 +94,26 @@ def realias(klass, method, alias_extension)
require 'typhoeus'
end

# The FakeWeb adapter must be required after WebMock's so
# that VCR's Net::HTTP monkey patch is loaded last.
# This allows us to disable it (i.e. by realiasing to
# the version of Net::HTTP's methods before it was loaded)
require 'vcr/http_stubbing_adapters/webmock'
require 'vcr/http_stubbing_adapters/fakeweb'

# All Net::HTTP monkey patches have now been loaded, so capture the
# appropriate method definitions so we can disable them later.
MonkeyPatches.init

# Disable FakeWeb/VCR Net::HTTP patches before WebMock
# subclasses Net::HTTP and inherits them...
MonkeyPatches.disable_all!

require 'vcr/http_stubbing_adapters/webmock'
$original_webmock_callbacks = ::WebMock::CallbackRegistry.callbacks

# disable all by default; we'll enable specific ones when we need them
MonkeyPatches.disable_all!

RSpec.configure do |config|
[:fakeweb, :webmock, :vcr].each do |scope|
config.before(:all, :with_monkey_patches => scope) { MonkeyPatches.enable!(scope) }
config.after(:all, :with_monkey_patches => scope) { MonkeyPatches.disable_all! }
end
end

16 changes: 0 additions & 16 deletions spec/spec_helper.rb
Expand Up @@ -78,22 +78,6 @@ def reset!(stubbing_lib = :fakeweb)
$stderr = @orig_std_err
end

config.before(:all, :without_webmock_callbacks => true) do
@original_webmock_callbacks = ::WebMock::CallbackRegistry.callbacks
::WebMock::CallbackRegistry.reset
end

config.after(:all, :without_webmock_callbacks => true) do
@original_webmock_callbacks.each do |cb|
::WebMock::CallbackRegistry.add_callback(cb[:options], cb[:block])
end
end

[:all, :vcr].each do |scope|
config.before(:each, :without_monkey_patches => scope) { MonkeyPatches.disable!(scope) }
config.after(:each, :without_monkey_patches => scope) { MonkeyPatches.enable!(scope) }
end

config.filter_run :focus => true
config.run_all_when_everything_filtered = true

Expand Down
2 changes: 1 addition & 1 deletion spec/vcr/extensions/net_http_response_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe VCR::Net::HTTPResponse, :without_monkey_patches => :all do
describe VCR::Net::HTTPResponse do
def self.it_allows_the_body_to_be_read(expected_regex)
it 'allows the body to be read using #body' do
response.body.to_s.should =~ expected_regex
Expand Down
2 changes: 1 addition & 1 deletion spec/vcr/extensions/net_http_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe "Net::HTTP Extensions", :without_webmock_callbacks => true do
describe "Net::HTTP Extensions", :with_monkey_patches => :vcr do
before(:all) { VCR::SinatraApp.port } # ensure the server is started before instantiating any Net::HTTP instances

let(:uri) { URI.parse("http://localhost:#{VCR::SinatraApp.port}/") }
Expand Down
2 changes: 1 addition & 1 deletion spec/vcr/http_stubbing_adapters/excon_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe VCR::HttpStubbingAdapters::Excon, :without_monkey_patches => :vcr do
describe VCR::HttpStubbingAdapters::Excon do
it_behaves_like 'an http stubbing adapter',
['excon'],
[:method, :uri, :host, :path, :body, :headers],
Expand Down
2 changes: 1 addition & 1 deletion spec/vcr/http_stubbing_adapters/fakeweb_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe VCR::HttpStubbingAdapters::FakeWeb, :without_webmock_callbacks => true do
describe VCR::HttpStubbingAdapters::FakeWeb, :with_monkey_patches => :fakeweb do
it_behaves_like 'an http stubbing adapter', ['net/http'], [:method, :uri, :host, :path], :needs_net_http_extension

it_performs('version checking',
Expand Down
2 changes: 1 addition & 1 deletion spec/vcr/http_stubbing_adapters/faraday_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe VCR::HttpStubbingAdapters::Faraday, :without_monkey_patches => :all do
describe VCR::HttpStubbingAdapters::Faraday do
it_behaves_like 'an http stubbing adapter',
%w[ faraday-typhoeus faraday-net_http faraday-patron ],
[:method, :uri, :host, :path, :body, :headers],
Expand Down
2 changes: 1 addition & 1 deletion spec/vcr/http_stubbing_adapters/typhoeus_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe VCR::HttpStubbingAdapters::Typhoeus, :without_monkey_patches => :vcr do
describe VCR::HttpStubbingAdapters::Typhoeus do
before(:each) do
::Typhoeus::Hydra.stubs = []
::Typhoeus::Hydra.allow_net_connect = true
Expand Down
2 changes: 1 addition & 1 deletion spec/vcr/http_stubbing_adapters/webmock_spec.rb
@@ -1,6 +1,6 @@
require 'spec_helper'

describe VCR::HttpStubbingAdapters::WebMock, :without_monkey_patches => :vcr do
describe VCR::HttpStubbingAdapters::WebMock, :with_monkey_patches => :webmock do
it_behaves_like 'an http stubbing adapter',
%w[net/http patron httpclient em-http-request curb],
[:method, :uri, :host, :path, :body, :headers]
Expand Down

0 comments on commit 9377c2b

Please sign in to comment.