From e4aef8e051ae787b0a569316239b9e9dbd9336d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Rodr=C3=ADguez?= Date: Tue, 27 Jun 2023 17:49:57 +0200 Subject: [PATCH] Implement MetadataFinder for Swift --- swift/lib/dependabot/swift/metadata_finder.rb | 19 +++++ .../dependabot/swift/metadata_finder_spec.rb | 75 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 swift/spec/dependabot/swift/metadata_finder_spec.rb diff --git a/swift/lib/dependabot/swift/metadata_finder.rb b/swift/lib/dependabot/swift/metadata_finder.rb index 86dd09efa625..1c472e47b42a 100644 --- a/swift/lib/dependabot/swift/metadata_finder.rb +++ b/swift/lib/dependabot/swift/metadata_finder.rb @@ -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 diff --git a/swift/spec/dependabot/swift/metadata_finder_spec.rb b/swift/spec/dependabot/swift/metadata_finder_spec.rb new file mode 100644 index 000000000000..12f9abacd4e6 --- /dev/null +++ b/swift/spec/dependabot/swift/metadata_finder_spec.rb @@ -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