From 80c281b6ce1cdb63635147059e47303d42e100bd Mon Sep 17 00:00:00 2001 From: Myron Marston Date: Sun, 2 Oct 2011 14:14:43 -0700 Subject: [PATCH] Remove #after_adapters_loaded from adapter interface. Instead, use a callback. --- lib/vcr.rb | 5 ++++- lib/vcr/http_stubbing_adapters/common.rb | 4 ---- lib/vcr/http_stubbing_adapters/typhoeus.rb | 16 ++++++++-------- lib/vcr/util/hooks.rb | 2 +- spec/vcr/http_stubbing_adapters/typhoeus_spec.rb | 5 +++-- spec/vcr_spec.rb | 6 ++++-- 6 files changed, 20 insertions(+), 18 deletions(-) diff --git a/lib/vcr.rb b/lib/vcr.rb index 01ba6cbc..1a38ca88 100644 --- a/lib/vcr.rb +++ b/lib/vcr.rb @@ -14,8 +14,11 @@ module VCR include VariableArgsBlockCaller + include Hooks extend self + define_hook :after_http_stubbing_adapters_loaded + autoload :BasicObject, 'vcr/util/basic_object' autoload :CucumberTags, 'vcr/test_frameworks/cucumber' autoload :InternetConnection, 'vcr/util/internet_connection' @@ -107,7 +110,7 @@ def http_stubbing_adapters adapters = VCR.configuration.http_stubbing_libraries.map { |l| adapter_for(l) } raise ArgumentError.new("The http stubbing library is not configured.") if adapters.empty? - adapters.each { |a| a.after_adapters_loaded } + invoke_hook(:after_http_stubbing_adapters_loaded) adapters end end diff --git a/lib/vcr/http_stubbing_adapters/common.rb b/lib/vcr/http_stubbing_adapters/common.rb index 0f6fc1d7..c3ffa6ae 100644 --- a/lib/vcr/http_stubbing_adapters/common.rb +++ b/lib/vcr/http_stubbing_adapters/common.rb @@ -31,10 +31,6 @@ def enabled? [nil, self].include? VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter end - def after_adapters_loaded - # no-op - end - def exclusively_enabled VCR::HttpStubbingAdapters::Common.exclusively_enabled_adapter = self diff --git a/lib/vcr/http_stubbing_adapters/typhoeus.rb b/lib/vcr/http_stubbing_adapters/typhoeus.rb index 02b90144..93d21494 100644 --- a/lib/vcr/http_stubbing_adapters/typhoeus.rb +++ b/lib/vcr/http_stubbing_adapters/typhoeus.rb @@ -8,14 +8,6 @@ module Typhoeus include VCR::HttpStubbingAdapters::Common extend self - def after_adapters_loaded - # ensure WebMock's Typhoeus adapter does not conflict with us here - # (i.e. to double record requests or whatever). - if defined?(::WebMock::HttpLibAdapters::TyphoeusAdapter) - ::WebMock::HttpLibAdapters::TyphoeusAdapter.disable! - end - end - class RequestHandler extend Forwardable @@ -113,3 +105,11 @@ def find_stub_from_request(request) end end +VCR.after_http_stubbing_adapters_loaded do + # ensure WebMock's Typhoeus adapter does not conflict with us here + # (i.e. to double record requests or whatever). + if defined?(WebMock::HttpLibAdapters::TyphoeusAdapter) + WebMock::HttpLibAdapters::TyphoeusAdapter.disable! + end +end + diff --git a/lib/vcr/util/hooks.rb b/lib/vcr/util/hooks.rb index f04baa3d..daf209bb 100644 --- a/lib/vcr/util/hooks.rb +++ b/lib/vcr/util/hooks.rb @@ -6,7 +6,7 @@ def self.included(klass) klass.extend(ClassMethods) end - def invoke_hook(hook, tag, *args) + def invoke_hook(hook, tag=nil, *args) hooks_for(hook, tag).each do |callback| call_block(callback, *args) end diff --git a/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb b/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb index d9b7c657..1170b42d 100644 --- a/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb +++ b/spec/vcr/http_stubbing_adapters/typhoeus_spec.rb @@ -17,10 +17,11 @@ end end - describe ".after_adapters_loaded" do + describe "VCR.after_http_stubbing_adapters_loaded hook", :disable_warnings do it 'disables the webmock typhoeus adapter so it does not conflict with our typhoeus adapter' do + load "vcr/http_stubbing_adapters/typhoeus.rb" ::WebMock::HttpLibAdapters::TyphoeusAdapter.should_receive(:disable!) - described_class.after_adapters_loaded + VCR.invoke_hook(:after_http_stubbing_adapters_loaded) end end end unless RUBY_PLATFORM == 'java' diff --git a/spec/vcr_spec.rb b/spec/vcr_spec.rb index 0c4bc38a..3e613dc5 100644 --- a/spec/vcr_spec.rb +++ b/spec/vcr_spec.rb @@ -204,10 +204,12 @@ def insert_cassette(name = :cassette_test) end end - it 'calls #after_adapters_loaded on the configured stubbing adapters' do - VCR::HttpStubbingAdapters::FakeWeb.should_receive(:after_adapters_loaded) + it 'invokes the after_htp_stubbing_adapters_loaded hook' do + called = false + VCR.after_http_stubbing_adapters_loaded { called = true } VCR.configuration.stub_with :fakeweb VCR.http_stubbing_adapters + called.should be_true end it 'raises an error if both :fakeweb and :webmock are configured' do