Skip to content

Commit

Permalink
Merge pull request #223 from github/autogenerate-data-for-package-lic…
Browse files Browse the repository at this point in the history
…ense

Generate license text when missing for detected license
  • Loading branch information
jonabc authored Nov 9, 2019
2 parents 370ab4d + eed9b06 commit 516908e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
29 changes: 26 additions & 3 deletions lib/licensed/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,12 @@ def license_key
# Returns the license text content from all matched sources
# except the package file, which doesn't contain license text.
def license_contents
matched_files.reject { |f| f == package_file }
.group_by(&:content)
.map { |content, files| { "sources" => license_content_sources(files), "text" => content } }
files = matched_files.reject { |f| f == package_file }
.group_by(&:content)
.map { |content, files| { "sources" => license_content_sources(files), "text" => content } }

files << generated_license_contents if files.empty?
files.compact
end

# Returns legal notices found at the dependency path
Expand Down Expand Up @@ -133,5 +136,25 @@ def license_metadata
"license" => license_key
})
end

# Returns a generated license content source and text for the dependency's
# license if it exists and is not "other"
def generated_license_contents
return unless license
return if license.key == "other"

# strip copyright clauses and any extra newlines
# many package managers don't provide enough information to
# autogenerate a copyright clause
text = license.text.lines
.reject { |l| l =~ Licensee::Matchers::Copyright::REGEX }
.join
.gsub(/\n\n\n/, "\n\n")

{
"sources" => "Auto-generated #{license.spdx_id} license text",
"text" => text
}
end
end
end
38 changes: 37 additions & 1 deletion test/dependency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,49 @@ def mkproject(&block)
end
end

it "does not get license content from package manager file" do
it "autogenerates license content if explicit content is not found" do
mkproject do |dependency|
File.write "project.gemspec", "s.license = 'mit'"

contents = dependency.license_contents.first
assert contents
assert_match /auto-generated/i, contents["sources"]
refute_match /copyright \(c\)/i, contents["text"]

file = Licensee::ProjectFiles::LicenseFile.new(contents["text"])
assert_equal "mit", file.license&.key
end
end

it "does not autogenerate license content for 'other' license key" do
mkproject do |dependency|
File.write "project.gemspec", "s.license = 'other'"

assert_empty dependency.license_contents
end
end

it "does not autogenerate license content for licenses unknown to Licensee" do
mkproject do |dependency|
File.write "project.gemspec", "s.license = 'nit'"

assert_empty dependency.license_contents
end
end

it "does not autogenerate license content if license is not found" do
mkproject do |dependency|
assert_empty dependency.license_contents
end
end

it "does not autogenerate license content if explicit content is set" do
mkproject do |dependency|
File.write "LICENSE", Licensee::License.find("mit").text
refute dependency.license_contents.any? { |c| c["sources"] =~ /auto-generated/i }
end
end

it "gets license from readme" do
mkproject do |dependency|
File.write "README.md", "# License\n" + Licensee::License.find("mit").text
Expand Down

0 comments on commit 516908e

Please sign in to comment.