Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that nuget dependency path exists before reading #280

Merged
merged 2 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/licensed/sources/nuget.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ class NuGetDependency < Licensed::Dependency
PROJECT_URL_REGEX = /<projectUrl>\s*(.*)\s*<\/projectUrl>/ix.freeze
PROJECT_DESC_REGEX = /<description>\s*(.*)\s*<\/description>/ix.freeze

def initialize(name:, version:, path:, search_root: nil, metadata: {}, errors: [])
super(name: name, version: version, path: path, search_root: search_root, metadata: metadata, errors: errors)
@metadata["homepage"] = project_url if project_url
@metadata["summary"] = description if description
# Returns the metadata that represents this dependency. This metadata
# is written to YAML in the dependencys cached text file
def license_metadata
super.tap do |record_metadata|
record_metadata["homepage"] = project_url if project_url
record_metadata["summary"] = description if description
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I wanted to do something like this but I think wasn't sure if it was cool to override license_metadata. Also TIL about tap, thanks for the knowledge 👍

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking of it in terms on C# is it akin to overriding virtual property? That is:

  1. Get property value from base class
  2. Assign two metadata values
  3. Return modified property

Copy link
Contributor Author

@jonabc jonabc Jun 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paveliak yup 👍 . In ruby all methods can be overridden by default. There is no special syntax like virtual and override which explicitly tell which methods can be overridden and which methods are overriding base classes. The call to super here is equivalent to calling base.licensed_metadata() in c#.

end

def nuspec_path
Expand All @@ -29,23 +32,26 @@ def nuspec_path
end

def nuspec_contents
return unless nuspec_path
@nuspec_contents ||= File.read(nuspec_path)
return @nuspec_contents if defined?(@nuspec_contents)
@nuspec_contents = begin
return unless nuspec_path && File.exist?(nuspec_path)
File.read(nuspec_path)
end
end

def project_url
return @project_url if defined?(@project_url)
return unless nuspec_contents
@project_url = begin
return unless nuspec_contents
match = nuspec_contents.match PROJECT_URL_REGEX
match[1] if match && match[1]
end
end

def description
return @description if defined?(@description)
return unless nuspec_contents
@description = begin
return unless nuspec_contents
match = nuspec_contents.match PROJECT_DESC_REGEX
match[1] if match && match[1]
end
Expand Down
15 changes: 15 additions & 0 deletions test/sources/nuget_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@
end

describe Licensed::Sources::NuGet::NuGetDependency do
it "does not error for paths that don't exist" do
path = Dir.mktmpdir
FileUtils.rm_rf(path)

dep = Licensed::Sources::NuGet::NuGetDependency.new(
name: "test",
version: "1.0",
path: path,
metadata: {
"name" => "test"
}
)
assert dep.record
end

describe "retreive license" do
it "caches downloaded urls" do
response = Net::HTTPSuccess.new(1.0, "200", "OK")
Expand Down