Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend gemspec pattern matching #246

Merged
merged 4 commits into from
Dec 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions lib/licensee/matchers/gemspec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion spec/licensee/matchers/gemspec_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
'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 }
Expand All @@ -44,4 +46,12 @@
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