mirror of https://github.com/licensee/licensee.git
metaprogrammign test coverage
This commit is contained in:
parent
d877c13c75
commit
4dfe4b1d7b
|
@ -4,8 +4,8 @@ require 'digest'
|
|||
module Licensee
|
||||
module ContentHelper
|
||||
DIGEST = Digest::SHA1
|
||||
START_REGEX = /\A\s*/
|
||||
END_OF_TERMS_REGEX = /^[\s#*_]*end of terms and conditions\s*$/i
|
||||
START_REGEX = /\A\s*/.freeze
|
||||
END_OF_TERMS_REGEX = /^[\s#*_]*end of terms and conditions\s*$/i.freeze
|
||||
ALT_TITLE_REGEX = License::ALT_TITLE_REGEX
|
||||
REGEXES = {
|
||||
hrs: /^\s*[=\-\*]{3,}\s*$/,
|
||||
|
|
|
@ -95,8 +95,8 @@ module Licensee
|
|||
'bsd-3-clause-clear' => /(?:clear bsd|bsd 3-clause(?: clear)?)/i
|
||||
}.freeze
|
||||
|
||||
SOURCE_PREFIX = %r{https?://(?:www\.)?}i
|
||||
SOURCE_SUFFIX = %r{(?:\.html?|\.txt|\/)(?:\?[^\s]*)?}i
|
||||
SOURCE_PREFIX = %r{https?://(?:www\.)?}i.freeze
|
||||
SOURCE_SUFFIX = %r{(?:\.html?|\.txt|\/)(?:\?[^\s]*)?}i.freeze
|
||||
|
||||
HASH_METHODS = %i[
|
||||
key spdx_id meta url rules fields other? gpl? lgpl? cc?
|
||||
|
|
|
@ -45,7 +45,7 @@ module Licensee
|
|||
end
|
||||
|
||||
alias key name
|
||||
FIELD_REGEX = /\[(#{Regexp.union(LicenseField.keys)})\]/
|
||||
FIELD_REGEX = /\[(#{Regexp.union(LicenseField.keys)})\]/.freeze
|
||||
|
||||
# The human-readable field name
|
||||
def label
|
||||
|
|
|
@ -3,7 +3,7 @@ module Licensee
|
|||
class Cabal < Licensee::Matchers::Package
|
||||
# While we could parse the cabal file, prefer
|
||||
# a lenient regex for speed and security. Moar parsing moar problems.
|
||||
LICENSE_REGEX = /^\s*license\s*\:\s*([a-z\-0-9\.]+)\s*$/ix
|
||||
LICENSE_REGEX = /^\s*license\s*\:\s*([a-z\-0-9\.]+)\s*$/ix.freeze
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ module Licensee
|
|||
class Cargo < Licensee::Matchers::Package
|
||||
LICENSE_REGEX = %r{
|
||||
^\s*[\'\"]?license[\'\"]?\s*=\s*[\'\"]([a-z\-0-9\. +()\/]+)[\'\"]\s*
|
||||
}ix
|
||||
}ix.freeze
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ module Licensee
|
|||
|
||||
# rubocop:disable Metrics/LineLength
|
||||
COPYRIGHT_SYMBOLS = Regexp.union([/copyright/i, /\(c\)/i, "\u00A9", "\xC2\xA9"])
|
||||
REGEX = /#{ContentHelper::START_REGEX}(?:portions )?(\s*#{COPYRIGHT_SYMBOLS}.*$)+$/i
|
||||
REGEX = /#{ContentHelper::START_REGEX}(?:portions )?(\s*#{COPYRIGHT_SYMBOLS}.*$)+$/i.freeze
|
||||
# rubocop:enable Metrics/LineLength
|
||||
|
||||
def match
|
||||
|
|
|
@ -5,9 +5,9 @@ module Licensee
|
|||
|
||||
# While we could parse the DESCRIPTION file, prefer
|
||||
# a lenient regex for speed and security. Moar parsing moar problems.
|
||||
LICENSE_FIELD_REGEX = /^license:\s*(.+)/i
|
||||
PLUS_FILE_LICENSE_REGEX = /\s*\+\s*file\s+LICENSE$/i
|
||||
GPL_VERSION_REGEX = /^GPL(?:-([23])|\s*\(\s*>=\s*([23])\s*\))$/i
|
||||
LICENSE_FIELD_REGEX = /^license:\s*(.+)/i.freeze
|
||||
PLUS_FILE_LICENSE_REGEX = /\s*\+\s*file\s+LICENSE$/i.freeze
|
||||
GPL_VERSION_REGEX = /^GPL(?:-([23])|\s*\(\s*>=\s*([23])\s*\))$/i.freeze
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ module Licensee
|
|||
class DistZilla < Licensee::Matchers::Package
|
||||
attr_reader :file
|
||||
|
||||
LICENSE_REGEX = /^license\s*=\s*([a-z\-0-9\._]+)/i
|
||||
LICENSE_REGEX = /^license\s*=\s*([a-z\-0-9\._]+)/i.freeze
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -3,24 +3,24 @@ module Licensee
|
|||
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
|
||||
VALUE_REGEX = /\s*[\'\"]([a-z\-0-9\.]+)[\'\"](?:\.freeze)?\s*/i.freeze
|
||||
|
||||
# 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
|
||||
ARRAY_REGEX = /\s*\[#{VALUE_REGEX}(?:,#{VALUE_REGEX})*\]\s*/i.freeze
|
||||
|
||||
DECLARATION_REGEX = /
|
||||
^\s*[a-z0-9_]+\.([a-z0-9_]+)\s*\=#{VALUE_REGEX}$
|
||||
/ix
|
||||
/ix.freeze
|
||||
|
||||
LICENSE_REGEX = /
|
||||
^\s*[a-z0-9_]+\.license\s*\=#{VALUE_REGEX}$
|
||||
/ix
|
||||
/ix.freeze
|
||||
|
||||
LICENSE_ARRAY_REGEX = /
|
||||
^\s*[a-z0-9_]+\.licenses\s*\=#{ARRAY_REGEX}$
|
||||
/ix
|
||||
/ix.freeze
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ module Licensee
|
|||
# a lenient regex for speed and security. Moar parsing moar problems.
|
||||
LICENSE_REGEX = /
|
||||
\s*[\"\']license[\"\']\s*\:\s*[\'\"]([a-z\-0-9\.+ ()]+)[\'\"],?\s*
|
||||
/ix
|
||||
/ix.freeze
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ module Licensee
|
|||
class Spdx < Licensee::Matchers::Package
|
||||
# While we could parse the LICENSE.spdx file, prefer
|
||||
# a lenient regex for speed and security. Moar parsing moar problems.
|
||||
LICENSE_REGEX = /PackageLicenseDeclared:\s*([a-z\-0-9\. +()]+)\s*/i
|
||||
LICENSE_REGEX = /PackageLicenseDeclared:\s*([a-z\-0-9\. +()]+)\s*/i.freeze
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -5,22 +5,22 @@ module Licensee
|
|||
|
||||
# List of extensions to give preference to
|
||||
PREFERRED_EXT = %w[md markdown txt].freeze
|
||||
PREFERRED_EXT_REGEX = /\.#{Regexp.union(PREFERRED_EXT)}\z/
|
||||
PREFERRED_EXT_REGEX = /\.#{Regexp.union(PREFERRED_EXT)}\z/.freeze
|
||||
|
||||
# Regex to match any extension except .spdx or .header
|
||||
OTHER_EXT_REGEX = %r{\.(?!spdx|header|gemspec)[^./]+\z}i
|
||||
OTHER_EXT_REGEX = %r{\.(?!spdx|header|gemspec)[^./]+\z}i.freeze
|
||||
|
||||
# Regex to match, LICENSE, LICENCE, unlicense, etc.
|
||||
LICENSE_REGEX = /(un)?licen[sc]e/i
|
||||
LICENSE_REGEX = /(un)?licen[sc]e/i.freeze
|
||||
|
||||
# Regex to match COPYING, COPYRIGHT, etc.
|
||||
COPYING_REGEX = /copy(ing|right)/i
|
||||
COPYING_REGEX = /copy(ing|right)/i.freeze
|
||||
|
||||
# Regex to match OFL.
|
||||
OFL_REGEX = /ofl/i
|
||||
OFL_REGEX = /ofl/i.freeze
|
||||
|
||||
# BSD + PATENTS patent file
|
||||
PATENTS_REGEX = /patents/i
|
||||
PATENTS_REGEX = /patents/i.freeze
|
||||
|
||||
# Hash of Regex => score with which to score potential license files
|
||||
FILENAME_REGEXES = {
|
||||
|
@ -46,7 +46,7 @@ module Licensee
|
|||
# detected as CC-BY or CC-BY-SA which are 98%+ similar
|
||||
CC_FALSE_POSITIVE_REGEX = /
|
||||
^(creative\ commons\ )?Attribution-(NonCommercial|NoDerivatives)
|
||||
/xi
|
||||
/xi.freeze
|
||||
|
||||
def possible_matchers
|
||||
[Matchers::Copyright, Matchers::Exact, Matchers::Dice]
|
||||
|
|
|
@ -7,8 +7,8 @@ module Licensee
|
|||
/\AREADME\.(#{Regexp.union(EXTENSIONS).source})\z/i => 0.9
|
||||
}.freeze
|
||||
|
||||
TITLE_REGEX = /licen[sc]e:?/i
|
||||
UNDERLINE_REGEX = /\n[-=]+/m
|
||||
TITLE_REGEX = /licen[sc]e:?/i.freeze
|
||||
UNDERLINE_REGEX = /\n[-=]+/m.freeze
|
||||
CONTENT_REGEX = /^
|
||||
(?: # Header lookbehind
|
||||
[\#=]+\s#{TITLE_REGEX}\s*[\#=]* # Start of hashes or rdoc header
|
||||
|
@ -25,7 +25,7 @@ module Licensee
|
|||
|
|
||||
\z # End of file
|
||||
)
|
||||
/mix
|
||||
/mix.freeze
|
||||
|
||||
def possible_matchers
|
||||
super.push(Matchers::Reference)
|
||||
|
|
|
@ -13,7 +13,7 @@ module Licensee
|
|||
# If there's any trailing data (e.g. `.git`) this pattern will ignore it:
|
||||
# we're going to use the API rather than clone the repo.
|
||||
GITHUB_REPO_PATTERN =
|
||||
%r{https://github.com/([^\/]+\/([^\/]+(?=\.git)|[^\/]+)).*}
|
||||
%r{https://github.com/([^\/]+\/([^\/]+(?=\.git)|[^\/]+)).*}.freeze
|
||||
|
||||
class RepoNotFound < StandardError; end
|
||||
|
||||
|
|
|
@ -351,4 +351,18 @@ RSpec.describe Licensee::ContentHelper do
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'metaprogramming' do
|
||||
it 'raises on invalid normalization' do
|
||||
expect { subject.send(:normalize, :foo) }.to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it 'raises on invalid strip' do
|
||||
expect { subject.send(:strip, :foo) }.to raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it 'backwards compatibalizes regexes' do
|
||||
expect(described_class::WHITESPACE_REGEX).to eql(/\s+/)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue