2012-08-28 12:57:39 -04:00
|
|
|
module Mutant
|
|
|
|
# Comandline adapter or mutant runner
|
|
|
|
class CLI
|
|
|
|
include Immutable
|
|
|
|
|
|
|
|
# Error raised when CLI argv is inalid
|
|
|
|
Error = Class.new(RuntimeError)
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Run cli with arguments
|
|
|
|
#
|
|
|
|
# @param [Array<String>] arguments
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
# returns exit status
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def self.run(*arguments)
|
2012-08-28 13:43:15 -04:00
|
|
|
error = Runner.run(new(*arguments).runner_options).fail?
|
|
|
|
error ? 1 : 0
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Return options for runner
|
|
|
|
#
|
|
|
|
# @return [Hash]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def runner_options
|
|
|
|
{
|
|
|
|
:mutation_filter => mutation_filter,
|
|
|
|
:matcher => matcher,
|
|
|
|
:reporter => Reporter::CLI.new($stderr),
|
2012-08-28 13:44:59 -04:00
|
|
|
:killer => Killer::Rspec::Forking
|
2012-08-28 12:57:39 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
memoize :runner_options
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
OPTIONS = {
|
|
|
|
'--code' => [:add_filter, Mutation::Filter::Code].freeze
|
|
|
|
}.deep_freeze
|
|
|
|
|
|
|
|
OPTION_PATTERN = %r(\A-(?:-)?[a-z0-9]+\z).freeze
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Return option for argument with index
|
|
|
|
#
|
|
|
|
# @param [Fixnum] index
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def option(index)
|
|
|
|
@arguments.fetch(index+1)
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Initialize CLI
|
|
|
|
#
|
|
|
|
# @param [Array<String>] arguments
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def initialize(arguments)
|
|
|
|
@filters, @matchers = [], []
|
|
|
|
|
|
|
|
@arguments = arguments
|
|
|
|
|
|
|
|
@index = 0
|
|
|
|
|
|
|
|
while @index < @arguments.length
|
|
|
|
dispatch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Return current argument
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def current_argument
|
|
|
|
@arguments.fetch(@index)
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Return current option value
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @raise [CLI::Error]
|
|
|
|
# raises error when option is missing
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def current_option_value
|
|
|
|
@arguments.fetch(@index+1)
|
|
|
|
rescue IndexError
|
|
|
|
raise Error,"#{current_argument.inspect} is missing an argument"
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Process current argument
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def dispatch
|
|
|
|
if OPTION_PATTERN.match(current_argument)
|
|
|
|
dispatch_option
|
|
|
|
else
|
|
|
|
dispatch_matcher
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Move processed argument by amount
|
|
|
|
#
|
|
|
|
# @param [Fixnum] amount
|
|
|
|
# the amount of arguments to be consumed
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def consume(amount)
|
|
|
|
@index += amount
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Process matcher argument
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def dispatch_matcher
|
|
|
|
argument = current_argument
|
|
|
|
matcher = Mutant::Matcher.from_string(argument)
|
|
|
|
|
|
|
|
unless matcher
|
|
|
|
raise Error, "Invalid matcher syntax: #{argument.inspect}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@matchers << matcher
|
|
|
|
|
|
|
|
consume(1)
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Process option argument
|
|
|
|
#
|
|
|
|
# @return [Undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def dispatch_option
|
|
|
|
argument = current_argument
|
|
|
|
arguments = *OPTIONS.fetch(argument) do
|
|
|
|
raise Error, "Unknown option: #{argument.inspect}"
|
|
|
|
end
|
|
|
|
send(*arguments)
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Add mutation filter
|
|
|
|
#
|
|
|
|
# @param [Class<Mutant::Filter>]
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def add_filter(klass)
|
|
|
|
@filters << klass.new(current_option_value)
|
|
|
|
consume(2)
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Return matcher
|
|
|
|
#
|
|
|
|
# @return [Mutant::Matcher]
|
|
|
|
#
|
|
|
|
# @raise [CLI::Error]
|
|
|
|
# raises error when matcher is not given
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def matcher
|
|
|
|
if @matchers.empty?
|
|
|
|
raise Error, 'No matchers given'
|
|
|
|
end
|
|
|
|
|
|
|
|
Mutant::Matcher::Chain.new(@matchers)
|
|
|
|
end
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Return mutation filter
|
|
|
|
#
|
|
|
|
# @return [Mutant::Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-08-28 12:57:39 -04:00
|
|
|
def mutation_filter
|
|
|
|
if @filters.empty?
|
|
|
|
Mutation::Filter::ALL
|
|
|
|
else
|
|
|
|
Mutation::Filter::Whitelist.new(@filters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|