Skip to content

Commit

Permalink
Addition of test cases and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
sachin-sandhu authored and honeyankit committed May 24, 2024
1 parent 3edc50d commit dedfbbb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

require "bundler/spec_set"

# description needs update
#
# 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|
Expand Down
76 changes: 51 additions & 25 deletions bundler/helpers/v2/spec/definition_bundler_spec_set_patch_spec.rb
Original file line number Diff line number Diff line change
@@ -1,42 +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::LazySpecification do
let(:specification) do
spec = Bundler::LazySpecification.new("example", "1.1.0", nil)
spec.extend(BundlerSpecSetPatch)
spec
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

describe "#default_gem?" do
let(:default_spec_dir) { "/mocked/default/spec/dir" }
before do
allow(Gem).to receive(:default_specifications_dir).and_return(default_spec_dir)
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]) }

context "when the gem is a default gem" do
it "returns true if the loaded_from path is in the default specifications directory" do
specification.loaded_from = File.join(default_spec_dir, "example-1.1.0.gemspec")
expect(specification.default_gem?).to be true
before do
described_class.prepend(BundlerSpecSetPatch)
end
end

context "when loaded_from is nil" do
it "returns false" do
specification.loaded_from = nil
expect(specification.default_gem?).to be false
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
end

context "when the gem is not a default gem" do
it "returns false if the loaded_from path is not in the default specifications directory" do
non_default_dir = "/path/to/non/default/directory"
specification.loaded_from = File.join(non_default_dir, "example-1.1.0.gemspec")
expect(specification.default_gem?).to be false
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

0 comments on commit dedfbbb

Please sign in to comment.