2013-05-14 21:25:16 -04:00
|
|
|
require 'optparse'
|
|
|
|
|
2012-08-28 12:57:39 -04:00
|
|
|
module Mutant
|
2013-02-02 10:56:48 -05:00
|
|
|
|
2012-09-15 18:51:47 -04:00
|
|
|
# Comandline parser
|
2013-05-14 21:25:16 -04:00
|
|
|
class CLI
|
2013-04-20 15:10:14 -04:00
|
|
|
include Adamantium::Flat, Equalizer.new(:config)
|
2012-08-28 12:57:39 -04:00
|
|
|
|
2013-05-14 21:25:16 -04:00
|
|
|
# Error raised when CLI argv is inalid
|
|
|
|
Error = Class.new(RuntimeError)
|
|
|
|
|
|
|
|
EXIT_FAILURE = 1
|
|
|
|
EXIT_SUCCESS = 0
|
|
|
|
|
2012-08-28 13:43:15 -04:00
|
|
|
# Run cli with arguments
|
|
|
|
#
|
|
|
|
# @param [Array<String>] arguments
|
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
# returns exit status
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-05-14 21:25:16 -04:00
|
|
|
def self.run(arguments)
|
|
|
|
config = new(arguments).config
|
2013-02-02 10:32:13 -05:00
|
|
|
runner = Runner::Config.run(config)
|
|
|
|
runner.success? ? EXIT_SUCCESS : EXIT_FAILURE
|
2012-11-21 16:28:08 -05:00
|
|
|
rescue Error => exception
|
|
|
|
$stderr.puts(exception.message)
|
|
|
|
EXIT_FAILURE
|
|
|
|
end
|
|
|
|
|
2013-02-02 10:56:48 -05:00
|
|
|
# Initialize objecct
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
2013-02-02 10:56:48 -05:00
|
|
|
# @param [Array<String>]
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
2013-02-02 10:56:48 -05:00
|
|
|
# @return [undefined]
|
2012-11-21 16:28:08 -05:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-05-14 21:25:16 -04:00
|
|
|
def initialize(arguments=[])
|
2013-02-02 10:56:48 -05:00
|
|
|
@filters, @matchers = [], []
|
2013-05-14 21:25:16 -04:00
|
|
|
|
|
|
|
parse(arguments)
|
2013-02-02 10:56:48 -05:00
|
|
|
strategy
|
|
|
|
matcher
|
2012-11-21 16:28:08 -05:00
|
|
|
end
|
|
|
|
|
2013-02-24 14:40:23 -05:00
|
|
|
# Return config
|
|
|
|
#
|
|
|
|
# @return [Config]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def config
|
|
|
|
Config.new(
|
|
|
|
:debug => debug?,
|
|
|
|
:matcher => matcher,
|
|
|
|
:filter => filter,
|
|
|
|
:strategy => strategy,
|
|
|
|
:reporter => reporter
|
|
|
|
)
|
|
|
|
end
|
|
|
|
memoize :config
|
|
|
|
|
|
|
|
private
|
|
|
|
|
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-04-20 14:55:37 -04:00
|
|
|
@strategy or 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
|
2013-04-20 20:55:09 -04:00
|
|
|
Reporter::CLI.new($stdout)
|
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
|
|
|
|
2013-02-02 10:56:48 -05:00
|
|
|
# Return matcher
|
2012-08-28 13:43:15 -04:00
|
|
|
#
|
2013-02-02 10:56:48 -05:00
|
|
|
# @return [Mutant::Matcher]
|
2012-08-28 13:43:15 -04:00
|
|
|
#
|
|
|
|
# @raise [CLI::Error]
|
2013-02-02 10:56:48 -05:00
|
|
|
# raises error when matcher is not given
|
2012-08-28 13:43:15 -04:00
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-02-02 10:56:48 -05:00
|
|
|
def matcher
|
|
|
|
if @matchers.empty?
|
|
|
|
raise Error, 'No matchers given'
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
2013-02-02 10:56:48 -05:00
|
|
|
|
2013-04-20 20:55:09 -04:00
|
|
|
Matcher::Chain.build(@matchers)
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
2013-02-02 10:56:48 -05:00
|
|
|
memoize :matcher
|
2012-08-28 12:57:39 -04:00
|
|
|
|
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
|
|
|
#
|
2013-05-14 21:25:16 -04:00
|
|
|
# @param [String] filter
|
|
|
|
#
|
2012-08-28 13:43:15 -04:00
|
|
|
# @return [undefined]
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
2013-05-14 21:25:16 -04:00
|
|
|
def add_filter(klass,filter)
|
|
|
|
@filters << klass.new(filter)
|
2012-08-28 12:57:39 -04:00
|
|
|
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
|
|
|
|
@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)
|
|
|
|
@strategy = strategy
|
|
|
|
end
|
2013-05-14 21:25:16 -04:00
|
|
|
|
|
|
|
# Parses the command-line options.
|
|
|
|
#
|
|
|
|
# @param [Array<String>] arguments
|
|
|
|
# Command-line options and arguments to be parsed.
|
|
|
|
#
|
|
|
|
# @raise [Error]
|
|
|
|
# An error occurred while parsing the options.
|
|
|
|
#
|
|
|
|
# @api private
|
|
|
|
#
|
|
|
|
def parse(arguments)
|
|
|
|
opts = OptionParser.new do |opts|
|
|
|
|
opts.banner = 'usage: mutant STRATEGY [options] MATCHERS ...'
|
|
|
|
|
|
|
|
opts.separator ''
|
|
|
|
opts.separator 'Strategies:'
|
|
|
|
|
2013-05-15 04:25:33 -04:00
|
|
|
opts.on('--rspec-unit', 'executes all specs under ./spec/unit') do
|
2013-05-14 21:25:16 -04:00
|
|
|
set_strategy Strategy::Rspec::Unit
|
|
|
|
end
|
|
|
|
|
2013-05-15 04:25:33 -04:00
|
|
|
opts.on('--rspec-full', 'executes all specs under ./spec') do
|
2013-05-14 21:25:16 -04:00
|
|
|
set_strategy Strategy::Rspec::Full
|
|
|
|
end
|
|
|
|
|
2013-05-15 04:25:33 -04:00
|
|
|
opts.on('--rspec-dm2', 'executes spec/unit/namespace/class/method_spec.rb') do
|
2013-05-14 21:25:16 -04:00
|
|
|
set_strategy Strategy::Rspec::DM2
|
|
|
|
end
|
|
|
|
|
|
|
|
opts.separator ''
|
|
|
|
opts.separator 'Options:'
|
|
|
|
|
2013-05-15 04:25:33 -04:00
|
|
|
opts.on('--code FILTER', 'Adds a code filter') do |filter|
|
2013-05-14 21:25:16 -04:00
|
|
|
add_filter Mutation::Filter::Code, filter
|
|
|
|
end
|
|
|
|
|
2013-05-15 04:25:33 -04:00
|
|
|
opts.on('-d','--debug', 'Enable debugging output') do
|
2013-05-14 21:25:16 -04:00
|
|
|
set_debug
|
|
|
|
end
|
|
|
|
|
2013-05-15 04:25:33 -04:00
|
|
|
opts.on_tail('-h', '--help', 'Show this message') do
|
2013-05-14 21:25:16 -04:00
|
|
|
puts opts
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
matchers = begin
|
|
|
|
opts.parse!(arguments)
|
2013-05-15 04:35:13 -04:00
|
|
|
rescue OptionParser::ParseError => error
|
|
|
|
raise(Error, error.message, error.backtrace)
|
2013-05-14 21:25:16 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
matchers.each do |pattern|
|
|
|
|
matcher = Classifier.build(pattern)
|
|
|
|
@matchers << matcher if matcher
|
|
|
|
end
|
|
|
|
end
|
2012-08-28 12:57:39 -04:00
|
|
|
end
|
|
|
|
end
|