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

Generate license text when missing for detected license #223

Merged
merged 1 commit into from
Nov 9, 2019
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
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