metaprogrammign test coverage

This commit is contained in:
Ben Balter 2018-11-02 12:18:52 -07:00
parent d877c13c75
commit 4dfe4b1d7b
No known key found for this signature in database
GPG Key ID: DBB67C246AD356C4
15 changed files with 44 additions and 30 deletions

View File

@ -4,8 +4,8 @@ require 'digest'
module Licensee module Licensee
module ContentHelper module ContentHelper
DIGEST = Digest::SHA1 DIGEST = Digest::SHA1
START_REGEX = /\A\s*/ START_REGEX = /\A\s*/.freeze
END_OF_TERMS_REGEX = /^[\s#*_]*end of terms and conditions\s*$/i END_OF_TERMS_REGEX = /^[\s#*_]*end of terms and conditions\s*$/i.freeze
ALT_TITLE_REGEX = License::ALT_TITLE_REGEX ALT_TITLE_REGEX = License::ALT_TITLE_REGEX
REGEXES = { REGEXES = {
hrs: /^\s*[=\-\*]{3,}\s*$/, hrs: /^\s*[=\-\*]{3,}\s*$/,

View File

@ -95,8 +95,8 @@ module Licensee
'bsd-3-clause-clear' => /(?:clear bsd|bsd 3-clause(?: clear)?)/i 'bsd-3-clause-clear' => /(?:clear bsd|bsd 3-clause(?: clear)?)/i
}.freeze }.freeze
SOURCE_PREFIX = %r{https?://(?:www\.)?}i SOURCE_PREFIX = %r{https?://(?:www\.)?}i.freeze
SOURCE_SUFFIX = %r{(?:\.html?|\.txt|\/)(?:\?[^\s]*)?}i SOURCE_SUFFIX = %r{(?:\.html?|\.txt|\/)(?:\?[^\s]*)?}i.freeze
HASH_METHODS = %i[ HASH_METHODS = %i[
key spdx_id meta url rules fields other? gpl? lgpl? cc? key spdx_id meta url rules fields other? gpl? lgpl? cc?

View File

@ -45,7 +45,7 @@ module Licensee
end end
alias key name alias key name
FIELD_REGEX = /\[(#{Regexp.union(LicenseField.keys)})\]/ FIELD_REGEX = /\[(#{Regexp.union(LicenseField.keys)})\]/.freeze
# The human-readable field name # The human-readable field name
def label def label

View File

@ -3,7 +3,7 @@ module Licensee
class Cabal < Licensee::Matchers::Package class Cabal < Licensee::Matchers::Package
# While we could parse the cabal file, prefer # While we could parse the cabal file, prefer
# a lenient regex for speed and security. Moar parsing moar problems. # 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 private

View File

@ -3,7 +3,7 @@ module Licensee
class Cargo < Licensee::Matchers::Package class Cargo < Licensee::Matchers::Package
LICENSE_REGEX = %r{ LICENSE_REGEX = %r{
^\s*[\'\"]?license[\'\"]?\s*=\s*[\'\"]([a-z\-0-9\. +()\/]+)[\'\"]\s* ^\s*[\'\"]?license[\'\"]?\s*=\s*[\'\"]([a-z\-0-9\. +()\/]+)[\'\"]\s*
}ix }ix.freeze
private private

View File

@ -5,7 +5,7 @@ module Licensee
# rubocop:disable Metrics/LineLength # rubocop:disable Metrics/LineLength
COPYRIGHT_SYMBOLS = Regexp.union([/copyright/i, /\(c\)/i, "\u00A9", "\xC2\xA9"]) 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 # rubocop:enable Metrics/LineLength
def match def match

View File

@ -5,9 +5,9 @@ module Licensee
# While we could parse the DESCRIPTION file, prefer # While we could parse the DESCRIPTION file, prefer
# a lenient regex for speed and security. Moar parsing moar problems. # a lenient regex for speed and security. Moar parsing moar problems.
LICENSE_FIELD_REGEX = /^license:\s*(.+)/i LICENSE_FIELD_REGEX = /^license:\s*(.+)/i.freeze
PLUS_FILE_LICENSE_REGEX = /\s*\+\s*file\s+LICENSE$/i PLUS_FILE_LICENSE_REGEX = /\s*\+\s*file\s+LICENSE$/i.freeze
GPL_VERSION_REGEX = /^GPL(?:-([23])|\s*\(\s*>=\s*([23])\s*\))$/i GPL_VERSION_REGEX = /^GPL(?:-([23])|\s*\(\s*>=\s*([23])\s*\))$/i.freeze
private private

View File

@ -3,7 +3,7 @@ module Licensee
class DistZilla < Licensee::Matchers::Package class DistZilla < Licensee::Matchers::Package
attr_reader :file 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 private

View File

@ -3,24 +3,24 @@ module Licensee
class Gemspec < Licensee::Matchers::Package class Gemspec < Licensee::Matchers::Package
# a value is a string surrounded by any amount of whitespace # a value is a string surrounded by any amount of whitespace
# optionally ended with (non-captured) ".freeze" # 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, # an array contains one or more values. all values, or array itself,
# can be surrounded by any amount of whitespace. do not capture # can be surrounded by any amount of whitespace. do not capture
# non-value groups # non-value groups
ARRAY_REGEX = /\s*\[#{VALUE_REGEX}(?:,#{VALUE_REGEX})*\]\s*/i ARRAY_REGEX = /\s*\[#{VALUE_REGEX}(?:,#{VALUE_REGEX})*\]\s*/i.freeze
DECLARATION_REGEX = / DECLARATION_REGEX = /
^\s*[a-z0-9_]+\.([a-z0-9_]+)\s*\=#{VALUE_REGEX}$ ^\s*[a-z0-9_]+\.([a-z0-9_]+)\s*\=#{VALUE_REGEX}$
/ix /ix.freeze
LICENSE_REGEX = / LICENSE_REGEX = /
^\s*[a-z0-9_]+\.license\s*\=#{VALUE_REGEX}$ ^\s*[a-z0-9_]+\.license\s*\=#{VALUE_REGEX}$
/ix /ix.freeze
LICENSE_ARRAY_REGEX = / LICENSE_ARRAY_REGEX = /
^\s*[a-z0-9_]+\.licenses\s*\=#{ARRAY_REGEX}$ ^\s*[a-z0-9_]+\.licenses\s*\=#{ARRAY_REGEX}$
/ix /ix.freeze
private private

View File

@ -5,7 +5,7 @@ module Licensee
# a lenient regex for speed and security. Moar parsing moar problems. # a lenient regex for speed and security. Moar parsing moar problems.
LICENSE_REGEX = / LICENSE_REGEX = /
\s*[\"\']license[\"\']\s*\:\s*[\'\"]([a-z\-0-9\.+ ()]+)[\'\"],?\s* \s*[\"\']license[\"\']\s*\:\s*[\'\"]([a-z\-0-9\.+ ()]+)[\'\"],?\s*
/ix /ix.freeze
private private

View File

@ -3,7 +3,7 @@ module Licensee
class Spdx < Licensee::Matchers::Package class Spdx < Licensee::Matchers::Package
# While we could parse the LICENSE.spdx file, prefer # While we could parse the LICENSE.spdx file, prefer
# a lenient regex for speed and security. Moar parsing moar problems. # 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 private

View File

@ -5,22 +5,22 @@ module Licensee
# List of extensions to give preference to # List of extensions to give preference to
PREFERRED_EXT = %w[md markdown txt].freeze 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 # 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. # 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. # Regex to match COPYING, COPYRIGHT, etc.
COPYING_REGEX = /copy(ing|right)/i COPYING_REGEX = /copy(ing|right)/i.freeze
# Regex to match OFL. # Regex to match OFL.
OFL_REGEX = /ofl/i OFL_REGEX = /ofl/i.freeze
# BSD + PATENTS patent file # BSD + PATENTS patent file
PATENTS_REGEX = /patents/i PATENTS_REGEX = /patents/i.freeze
# Hash of Regex => score with which to score potential license files # Hash of Regex => score with which to score potential license files
FILENAME_REGEXES = { FILENAME_REGEXES = {
@ -46,7 +46,7 @@ module Licensee
# detected as CC-BY or CC-BY-SA which are 98%+ similar # detected as CC-BY or CC-BY-SA which are 98%+ similar
CC_FALSE_POSITIVE_REGEX = / CC_FALSE_POSITIVE_REGEX = /
^(creative\ commons\ )?Attribution-(NonCommercial|NoDerivatives) ^(creative\ commons\ )?Attribution-(NonCommercial|NoDerivatives)
/xi /xi.freeze
def possible_matchers def possible_matchers
[Matchers::Copyright, Matchers::Exact, Matchers::Dice] [Matchers::Copyright, Matchers::Exact, Matchers::Dice]

View File

@ -7,8 +7,8 @@ module Licensee
/\AREADME\.(#{Regexp.union(EXTENSIONS).source})\z/i => 0.9 /\AREADME\.(#{Regexp.union(EXTENSIONS).source})\z/i => 0.9
}.freeze }.freeze
TITLE_REGEX = /licen[sc]e:?/i TITLE_REGEX = /licen[sc]e:?/i.freeze
UNDERLINE_REGEX = /\n[-=]+/m UNDERLINE_REGEX = /\n[-=]+/m.freeze
CONTENT_REGEX = /^ CONTENT_REGEX = /^
(?: # Header lookbehind (?: # Header lookbehind
[\#=]+\s#{TITLE_REGEX}\s*[\#=]* # Start of hashes or rdoc header [\#=]+\s#{TITLE_REGEX}\s*[\#=]* # Start of hashes or rdoc header
@ -25,7 +25,7 @@ module Licensee
| |
\z # End of file \z # End of file
) )
/mix /mix.freeze
def possible_matchers def possible_matchers
super.push(Matchers::Reference) super.push(Matchers::Reference)

View File

@ -13,7 +13,7 @@ module Licensee
# If there's any trailing data (e.g. `.git`) this pattern will ignore it: # 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. # we're going to use the API rather than clone the repo.
GITHUB_REPO_PATTERN = GITHUB_REPO_PATTERN =
%r{https://github.com/([^\/]+\/([^\/]+(?=\.git)|[^\/]+)).*} %r{https://github.com/([^\/]+\/([^\/]+(?=\.git)|[^\/]+)).*}.freeze
class RepoNotFound < StandardError; end class RepoNotFound < StandardError; end

View File

@ -351,4 +351,18 @@ RSpec.describe Licensee::ContentHelper do
end end
end 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 end