2012-08-28 12:57:39 -04:00
|
|
|
module Mutant
|
2012-09-15 18:51:47 -04:00
|
|
|
# Comandline parser
|
2012-08-28 12:57:39 -04:00
|
|
|
class CLI
|
2012-12-10 18:17:19 -05:00
|
|
|
include Adamantium::Flat, Equalizer.new(:matcher, :filter, :strategy, :reporter)
|
2012-08-28 12:57:39 -04:00
|
|
|
|
|
|
|
# Error raised when CLI argv is inalid
|
|
|
|
Error = Class.new(RuntimeError)
|
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
EXIT_FAILURE = 1
|
|
|
|
EXIT_SUCCESS = 0
|
|
|
|
|
2013-01-21 14:08:30 -05:00
|
|
|
OPTIONS = {
|
|
|
|
'--code' => [:add_filter, Mutation::Filter::Code ],
|
|
|
|
'--debug' => [:set_debug ],
|
|
|
|
'-d' => [:set_debug ],
|
|
|
|
'--rspec-unit' => [:set_strategy, Strategy::Rspec::Unit ],
|
|
|
|
'--rspec-full' => [:set_strategy, Strategy::Rspec::Full ],
|
|
|
|
'--rspec-dm2' => [:set_strategy, Strategy::Rspec::DM2 ],
|
|
|
|
'--static-fail' => [:set_strategy, Strategy::Static::Fail ],
|
|
|
|
'--static-success' => [:set_strategy, Strategy::Static::Success ]
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
OPTION_PATTERN = %r(\A-(?:-)?[a-zA-Z0-9\-]+\z).freeze
|
|
|
|
|
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-11-21 16:28:08 -05:00
|
|
|
error = Runner.run(new(*arguments)).fail?
|
|
|
|
error ? EXIT_FAILURE : EXIT_SUCCESS
|
|
|
|
rescue Error => exception
|
|
|
|
$stderr.puts(exception.message)
|
|
|
|
EXIT_FAILURE
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return matcher
|
|
|
|
#
|
|
|
|
# @return [Mutant::Matcher]
|
|
|
|
#
|
|
|
|
# @raise [CLI::Error]
|
|
|
|
# raises error when matcher is not given
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def matcher
|
|
|
|
if @matchers.empty?
|
|
|
|
raise Error, 'No matchers given'
|
|
|
|
end
|
|
|
|
|
|
|
|
Mutant::Matcher::Chain.build(@matchers)
|
|
|
|
end
|
|
|
|
memoize :matcher
|
|
|
|
|
2012-12-10 18:17:19 -05:00
|
|
|
# Test for running in debug mode
|
|
|
|
#
|
|
|
|
# @return [true]
|
|
|
|
# if debug mode is active
|
|
|
|
#
|
|
|
|
# @return [false]
|
|
|
|
# otherwise
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def debug?
|
|
|
|
!!@debug
|
|
|
|
end
|
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
# Return mutation filter
|
|
|
|
#
|
|
|
|
# @return [Mutant::Matcher]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def filter
|
|
|
|
if @filters.empty?
|
|
|
|
Mutation::Filter::ALL
|
|
|
|
else
|
|
|
|
Mutation::Filter::Whitelist.new(@filters)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
memoize :filter
|
|
|
|
|
2012-11-21 18:10:50 -05:00
|
|
|
# Return stratety
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
2012-11-21 18:10:50 -05:00
|
|
|
# @return [Strategy]
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-11-21 18:10:50 -05:00
|
|
|
def strategy
|
2013-01-14 08:13:39 -05:00
|
|
|
@strategy || raise(Error, 'No strategy was set!')
|
2012-12-10 18:17:19 -05:00
|
|
|
@strategy.new(self)
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
2012-12-10 18:17:19 -05:00
|
|
|
memoize :strategy
|
2012-08-28 12:57:39 -04:00
|
|
|
|
2012-11-21 16:28:08 -05:00
|
|
|
# Return reporter
|
2012-08-28 13:43:15 -04:00
|
|
|
#
|
2012-11-21 16:28:08 -05:00
|
|
|
# @return [Mutant::Reporter::CLI]
|
2012-08-28 13:43:15 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-11-21 16:28:08 -05:00
|
|
|
def reporter
|
2012-12-10 18:17:19 -05:00
|
|
|
Mutant::Reporter::CLI.new(self)
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
2012-11-21 16:28:08 -05:00
|
|
|
memoize :reporter
|
2012-08-28 12:57:39 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
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 = [], []
|
|
|
|
|
2012-09-15 18:51:47 -04:00
|
|
|
@arguments, @index = arguments, 0
|
2012-08-28 12:57:39 -04:00
|
|
|
|
|
|
|
while @index < @arguments.length
|
|
|
|
dispatch
|
|
|
|
end
|
2012-11-21 16:28:08 -05:00
|
|
|
|
2012-11-21 18:10:50 -05:00
|
|
|
strategy
|
2012-11-21 16:28:08 -05:00
|
|
|
matcher
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
|
|
|
|
2012-12-10 18:17:19 -05:00
|
|
|
# Return option for argument with index
|
|
|
|
#
|
|
|
|
# @param [Fixnum] index
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def option(index)
|
|
|
|
@arguments.fetch(index+1)
|
|
|
|
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
|
2012-09-15 18:51:47 -04:00
|
|
|
raise Error, "#{current_argument.inspect} is missing an argument"
|
2012-08-28 12:57:39 -04:00
|
|
|
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
|
2012-11-21 18:10:50 -05:00
|
|
|
if OPTION_PATTERN =~ current_argument
|
2012-08-28 12:57:39 -04:00
|
|
|
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
|
|
|
|
consume(1)
|
2013-01-21 14:08:30 -05:00
|
|
|
@matchers << Classifier.build(argument)
|
2012-08-28 12:57:39 -04:00
|
|
|
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
|
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @param [Class<Mutant::Filter>] klass
|
2012-08-28 13:43:15 -04:00
|
|
|
#
|
|
|
|
# @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-12-10 18:17:19 -05:00
|
|
|
# Set debug mode
|
2012-11-21 18:10:50 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
# @return [undefined]
|
2012-11-21 18:10:50 -05:00
|
|
|
#
|
2012-12-10 18:17:19 -05:00
|
|
|
def set_debug
|
2012-11-21 16:55:53 -05:00
|
|
|
consume(1)
|
2012-12-10 18:17:19 -05:00
|
|
|
@debug = true
|
2012-11-21 16:55:53 -05:00
|
|
|
end
|
|
|
|
|
2012-11-21 18:10:50 -05:00
|
|
|
# Set strategy
|
|
|
|
#
|
2012-12-12 16:31:14 -05:00
|
|
|
# @param [Strategy] strategy
|
2012-11-21 18:10:50 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
def set_strategy(strategy)
|
|
|
|
consume(1)
|
|
|
|
@strategy = strategy
|
|
|
|
end
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
|
|
|
end
|