Sha256: 15fa3d5ec9094033a8454e805131b16c71955647a3b18de5120ff9eba772a402

Contents?: true

Size: 1.97 KB

Versions: 4

Compression:

Stored size: 1.97 KB

Contents

# Pulled from helper.rb because something in the test suite monkey patches benchmarking

require 'securerandom'
require_relative '../lib/licensee'

def fixtures_base
  File.expand_path "fixtures", File.dirname( __FILE__ )
end

def fixture_path(fixture)
  File.expand_path fixture, fixtures_base
end

def license_from_path(path)
  license = File.open(path).read.match(/\A(---\n.*\n---\n+)?(.*)/m).to_a[2]
  license.sub! "[fullname]", "Ben Balter"
  license.sub! "[year]", "2014"
  license.sub! "[email]", "ben@github.invalid"
  license
end

FakeBlob = Licensee::FilesystemRepository::Blob

def chaos_monkey(string)
  Random.rand(3).times do
    string[Random.rand(string.length)] = SecureRandom.base64(Random.rand(3))
  end
  string
end

def verify_license_file(license, chaos = false, wrap=false)
  expected = File.basename(license, ".txt")

  text = license_from_path(license)
  text = chaos_monkey(text) if chaos
  text = wrap(text, wrap) if wrap

  blob = FakeBlob.new(text)
  license_file = Licensee::ProjectFile.new(blob, "LICENSE")

  actual = license_file.match
  msg = "No match for #{expected}."

  unless actual
    Licensee.matchers.each do |matcher|
      matcher = matcher.new(license_file)
      msg << "#{matcher.class}: #{matcher.confidence}% #{matcher.match.inspect}\n"
    end
    msg << "Here's the test text:\n#{text}"
  end

  assert actual, msg
  assert_equal expected, actual.key, "expeceted #{expected} but got #{actual.key} for .match. Confidence: #{license_file.confidence}. Method: #{license_file.matcher.class}"
end

def wrap(text, line_width=80)
  text = text.clone
  copyright = /^#{Licensee::CopyrightMatcher::REGEX}$/i.match(text)
  text.gsub! /^#{Licensee::CopyrightMatcher::REGEX}$/i, '[COPYRIGHT]' if copyright
  text.gsub! /([^\n])\n([^\n])/, '\1 \2'
  text = text.split("\n").collect do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end * "\n"
  text.gsub! "[COPYRIGHT]", "\n#{copyright}\n" if copyright
  text.strip
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
licensee-5.0.0 test/functions.rb
licensee-5.0.0b7 test/functions.rb
licensee-5.0.0b6 test/functions.rb
licensee-5.0.0b5 test/functions.rb