diff --git a/lib/bundler/patch.rb b/lib/bundler/patch.rb index 101a4cd..0bfec62 100644 --- a/lib/bundler/patch.rb +++ b/lib/bundler/patch.rb @@ -5,6 +5,7 @@ module Patch end end +require 'bundler/patch/new_version_calculator' require 'bundler/patch/updater' require 'bundler/patch/gemfile' require 'bundler/patch/ruby_version' diff --git a/lib/bundler/patch/new_version_calculator.rb b/lib/bundler/patch/new_version_calculator.rb new file mode 100644 index 0000000..e3de5ee --- /dev/null +++ b/lib/bundler/patch/new_version_calculator.rb @@ -0,0 +1,26 @@ +class NewVersionCalculator + def initialize(old_version, patched_versions) + @old_version = old_version + @patched_versions = patched_versions + end + + def run + return old_version if patched_versions.include?(old_version) + + candidates[candidates.index(old_version) + 1] + end + + private + + attr_reader :old_version, :patched_versions + + def candidates + [ + patched_versions, old_version + ].flatten(1).select { |version| first_part(version) == first_part(old_version) }.sort + end + + def first_part(version) + version.split(/\./).first + end +end diff --git a/lib/bundler/patch/updater.rb b/lib/bundler/patch/updater.rb index 6b25df9..1aeb499 100644 --- a/lib/bundler/patch/updater.rb +++ b/lib/bundler/patch/updater.rb @@ -17,14 +17,7 @@ def target_path_fn end def calc_new_version(old_version) - old = old_version - all = @patched_versions.dup - return old_version if all.include?(old) - - all << old - all.sort! - all.delete_if { |v| v.split(/\./).first != old.split(/\./).first } # strip non-matching major revs - all[all.index(old) + 1] + NewVersionCalculator.new(old_version, @patched_versions).run end def file_replace diff --git a/spec/bundler/unit/new_version_calculator_spec.rb b/spec/bundler/unit/new_version_calculator_spec.rb new file mode 100644 index 0000000..b24b550 --- /dev/null +++ b/spec/bundler/unit/new_version_calculator_spec.rb @@ -0,0 +1,79 @@ +require_relative '../../spec_helper' + +describe NewVersionCalculator do + describe '#run' do + let(:candidates) { %w(1.9.3-p550 2.1.4 ruby-2.1.4-p265 jruby-1.7.16.1) } + + def expect(old_version:, patched_versions: candidates) + NewVersionCalculator.new(old_version, patched_versions).run + end + + context 'ruby version' do + it 'upgrades from 1.7 to 1.9.3-p550' do + expect(old_version: '1.7').should eq '1.9.3-p550' + end + + it 'upgrades from 1.8 to 1.9.3-p550' do + expect(old_version: '1.8').should eq '1.9.3-p550' + end + + it 'upgrades from 1.9 to 1.9.3-p550' do + expect(old_version: '1.9').should eq '1.9.3-p550' + end + + it 'upgrades 1.9.3-p484 to 1.9.3-p550' do + expect(old_version: '1.9.3-p484').should eq '1.9.3-p550' + end + + it 'upgrades from 2 to 2.1.4' do + expect(old_version: '2').should eq '2.1.4' + end + + it 'upgrades from 2.0.0-p95 to 2.1.4' do + expect(old_version: '2.0.0-p95').should eq '2.1.4' + end + + it 'upgrades 2.1.2 to 2.1.4' do + expect(old_version: '2.1.2').should eq '2.1.4' + end + + it 'upgrades from 2.1.2-p95 to 2.1.4' do + expect(old_version: '2.1.2-p95').should eq '2.1.4' + end + + it 'upgrades from ruby-2.1.2 to ruby-2.1.4-p265' do + expect(old_version: 'ruby-2.1.2').should eq 'ruby-2.1.4-p265' + end + + it 'upgrades from ruby-2.1.2-p0 to ruby-2.1.4-p265' do + expect(old_version: 'ruby-2.1.2-p0').should eq 'ruby-2.1.4-p265' + end + + it 'upgrades from ruby-2.1.2-p95 to ruby-2.1.4-p265' do + expect(old_version: 'ruby-2.1.2-p95').should eq 'ruby-2.1.4-p265' + end + + it 'upgrades from jruby-1.6.5 to jruby-1.7.16.1' do + expect(old_version: 'jruby-1.6.5').should eq 'jruby-1.7.16.1' + end + + it 'upgrades from jruby-1.7 to jruby-1.7.16.1' do + expect(old_version: 'jruby-1.7').should eq 'jruby-1.7.16.1' + end + + it 'upgrades jruby-1.7.16 to jruby-1.7.16.1' do + expect(old_version: 'jruby-1.7.16').should eq 'jruby-1.7.16.1' + end + end + + context 'gem version' do + it 'do not upgrade to major version' do + expect(old_version: '2.4', patched_versions: %w(3.1.1)).should eq nil + end + + it 'upgrade from rails 3.2.2 to 3.2.22.2' do + expect(old_version: '3.2.2', patched_versions: %w(3.2.22.2)).should eq '3.2.22.2' + end + end + end +end diff --git a/spec/bundler/unit/updater_spec.rb b/spec/bundler/unit/updater_spec.rb index 795eeaa..5dad367 100644 --- a/spec/bundler/unit/updater_spec.rb +++ b/spec/bundler/unit/updater_spec.rb @@ -3,38 +3,5 @@ require 'fileutils' describe UpdateSpec do - describe 'calc_new_version' do - describe 'ruby versions' do - before do - patched_versions = %w(1.9.3-p550 2.1.4 ruby-2.1.4-p265 jruby-1.7.16.1) - @u = UpdateSpec.new(patched_versions: patched_versions) - end - - it 'ruby versions' do - @u.calc_new_version('1.8').should == '1.9.3-p550' - @u.calc_new_version('1.9').should == '1.9.3-p550' - @u.calc_new_version('1.9.3-p484').should == '1.9.3-p550' - @u.calc_new_version('2.0.0-p95').should == '2.1.4' - @u.calc_new_version('2').should == '2.1.4' - @u.calc_new_version('2.1.2').should == '2.1.4' - @u.calc_new_version('2.1.2-p95').should == '2.1.4' - @u.calc_new_version('jruby-1.7').should == 'jruby-1.7.16.1' - @u.calc_new_version('jruby-1.6.5').should == 'jruby-1.7.16.1' - @u.calc_new_version('1.7').should == '1.9.3-p550' - @u.calc_new_version('ruby-2.1.2-p95').should == 'ruby-2.1.4-p265' - @u.calc_new_version('ruby-2.1.2-p0').should == 'ruby-2.1.4-p265' - @u.calc_new_version('ruby-2.1.2').should == 'ruby-2.1.4-p265' - end - end - - describe 'gem versions' do - it 'should stay put on major version upgrade' do - # major version should mean breaking changes, so don't do it. - @u = UpdateSpec.new(patched_versions: %w(3.1.1)) - @u.calc_new_version('2.4').should == nil - end - end - end - it 'should not dump output on test run' end