diff --git a/lib/licensed/dependency.rb b/lib/licensed/dependency.rb index f3c3222b..83ccbc57 100644 --- a/lib/licensed/dependency.rb +++ b/lib/licensed/dependency.rb @@ -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 @@ -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 diff --git a/test/dependency_test.rb b/test/dependency_test.rb index 908634ff..b012e43d 100644 --- a/test/dependency_test.rb +++ b/test/dependency_test.rb @@ -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