From f6ed904008a755dfc04174ecd129852f2d7680b8 Mon Sep 17 00:00:00 2001 From: Marc Rohloff Date: Tue, 8 Nov 2022 10:38:43 -0700 Subject: [PATCH] Version checker fails under some conditions Currently it prints a warning for Curb 0.9.7 --- lib/webmock/util/version_checker.rb | 11 ++--- spec/unit/util/version_checker_spec.rb | 60 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/lib/webmock/util/version_checker.rb b/lib/webmock/util/version_checker.rb index d9830b1a2..e8fa4f9b5 100644 --- a/lib/webmock/util/version_checker.rb +++ b/lib/webmock/util/version_checker.rb @@ -84,12 +84,13 @@ def warn_in_red(text) def compare_version case when @major < @min_major then :too_low + when @major == @min_major && @minor < @min_minor then :too_low + when @major == @min_major && @minor == @min_minor && @patch < @min_patch 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 + when @max_major && @major == @max_major && @max_minor && @minor > @max_minor then :too_high + + else :ok end end diff --git a/spec/unit/util/version_checker_spec.rb b/spec/unit/util/version_checker_spec.rb index c801f469c..ce819da94 100644 --- a/spec/unit/util/version_checker_spec.rb +++ b/spec/unit/util/version_checker_spec.rb @@ -8,18 +8,36 @@ module WebMock checker.check_version! end + it 'prints a warning if the major version is too low - 2' do + checker = VersionChecker.new('foo', '0.7.3', '1.0.0', '2.1') + expect(Kernel).to receive(:warn).with("\e[31mYou are using foo 0.7.3. WebMock supports version >= 1.0.0, < 2.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') expect(Kernel).to 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 minor version is too low - 2' do + checker = VersionChecker.new('foo', '0.7.8', '0.9.7', '1.0.1') + expect(Kernel).to receive(:warn).with("\e[31mYou are using foo 0.7.8. WebMock supports version >= 0.9.7, < 1.1.\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') expect(Kernel).to 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 - 2' do + checker = VersionChecker.new('foo', '0.9.1', '0.9.7', '1.0.1') + expect(Kernel).to receive(:warn).with("\e[31mYou are using foo 0.9.1. WebMock supports version >= 0.9.7, < 1.1.\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') expect(Kernel).to receive(:warn).with("\e[31mYou are using foo 1.0.8. WebMock supports version >= 1.0.10.\e[0m") @@ -32,12 +50,24 @@ module WebMock checker.check_version! end + it 'prints a warning if the major version is too high - 2' do + checker = VersionChecker.new('foo', '2.0.0', '0.9.7', '1.1') + expect(Kernel).to 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') expect(Kernel).to receive(:warn).with(/may not work with this version/) checker.check_version! end + it 'prints a warning if the minor version is too high - 2' do + checker = VersionChecker.new('foo', '1.2.0', '0.9.7', '1.1') + expect(Kernel).to 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') expect(Kernel).not_to receive(:warn) @@ -50,12 +80,42 @@ module WebMock 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 1.0.1 and the version is 0.7.3' do + checker = VersionChecker.new('foo', '0.7.3', '0.6.5', '1.0.1') + expect(Kernel).not_to 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 1.0.1 and the version is 0.7.3' do + checker = VersionChecker.new('foo', '0.7.3', '0.6.5', '1.0.1') + expect(Kernel).not_to 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 1.0.1 and the version is 0.6.5' do + checker = VersionChecker.new('foo', '0.7.3', '0.6.5', '1.0.1') + expect(Kernel).not_to 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 1.0.1 and the version is 1.0.1' do + checker = VersionChecker.new('foo', '0.7.3', '0.6.5', '1.0.1') + expect(Kernel).not_to 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') expect(Kernel).not_to 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.6.5' do + checker = VersionChecker.new('foo', '0.8.3', '0.6.5') + expect(Kernel).not_to receive(:warn) + checker.check_version! + end + it "prints warning if version is unsupported" do checker = VersionChecker.new('foo', '2.0.0', '1.0.0', '3.0', ['2.0.0']) expect(Kernel).to receive(:warn).with(%r{You are using foo 2.0.0. WebMock does not support this version. WebMock supports versions >= 1.0.0, < 3.1, except versions 2.0.0.})