Skip to content

Commit

Permalink
Change --zombie to be a true global option
Browse files Browse the repository at this point in the history
* Currently the CLI runner evaluates this globally but we could only set
  this on the environment related subcommands.
* Running in zombie mode (only useful for mutant developers, users do
  not have to care) is useful in any other mode.
  • Loading branch information
mbj committed Aug 8, 2022
1 parent 5a6afef commit f7c8750
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 45 deletions.
3 changes: 1 addition & 2 deletions lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ class Config
matcher: Matcher::Config::DEFAULT,
mutation: Mutation::Config::DEFAULT,
reporter: Reporter::CLI.build(WORLD.stdout),
requires: EMPTY_ARRAY,
zombie: false
requires: EMPTY_ARRAY
)
end # Config

Expand Down
33 changes: 23 additions & 10 deletions lib/mutant/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mutant
module CLI
# rubocop:disable Metrics/ClassLength
class Command
include AbstractType, Anima.new(:world, :main, :parent)
include AbstractType, Anima.new(:world, :main, :parent, :zombie)

OPTIONS = [].freeze
SUBCOMMANDS = [].freeze
Expand All @@ -19,9 +19,17 @@ class OptionParser < ::OptionParser
# Parse command
#
# @return [Command]
def self.parse(arguments:, parent: nil, world:)
new(main: nil, parent: parent, world: world).__send__(:parse, arguments)
#
# rubocop:disable Metrics/ParameterLists
def self.parse(arguments:, parent: nil, world:, zombie: false)
new(
main: nil,
parent: parent,
world: world,
zombie: zombie
).__send__(:parse, arguments)
end
# rubocop:enable Metrics/ParameterLists

# Command name
#
Expand Down Expand Up @@ -51,12 +59,7 @@ def full_name
[*parent&.full_name, self.class.command_name].join(' ')
end

# Test if command needs to be executed in zombie environment
#
# @return [Bool]
def zombie?
false
end
alias_method :zombie?, :zombie

abstract_method :action

Expand Down Expand Up @@ -118,6 +121,7 @@ def add_summary(parser)
parser.separator(nil)
end

# rubocop:disable Metrics/MethodLength
def add_global_options(parser)
parser.separator("mutant version: #{VERSION}")
parser.separator(nil)
Expand All @@ -131,6 +135,10 @@ def add_global_options(parser)
parser.on('--version', 'Print mutants version') do
capture_main { world.stdout.puts("mutant-#{VERSION}"); true }
end

parser.on('--zombie', 'Run mutant zombified') do
@zombie = true
end
end

def add_subcommands(parser)
Expand Down Expand Up @@ -167,7 +175,12 @@ def parse_subcommand(arguments)
Either::Left.new(with_help('Missing required subcommand!'))
else
find_command(command_name).bind do |command|
command.parse(world: world, parent: self, arguments: arguments)
command.parse(
arguments: arguments,
parent: self,
world: world,
zombie: zombie
)
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions lib/mutant/cli/command/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ def add_matcher(attribute, value)
# rubocop:disable Metrics/MethodLength
def add_environment_options(parser)
parser.separator('Environment:')
parser.on('--zombie', 'Run mutant zombified') do
set(zombie: true)
end
parser.on('-I', '--include DIRECTORY', 'Add DIRECTORY to $LOAD_PATH') do |directory|
add(:includes, directory)
end
Expand Down
7 changes: 0 additions & 7 deletions lib/mutant/cli/command/environment/run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ class Run < self
===============
MESSAGE

# Test if command needs to be executed in zombie environment
#
# @return [Bool]
def zombie?
@config.zombie
end

private

def action
Expand Down
8 changes: 3 additions & 5 deletions lib/mutant/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ class Config
:matcher,
:mutation,
:reporter,
:requires,
:zombie
:requires
)

%i[fail_fast zombie].each do |name|
%i[fail_fast].each do |name|
define_method(:"#{name}?") { public_send(name) }
end

Expand Down Expand Up @@ -74,8 +73,7 @@ def merge(other)
jobs: other.jobs || jobs,
matcher: matcher.merge(other.matcher),
mutation: mutation.merge(other.mutation),
requires: requires + other.requires,
zombie: zombie || other.zombie
requires: requires + other.requires
)
end
# rubocop:enable Metrics/AbcSize
Expand Down
76 changes: 64 additions & 12 deletions spec/unit/mutant/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
describe '.parse' do
let(:env_config) { Mutant::Config::DEFAULT.with(jobs: 4) }
let(:events) { [] }
let(:expect_zombie) { false }
let(:expected_zombie) { false }
let(:kernel) { class_double(Kernel) }
let(:load_config_file_config) { Mutant::Config::DEFAULT }
let(:stderr) { instance_double(IO, :stderr, tty?: false) }
Expand Down Expand Up @@ -56,7 +56,7 @@ def apply
end

it 'sets expected zombie flag' do
expect(apply.from_right.zombie?).to be(expect_zombie)
expect(apply.from_right.zombie?).to be(expected_zombie)
end
end

Expand Down Expand Up @@ -96,6 +96,7 @@ def self.main_body
--help Print help
--version Print mutants version
--zombie Run mutant zombified
Available subcommands:
Expand Down Expand Up @@ -159,6 +160,17 @@ def self.main_body
}
end

make do
message = "#{main_body}\n"

{
arguments: %w[--zombie --help],
expected_events: [[:stdout, :puts, message]],
expected_exit: true,
expected_zombie: true
}
end

make do
{
arguments: %w[--version],
Expand All @@ -182,6 +194,7 @@ def self.main_body
--help Print help
--version Print mutants version
--zombie Run mutant zombified
Available subcommands:
Expand Down Expand Up @@ -218,6 +231,7 @@ def self.main_body
--help Print help
--version Print mutants version
--zombie Run mutant zombified
MESSAGE

{
Expand All @@ -240,10 +254,10 @@ def self.main_body
--help Print help
--version Print mutants version
--zombie Run mutant zombified
Environment:
--zombie Run mutant zombified
-I, --include DIRECTORY Add DIRECTORY to $LOAD_PATH
-r, --require NAME Require file with NAME
--env KEY=VALUE Set environment variable
Expand Down Expand Up @@ -273,6 +287,51 @@ def self.main_body
}
end

make do
message = <<~MESSAGE
usage: mutant run [options]
Summary: Run code analysis
mutant version: #{Mutant::VERSION}
Global Options:
--help Print help
--version Print mutants version
--zombie Run mutant zombified
Environment:
-I, --include DIRECTORY Add DIRECTORY to $LOAD_PATH
-r, --require NAME Require file with NAME
--env KEY=VALUE Set environment variable
Runner:
--fail-fast Fail fast
-j, --jobs NUMBER Number of kill jobs. Defaults to number of processors.
-t, --mutation-timeout NUMBER Per mutation analysis timeout
Integration:
--use INTEGRATION Use INTEGRATION to kill mutations
Matcher:
--ignore-subject EXPRESSION Ignore subjects that match EXPRESSION as prefix
--start-subject EXPRESSION Start mutation testing at a specific subject
--since REVISION Only select subjects touched since REVISION
MESSAGE

{
arguments: %w[--zombie run --help],
expected_events: [[:stdout, :puts, message]],
expected_exit: true,
expected_zombie: true
}
end

make do
message = <<~MESSAGE
usage: mutant util <mutation> [options]
Expand All @@ -285,6 +344,7 @@ def self.main_body
--help Print help
--version Print mutants version
--zombie Run mutant zombified
Available subcommands:
Expand All @@ -311,6 +371,7 @@ def self.main_body
--help Print help
--version Print mutants version
--zombie Run mutant zombified
-e, --evaluate SOURCE
Expand Down Expand Up @@ -1065,15 +1126,6 @@ def self.main_body
include_examples 'CLI run'
end

context 'with --zombie flag' do
let(:arguments) { super() + %w[--zombie] }
let(:bootstrap_config) { super().with(zombie: true) }
let(:expect_zombie) { true }
let(:load_config_file_config) { super().with(zombie: true) }

include_examples 'CLI run'
end

context 'with --fail-fast option' do
let(:arguments) { super() + %w[--fail-fast] }
let(:bootstrap_config) { super().with(fail_fast: true) }
Expand Down
6 changes: 0 additions & 6 deletions spec/unit/mutant/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,6 @@ def expect_value(value)
include_examples 'descendant merge'
end

context 'merging zombie' do
let(:key) { :zombie }

include_examples 'sticky boolean'
end

context 'merging fail fast' do
let(:key) { :fail_fast }

Expand Down

0 comments on commit f7c8750

Please sign in to comment.