fix tests and scripts for rubocop

This commit is contained in:
Ben Balter 2016-01-09 20:44:21 -05:00
parent 4a9ec6218b
commit 5e264f504a
15 changed files with 88 additions and 38 deletions

View File

@ -3,7 +3,13 @@ AllCops:
Exclude:
- tmp/**/*
- vendor/**/*
- test/fixtures/**/*
- test/functions.rb
Style/AlignHash:
EnforcedHashRocketStyle: table
EnforcedColonStyle: table
Metrics/ClassLength:
Exclude:
- test/**/*

View File

@ -9,7 +9,7 @@ def run_benchmark(bench_name, &_block)
Benchmark.bm(12, 'sum:', 'average:') do |benchmark|
times = []
yield benchmark, times
sum = times.inject(nil) { |sum, t| sum.nil? ? sum = t : sum += t }
sum = times.inject { |a, e| a + e }
avg = sum / times.size
[sum, avg]
end

View File

@ -4,6 +4,8 @@ set -e
bundle exec rake test
bundle exec rubocop
bundle exec script/benchmark
gem build licensee.gemspec

View File

@ -39,16 +39,27 @@ def verify_license_file(license, chaos = false, wrap = false)
msg = "No match for #{expected}."
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}"
msg = "Expeceted #{expected} but got #{actual.key} for .match. "
msg << "Confidence: #{license_file.confidence}. "
msg << "Method: #{license_file.matcher.class}"
assert_equal expected, actual.key, msg
end
def wrap(text, line_width = 80)
text = text.clone
copyright = /^#{Licensee::Matchers::Copyright::REGEX}$/i.match(text)
text.gsub! /^#{Licensee::Matchers::Copyright::REGEX}$/i, '[COPYRIGHT]' if copyright
text.gsub! /([^\n])\n([^\n])/, '\1 \2'
if copyright
text.gsub!(/^#{Licensee::Matchers::Copyright::REGEX}$/i, '[COPYRIGHT]')
end
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
if line.length > line_width
line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
else
line
end
end * "\n"
text.gsub! '[COPYRIGHT]', "\n#{copyright}\n" if copyright
text.strip

View File

@ -5,3 +5,8 @@ require 'shoulda'
require 'open3'
require_relative 'functions'
require_relative '../lib/licensee'
def assert_license_content(expected, readme)
content = Licensee::Project::Readme.license_content(readme)
assert_equal expected, content
end

View File

@ -13,7 +13,8 @@ class TestLicensee < Minitest::Test
end
should 'init a project' do
assert_equal Licensee::GitProject, Licensee.project(fixture_path('licenses.git')).class
project = Licensee.project(fixture_path('licenses.git'))
assert_equal Licensee::GitProject, project.class
end
context 'confidence threshold' do

View File

@ -5,8 +5,14 @@ class TestLicenseeBin < Minitest::Test
root = File.expand_path '..', File.dirname(__FILE__)
Dir.chdir root
stdout, stderr, status = Open3.capture3("#{root}/bin/licensee")
assert stdout.include?('License: MIT'), "expected #{stdout} to include `License: MIT`"
assert stdout.include?('License file: LICENSE.md'), "expected #{stdout} to include `Matched file: LICENSE.md`"
msg = "expected #{stdout} to include `License: MIT`"
assert stdout.include?('License: MIT'), msg
msg = "expected #{stdout} to include `Matched file: LICENSE.md`"
assert stdout.include?('License file: LICENSE.md'), msg
assert_equal 0, status
assert stderr.nil?
end
end

View File

@ -49,4 +49,4 @@ class TestLicenseeCopyrightMatchers < Minitest::Test
file = Licensee::Project::LicenseFile.new(text)
assert_equal 'no-license', Licensee::Matchers::Copyright.new(file).match.key
end
end
end

View File

@ -7,7 +7,8 @@ class TestLicenseeLicense < Minitest::Test
should 'read the license body' do
assert @license.body
assert @license.text =~ /MIT/, "Expected the following license body to contain MIT:\n#{@license.body}"
msg = "Expected the following to contain MIT:\n#{@license.body}"
assert @license.text =~ /MIT/, msg
end
should 'read the license body if it contains `---`' do
@ -22,7 +23,8 @@ class TestLicenseeLicense < Minitest::Test
end
should 'know the license path' do
assert_equal File.expand_path('./vendor/choosealicense.com/_licenses/mit.txt'), @license.path
path = File.expand_path('./vendor/choosealicense.com/_licenses/mit.txt')
assert_equal path, @license.path
end
should 'know the license name' do
@ -92,7 +94,9 @@ class TestLicenseeLicense < Minitest::Test
end
should 'fail loudly for invalid licenses' do
assert_raises(Licensee::InvalidLicense) { Licensee::License.new('foo').name }
assert_raises(Licensee::InvalidLicense) do
Licensee::License.new('foo').name
end
end
should "support 'other' licenses" do
@ -109,10 +113,15 @@ class TestLicenseeLicense < Minitest::Test
describe 'name without version' do
should 'strip the version from the license name' do
expected = 'GNU Affero General Public License'
assert_equal expected, Licensee::License.find('agpl-3.0').name_without_version
name = Licensee::License.find('agpl-3.0').name_without_version
assert_equal expected, name
expected = 'GNU General Public License'
assert_equal expected, Licensee::License.find('gpl-2.0').name_without_version
assert_equal expected, Licensee::License.find('gpl-3.0').name_without_version
name = Licensee::License.find('gpl-2.0').name_without_version
assert_equal expected, name
name = Licensee::License.find('gpl-3.0').name_without_version
assert_equal expected, name
end
Licensee.licenses.each do |license|
@ -144,8 +153,12 @@ class TestLicenseeLicense < Minitest::Test
assert_equal 24, Licensee::License.all(hidden: true).size
assert_equal 3, Licensee::License.all(featured: true).size
assert_equal 12, Licensee::License.all(featured: false).size
assert_equal 21, Licensee::License.all(featured: false, hidden: true).size
assert_equal 12, Licensee::License.all(featured: false, hidden: false).size
licenses = Licensee::License.all(featured: false, hidden: true)
assert_equal 21, licenses.size
licenses = Licensee::License.all(featured: false, hidden: false)
assert_equal 12, licenses.size
end
end
end

View File

@ -3,7 +3,8 @@ require 'helper'
class TestLicenseeLicenseFile < Minitest::Test
def setup
@repo = Rugged::Repository.new(fixture_path('licenses.git'))
blob, = Rugged::Blob.to_buffer(@repo, 'bcb552d06d9cf1cd4c048a6d3bf716849c2216cc')
ref = 'bcb552d06d9cf1cd4c048a6d3bf716849c2216cc'
blob, = Rugged::Blob.to_buffer(@repo, ref)
@file = Licensee::Project::LicenseFile.new(blob)
end
@ -49,7 +50,8 @@ class TestLicenseeLicenseFile < Minitest::Test
EXPECTATIONS.each do |filename, expected|
should "score a license named `#{filename}` as `#{expected}`" do
assert_equal expected, Licensee::Project::LicenseFile.name_score(filename)
score = Licensee::Project::LicenseFile.name_score(filename)
assert_equal expected, score
end
end
end

View File

@ -11,7 +11,8 @@ class TestLicenseePackageInfo < Minitest::Test
EXPECTATIONS.each do |filename, expected|
should "score a license named `#{filename}` as `#{expected}`" do
assert_equal expected, Licensee::Project::PackageInfo.name_score(filename)
score = Licensee::Project::PackageInfo.name_score(filename)
assert_equal expected, score
end
end
end

View File

@ -78,14 +78,17 @@ class TestLicenseeProject < Minitest::Test
describe 'packages' do
should 'detect a package file' do
project = Licensee::GitProject.new(fixture_path('npm.git'), detect_packages: true)
path = fixture_path('npm.git')
options = { detect_packages: true }
project = Licensee::GitProject.new(path, options)
assert_equal 'package.json', project.package_file.filename
assert_equal 'mit', project.license.key
end
should 'skip readme if no license content' do
project = Licensee::FSProject.new(fixture_path('bower-with-readme'),
detect_packages: true, detect_readme: true)
path = fixture_path('bower-with-readme')
options = { detect_packages: true, detect_readme: true }
project = Licensee::FSProject.new(path, options)
assert_equal 'mit', project.license.key
end
end

View File

@ -3,7 +3,8 @@ require 'helper'
class TestLicenseeProjectFile < Minitest::Test
def setup
@repo = Rugged::Repository.new(fixture_path('licenses.git'))
blob, = Rugged::Blob.to_buffer(@repo, 'bcb552d06d9cf1cd4c048a6d3bf716849c2216cc')
ref = 'bcb552d06d9cf1cd4c048a6d3bf716849c2216cc'
blob, = Rugged::Blob.to_buffer(@repo, ref)
@file = Licensee::Project::LicenseFile.new(blob)
@gpl = Licensee::License.find 'GPL-3.0'
@mit = Licensee::License.find 'MIT'

View File

@ -20,33 +20,28 @@ class TestLicenseeReadme < Minitest::Test
context 'readme content' do
should 'be blank if not license text' do
content = Licensee::Project::Readme.license_content('There is no License in this README')
assert_equal nil, content
assert_license_content nil, 'There is no License in this README'
end
should 'get content after h1' do
content = Licensee::Project::Readme.license_content("# License\n\nhello world")
assert_equal 'hello world', content
assert_license_content 'hello world', "# License\n\nhello world"
end
should 'get content after h2' do
content = Licensee::Project::Readme.license_content("## License\n\nhello world")
assert_equal 'hello world', content
assert_license_content 'hello world', "## License\n\nhello world"
end
should 'be case-insensitive' do
content = Licensee::Project::Readme.license_content("## LICENSE\n\nhello world")
assert_equal 'hello world', content
assert_license_content 'hello world', "## LICENSE\n\nhello world"
end
should 'be british' do
content = Licensee::Project::Readme.license_content("## Licence\n\nhello world")
assert_equal 'hello world', content
assert_license_content 'hello world', "## Licence\n\nhello world"
end
should 'not include trailing content' do
content = Licensee::Project::Readme.license_content("## License\n\nhello world\n\n# Contributing")
assert_equal 'hello world', content
readme = "## License\n\nhello world\n\n# Contributing"
assert_license_content 'hello world', readme
end
end
end

View File

@ -10,7 +10,9 @@ class TestLicenseeVendor < Minitest::Test
context 'when modified' do
should "detect the #{license} license" do
verify_license_file(license, true) unless SKIP.include?(File.basename(license, '.txt'))
unless SKIP.include?(File.basename(license, '.txt'))
verify_license_file(license, true)
end
end
end
@ -21,7 +23,9 @@ class TestLicenseeVendor < Minitest::Test
context 'when modified' do
should "detect the #{license} license" do
verify_license_file(license, true, 50) unless SKIP.include?(File.basename(license, '.txt'))
unless SKIP.include?(File.basename(license, '.txt'))
verify_license_file(license, true, 50)
end
end
end
end