Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Monkey patch on Bundler::materialize_for_installation for resolve Bundler::LazySpecification issue #9807

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# typed: false
# frozen_string_literal: true

require "bundler/spec_set"

# monkey patch materialized_for_all_platforms for lazy specification issue resolution
# https://github.com/dependabot/dependabot-core/pull/9807
module BundlerSpecSetPatch
def materialized_for_all_platforms
@specs.map do |s|
next s unless s.is_a?(Bundler::LazySpecification)

s.source.cached!
s.source.remote!
spec = s.materialize_for_installation
raise Bundler::GemNotFound, "Could not find #{s.full_name} in any of the sources" unless spec

spec
end
end
end

Bundler::SpecSet.prepend(BundlerSpecSetPatch)
1 change: 1 addition & 0 deletions bundler/helpers/v2/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require "definition_ruby_version_patch"
require "definition_bundler_version_patch"
require "git_source_patch"
require "definition_bundler_spec_set_patch"

require "functions"

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# typed: false
# frozen_string_literal: true

# rubocop:disable RSpec/FilePath
# rubocop:disable RSpec/SpecFilePathFormat

require "native_spec_helper"
require "shared_contexts"
require "bundler/spec_set"

RSpec.describe Bundler::SpecSet do
let(:primary_source) { instance_double(Bundler::Source::Git) }
let(:secondary_source) { instance_double(Bundler::Source::Path) }
let(:primary_spec_set) do
instance_double(Bundler::LazySpecification, full_name: "foo-1.0.0-x86_64-linux", source: primary_source)
end
let(:secondary_spec_set) do
instance_double(Bundler::LazySpecification, full_name: "foo-1.0.0-arm64-darwin", source: secondary_source)
end

before do
allow(primary_spec_set).to receive(:is_a?).with(Bundler::LazySpecification).and_return(true)
allow(secondary_spec_set).to receive(:is_a?).with(Bundler::LazySpecification).and_return(true)

allow(primary_source).to receive(:cached!)
allow(primary_source).to receive(:remote!)
allow(secondary_source).to receive(:cached!)
allow(secondary_source).to receive(:remote!)

allow(primary_spec_set).to receive(:materialize_for_installation).and_return(primary_spec_set)
allow(secondary_spec_set).to receive(:materialize_for_installation).and_return(secondary_spec_set)
end

describe "#materialized_for_all_platforms" do
context "when cache_all_platforms is enabled" do
let(:spec_set) { described_class.new([primary_spec_set, secondary_spec_set]) }

before do
described_class.prepend(BundlerSpecSetPatch)
end

it "uses cached gems for secondary sources" do
expect(primary_spec_set.source).to receive(:cached!).ordered
expect(primary_spec_set.source).to receive(:remote!).ordered
expect(primary_spec_set).to receive(:materialize_for_installation).and_return(primary_spec_set).ordered

expect(secondary_spec_set.source).to receive(:cached!).ordered
expect(secondary_spec_set.source).to receive(:remote!).ordered
expect(secondary_spec_set).to receive(:materialize_for_installation).and_return(secondary_spec_set).ordered

result = spec_set.materialized_for_all_platforms
expect(result).to include(primary_spec_set, secondary_spec_set)
end

it "raises an error if a gem cannot be found in any of the sources" do
allow(primary_spec_set).to receive(:materialize_for_installation).and_return(nil)

expect do
spec_set.materialized_for_all_platforms
end.to raise_error(Bundler::GemNotFound,
"Could not find foo-1.0.0-x86_64-linux in any of the sources")
end
end
end
end

# rubocop:enable RSpec/FilePath
# rubocop:enable RSpec/SpecFilePathFormat
1 change: 1 addition & 0 deletions bundler/helpers/v2/spec/native_spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
# Bundler monkey patches
require "definition_ruby_version_patch"
require "definition_bundler_version_patch"
require "definition_bundler_spec_set_patch"
require "git_source_patch"

require "functions"
Expand Down