Skip to content

Commit

Permalink
Merge pull request #306 from github/handle-parse-failures
Browse files Browse the repository at this point in the history
Handle yaml parsing errors when reading dependency records from disk
  • Loading branch information
jonabc authored Sep 23, 2020
2 parents bb8789c + 9542e3a commit 248e470
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/licensed/commands/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def run_dependency(app, source, dependency)
end

evaluate_dependency(app, source, dependency, report)
rescue Licensed::Shell::Error => err
rescue Licensed::DependencyRecord::Error, Licensed::Shell::Error => err
report.errors << err.message
false
end
Expand Down
4 changes: 4 additions & 0 deletions lib/licensed/dependency_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

module Licensed
class DependencyRecord
class Error < StandardError; end

class License
attr_reader :text, :sources
def initialize(content)
Expand Down Expand Up @@ -46,6 +48,8 @@ def self.read(filename)
notices: data.delete("notices"),
metadata: data
)
rescue Psych::SyntaxError => e
raise Licensed::DependencyRecord::Error.new(e.message)
end

def_delegators :@metadata, :[], :[]=
Expand Down
11 changes: 11 additions & 0 deletions test/commands/command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@
end
end

it "catches dependency record errors thrown when evaluating a dependency" do
proc = lambda { |app, source, dep| raise Licensed::DependencyRecord::Error.new("dependency record error") }
refute command.run(evaluate_proc: proc)

reports = command.reporter.report.all_reports.select { |report| report.target.is_a?(Licensed::Dependency) }
refute_empty reports
reports.each do |report|
assert_includes report.errors, "dependency record error"
end
end

it "reports errors found on a dependency" do
dependency_name = "#{apps.first["name"]}.test.dependency"
proc = lambda { |app, source, dep| dep.errors << "error" }
Expand Down
7 changes: 7 additions & 0 deletions test/dependency_record_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
assert_equal ["license1", "license2"], content.licenses.map(&:text)
assert_equal ["notice", "author"], content.notices
end

it "raises an error on invalid YAML" do
File.write(@filename, "name: [")
assert_raises Licensed::DependencyRecord::Error do
Licensed::DependencyRecord.read(@filename)
end
end
end

describe "save" do
Expand Down

0 comments on commit 248e470

Please sign in to comment.