Merge pull request #246 from benbalter/extend-gemspec-patterns

Extend gemspec pattern matching
This commit is contained in:
Ben Balter 2017-12-13 12:32:21 -05:00 committed by GitHub
commit 69a67f055b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 6 deletions

View File

@ -1,19 +1,45 @@
module Licensee
module Matchers
class Gemspec < Licensee::Matchers::Package
# a value is a string surrounded by any amount of whitespace
# optionally ended with (non-captured) ".freeze"
VALUE_REGEX = /\s*[\'\"]([a-z\-0-9\.]+)[\'\"](?:\.freeze)?\s*/i
# an array contains one or more values. all values, or array itself,
# can be surrounded by any amount of whitespace. do not capture
# non-value groups
ARRAY_REGEX = /\s*\[#{VALUE_REGEX}(?:,#{VALUE_REGEX})*\]\s*/i
DECLARATION_REGEX = /
^\s*[a-z0-9_]+\.([a-z0-9_]+)\s*\=\s*[\'\"]([a-z\-0-9\.]+)[\'\"]\s*$
/ix
^\s*[a-z0-9_]+\.([a-z0-9_]+)\s*\=#{VALUE_REGEX}$
/ix
LICENSE_REGEX = /
^\s*[a-z0-9_]+\.license\s*\=\s*[\'\"]([a-z\-0-9\.]+)[\'\"]\s*$
/ix
^\s*[a-z0-9_]+\.license\s*\=#{VALUE_REGEX}$
/ix
LICENSE_ARRAY_REGEX = /
^\s*[a-z0-9_]+\.licenses\s*\=#{ARRAY_REGEX}$
/ix
private
def license_property
match = @file.content.match LICENSE_REGEX
match[1].downcase if match && match[1]
return match[1].downcase if match && match[1]
# check for a licenses array property
licenses = license_array_property
return unless licenses
# use 'other' if array contains multiple licenses
return 'other' unless licenses.size == 1
licenses[0]
end
def license_array_property
match = @file.content.match LICENSE_ARRAY_REGEX
match.captures.compact.map(&:downcase) if match
end
def declarations

View File

@ -18,7 +18,9 @@ RSpec.describe Licensee::Matchers::Gemspec do
'as spec.' => "spec.license = 'mit'",
'double quotes' => 's.license = "mit"',
'no whitespace' => "s.license='mit'",
'uppercase' => "s.license = 'MIT'"
'uppercase' => "s.license = 'MIT'",
'array' => "s.licenses = ['mit']",
'frozen' => "s.license = 'mit'.freeze"
}.each do |description, license_declaration|
context "with a #{description} declaration" do
let(:content) { license_declaration }
@ -44,4 +46,12 @@ RSpec.describe Licensee::Matchers::Gemspec do
expect(subject.match).to eql(Licensee::License.find('other'))
end
end
context 'a licenses property with multiple licenses' do
let(:content) { "s.licenses = ['mit', 'bsd-3-clause']" }
it 'returns other' do
expect(subject.match).to eql(Licensee::License.find('other'))
end
end
end