2014-07-12 12:38:51 -04:00
|
|
|
require 'morpher'
|
|
|
|
require 'anima'
|
2014-10-26 19:50:27 -04:00
|
|
|
require 'mutant'
|
2014-07-12 12:38:51 -04:00
|
|
|
|
2015-06-13 01:04:07 -04:00
|
|
|
module MutantSpec
|
2015-07-28 14:50:54 -04:00
|
|
|
ROOT = Pathname.new(__FILE__).parent.parent.parent
|
|
|
|
|
2015-06-13 01:04:07 -04:00
|
|
|
# Namespace module for corpus testing
|
|
|
|
#
|
|
|
|
# rubocop:disable MethodLength
|
|
|
|
module Corpus
|
|
|
|
# Project under corpus test
|
|
|
|
# rubocop:disable ClassLength
|
|
|
|
TMP = ROOT.join('tmp').freeze
|
|
|
|
|
|
|
|
class Project
|
|
|
|
MUTEX = Mutex.new
|
|
|
|
include Adamantium, Anima.new(
|
|
|
|
:exclude,
|
2015-11-15 15:13:48 -05:00
|
|
|
:expect_coverage,
|
2015-06-13 01:04:07 -04:00
|
|
|
:mutation_coverage,
|
|
|
|
:mutation_generation,
|
2015-11-15 15:13:48 -05:00
|
|
|
:name,
|
2015-06-13 01:04:07 -04:00
|
|
|
:namespace,
|
2015-11-15 15:13:48 -05:00
|
|
|
:repo_uri
|
2015-06-13 01:04:07 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
# Verify mutation coverage
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# if successful
|
|
|
|
#
|
|
|
|
# @raise [Exception]
|
|
|
|
#
|
2015-07-01 23:35:54 -04:00
|
|
|
# @api private
|
2015-06-13 01:04:07 -04:00
|
|
|
def verify_mutation_coverage
|
|
|
|
checkout
|
|
|
|
Dir.chdir(repo_path) do
|
|
|
|
Bundler.with_clean_env do
|
|
|
|
install_mutant
|
|
|
|
system(
|
|
|
|
%W[
|
|
|
|
bundle exec mutant
|
|
|
|
--use rspec
|
|
|
|
--include lib
|
|
|
|
--require #{name}
|
|
|
|
--expected-coverage #{expect_coverage}
|
|
|
|
#{namespace}*
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
|
|
|
|
# Verify mutation generation
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
# if successful
|
|
|
|
#
|
|
|
|
# @raise [Exception]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# rubocop:disable AbcSize
|
|
|
|
#
|
2015-07-01 23:35:54 -04:00
|
|
|
# @api private
|
2015-06-13 01:04:07 -04:00
|
|
|
def verify_mutation_generation
|
|
|
|
checkout
|
|
|
|
start = Time.now
|
|
|
|
paths = Pathname.glob(repo_path.join('**/*.rb')).sort_by(&:size).reverse
|
|
|
|
options = {
|
|
|
|
finish: method(:finish),
|
|
|
|
start: method(:start),
|
|
|
|
in_processes: parallel_processes
|
|
|
|
}
|
|
|
|
total = Parallel.map(paths, options) do |path|
|
|
|
|
count = 0
|
|
|
|
node =
|
|
|
|
begin
|
|
|
|
Parser::CurrentRuby.parse(path.read)
|
|
|
|
rescue EncodingError, ArgumentError
|
|
|
|
nil # Make rubocop happy
|
|
|
|
end
|
|
|
|
if node
|
|
|
|
Mutant::Mutator::Node.each(node) do
|
|
|
|
count += 1
|
|
|
|
end
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
count
|
|
|
|
end.inject(0, :+)
|
|
|
|
took = Time.now - start
|
|
|
|
puts format(
|
|
|
|
'Total Mutations/Time/Parse-Errors: %s/%0.2fs - %0.2f/s',
|
|
|
|
total,
|
|
|
|
took,
|
|
|
|
total / took
|
|
|
|
)
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
# Checkout repository
|
|
|
|
#
|
|
|
|
# @return [self]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def checkout
|
|
|
|
return self if noinstall?
|
|
|
|
TMP.mkdir unless TMP.directory?
|
|
|
|
if repo_path.exist?
|
|
|
|
Dir.chdir(repo_path) do
|
|
|
|
system(%w[git fetch origin])
|
|
|
|
system(%w[git reset --hard])
|
|
|
|
system(%w[git clean -f -d -x])
|
|
|
|
system(%w[git checkout origin/master])
|
|
|
|
system(%w[git reset --hard])
|
|
|
|
system(%w[git clean -f -d -x])
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
else
|
|
|
|
system(%W[git clone #{repo_uri} #{repo_path}])
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
self
|
|
|
|
end
|
|
|
|
memoize :checkout
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Install mutant
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def install_mutant
|
|
|
|
return if noinstall?
|
|
|
|
relative = ROOT.relative_path_from(repo_path)
|
|
|
|
repo_path.join('Gemfile').open('a') do |file|
|
|
|
|
file << "gem 'mutant', path: '#{relative}'\n"
|
|
|
|
file << "gem 'mutant-rspec', path: '#{relative}'\n"
|
2015-09-04 18:31:04 -04:00
|
|
|
file << "eval_gemfile File.expand_path('#{relative.join('Gemfile.shared')}')\n"
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
lockfile = repo_path.join('Gemfile.lock')
|
|
|
|
lockfile.delete if lockfile.exist?
|
|
|
|
system('bundle install')
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
|
|
|
|
# Not in the docs. Number from chatting with their support.
|
|
|
|
CIRCLE_CI_CONTAINER_PROCESSES = 2
|
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Number of parallel processes to use
|
2015-06-13 01:04:07 -04:00
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def parallel_processes
|
|
|
|
if ENV['CI']
|
|
|
|
Mutant::Config::DEFAULT.jobs
|
|
|
|
else
|
|
|
|
Parallel.processor_count
|
|
|
|
end
|
2014-12-22 10:52:15 -05:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
|
2015-07-03 11:21:39 -04:00
|
|
|
# Repository path
|
2015-06-13 01:04:07 -04:00
|
|
|
#
|
|
|
|
# @return [Pathname]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def repo_path
|
|
|
|
TMP.join(name)
|
2014-08-06 13:44:21 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
|
|
|
|
# Test if installation should be skipped
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def noinstall?
|
|
|
|
ENV.key?('NOINSTALL')
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
|
|
|
|
# Print start progress
|
|
|
|
#
|
|
|
|
# @param [Pathname] path
|
|
|
|
# @param [Fixnum] _index
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
def start(path, _index)
|
|
|
|
MUTEX.synchronize do
|
|
|
|
puts format('Starting - %s', path)
|
|
|
|
end
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
|
|
|
|
# Print finish progress
|
|
|
|
#
|
|
|
|
# @param [Pathname] path
|
|
|
|
# @param [Fixnum] _index
|
|
|
|
# @param [Fixnum] count
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
def finish(path, _index, count)
|
|
|
|
MUTEX.synchronize do
|
|
|
|
puts format('Mutations - %4i - %s', count, path)
|
|
|
|
end
|
2014-07-12 12:26:28 -04:00
|
|
|
end
|
2015-06-13 01:04:07 -04:00
|
|
|
|
|
|
|
# Helper method to execute system commands
|
|
|
|
#
|
|
|
|
# @param [Array<String>] arguments
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
def system(arguments)
|
|
|
|
return if Kernel.system(*arguments)
|
|
|
|
if block_given?
|
|
|
|
yield
|
|
|
|
else
|
2015-07-23 17:36:46 -04:00
|
|
|
fail "System command failed!: #{arguments.join(' ')}"
|
2015-06-13 01:04:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# rubocop:disable ClosingParenthesisIndentation
|
|
|
|
LOADER = Morpher.build do
|
|
|
|
s(:block,
|
|
|
|
s(:guard, s(:primitive, Array)),
|
|
|
|
s(:map,
|
|
|
|
s(:block,
|
|
|
|
s(:guard, s(:primitive, Hash)),
|
|
|
|
s(:hash_transform,
|
|
|
|
s(:key_symbolize, :repo_uri, s(:guard, s(:primitive, String))),
|
|
|
|
s(:key_symbolize, :name, s(:guard, s(:primitive, String))),
|
|
|
|
s(:key_symbolize, :namespace, s(:guard, s(:primitive, String))),
|
|
|
|
s(:key_symbolize, :expect_coverage, s(:guard, s(:primitive, Fixnum))),
|
|
|
|
s(:key_symbolize, :mutation_coverage,
|
|
|
|
s(:guard, s(:or, s(:primitive, TrueClass), s(:primitive, FalseClass)))),
|
|
|
|
s(:key_symbolize, :mutation_generation,
|
|
|
|
s(:guard, s(:or, s(:primitive, TrueClass), s(:primitive, FalseClass)))),
|
|
|
|
s(:key_symbolize, :exclude, s(:map, s(:guard, s(:primitive, String))))
|
|
|
|
),
|
|
|
|
s(:load_attribute_hash,
|
|
|
|
# NOTE: The domain param has no DSL currently!
|
|
|
|
Morpher::Evaluator::Transformer::Domain::Param.new(
|
|
|
|
Project,
|
|
|
|
%i[repo_uri name exclude mutation_coverage mutation_generation]
|
|
|
|
)
|
2014-07-12 12:26:28 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
2015-06-13 01:04:07 -04:00
|
|
|
end
|
2014-07-12 12:26:28 -04:00
|
|
|
|
2015-06-13 01:04:07 -04:00
|
|
|
ALL = LOADER.call(YAML.load_file(ROOT.join('spec', 'integrations.yml')))
|
|
|
|
end # Project
|
|
|
|
end # Corpus
|
|
|
|
end # MutantSpec
|