Skip to content

Commit

Permalink
Update Bundler deprecation unsupported check to use detected version (#…
Browse files Browse the repository at this point in the history
…11222)

* update specs to fix the re-initialite issue
  • Loading branch information
kbukum1 authored Jan 4, 2025
1 parent c9d550b commit eb6d6fa
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 337 deletions.
10 changes: 8 additions & 2 deletions bundler/lib/dependabot/bundler/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ def ecosystem

sig { returns(Ecosystem::VersionManager) }
def package_manager
@package_manager ||= PackageManager.new(bundler_raw_version, package_manager_requirement)
@package_manager ||= PackageManager.new(
detected_version: bundler_version,
raw_version: bundler_raw_version,
requirement: package_manager_requirement
)
end

def package_manager_requirement
Expand Down Expand Up @@ -355,7 +359,9 @@ def imported_ruby_files
def bundler_raw_version
return bundler_raw_version if defined?(@bundler_raw_version)

package_manager = PackageManager.new(bundler_version)
package_manager = PackageManager.new(
detected_version: bundler_version
)

# If the selected version is unsupported, an unsupported error will be raised,
# so there’s no need to attempt retrieving the raw version.
Expand Down
8 changes: 5 additions & 3 deletions bundler/lib/dependabot/bundler/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ class PackageManager < Dependabot::Ecosystem::VersionManager

sig do
params(
raw_version: String,
detected_version: String,
raw_version: T.nilable(String),
requirement: T.nilable(Requirement)
).void
end
def initialize(raw_version, requirement = nil)
def initialize(detected_version:, raw_version: nil, requirement: nil)
super(
name: PACKAGE_MANAGER,
version: Version.new(raw_version),
detected_version: Version.new(detected_version),
version: raw_version ? Version.new(raw_version) : nil,
deprecated_versions: DEPRECATED_BUNDLER_VERSIONS,
supported_versions: SUPPORTED_BUNDLER_VERSIONS,
requirement: requirement,
Expand Down
157 changes: 78 additions & 79 deletions bundler/spec/dependabot/bundler/package_manager_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,41 @@
require "spec_helper"

RSpec.describe Dependabot::Bundler::PackageManager do
let(:package_manager) { described_class.new(version, requirement) }
let(:package_manager) do
described_class.new(
detected_version: detected_version,
raw_version: raw_version,
requirement: requirement
)
end
let(:requirement) { nil }

describe "#initialize" do
context "when version is a String" do
let(:version) { "2" }

it "sets the version correctly" do
expect(package_manager.version).to eq(Dependabot::Bundler::Version.new(version))
end

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

it "sets the deprecated_versions correctly" do
expect(package_manager.deprecated_versions).to eq(Dependabot::Bundler::DEPRECATED_BUNDLER_VERSIONS)
end

it "sets the supported_versions correctly" do
expect(package_manager.supported_versions).to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
end
end
context "when versions are set" do
let(:detected_version) { "2" }
let(:raw_version) { "2.0.1" }

context "when version is a Dependabot::Bundler::Version" do
let(:version) { "2" }

it "sets the version correctly" do
expect(package_manager.version).to eq(version)
it "sets detected and raw versions correctly" do
expect(package_manager.detected_version).to eq(Dependabot::Bundler::Version.new(detected_version))
expect(package_manager.version).to eq(Dependabot::Bundler::Version.new(raw_version))
end

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

it "sets the deprecated_versions correctly" do
it "sets deprecated_versions correctly" do
expect(package_manager.deprecated_versions).to eq(Dependabot::Bundler::DEPRECATED_BUNDLER_VERSIONS)
end

it "sets the supported_versions correctly" do
it "sets supported_versions correctly" do
expect(package_manager.supported_versions).to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
end
end

context "when a requirement is provided" do
let(:version) { "2.1" }
let(:detected_version) { "2.1" }
let(:raw_version) { "2.1.3" }
let(:requirement) { Dependabot::Bundler::Requirement.new(">= 1.12.0, ~> 2.3.0") }

it "sets the requirement correctly" do
Expand All @@ -66,101 +55,111 @@
expect(package_manager.requirement.max_version).to eq(Dependabot::Version.new("2.4.0"))
end
end
end

context "when a single minimum constraint is provided" do
let(:version) { "2.1" }
let(:requirement) { Dependabot::Bundler::Requirement.new(">= 1.5") }

it "sets the requirement correctly" do
expect(package_manager.requirement.to_s).to eq(">= 1.5")
end

it "calculates the correct min_version" do
expect(package_manager.requirement.min_version).to eq(Dependabot::Version.new("1.5"))
end
describe "#deprecated?" do
context "when detected_version is deprecated but raw_version is not" do
let(:detected_version) { "1" }
let(:raw_version) { "2.0.1" }

it "returns nil for max_version" do
expect(package_manager.requirement.max_version).to be_nil
it "returns false because no deprecated versions exist" do
allow(package_manager).to receive(:unsupported?).and_return(false)
expect(package_manager.deprecated?).to be false
end
end

context "when multiple maximum constraints are provided" do
let(:version) { "2.1" }
let(:requirement) { Dependabot::Bundler::Requirement.new("<= 2.5, < 3.0") }
context "when detected_version and raw_version are both deprecated" do
let(:detected_version) { "1" }
let(:raw_version) { "1.0.3" }

it "sets the requirement correctly" do
expect(package_manager.requirement.to_s).to eq("<= 2.5, < 3.0")
end

it "calculates the correct max_version" do
expect(package_manager.requirement.max_version).to eq(Dependabot::Version.new("2.5"))
end

it "returns nil for min_version" do
expect(package_manager.requirement.min_version).to be_nil
it "returns false because no deprecated versions exist" do
allow(package_manager).to receive(:unsupported?).and_return(false)
expect(package_manager.deprecated?).to be false
end
end
end

describe "SUPPORTED_BUNDLER_VERSIONS" do
it "is in ascending order" do
expect(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
.to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS.sort)
end
end

describe "#deprecated?" do
context "when version is deprecated but not unsupported" do
let(:version) { "1" }
context "when detected_version is unsupported" do
let(:detected_version) { "0.9" }
let(:raw_version) { "1.0.4" }

it "returns true" do
allow(package_manager).to receive_messages(deprecated?: true)
expect(package_manager.deprecated?).to be true
it "returns false, as unsupported takes precedence" do
expect(package_manager.deprecated?).to be false
end
end

context "when version is unsupported" do
let(:version) { "0.9" }
context "when raw_version is nil" do
let(:detected_version) { "1" }
let(:raw_version) { nil }

it "returns false, as unsupported takes precedence" do
it "returns false because no deprecated versions exist" do
allow(package_manager).to receive(:unsupported?).and_return(false)
expect(package_manager.deprecated?).to be false
end
end
end

describe "#unsupported?" do
context "when version is supported" do
let(:version) { "2" }
context "when detected_version is supported" do
let(:detected_version) { "2" }
let(:raw_version) { "2.1.3" }

it "returns false" do
expect(package_manager.unsupported?).to be false
end
end

context "when version is not supported" do
let(:version) { "0.9" }
context "when detected_version is unsupported" do
let(:detected_version) { "0.9" }
let(:raw_version) { "0.9.2" }

it "returns true" do
expect(package_manager.unsupported?).to be true
end
end

context "when raw_version is nil" do
let(:detected_version) { "0.9" }
let(:raw_version) { nil }

it "returns true based on detected_version" do
expect(package_manager.unsupported?).to be true
end
end
end

describe "#raise_if_unsupported!" do
context "when version is unsupported" do
let(:version) { "0.9" }
context "when detected_version is unsupported" do
let(:detected_version) { "0.9" }
let(:raw_version) { "0.9.2" }

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

context "when version is supported" do
let(:version) { "2.1" }
context "when detected_version is supported" do
let(:detected_version) { "2.1" }
let(:raw_version) { "2.1.4" }

it "does not raise an error" do
expect { package_manager.raise_if_unsupported! }.not_to raise_error
end
end

context "when raw_version is nil but detected_version is unsupported" do
let(:detected_version) { "0.9" }
let(:raw_version) { nil }

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

describe "SUPPORTED_BUNDLER_VERSIONS" do
it "is in ascending order" do
expect(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS)
.to eq(Dependabot::Bundler::SUPPORTED_BUNDLER_VERSIONS.sort)
end
end
end
Loading

0 comments on commit eb6d6fa

Please sign in to comment.