Skip to content

Commit

Permalink
Merge pull request dependabot#8835 from etiennebarrie/always-use-ruby…
Browse files Browse the repository at this point in the history
…-version

Always use .ruby-version for Bundler dependency resolution
  • Loading branch information
abdulapopoola authored Jan 29, 2024
2 parents 178f3df + 08ac07e commit 5d66685
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
require "bundler/definition"

module BundlerDefinitionRubyVersionPatch
def ruby_version
super || begin
Bundler::RubyVersion.from_string(File.read(".ruby-version", chomp: true))
rescue SystemCallError
# .ruby-version doesn't exist, fallback to the Ruby Dependabot runs
end
end

def source_requirements
if ruby_version
requested_version = ruby_version.gem_version
Expand Down
1 change: 0 additions & 1 deletion bundler/lib/dependabot/bundler/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def gemspec_directories

def ruby_version_file
return unless gemfile
return unless gemfile.content.include?(".ruby-version")

@ruby_version_file ||=
fetch_file_if_present(".ruby-version")
Expand Down
57 changes: 32 additions & 25 deletions bundler/spec/dependabot/bundler/file_fetcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
end

before { allow(file_fetcher_instance).to receive(:commit).and_return("sha") }
before do
stub_request(:get, File.join(url, ".ruby-version?ref=sha"))
.with(headers: { "Authorization" => "token token" })
.to_return(
status: 200,
body: fixture("github", "ruby_version_content.json"),
headers: { "content-type" => "application/json" }
)
end

context "with a directory" do
let(:directory) { "/test" }
Expand Down Expand Up @@ -63,9 +72,9 @@
end

it "fetches the files as normal" do
expect(file_fetcher_instance.files.count).to eq(2)
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.map(&:name))
.to match_array(%w(Gemfile Gemfile.lock))
.to match_array(%w(Gemfile Gemfile.lock .ruby-version))
end

context "that can't be found" do
Expand Down Expand Up @@ -146,16 +155,6 @@
)
end

before do
stub_request(:get, url + ".ruby-version?ref=sha")
.with(headers: { "Authorization" => "token token" })
.to_return(
status: 200,
body: fixture("github", "ruby_version_content.json"),
headers: { "content-type" => "application/json" }
)
end

it "fetches the ruby-version file" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.map(&:name))
Expand Down Expand Up @@ -191,9 +190,9 @@
end

it "fetches the ruby-version file" do
expect(file_fetcher_instance.files.count).to eq(2)
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.map(&:name))
.to eq(%w(gems.rb gems.locked))
.to eq(%w(gems.rb gems.locked .ruby-version))
end
end

Expand Down Expand Up @@ -246,7 +245,7 @@
end

it "fetches the path dependency" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.count).to eq(4)
expect(file_fetcher_instance.files.map(&:name))
.to include("../constants.rb")
end
Expand Down Expand Up @@ -299,7 +298,7 @@
end

it "fetches gemspec from path dependency" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.count).to eq(4)
expect(file_fetcher_instance.files.map(&:name))
.to include("plugins/bump-core/bump-core.gemspec")
end
Expand Down Expand Up @@ -348,7 +347,7 @@
end

it "fetches gemspec from path dependency" do
expect(file_fetcher_instance.files.count).to eq(4)
expect(file_fetcher_instance.files.count).to eq(5)
expect(file_fetcher_instance.files.map(&:name))
.to include("plugins/bump-core/bump-core/bump-core.gemspec")
expect(file_fetcher_instance.files.map(&:name))
Expand Down Expand Up @@ -403,7 +402,7 @@
end

it "fetches gemspec from path dependency" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.count).to eq(4)
expect(file_fetcher_instance.files.map(&:name))
.to include("plugins/bump-core/bump-core.gemspec")
end
Expand All @@ -418,10 +417,18 @@
body: fixture("github", "contents_ruby_no_lockfile.json"),
headers: { "content-type" => "application/json" }
)

stub_request(:get, url + ".ruby-version?ref=sha")
.with(headers: { "Authorization" => "token token" })
.to_return(
status: 200,
body: fixture("github", "ruby_version_content.json"),
headers: { "content-type" => "application/json" }
)
end

it "fetches gemspec from path dependency" do
expect(file_fetcher_instance.files.count).to eq(2)
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.map(&:name))
.to include("plugins/bump-core/bump-core.gemspec")
end
Expand Down Expand Up @@ -468,7 +475,7 @@
end

it "fetches the .specification from path dependency" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.count).to eq(4)
expect(file_fetcher_instance.files.map(&:name))
.to include("plugins/bump-core/.specification")
end
Expand Down Expand Up @@ -526,7 +533,7 @@
end

it "quietly ignores the error" do
expect(file_fetcher_instance.files.count).to eq(2)
expect(file_fetcher_instance.files.count).to eq(3)
end
end
end
Expand Down Expand Up @@ -693,7 +700,7 @@
end

it "fetches gemspec" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.count).to eq(4)
expect(file_fetcher_instance.files.map(&:name))
.to include("business.gemspec")
end
Expand Down Expand Up @@ -731,7 +738,7 @@
end

it "fetches gemspec" do
expect(file_fetcher_instance.files.count).to eq(2)
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.map(&:name))
.to include("dev/business.gemspec")
end
Expand Down Expand Up @@ -766,7 +773,7 @@
end

it "fetches gemspec" do
expect(file_fetcher_instance.files.count).to eq(2)
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.map(&:name))
.to include("business.gemspec")
end
Expand Down Expand Up @@ -808,7 +815,7 @@
end

it "fetches gemspecs" do
expect(file_fetcher_instance.files.count).to eq(3)
expect(file_fetcher_instance.files.count).to eq(4)
expect(file_fetcher_instance.files.map(&:name))
.to include("business.gemspec")
expect(file_fetcher_instance.files.map(&:name))
Expand Down

0 comments on commit 5d66685

Please sign in to comment.