Skip to content

Commit eb6d6fa

Browse files
authored
Update Bundler deprecation unsupported check to use detected version (#11222)
* update specs to fix the re-initialite issue
1 parent c9d550b commit eb6d6fa

File tree

8 files changed

+490
-337
lines changed

8 files changed

+490
-337
lines changed

bundler/lib/dependabot/bundler/file_parser.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ def ecosystem
5050

5151
sig { returns(Ecosystem::VersionManager) }
5252
def package_manager
53-
@package_manager ||= PackageManager.new(bundler_raw_version, package_manager_requirement)
53+
@package_manager ||= PackageManager.new(
54+
detected_version: bundler_version,
55+
raw_version: bundler_raw_version,
56+
requirement: package_manager_requirement
57+
)
5458
end
5559

5660
def package_manager_requirement
@@ -355,7 +359,9 @@ def imported_ruby_files
355359
def bundler_raw_version
356360
return bundler_raw_version if defined?(@bundler_raw_version)
357361

358-
package_manager = PackageManager.new(bundler_version)
362+
package_manager = PackageManager.new(
363+
detected_version: bundler_version
364+
)
359365

360366
# If the selected version is unsupported, an unsupported error will be raised,
361367
# so there’s no need to attempt retrieving the raw version.

bundler/lib/dependabot/bundler/package_manager.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,16 @@ class PackageManager < Dependabot::Ecosystem::VersionManager
2525

2626
sig do
2727
params(
28-
raw_version: String,
28+
detected_version: String,
29+
raw_version: T.nilable(String),
2930
requirement: T.nilable(Requirement)
3031
).void
3132
end
32-
def initialize(raw_version, requirement = nil)
33+
def initialize(detected_version:, raw_version: nil, requirement: nil)
3334
super(
3435
name: PACKAGE_MANAGER,
35-
version: Version.new(raw_version),
36+
detected_version: Version.new(detected_version),
37+
version: raw_version ? Version.new(raw_version) : nil,
3638
deprecated_versions: DEPRECATED_BUNDLER_VERSIONS,
3739
supported_versions: SUPPORTED_BUNDLER_VERSIONS,
3840
requirement: requirement,

bundler/spec/dependabot/bundler/package_manager_spec.rb

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,41 @@
66
require "spec_helper"
77

88
RSpec.describe Dependabot::Bundler::PackageManager do
9-
let(:package_manager) { described_class.new(version, requirement) }
9+
let(:package_manager) do
10+
described_class.new(
11+
detected_version: detected_version,
12+
raw_version: raw_version,
13+
requirement: requirement
14+
)
15+
end
1016
let(:requirement) { nil }
1117

1218
describe "#initialize" do
13-
context "when version is a String" do
14-
let(:version) { "2" }
15-
16-
it "sets the version correctly" do
17-
expect(package_manager.version).to eq(Dependabot::Bundler::Version.new(version))
18-
end
19-
20-
it "sets the name correctly" do
21-
expect(package_manager.name).to eq(Dependabot::Bundler::PACKAGE_MANAGER)
22-
end
23-
24-
it "sets the deprecated_versions correctly" do
25-
expect(package_manager.deprecated_versions).to eq(Dependabot::Bundler::DEPRECATED_BUNDLER_VERSIONS)
26-
end
27-
28-
it "sets the supported_versions correctly" do
29-
expect(package_manager.supported_versions).to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
30-
end
31-
end
19+
context "when versions are set" do
20+
let(:detected_version) { "2" }
21+
let(:raw_version) { "2.0.1" }
3222

33-
context "when version is a Dependabot::Bundler::Version" do
34-
let(:version) { "2" }
35-
36-
it "sets the version correctly" do
37-
expect(package_manager.version).to eq(version)
23+
it "sets detected and raw versions correctly" do
24+
expect(package_manager.detected_version).to eq(Dependabot::Bundler::Version.new(detected_version))
25+
expect(package_manager.version).to eq(Dependabot::Bundler::Version.new(raw_version))
3826
end
3927

4028
it "sets the name correctly" do
4129
expect(package_manager.name).to eq(Dependabot::Bundler::PACKAGE_MANAGER)
4230
end
4331

44-
it "sets the deprecated_versions correctly" do
32+
it "sets deprecated_versions correctly" do
4533
expect(package_manager.deprecated_versions).to eq(Dependabot::Bundler::DEPRECATED_BUNDLER_VERSIONS)
4634
end
4735

48-
it "sets the supported_versions correctly" do
36+
it "sets supported_versions correctly" do
4937
expect(package_manager.supported_versions).to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
5038
end
5139
end
5240

5341
context "when a requirement is provided" do
54-
let(:version) { "2.1" }
42+
let(:detected_version) { "2.1" }
43+
let(:raw_version) { "2.1.3" }
5544
let(:requirement) { Dependabot::Bundler::Requirement.new(">= 1.12.0, ~> 2.3.0") }
5645

5746
it "sets the requirement correctly" do
@@ -66,101 +55,111 @@
6655
expect(package_manager.requirement.max_version).to eq(Dependabot::Version.new("2.4.0"))
6756
end
6857
end
58+
end
6959

70-
context "when a single minimum constraint is provided" do
71-
let(:version) { "2.1" }
72-
let(:requirement) { Dependabot::Bundler::Requirement.new(">= 1.5") }
73-
74-
it "sets the requirement correctly" do
75-
expect(package_manager.requirement.to_s).to eq(">= 1.5")
76-
end
77-
78-
it "calculates the correct min_version" do
79-
expect(package_manager.requirement.min_version).to eq(Dependabot::Version.new("1.5"))
80-
end
60+
describe "#deprecated?" do
61+
context "when detected_version is deprecated but raw_version is not" do
62+
let(:detected_version) { "1" }
63+
let(:raw_version) { "2.0.1" }
8164

82-
it "returns nil for max_version" do
83-
expect(package_manager.requirement.max_version).to be_nil
65+
it "returns false because no deprecated versions exist" do
66+
allow(package_manager).to receive(:unsupported?).and_return(false)
67+
expect(package_manager.deprecated?).to be false
8468
end
8569
end
8670

87-
context "when multiple maximum constraints are provided" do
88-
let(:version) { "2.1" }
89-
let(:requirement) { Dependabot::Bundler::Requirement.new("<= 2.5, < 3.0") }
71+
context "when detected_version and raw_version are both deprecated" do
72+
let(:detected_version) { "1" }
73+
let(:raw_version) { "1.0.3" }
9074

91-
it "sets the requirement correctly" do
92-
expect(package_manager.requirement.to_s).to eq("<= 2.5, < 3.0")
93-
end
94-
95-
it "calculates the correct max_version" do
96-
expect(package_manager.requirement.max_version).to eq(Dependabot::Version.new("2.5"))
97-
end
98-
99-
it "returns nil for min_version" do
100-
expect(package_manager.requirement.min_version).to be_nil
75+
it "returns false because no deprecated versions exist" do
76+
allow(package_manager).to receive(:unsupported?).and_return(false)
77+
expect(package_manager.deprecated?).to be false
10178
end
10279
end
103-
end
104-
105-
describe "SUPPORTED_BUNDLER_VERSIONS" do
106-
it "is in ascending order" do
107-
expect(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
108-
.to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS.sort)
109-
end
110-
end
11180

112-
describe "#deprecated?" do
113-
context "when version is deprecated but not unsupported" do
114-
let(:version) { "1" }
81+
context "when detected_version is unsupported" do
82+
let(:detected_version) { "0.9" }
83+
let(:raw_version) { "1.0.4" }
11584

116-
it "returns true" do
117-
allow(package_manager).to receive_messages(deprecated?: true)
118-
expect(package_manager.deprecated?).to be true
85+
it "returns false, as unsupported takes precedence" do
86+
expect(package_manager.deprecated?).to be false
11987
end
12088
end
12189

122-
context "when version is unsupported" do
123-
let(:version) { "0.9" }
90+
context "when raw_version is nil" do
91+
let(:detected_version) { "1" }
92+
let(:raw_version) { nil }
12493

125-
it "returns false, as unsupported takes precedence" do
94+
it "returns false because no deprecated versions exist" do
95+
allow(package_manager).to receive(:unsupported?).and_return(false)
12696
expect(package_manager.deprecated?).to be false
12797
end
12898
end
12999
end
130100

131101
describe "#unsupported?" do
132-
context "when version is supported" do
133-
let(:version) { "2" }
102+
context "when detected_version is supported" do
103+
let(:detected_version) { "2" }
104+
let(:raw_version) { "2.1.3" }
134105

135106
it "returns false" do
136107
expect(package_manager.unsupported?).to be false
137108
end
138109
end
139110

140-
context "when version is not supported" do
141-
let(:version) { "0.9" }
111+
context "when detected_version is unsupported" do
112+
let(:detected_version) { "0.9" }
113+
let(:raw_version) { "0.9.2" }
142114

143115
it "returns true" do
144116
expect(package_manager.unsupported?).to be true
145117
end
146118
end
119+
120+
context "when raw_version is nil" do
121+
let(:detected_version) { "0.9" }
122+
let(:raw_version) { nil }
123+
124+
it "returns true based on detected_version" do
125+
expect(package_manager.unsupported?).to be true
126+
end
127+
end
147128
end
148129

149130
describe "#raise_if_unsupported!" do
150-
context "when version is unsupported" do
151-
let(:version) { "0.9" }
131+
context "when detected_version is unsupported" do
132+
let(:detected_version) { "0.9" }
133+
let(:raw_version) { "0.9.2" }
152134

153135
it "raises a ToolVersionNotSupported error" do
154136
expect { package_manager.raise_if_unsupported! }.to raise_error(Dependabot::ToolVersionNotSupported)
155137
end
156138
end
157139

158-
context "when version is supported" do
159-
let(:version) { "2.1" }
140+
context "when detected_version is supported" do
141+
let(:detected_version) { "2.1" }
142+
let(:raw_version) { "2.1.4" }
160143

161144
it "does not raise an error" do
162145
expect { package_manager.raise_if_unsupported! }.not_to raise_error
163146
end
164147
end
148+
149+
context "when raw_version is nil but detected_version is unsupported" do
150+
let(:detected_version) { "0.9" }
151+
let(:raw_version) { nil }
152+
153+
it "raises a ToolVersionNotSupported error" do
154+
expect { package_manager.raise_if_unsupported! }.to raise_error(Dependabot::ToolVersionNotSupported)
155+
end
156+
end
157+
end
158+
159+
describe "SUPPORTED_BUNDLER_VERSIONS" do
160+
it "is in ascending order" do
161+
expect(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
162+
.to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS.sort)
163+
end
165164
end
166165
end

0 commit comments

Comments
 (0)