licensee 4.6.0 → 4.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 046dcbf68cab0fbba5bd8624102a73c11fbe2c4e
4
- data.tar.gz: 2b569387243ebeed517c5b873b8f4529149ba62d
3
+ metadata.gz: de46f921456858704f9f2260d3d699d9ddc61501
4
+ data.tar.gz: 043ec663c844f2c845a33aab4df9bd9e70d277a3
5
5
  SHA512:
6
- metadata.gz: 0fe55bf7698ecc52aaa2de8dd77f85cb8ad9357e29abeeb8a3237a1b258fcb1ca172d7329027d0a6f045e2c869029af20fe342ac91011199e9ccdf5423c05af5
7
- data.tar.gz: a4f92f1e13d453c7819c52dc591243edde132ec41c9f743c397b06902be2a40019776558ce03b935a3998682962e63af6f902bd4496032d8f79b740fb002d8fd
6
+ metadata.gz: 497beceba2f3b24cf06bafcf3042e9a202d3aaa8668431c09736cd3bc11267a937d90a522f5ade5ff67e45cdb585d07d1bcc5584034ff84284fc62146d681f94
7
+ data.tar.gz: 9e41896143cb6c2bc55f9ac5d6953f506f2d2b8223499bdbdf2f904f4feea9dbf7684a9cbe2a81deb85dc7264a8e006eea02c3cb91d4b6e2a38ff262ea4c8c98
@@ -1,4 +1,5 @@
1
1
  class Licensee
2
+ class InvalidLicense < ArgumentError; end
2
3
  class License
3
4
 
4
5
  def self.all
@@ -18,20 +19,23 @@ class Licensee
18
19
 
19
20
  # Raw content of license file, including YAML front matter
20
21
  def content
21
- @content ||= File.open(path).read
22
- rescue
23
- ""
22
+ @content ||= if File.exists?(path)
23
+ File.open(path).read
24
+ elsif key == "other" # A pseudo-license with no content
25
+ nil
26
+ else
27
+ raise Licensee::InvalidLicense, "'#{key}' is not a valid license key"
28
+ end
24
29
  end
25
30
 
26
31
  # License metadata from YAML front matter
27
32
  def meta
28
- @meta ||= YAML.load(parts[1]) if parts[1]
29
- rescue
30
- nil
33
+ @meta ||= YAML.load(parts[1]) if parts && parts[1]
31
34
  end
32
35
 
36
+ # Returns the human-readable license name
33
37
  def name
34
- meta["title"] if meta
38
+ meta.nil? ? key.capitalize : meta["title"]
35
39
  end
36
40
 
37
41
  def featured?
@@ -41,7 +45,7 @@ class Licensee
41
45
 
42
46
  # The license body (e.g., contents - frontmatter)
43
47
  def body
44
- @body ||= parts[2] if parts[2]
48
+ @body ||= parts[2] if parts && parts[2]
45
49
  end
46
50
  alias_method :to_s, :body
47
51
  alias_method :text, :body
@@ -70,7 +74,7 @@ class Licensee
70
74
  private
71
75
 
72
76
  def parts
73
- @parts ||= content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a
77
+ @parts ||= content.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a if content
74
78
  end
75
79
  end
76
80
  end
@@ -2,17 +2,6 @@ class Licensee
2
2
  class Project
3
3
  attr_reader :repository
4
4
 
5
- # Array of file names to look for potential license files, in order
6
- # Filenames should be lower case as candidates are downcased before comparison
7
- LICENSE_FILENAMES = %w[
8
- license
9
- license.txt
10
- license.md
11
- unlicense
12
- copying
13
- copyright
14
- ]
15
-
16
5
  # Initializes a new project
17
6
  #
18
7
  # path_or_repo path to git repo or Rugged::Repository instance
@@ -45,6 +34,19 @@ class Licensee
45
34
  @license ||= license_file.match if license_file
46
35
  end
47
36
 
37
+ # Scores a given file as a potential license
38
+ #
39
+ # filename - (string) the name of the file to score
40
+ #
41
+ # Returns 1 if the file is definately a license file
42
+ # Return 0.5 if the file is likely a license file
43
+ # Returns 0 if the file is definately not a license file
44
+ def self.match_license_file(filename)
45
+ return 1 if self.license_file?(filename)
46
+ return 0.5 if self.maybe_license_file?(filename)
47
+ return 0
48
+ end
49
+
48
50
  private
49
51
 
50
52
  def commit
@@ -58,11 +60,8 @@ class Licensee
58
60
  # Detects the license file, if any
59
61
  # Returns the blob hash as detected in the tree
60
62
  def license_hash
61
- # Prefer an exact match to one of our known file names
62
- license_hash = tree.find { |blob| LICENSE_FILENAMES.include? blob[:name].downcase }
63
-
64
- # Fall back to the first file in the project root that has the word license in it
65
- license_hash || tree.find { |blob| blob[:name] =~ /licen(s|c)e/i }
63
+ license_hash = tree.find { |blob| self.class.license_file?(blob[:name]) }
64
+ license_hash ||= tree.find { |blob| self.class.maybe_license_file?(blob[:name]) }
66
65
  end
67
66
 
68
67
  def license_blob
@@ -72,5 +71,26 @@ class Licensee
72
71
  def license_path
73
72
  license_hash[:name] if license_hash
74
73
  end
74
+
75
+ # Regex to detect license files
76
+ #
77
+ # Examples it should match:
78
+ # - LICENSE.md
79
+ # - licence.txt
80
+ # - unlicense
81
+ # - copying
82
+ # - copyright
83
+ def self.license_file?(filename)
84
+ !!(filename =~ /\A(un)?licen[sc]e|copy(ing|right)(\.[^.]+)?\z/i)
85
+ end
86
+
87
+ # Regex to detect things that look like license files
88
+ #
89
+ # Examples it should match:
90
+ # - license-MIT.txt
91
+ # - MIT-LICENSE
92
+ def self.maybe_license_file?(filename)
93
+ !!(filename =~ /licen[sc]e/i)
94
+ end
75
95
  end
76
96
  end
@@ -1,3 +1,3 @@
1
1
  class Licensee
2
- VERSION = "4.6.0"
2
+ VERSION = "4.7.0"
3
3
  end
@@ -30,8 +30,8 @@ class TestLicenseeLicense < Minitest::Test
30
30
  should "know if the license is featured" do
31
31
  assert @license.featured?
32
32
  assert_equal TrueClass, @license.featured?.class
33
- refute Licensee::License.new("cc0").featured?
34
- assert_equal FalseClass, Licensee::License.new("cc0").featured?.class
33
+ refute Licensee::License.new("cc0-1.0").featured?
34
+ assert_equal FalseClass, Licensee::License.new("cc0-1.0").featured?.class
35
35
  end
36
36
 
37
37
  should "parse the license parts" do
@@ -50,4 +50,15 @@ class TestLicenseeLicense < Minitest::Test
50
50
  should "strip leading newlines from the license" do
51
51
  assert_equal "T", @license.body[0]
52
52
  end
53
+
54
+ should "fail loudly for invalid licenses" do
55
+ assert_raises(Licensee::InvalidLicense) { Licensee::License.new("foo").name }
56
+ end
57
+
58
+ should "support 'other' licenses" do
59
+ license = Licensee::License.new("other")
60
+ assert_equal nil, license.content
61
+ assert_equal "Other", license.name
62
+ refute license.featured?
63
+ end
53
64
  end
@@ -79,4 +79,42 @@ class TestLicenseeProject < Minitest::Test
79
79
  end
80
80
  end
81
81
 
82
+ context "license filename matching" do
83
+ # Standard license names
84
+ ["license",
85
+ "LICENSE",
86
+ "LICENCE",
87
+ "license.md",
88
+ "unlicense",
89
+ "unlicence",
90
+ "copying",
91
+ "copyRIGHT",
92
+ "license.txt"
93
+ ].each do |license|
94
+ should "match #{license}" do
95
+ assert Licensee::Project.license_file?(license)
96
+ end
97
+ end
98
+
99
+ should "not match MIT-LICENSE" do
100
+ refute Licensee::Project.license_file?("MIT-LICENSE")
101
+ end
102
+
103
+ # Abnormal license names
104
+ [
105
+ "LICENSE-MIT",
106
+ "MIT-LICENSE.txt",
107
+ "mit-license-foo.md"
108
+ ].each do |license|
109
+ should "match #{license}" do
110
+ assert Licensee::Project.maybe_license_file?(license)
111
+ end
112
+ end
113
+
114
+ should "return the proper license files scores" do
115
+ assert_equal 1, Licensee::Project.match_license_file("LICENSE.md")
116
+ assert_equal 0.5, Licensee::Project.match_license_file("MIT-LICENSE")
117
+ assert_equal 0, Licensee::Project.match_license_file("README.txt")
118
+ end
119
+ end
82
120
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: licensee
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.6.0
4
+ version: 4.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Balter
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-27 00:00:00.000000000 Z
11
+ date: 2015-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rugged
@@ -208,7 +208,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
208
208
  version: '0'
209
209
  requirements: []
210
210
  rubyforge_project:
211
- rubygems_version: 2.2.3
211
+ rubygems_version: 2.4.8
212
212
  signing_key:
213
213
  specification_version: 4
214
214
  summary: A Ruby Gem to detect under what license a project is distributed