Skip to content

Commit

Permalink
Handle errors when constructing dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
mattt committed May 25, 2021
1 parent 5b44c60 commit 4072e1a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/licensed/sources/swift.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@ def enabled?

def enumerate_dependencies
pins.map { |pin|
errors = []

begin
name = pin["package"]
version = pin.dig("state", "version")
path = dependency_path_for_url(pin["repositoryURL"])
rescue => e
errors << e
end

Dependency.new(
name: pin["package"],
path: dependency_path_for_url(pin["repositoryURL"]),
version: pin.dig("state", "version")
name: name,
path: path,
version: version,
errors: errors
)
}
end
Expand All @@ -31,8 +42,8 @@ def pins
@pins = begin
json = JSON.parse(File.read(package_resolved_file_path))
json.dig("object", "pins")
rescue JSON::ParserError => e
message = "Licensed was unable to parse the Package.resolved file. JSON Error: #{e.message}"
rescue => e
message = "Licensed was unable to read the Package.resolved file. Error: #{e.message}"
raise Licensed::Sources::Source::Error, message
end
end
Expand Down
32 changes: 32 additions & 0 deletions test/sources/swift_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true
require "test_helper"
require "tmpdir"
require "pp"

if Licensed::Shell.tool_available?("swift")
describe Licensed::Sources::Swift do
Expand Down Expand Up @@ -48,6 +49,37 @@
refute dep
end
end

it "handles invalid repositoryURL field" do
source.stubs(:pins).returns(
JSON.parse <<-JSON
[{
"package": "Invalid",
"repositoryURL": "Invalid",
"state": {
"version": "1.0.0"
}
}]
JSON
)

dep = source.enumerate_dependencies.find { |d| d.name == "Invalid" }
assert dep
assert dep.errors
end

it "handles invalid Package.resolved file" do
Dir.mktmpdir do |dir|
FileUtils.cp_r(fixtures, dir)
File.write(File.join(dir, "Package.resolved"), %("Invalid"))

Dir.chdir(dir) do
assert_raises ::Licensed::Sources::Source::Error do
source.enumerate_dependencies
end
end
end
end
end
end
end

0 comments on commit 4072e1a

Please sign in to comment.