mirror of https://github.com/licensee/licensee.git
Only use the README if it has license content
This commit is contained in:
parent
ee4fe3e00d
commit
669b34f6e4
|
@ -40,6 +40,7 @@ class Licensee
|
|||
return @readme if defined? @readme
|
||||
@readme = begin
|
||||
content, name = find_file { |name| Readme.name_score(name) }
|
||||
content = Readme.license_content(content)
|
||||
if content && name
|
||||
Readme.new(content, name)
|
||||
end
|
||||
|
|
|
@ -63,18 +63,17 @@ class Licensee
|
|||
|
||||
CONTENT_REGEX = /^#+ Licen[sc]e$(.*?)(?=#+|\z)/im
|
||||
|
||||
def initialize(content, filename = nil)
|
||||
match = CONTENT_REGEX.match(content)
|
||||
content = match ? match[1].strip : ""
|
||||
super content, filename
|
||||
end
|
||||
|
||||
def self.name_score(filename)
|
||||
SCORES.each do |pattern, score|
|
||||
return score if pattern =~ filename
|
||||
end
|
||||
return 0.0
|
||||
end
|
||||
|
||||
def self.license_content(content)
|
||||
match = CONTENT_REGEX.match(content)
|
||||
match[1].strip if match
|
||||
end
|
||||
end
|
||||
|
||||
class PackageInfo < File
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
This readme doesn't have a license, so detected the license from bower.
|
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"license": "mit"
|
||||
}
|
|
@ -83,5 +83,11 @@ class TestLicenseeProject < Minitest::Test
|
|||
assert_equal "package.json", project.package_file.filename
|
||||
assert_equal "mit", project.license.key
|
||||
end
|
||||
|
||||
should "skip readme if no license content" do
|
||||
project = Licensee::FSProject.new(fixture_path("bower-with-readme"),
|
||||
detect_packages: true, detect_readme: true)
|
||||
assert_equal "mit", project.license.key
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -72,33 +72,33 @@ class TestLicenseeProjectFile < Minitest::Test
|
|||
|
||||
context "readme content" do
|
||||
should "be blank if not license text" do
|
||||
file = Licensee::Project::Readme.new("There is no License in this README")
|
||||
assert_equal "", file.content
|
||||
content = Licensee::Project::Readme.license_content("There is no License in this README")
|
||||
assert_equal nil, content
|
||||
end
|
||||
|
||||
should "get content after h1" do
|
||||
file = Licensee::Project::Readme.new("# License\n\nhello world")
|
||||
assert_equal "hello world", file.content
|
||||
content = Licensee::Project::Readme.license_content("# License\n\nhello world")
|
||||
assert_equal "hello world", content
|
||||
end
|
||||
|
||||
should "get content after h2" do
|
||||
file = Licensee::Project::Readme.new("## License\n\nhello world")
|
||||
assert_equal "hello world", file.content
|
||||
content = Licensee::Project::Readme.license_content("## License\n\nhello world")
|
||||
assert_equal "hello world", content
|
||||
end
|
||||
|
||||
should "be case-insensitive" do
|
||||
file = Licensee::Project::Readme.new("## LICENSE\n\nhello world")
|
||||
assert_equal "hello world", file.content
|
||||
content = Licensee::Project::Readme.license_content("## LICENSE\n\nhello world")
|
||||
assert_equal "hello world", content
|
||||
end
|
||||
|
||||
should "be british" do
|
||||
file = Licensee::Project::Readme.new("## Licence\n\nhello world")
|
||||
assert_equal "hello world", file.content
|
||||
content = Licensee::Project::Readme.license_content("## Licence\n\nhello world")
|
||||
assert_equal "hello world", content
|
||||
end
|
||||
|
||||
should "not include trailing content" do
|
||||
file = Licensee::Project::Readme.new("## License\n\nhello world\n\n# Contributing")
|
||||
assert_equal "hello world", file.content
|
||||
content = Licensee::Project::Readme.license_content("## License\n\nhello world\n\n# Contributing")
|
||||
assert_equal "hello world", content
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue