Skip to content

Commit

Permalink
Implement MetadataFinder for Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Jul 18, 2023
1 parent a00afe2 commit 98b7e6d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
19 changes: 19 additions & 0 deletions swift/lib/dependabot/swift/metadata_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ class MetadataFinder < Dependabot::MetadataFinders::Base
private

def look_up_source
case new_source_type
when "git" then find_source_from_git_url
when "registry" then find_source_from_registry
else raise "Unexpected source type: #{new_source_type}"
end
end

def new_source_type
dependency.source_type
end

def find_source_from_git_url
info = dependency.source_details

url = info[:url] || info.fetch("url")
Source.from_url(url)
end

def find_source_from_registry
raise NotImplementedError
end
end
Expand Down
75 changes: 75 additions & 0 deletions swift/spec/dependabot/swift/metadata_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# frozen_string_literal: true

require "spec_helper"
require "dependabot/dependency"
require "dependabot/swift/metadata_finder"
require_common_spec "metadata_finders/shared_examples_for_metadata_finders"

RSpec.describe Dependabot::Swift::MetadataFinder do
it_behaves_like "a dependency metadata finder"

let(:credentials) do
[{
"type" => "git_source",
"host" => "github.com",
"username" => "x-access-token",
"password" => "token"
}]
end

let(:finder) do
described_class.new(dependency: dependency, credentials: credentials)
end

describe "#source_url" do
context "with a direct dependency" do
let(:dependency) do
Dependabot::Dependency.new(
name: "reactiveswift",
version: "7.1.1",
requirements: [{
file: "package.swfit",
requirement: "= 7.1.1",
groups: [],
source: {
"type" => "git",
"url" => "https://github.com/reactivecocoa/reactiveswift",
"ref" => "7.1.1",
"branch" => nil
}
}],
package_manager: "swift"
)
end

it "works" do
expect(finder.source_url).to eq "https://github.com/reactivecocoa/reactiveswift"
end
end

context "with an indirect dependency" do
let(:dependency) do
Dependabot::Dependency.new(
name: "reactiveswift",
version: "7.1.1",
requirements: [],
subdependency_metadata: [
{
source: {
"type" => "git",
"url" => "https://github.com/reactivecocoa/reactiveswift",
"ref" => "7.1.1",
"branch" => nil
}
}
],
package_manager: "swift"
)
end

it "works" do
expect(finder.source_url).to eq "https://github.com/reactivecocoa/reactiveswift"
end
end
end
end

0 comments on commit 98b7e6d

Please sign in to comment.