Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warning message is printed when an unsupported version of a http libr…
…ary is loaded.
- Loading branch information
Showing
6 changed files
with
150 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |