Skip to content

Commit

Permalink
Warning message is printed when an unsupported version of a http libr…
Browse files Browse the repository at this point in the history
…ary is loaded.
  • Loading branch information
bblimke committed Mar 19, 2012
1 parent 8955914 commit 3b4ef12
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 10 deletions.
24 changes: 14 additions & 10 deletions lib/webmock.rb
Expand Up @@ -6,23 +6,14 @@
require 'webmock/deprecation'
require 'webmock/version'

require 'webmock/http_lib_adapters/http_lib_adapter_registry'
require 'webmock/http_lib_adapters/http_lib_adapter'
require 'webmock/http_lib_adapters/net_http'
require 'webmock/http_lib_adapters/httpclient_adapter'
require 'webmock/http_lib_adapters/patron_adapter'
require 'webmock/http_lib_adapters/curb_adapter'
require 'webmock/http_lib_adapters/em_http_request_adapter'
require 'webmock/http_lib_adapters/typhoeus_hydra_adapter'
require 'webmock/http_lib_adapters/excon_adapter'

require 'webmock/errors'

require 'webmock/util/uri'
require 'webmock/util/headers'
require 'webmock/util/hash_counter'
require 'webmock/util/hash_keys_stringifier'
require 'webmock/util/json'
require 'webmock/util/version_checker'

require 'webmock/matchers/hash_including_matcher'

Expand All @@ -42,4 +33,17 @@
require 'webmock/request_registry'
require 'webmock/stub_registry'
require 'webmock/api'

require 'webmock/http_lib_adapters/http_lib_adapter_registry'
require 'webmock/http_lib_adapters/http_lib_adapter'
require 'webmock/http_lib_adapters/net_http'
require 'webmock/http_lib_adapters/httpclient_adapter'
require 'webmock/http_lib_adapters/patron_adapter'
require 'webmock/http_lib_adapters/curb_adapter'
require 'webmock/http_lib_adapters/em_http_request_adapter'
require 'webmock/http_lib_adapters/typhoeus_hydra_adapter'
require 'webmock/http_lib_adapters/excon_adapter'

require 'webmock/webmock'


2 changes: 2 additions & 0 deletions lib/webmock/http_lib_adapters/curb_adapter.rb
Expand Up @@ -5,6 +5,8 @@
end

if defined?(Curl)
WebMock::VersionChecker.new('Curb', Gem.loaded_specs['curb'].version.to_s, '0.7.16').check_version!

module WebMock
module HttpLibAdapters
class CurbAdapter < HttpLibAdapter
Expand Down
1 change: 1 addition & 0 deletions lib/webmock/http_lib_adapters/excon_adapter.rb
Expand Up @@ -5,6 +5,7 @@
end

if defined?(Excon)
WebMock::VersionChecker.new('Excon', Excon::VERSION, '0.9.6').check_version!

module WebMock
module HttpLibAdapters
Expand Down
1 change: 1 addition & 0 deletions lib/webmock/http_lib_adapters/typhoeus_hydra_adapter.rb
Expand Up @@ -5,6 +5,7 @@
end

if defined?(Typhoeus)
WebMock::VersionChecker.new('Typhoeus', Typhoeus::VERSION, '0.3.2').check_version!

module WebMock
module HttpLibAdapters
Expand Down
73 changes: 73 additions & 0 deletions lib/webmock/util/version_checker.rb
@@ -0,0 +1,73 @@
# This code was created based on https://github.com/myronmarston/vcr/blob/master/lib/vcr/util/version_checker.rb
# Thanks to @myronmarston

module WebMock
class VersionChecker
def initialize(library_name, library_version, min_patch_level, max_minor_version = nil)
@library_name, @library_version = library_name, library_version
@min_patch_level, @max_minor_version = min_patch_level, max_minor_version

@major, @minor, @patch = parse_version(library_version)
@min_major, @min_minor, @min_patch = parse_version(min_patch_level)
@max_major, @max_minor = parse_version(max_minor_version) if max_minor_version

@comparison_result = compare_version
end

def check_version!
warn_about_too_low if too_low?
warn_about_too_high if too_high?
end

private

def too_low?
@comparison_result == :too_low
end

def too_high?
@comparison_result == :too_high
end

def warn_about_too_low
warn_in_red "You are using #{@library_name} #{@library_version}. " +
"WebMock supports version #{version_requirement}."
end

def warn_about_too_high
warn_in_red "You are using #{@library_name} #{@library_version}. " +
"WebMock is known to work with #{@library_name} #{version_requirement}. " +
"It may not work with this version."
end

def warn_in_red(text)
Kernel.warn colorize(text, "\e[31m")
end

def compare_version
case
when @major < @min_major then :too_low
when @max_major && @major > @max_major then :too_high
when @major > @min_major then :ok
when @minor < @min_minor then :too_low
when @max_minor && @minor > @max_minor then :too_high
when @minor > @min_minor then :ok
when @patch < @min_patch then :too_low
end
end

def version_requirement
req = ">= #{@min_patch_level}"
req += ", < #{@max_major}.#{@max_minor + 1}" if @max_minor
req
end

def parse_version(version)
version.split('.').map { |v| v.to_i }
end

def colorize(text, color_code)
"#{color_code}#{text}\e[0m"
end
end
end
59 changes: 59 additions & 0 deletions spec/unit/util/version_checker_spec.rb
@@ -0,0 +1,59 @@
require 'spec_helper'

module WebMock
describe VersionChecker do
it 'prints a warning if the major version is too low' do
checker = VersionChecker.new('foo', '0.7.3', '1.0.0', '1.1')
Kernel.should_receive(:warn).with("\e[31mYou are using foo 0.7.3. WebMock supports version >= 1.0.0, < 1.2.\e[0m")
checker.check_version!
end

it 'prints a warning if the minor version is too low' do
checker = VersionChecker.new('foo', '1.0.99', '1.1.3', '1.2')
Kernel.should_receive(:warn).with("\e[31mYou are using foo 1.0.99. WebMock supports version >= 1.1.3, < 1.3.\e[0m")
checker.check_version!
end

it 'prints a warning if the patch version is too low' do
checker = VersionChecker.new('foo', '1.0.8', '1.0.10', '1.2')
Kernel.should_receive(:warn).with("\e[31mYou are using foo 1.0.8. WebMock supports version >= 1.0.10, < 1.3.\e[0m")
checker.check_version!
end

it 'prints a warning if the patch version is too low and max version is not specified' do
checker = VersionChecker.new('foo', '1.0.8', '1.0.10')
Kernel.should_receive(:warn).with("\e[31mYou are using foo 1.0.8. WebMock supports version >= 1.0.10.\e[0m")
checker.check_version!
end

it 'prints a warning if the major version is too high' do
checker = VersionChecker.new('foo', '2.0.0', '1.0.0', '1.1')
Kernel.should_receive(:warn).with(/may not work with this version/)
checker.check_version!
end

it 'prints a warning if the minor version is too high' do
checker = VersionChecker.new('foo', '1.2.0', '1.0.0', '1.1')
Kernel.should_receive(:warn).with(/may not work with this version/)
checker.check_version!
end

it 'does not raise an error or print a warning when the major version is between the min and max' do
checker = VersionChecker.new('foo', '2.0.0', '1.0.0', '3.0')
Kernel.should_not_receive(:warn)
checker.check_version!
end

it 'does not raise an error or print a warning when the min_patch is 0.6.5, the max_minor is 0.7 and the version is 0.7.3' do
checker = VersionChecker.new('foo', '0.7.3', '0.6.5', '0.7')
Kernel.should_not_receive(:warn)
checker.check_version!
end

it 'does not raise an error or print a warning when the min_patch is 0.6.5, the max_minor is not specified and the version is 0.8.3' do
checker = VersionChecker.new('foo', '0.8.3', '0.6.5')
Kernel.should_not_receive(:warn)
checker.check_version!
end
end
end

0 comments on commit 3b4ef12

Please sign in to comment.