Skip to content

Commit

Permalink
move language in seperate file.
Browse files Browse the repository at this point in the history
create/update specs for helpers, and language
  • Loading branch information
kbukum1 committed Oct 30, 2024
1 parent 8a9053c commit 2081402
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 16 deletions.
1 change: 1 addition & 0 deletions bundler/lib/dependabot/bundler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
require "dependabot/bundler/requirement"
require "dependabot/bundler/version"
require "dependabot/bundler/package_manager"
require "dependabot/bundler/language"

require "dependabot/pull_request_creator/labeler"
Dependabot::PullRequestCreator::Labeler
Expand Down
6 changes: 3 additions & 3 deletions bundler/lib/dependabot/bundler/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module Helpers
# newest version we support
DEFAULT = V2
BUNDLER_MAJOR_VERSION_REGEX = /BUNDLED WITH\s+(?<version>\d+)\./m
RUBY_VERSION_REGEX = /RUBY VERSION\s+ruby\s+([^\s]+)/

sig { params(lockfile: T.nilable(Dependabot::DependencyFile)).returns(String) }
def self.bundler_version(lockfile)
Expand Down Expand Up @@ -63,9 +64,8 @@ def self.ruby_version(gemfile, lockfile)
def self.ruby_version_from_lockfile(lockfile)
return nil unless lockfile

ruby_version = lockfile.content&.match(/RUBY VERSION\s+(?<version>[^\s]+)/)&.named_captures&.fetch(
"version", nil
)
# Updated regex to capture just the version number
ruby_version = lockfile.content&.match(RUBY_VERSION_REGEX)&.captures&.first
ruby_version
end

Expand Down
24 changes: 24 additions & 0 deletions bundler/lib/dependabot/bundler/language.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# typed: strong
# frozen_string_literal: true

require "sorbet-runtime"
require "dependabot/bundler/version"
require "dependabot/ecosystem"

module Dependabot
module Bundler
LANGUAGE = "ruby"

class Language < Dependabot::Ecosystem::VersionManager
extend T::Sig

sig { params(raw_version: String).void }
def initialize(raw_version)
super(
LANGUAGE,
Version.new(raw_version)
)
end
end
end
end
13 changes: 0 additions & 13 deletions bundler/lib/dependabot/bundler/package_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module Dependabot
module Bundler
ECOSYSTEM = "bundler"
PACKAGE_MANAGER = "bundler"
LANGUAGE = "ruby"

# Keep versions in ascending order
SUPPORTED_BUNDLER_VERSIONS = T.let([Version.new("2")].freeze, T::Array[Dependabot::Version])
Expand All @@ -33,17 +32,5 @@ def initialize(raw_version)
)
end
end

class Language < Dependabot::Ecosystem::VersionManager
extend T::Sig

sig { params(raw_version: String).void }
def initialize(raw_version)
super(
LANGUAGE,
Version.new(raw_version)
)
end
end
end
end
43 changes: 43 additions & 0 deletions bundler/spec/dependabot/bundler/helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@
LOCKFILE
end

let(:gemfile) do
Dependabot::DependencyFile.new(name: "Gemfile", content: <<~GEMFILE)
source 'https://rubygems.org'
gem 'rails'
GEMFILE
end

let(:lockfile_with_ruby_version) do
Dependabot::DependencyFile.new(name: "Gemfile.lock", content: <<~LOCKFILE)
RUBY VERSION
ruby 2.7.2
LOCKFILE
end

let(:no_ruby_version_file) { nil }

let(:ruby_version_file) do
Dependabot::DependencyFile.new(name: ".ruby-version", content: "ruby-2.7.1")
end

describe "#bundler_version" do
def described_method(lockfile)
described_class.bundler_version(lockfile)
Expand Down Expand Up @@ -92,4 +112,27 @@ def described_method(lockfile)
expect(described_method(lockfile_bundled_with_future_version)).to eql("3")
end
end

describe "#ruby_version" do
before do
bundler_definition = instance_double(::Bundler::Definition)
ruby_version = instance_double(::Bundler::RubyVersion, gem_version: Gem::Version.new(RUBY_VERSION))
allow(bundler_definition).to receive(:ruby_version).and_return(ruby_version)
allow(described_class).to receive(:build_definition).and_return(bundler_definition)
end

it "returns the Ruby version from the lockfile if available" do
expect(described_class.ruby_version(gemfile, lockfile_with_ruby_version)).to eq("2.7.2")
end

it "returns the Ruby version from the .ruby-version file if no lockfile Ruby version is available" do
allow(described_class).to receive(:ruby_version_from_ruby_version_file).and_return("2.7.1")
expect(described_class.ruby_version(gemfile, no_lockfile)).to eq("2.7.1")
end

it "falls back to the current Ruby version if no other version is found" do
allow(described_class).to receive(:ruby_version_from_ruby_version_file).and_return(nil)
expect(described_class.ruby_version(gemfile, no_lockfile)).to eq(RUBY_VERSION)
end
end
end
49 changes: 49 additions & 0 deletions bundler/spec/dependabot/bundler/language_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# typed: false
# frozen_string_literal: true

require "dependabot/bundler/language"
require "dependabot/ecosystem"
require "spec_helper"

RSpec.describe Dependabot::Bundler::Language do
let(:language) { described_class.new(version) }
let(:version) { "3.0.0" }

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

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

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

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

it "sets the version correctly" do
expect(language.version).to eq(version)
end

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

describe "#unsupported?" do
it "returns false by default as no specific support or deprecation for languages is currently defined" do
expect(language.unsupported?).to be false
end
end

describe "#deprecated?" do
it "returns false by default as no specific deprecation for languages is currently defined" do
expect(language.deprecated?).to be false
end
end
end

0 comments on commit 2081402

Please sign in to comment.