Update CLI.run to return exit status
* Also add yard docs
This commit is contained in:
parent
f972de5066
commit
b557a31b6f
2 changed files with 119 additions and 7 deletions
|
@ -6,16 +6,32 @@ module Mutant
|
||||||
# Error raised when CLI argv is inalid
|
# Error raised when CLI argv is inalid
|
||||||
Error = Class.new(RuntimeError)
|
Error = Class.new(RuntimeError)
|
||||||
|
|
||||||
|
# Run cli with arguments
|
||||||
|
#
|
||||||
|
# @param [Array<String>] arguments
|
||||||
|
#
|
||||||
|
# @return [Fixnum]
|
||||||
|
# returns exit status
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def self.run(*arguments)
|
def self.run(*arguments)
|
||||||
Runner.run(new(*arguments).runner_options)
|
error = Runner.run(new(*arguments).runner_options).fail?
|
||||||
|
error ? 1 : 0
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return options for runner
|
||||||
|
#
|
||||||
|
# @return [Hash]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def runner_options
|
def runner_options
|
||||||
{
|
{
|
||||||
:mutation_filter => mutation_filter,
|
:mutation_filter => mutation_filter,
|
||||||
:matcher => matcher,
|
:matcher => matcher,
|
||||||
:reporter => Reporter::CLI.new($stderr),
|
:reporter => Reporter::CLI.new($stderr),
|
||||||
:killer => Killer::Rspec::Forking
|
:killer => Killer::Rspec
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
memoize :runner_options
|
memoize :runner_options
|
||||||
|
@ -28,10 +44,26 @@ module Mutant
|
||||||
|
|
||||||
OPTION_PATTERN = %r(\A-(?:-)?[a-z0-9]+\z).freeze
|
OPTION_PATTERN = %r(\A-(?:-)?[a-z0-9]+\z).freeze
|
||||||
|
|
||||||
|
# Return option for argument with index
|
||||||
|
#
|
||||||
|
# @param [Fixnum] index
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def option(index)
|
def option(index)
|
||||||
@arguments.fetch(index+1)
|
@arguments.fetch(index+1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Initialize CLI
|
||||||
|
#
|
||||||
|
# @param [Array<String>] arguments
|
||||||
|
#
|
||||||
|
# @return [undefined]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def initialize(arguments)
|
def initialize(arguments)
|
||||||
@filters, @matchers = [], []
|
@filters, @matchers = [], []
|
||||||
|
|
||||||
|
@ -44,16 +76,37 @@ module Mutant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return current argument
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def current_argument
|
def current_argument
|
||||||
@arguments.fetch(@index)
|
@arguments.fetch(@index)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return current option value
|
||||||
|
#
|
||||||
|
# @return [String]
|
||||||
|
#
|
||||||
|
# @raise [CLI::Error]
|
||||||
|
# raises error when option is missing
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def current_option_value
|
def current_option_value
|
||||||
@arguments.fetch(@index+1)
|
@arguments.fetch(@index+1)
|
||||||
rescue IndexError
|
rescue IndexError
|
||||||
raise Error,"#{current_argument.inspect} is missing an argument"
|
raise Error,"#{current_argument.inspect} is missing an argument"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Process current argument
|
||||||
|
#
|
||||||
|
# @return [undefined]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def dispatch
|
def dispatch
|
||||||
if OPTION_PATTERN.match(current_argument)
|
if OPTION_PATTERN.match(current_argument)
|
||||||
dispatch_option
|
dispatch_option
|
||||||
|
@ -62,10 +115,25 @@ module Mutant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Move processed argument by amount
|
||||||
|
#
|
||||||
|
# @param [Fixnum] amount
|
||||||
|
# the amount of arguments to be consumed
|
||||||
|
#
|
||||||
|
# @return [undefined]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def consume(amount)
|
def consume(amount)
|
||||||
@index += amount
|
@index += amount
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Process matcher argument
|
||||||
|
#
|
||||||
|
# @return [undefined]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def dispatch_matcher
|
def dispatch_matcher
|
||||||
argument = current_argument
|
argument = current_argument
|
||||||
matcher = Mutant::Matcher.from_string(argument)
|
matcher = Mutant::Matcher.from_string(argument)
|
||||||
|
@ -79,6 +147,12 @@ module Mutant
|
||||||
consume(1)
|
consume(1)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Process option argument
|
||||||
|
#
|
||||||
|
# @return [Undefined]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def dispatch_option
|
def dispatch_option
|
||||||
argument = current_argument
|
argument = current_argument
|
||||||
arguments = *OPTIONS.fetch(argument) do
|
arguments = *OPTIONS.fetch(argument) do
|
||||||
|
@ -87,11 +161,28 @@ module Mutant
|
||||||
send(*arguments)
|
send(*arguments)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Add mutation filter
|
||||||
|
#
|
||||||
|
# @param [Class<Mutant::Filter>]
|
||||||
|
#
|
||||||
|
# @return [undefined]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def add_filter(klass)
|
def add_filter(klass)
|
||||||
@filters << klass.new(current_option_value)
|
@filters << klass.new(current_option_value)
|
||||||
consume(2)
|
consume(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return matcher
|
||||||
|
#
|
||||||
|
# @return [Mutant::Matcher]
|
||||||
|
#
|
||||||
|
# @raise [CLI::Error]
|
||||||
|
# raises error when matcher is not given
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def matcher
|
def matcher
|
||||||
if @matchers.empty?
|
if @matchers.empty?
|
||||||
raise Error, 'No matchers given'
|
raise Error, 'No matchers given'
|
||||||
|
@ -100,6 +191,12 @@ module Mutant
|
||||||
Mutant::Matcher::Chain.new(@matchers)
|
Mutant::Matcher::Chain.new(@matchers)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Return mutation filter
|
||||||
|
#
|
||||||
|
# @return [Mutant::Matcher]
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
#
|
||||||
def mutation_filter
|
def mutation_filter
|
||||||
if @filters.empty?
|
if @filters.empty?
|
||||||
Mutation::Filter::ALL
|
Mutation::Filter::ALL
|
||||||
|
|
|
@ -6,7 +6,7 @@ describe Mutant::CLI, '.run' do
|
||||||
let(:object) { described_class }
|
let(:object) { described_class }
|
||||||
let(:argv) { mock('ARGV') }
|
let(:argv) { mock('ARGV') }
|
||||||
let(:options) { mock('Options') }
|
let(:options) { mock('Options') }
|
||||||
let(:runner) { mock('Runner') }
|
let(:runner) { mock('Runner', :fail? => failure) }
|
||||||
let(:instance) { mock(described_class.name, :runner_options => options) }
|
let(:instance) { mock(described_class.name, :runner_options => options) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
|
@ -14,10 +14,25 @@ describe Mutant::CLI, '.run' do
|
||||||
Mutant::Runner.stub(:run => runner)
|
Mutant::Runner.stub(:run => runner)
|
||||||
end
|
end
|
||||||
|
|
||||||
it { should be(runner) }
|
context 'when runner NOT fails' do
|
||||||
|
let(:failure) { false }
|
||||||
|
|
||||||
it 'should run with options' do
|
it { should be(0) }
|
||||||
Mutant::Runner.should_receive(:run).with(options).and_return(runner)
|
|
||||||
should be(runner)
|
it 'should run with options' do
|
||||||
|
Mutant::Runner.should_receive(:run).with(options).and_return(runner)
|
||||||
|
should be(0)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when runner fails' do
|
||||||
|
let(:failure) { true }
|
||||||
|
|
||||||
|
it { should be(1) }
|
||||||
|
|
||||||
|
it 'should run with options' do
|
||||||
|
Mutant::Runner.should_receive(:run).with(options).and_return(runner)
|
||||||
|
should be(1)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue