[rubygems/rubygems] Let GemVersionPromoter sort in preferred order directly

So that we don't need to reverse the Array.

https://github.com/rubygems/rubygems/commit/aeea5e2e00
This commit is contained in:
David Rodriguez 2024-03-21 17:55:14 +01:00 коммит произвёл git
Родитель 2b82b7d192
Коммит d69ef1cc52
3 изменённых файлов: 28 добавлений и 28 удалений

Просмотреть файл

@ -59,20 +59,20 @@ module Bundler
a_pre = a.prerelease?
b_pre = b.prerelease?
next -1 if a_pre && !b_pre
next 1 if b_pre && !a_pre
next 1 if a_pre && !b_pre
next -1 if b_pre && !a_pre
end
if major? || locked_version.nil?
a <=> b
b <=> a
elsif either_version_older_than_locked?(a, b, locked_version)
a <=> b
b <=> a
elsif segments_do_not_match?(a, b, :major)
b <=> a
elsif !minor? && segments_do_not_match?(a, b, :minor)
b <=> a
else
a <=> b
elsif !minor? && segments_do_not_match?(a, b, :minor)
a <=> b
else
b <=> a
end
end
post_sort(result, package.unlock?, locked_version)
@ -137,13 +137,13 @@ module Bundler
if unlock || locked_version.nil?
result
else
move_version_to_end(result, locked_version)
move_version_to_beginning(result, locked_version)
end
end
def move_version_to_end(result, version)
def move_version_to_beginning(result, version)
move, keep = result.partition {|s| s.version.to_s == version.to_s }
keep.concat(move)
move.concat(keep)
end
end
end

Просмотреть файл

@ -359,7 +359,7 @@ module Bundler
def sort_versions_by_preferred(package, versions)
if versions.size > 1
@gem_version_promoter.sort_versions(package, versions).reverse
@gem_version_promoter.sort_versions(package, versions)
else
versions
end

Просмотреть файл

@ -33,13 +33,13 @@ RSpec.describe Bundler::GemVersionPromoter do
it "numerically sorts versions" do
versions = sorted_versions(candidates: %w[1.7.7 1.7.8 1.7.9 1.7.15 1.8.0], current: "1.7.8")
expect(versions).to eq %w[1.7.7 1.7.8 1.7.9 1.7.15 1.8.0]
expect(versions).to eq %w[1.8.0 1.7.15 1.7.9 1.7.8 1.7.7]
end
context "with no options" do
it "defaults to level=:major, strict=false, pre=false" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0], current: "0.3.0")
expect(versions).to eq %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0]
expect(versions).to eq %w[2.1.0 2.0.1 1.0.0 0.9.0 0.3.1 0.3.0 0.2.0]
end
end
@ -51,25 +51,25 @@ RSpec.describe Bundler::GemVersionPromoter do
it "keeps downgrades" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0], current: "0.3.0")
expect(versions).to eq %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0]
expect(versions).to eq %w[2.1.0 2.0.1 1.0.0 0.9.0 0.3.1 0.3.0 0.2.0]
end
end
context "when level is minor" do
before { gvp.level = :minor }
it "sorts highest minor within same major in last position" do
it "sorts highest minor within same major in first position" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0], current: "0.3.0")
expect(versions).to eq %w[0.2.0 2.0.1 2.1.0 1.0.0 0.3.0 0.3.1 0.9.0]
expect(versions).to eq %w[0.9.0 0.3.1 0.3.0 1.0.0 2.1.0 2.0.1 0.2.0]
end
end
context "when level is patch" do
before { gvp.level = :patch }
it "sorts highest patch within same minor in last position" do
it "sorts highest patch within same minor in first position" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0], current: "0.3.0")
expect(versions).to eq %w[0.2.0 2.1.0 2.0.1 1.0.0 0.9.0 0.3.0 0.3.1]
expect(versions).to eq %w[0.3.1 0.3.0 0.9.0 1.0.0 2.0.1 2.1.0 0.2.0]
end
end
end
@ -82,25 +82,25 @@ RSpec.describe Bundler::GemVersionPromoter do
it "orders by version" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0], current: "0.3.0")
expect(versions).to eq %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0]
expect(versions).to eq %w[2.1.0 2.0.1 1.0.0 0.9.0 0.3.1 0.3.0 0.2.0]
end
end
context "when level is minor" do
before { gvp.level = :minor }
it "favors downgrades, then upgrades by major descending, minor ascending, patch ascending" do
it "favors minor upgrades, then patch upgrades, then major upgrades, then downgrades" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0], current: "0.3.0")
expect(versions).to eq %w[0.2.0 2.0.1 2.1.0 1.0.0 0.3.0 0.3.1 0.9.0]
expect(versions).to eq %w[0.9.0 0.3.1 0.3.0 1.0.0 2.1.0 2.0.1 0.2.0]
end
end
context "when level is patch" do
before { gvp.level = :patch }
it "favors downgrades, then upgrades by major descending, minor descending, patch ascending" do
it "favors patch upgrades, then minor upgrades, then major upgrades, then downgrades" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.0.1 2.1.0], current: "0.3.0")
expect(versions).to eq %w[0.2.0 2.1.0 2.0.1 1.0.0 0.9.0 0.3.0 0.3.1]
expect(versions).to eq %w[0.3.1 0.3.0 0.9.0 1.0.0 2.0.1 2.1.0 0.2.0]
end
end
end
@ -110,7 +110,7 @@ RSpec.describe Bundler::GemVersionPromoter do
it "sorts regardless of prerelease status" do
versions = sorted_versions(candidates: %w[1.7.7.pre 1.8.0 1.8.1.pre 1.8.1 2.0.0.pre 2.0.0], current: "1.8.0")
expect(versions).to eq %w[1.7.7.pre 1.8.0 1.8.1.pre 1.8.1 2.0.0.pre 2.0.0]
expect(versions).to eq %w[2.0.0 2.0.0.pre 1.8.1 1.8.1.pre 1.8.0 1.7.7.pre]
end
end
@ -119,16 +119,16 @@ RSpec.describe Bundler::GemVersionPromoter do
it "deprioritizes prerelease gems" do
versions = sorted_versions(candidates: %w[1.7.7.pre 1.8.0 1.8.1.pre 1.8.1 2.0.0.pre 2.0.0], current: "1.8.0")
expect(versions).to eq %w[1.7.7.pre 1.8.1.pre 2.0.0.pre 1.8.0 1.8.1 2.0.0]
expect(versions).to eq %w[2.0.0 1.8.1 1.8.0 2.0.0.pre 1.8.1.pre 1.7.7.pre]
end
end
context "when locking and not major" do
before { gvp.level = :minor }
it "keeps the current version last" do
it "keeps the current version first" do
versions = sorted_versions(candidates: %w[0.2.0 0.3.0 0.3.1 0.9.0 1.0.0 2.1.0 2.0.1], current: "0.3.0", locked: ["bar"])
expect(versions.last).to eq("0.3.0")
expect(versions.first).to eq("0.3.0")
end
end
end