Skip to content

Commit 51d9e92

Browse files
OughtPutsmatzbot
authored andcommitted
[ruby/rubygems] Update spec_set to use lookup
ruby/rubygems@3a90d24e42
1 parent ace687e commit 51d9e92

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

lib/bundler/spec_set.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def -(other)
178178
end
179179

180180
def find_by_name_and_platform(name, platform)
181-
@specs.detect {|spec| spec.name == name && spec.installable_on_platform?(platform) }
181+
lookup[name]&.detect {|spec| spec.installable_on_platform?(platform) }
182182
end
183183

184184
def specs_with_additional_variants_from(other)
@@ -314,7 +314,7 @@ def valid_dependencies?(s)
314314
end
315315

316316
def sorted
317-
@sorted ||= ([@specs.find {|s| s.name == "rake" }] + tsort).compact.uniq
317+
@sorted ||= ([lookup["rake"]&.first] + tsort).compact.uniq
318318
rescue TSort::Cyclic => error
319319
cgems = extract_circular_gems(error)
320320
raise CyclicDependencyError, "Your bundle requires gems that depend" \

spec/bundler/bundler/spec_set_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@
4343
spec = described_class.new(specs).find_by_name_and_platform("b", platform)
4444
expect(spec).to eq platform_spec
4545
end
46+
47+
it "returns nil when the name is not present" do
48+
spec = described_class.new(specs).find_by_name_and_platform("missing", platform)
49+
expect(spec).to be_nil
50+
end
51+
52+
it "returns nil when the name exists but no spec is installable on the requested platform" do
53+
incompatible_platform = Gem::Platform.new("java")
54+
incompatible_spec = build_spec("a", "1.0", incompatible_platform).first
55+
56+
spec = described_class.new([incompatible_spec]).find_by_name_and_platform("a", platform)
57+
expect(spec).to be_nil
58+
end
59+
60+
it "returns the first installable spec for the given name in insertion order" do
61+
later_platform_spec = build_spec("b", "3.0", platform).first
62+
specs = [
63+
platform_spec,
64+
later_platform_spec,
65+
]
66+
67+
spec = described_class.new(specs).find_by_name_and_platform("b", platform)
68+
expect(spec).to eq platform_spec
69+
end
4670
end
4771

4872
describe "#to_a" do
@@ -55,5 +79,17 @@
5579
d-2.0
5680
]
5781
end
82+
83+
it "puts rake first when present" do
84+
specs = [
85+
build_spec("a", "1.0") {|s| s.dep "rake", ">= 0" },
86+
build_spec("rake", "13.0"),
87+
].flatten
88+
89+
expect(described_class.new(specs).to_a.map(&:full_name)).to eq %w[
90+
rake-13.0
91+
a-1.0
92+
]
93+
end
5894
end
5995
end

0 commit comments

Comments
 (0)