free_mutant/lib/mutant/cli.rb

287 lines
5.7 KiB
Ruby
Raw Normal View History

# encoding: utf-8
require 'optparse'
2012-08-28 12:57:39 -04:00
module Mutant
# Comandline parser
class CLI
include Adamantium::Flat, Equalizer.new(:config)
2012-08-28 12:57:39 -04:00
2013-06-13 13:10:34 -04:00
# Error raised when CLI argv is invalid
Error = Class.new(RuntimeError)
EXIT_FAILURE = 1
EXIT_SUCCESS = 0
# Run cli with arguments
#
# @param [Array<String>] arguments
#
# @return [Fixnum]
# returns exit status
#
# @api private
#
def self.run(arguments)
config = new(arguments).config
runner = Runner::Config.run(config)
runner.success? ? EXIT_SUCCESS : EXIT_FAILURE
rescue Error => exception
$stderr.puts(exception.message)
EXIT_FAILURE
end
# Initialize objecct
#
# @param [Array<String>]
#
# @return [undefined]
#
# @api private
#
def initialize(arguments = [])
@filters, @matchers = [], []
@cache = Mutant::Cache.new
parse(arguments)
strategy
matcher
end
# Return config
#
# @return [Config]
#
# @api private
#
def config
Config.new(
:cache => @cache,
2013-08-04 17:03:39 -04:00
:zombie => @zombie,
:debug => debug?,
:matcher => matcher,
:filter => filter,
:fail_fast => !!@fail_fast,
:strategy => strategy,
:reporter => reporter
)
end
memoize :config
private
# Test for running in debug mode
#
# @return [true]
# if debug mode is active
#
# @return [false]
# otherwise
#
# @api private
#
def debug?
!!@debug
end
# 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 18:10:50 -05:00
# @return [Strategy]
#
# @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-08-28 12:57:39 -04:00
end
memoize :strategy
2012-08-28 12:57:39 -04:00
# Return reporter
#
# @return [Mutant::Reporter::CLI]
#
# @api private
#
def reporter
Reporter::CLI.new($stdout)
2012-08-28 12:57:39 -04:00
end
memoize :reporter
2012-08-28 12:57:39 -04:00
# 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'
2012-08-28 12:57:39 -04:00
end
Matcher::Chain.build(@matchers)
2012-08-28 12:57:39 -04:00
end
memoize :matcher
2012-08-28 12:57:39 -04:00
# Add mutation filter
#
# @param [Class<Mutant::Filter>] klass
#
# @param [String] filter
#
# @return [undefined]
#
# @api private
#
def add_filter(klass, filter)
@filters << klass.new(filter)
2012-08-28 12:57:39 -04:00
end
2012-11-21 18:10:50 -05:00
# Set strategy
#
# @param [Strategy] strategy
2012-11-21 18:10:50 -05:00
#
# @api private
#
# @return [undefined]
#
def set_strategy(strategy)
@strategy = strategy
end
2013-06-15 10:37:43 -04:00
# Parse 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.
#
2013-06-15 10:37:43 -04:00
# @return [undefined]
#
# @api private
#
def parse(arguments)
2013-07-28 13:58:53 -04:00
opts = OptionParser.new do |builder|
builder.banner = 'usage: mutant STRATEGY [options] MATCHERS ...'
builder.separator ''
builder.separator 'Strategies:'
2013-07-28 13:58:53 -04:00
add_strategies(builder)
add_options(builder)
end
2013-06-21 11:21:04 -04:00
matchers =
begin
opts.parse!(arguments)
rescue OptionParser::ParseError => error
raise(Error, error.message, error.backtrace)
end
parse_matchers(matchers)
end
# Parse matchers
#
# @param [Enumerable<String>] patterns
#
2013-06-21 21:47:23 -04:00
# @return [undefined]
#
# @api private
#
def parse_matchers(patterns)
patterns.each do |pattern|
matcher = Classifier.build(@cache, pattern)
@matchers << matcher if matcher
end
end
2013-06-14 14:54:02 -04:00
2013-06-21 11:21:04 -04:00
# Add strategies
#
2013-08-04 17:03:39 -04:00
# @param [Object] opts
2013-06-21 11:21:04 -04:00
#
2013-06-21 21:47:23 -04:00
# @return [undefined]
#
2013-06-21 11:21:04 -04:00
# @api private
#
def add_strategies(opts)
opts.separator ''
opts.separator 'Strategies:'
2013-07-17 14:03:52 -04:00
opts.on('--static-success', 'does succeed on all mutations') do
set_strategy Strategy::Static::Success.new
end
opts.on('--static-fail', 'does fail on all mutations') do
set_strategy Strategy::Static::Fail.new
end
2013-07-17 13:45:47 -04:00
opts.on('--rspec', 'kills mutations with rspec') do
set_strategy Strategy::Rspec.new
2013-06-21 11:21:04 -04:00
end
end
2013-08-04 17:03:39 -04:00
# Add environmental options
#
# @param [Object] opts
#
# @return [undefined]
#
# @api private
#
def add_environmental_options(opts)
opts.on('--zombie', 'Run mutant zombified') do
@zombie = true
end.on('-I', 'Add directory to $LOAD_PATH') do |directory|
$LOAD_PATH << directory
end.on('-r', '--require NAME', 'Require file with NAME') do |name|
require name
end
end
2013-06-21 11:21:04 -04:00
# Add options
#
2013-08-04 17:03:39 -04:00
# @param [Object] opts
2013-06-21 11:21:04 -04:00
#
2013-06-21 21:47:23 -04:00
# @return [undefined]
#
2013-06-21 11:21:04 -04:00
# @api private
#
def add_options(opts)
opts.separator ''
opts.separator 'Options:'
opts.on('--version', 'Print mutants version') do |name|
2013-08-04 17:03:39 -04:00
puts("mutant-#{Mutant::VERSION}")
Kernel.exit(0)
2013-08-02 19:41:21 -04:00
end.on('--code FILTER', 'Adds a code filter') do |filter|
2013-08-04 17:03:39 -04:00
add_filter(Mutation::Filter::Code, filter)
end.on('--fail-fast', 'Fail fast') do
2013-08-04 17:03:39 -04:00
@fail_fast = true
end.on('-d', '--debug', 'Enable debugging output') do
2013-08-04 17:03:39 -04:00
@debug = true
2013-06-21 11:21:04 -04:00
end.on_tail('-h', '--help', 'Show this message') do
2013-08-04 17:03:39 -04:00
puts(opts)
2013-06-21 11:21:04 -04:00
exit
end
end
2013-06-14 14:54:02 -04:00
end # CLI
end # Mutant