Skip to content

Commit

Permalink
more conservatively indicate single license, following licensee
Browse files Browse the repository at this point in the history
leave one convenience: pull license text from github if no local
text and license on github is the same as asserted in package manager
  • Loading branch information
mlinksva committed Jul 10, 2018
1 parent 169ae44 commit ac4b4ce
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 46 deletions.
25 changes: 3 additions & 22 deletions lib/licensed/dependency.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,7 @@ def reset_license!

# Finds and returns a license from the Licensee::FSProject for this dependency.
def project_license
@project_license ||= if project.license && !project.license.other?
# if Licensee has determined a project to have a specific license, use it!
project.license
elsif project.license_files.map(&:license).uniq.size > 1
# if there are multiple license types found from license files,
# return 'other'
Licensee::License.find("other")
else
# check other project files if we haven't yet found a license type
project.licenses.reject(&:other?).first
end
@project_license ||= project.license
end

# Returns a Licensee::LicenseFile with the content of the license in the
Expand All @@ -99,25 +89,16 @@ def remote_license_file
# from the local LICENSE-type files, remote LICENSE, or the README, in that order
def license_text
content_files = Array(project.license_files)
content_files << remote_license_file if content_files.empty? && remote_license_file
content_files << remote_license_file if content_files.empty? && remote_license_file && remote_license_file.license.key == license_key
content_files << project.readme_file if content_files.empty? && project.readme_file
content_files.map(&:content).join("\n#{LICENSE_SEPARATOR}\n")
end

# Returns a string representing the project's license
def license_key
if project_license && !project_license.other?
# return a non-other license found from the Licensee
if project_license
project_license.key
elsif remote_license_file && !remote_license_file.license.other?
# return a non-other license found from the remote source
remote_license_file.license.key
elsif project.license || remote_license_file
# if a license was otherwise found but couldn't be identified to a
# single license, return "other"
"other"
else
# no license found
"none"
end
end
Expand Down
45 changes: 21 additions & 24 deletions test/dependency_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,6 @@ def mkproject(&block)
end
end

it "gets license from github" do
Licensed.use_github = true

VCR.use_cassette("sshirokov/csgtool/license") do
dependency = Licensed::Dependency.new(Dir.tmpdir, {
"homepage" => "https://github.com/sshirokov/csgtool"
})
dependency.detect_license!

assert_equal "mit", dependency["license"]
assert_match(/Yaroslav Shirokov/, dependency.text)
end
end

it "gets license from package manager" do
mkproject do |dependency|
File.write "project.gemspec", "s.license = 'mit'"
Expand All @@ -53,17 +39,17 @@ def mkproject(&block)
end
end

it "pulls license from package manager if LICENSE file is other" do
it "package manager does not override if LICENSE file is other" do
mkproject do |dependency|
File.write "LICENSE.md", "See project.gemspec"
File.write "project.gemspec", "s.license = 'mit'"
dependency.detect_license!

assert_equal "mit", dependency["license"]
assert_equal "other", dependency["license"]
end
end

it "pulls license from README if LICENSE and package manager are other" do
it "pulls license from README if package manager has no license assertion" do
mkproject do |dependency|
File.write "project.gemspec", "foo"
File.write "README.md", "# License\n" + Licensee::License.find("mit").text
Expand All @@ -74,17 +60,13 @@ def mkproject(&block)
end
end

it "pulls license from GitHub if local sources are all other" do
it "pulls license text from GitHub if no local license text" do
mkproject do |dependency|
File.write "LICENSE.md", "See README"
File.write "project.gemspec", "foo"
File.write "README.txt", "# License\n" + "The Remote MIT license"
File.write "project.gemspec", "s.license = 'mit'"
Licensed.use_github = true

VCR.use_cassette("sshirokov/csgtool/license") do
dependency = Licensed::Dependency.new(Dir.tmpdir, {
"homepage" => "https://github.com/sshirokov/csgtool"
})
dependency["homepage"] = "https://github.com/sshirokov/csgtool"
dependency.detect_license!

assert_equal "mit", dependency["license"]
Expand All @@ -93,6 +75,21 @@ def mkproject(&block)
end
end

it "does not pull license text from GitHub mismatch with local assertion" do
mkproject do |dependency|
File.write "project.gemspec", "s.license = 'mpl-2.0'"
Licensed.use_github = true

VCR.use_cassette("sshirokov/csgtool/license") do
dependency["homepage"] = "https://github.com/sshirokov/csgtool"
dependency.detect_license!

assert_equal "mpl-2.0", dependency["license"]
assert_empty(dependency.text)
end
end
end

it "extracts other legal notices" do
mkproject do |dependency|
File.write "AUTHORS", "authors"
Expand Down

0 comments on commit ac4b4ce

Please sign in to comment.