Skip to content

Commit

Permalink
Change to separate Cli::Command::Environment abstraction
Browse files Browse the repository at this point in the history
* This allows to add other subcommands that depend on the environment
  without adding duplication
  • Loading branch information
mbj committed Dec 1, 2020
1 parent 8edba09 commit 6f7ac46
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 51 deletions.
3 changes: 2 additions & 1 deletion lib/mutant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,9 @@ module Mutant
require 'mutant/config'
require 'mutant/cli'
require 'mutant/cli/command'
require 'mutant/cli/command/run'
require 'mutant/cli/command/subscription'
require 'mutant/cli/command/environment'
require 'mutant/cli/command/environment/run'
require 'mutant/cli/command/root'
require 'mutant/runner'
require 'mutant/runner/sink'
Expand Down
2 changes: 1 addition & 1 deletion lib/mutant/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def full_name
#
# @return [Bool]
def zombie?
instance_of?(Run)
false
end

private
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@
module Mutant
module CLI
class Command
# rubocop:disable Metrics/ClassLength
class Run < self
NAME = 'run'
SHORT_DESCRIPTION = 'Run code analysis'

class Environment < self
OPTIONS =
%i[
add_environment_options
Expand All @@ -16,59 +12,17 @@ class Run < self
add_matcher_options
].freeze

SLEEP = 40

UNLICENSED = <<~MESSAGE.lines.freeze
Soft fail, continuing in #{SLEEP} seconds
Next major version will enforce the license
See https://github.com/mbj/mutant#licensing
MESSAGE

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

private

def initialize(attributes)
super(attributes)
@config = Config.env
end

def execute
soft_fail(License.apply(world))
.bind { Config.load_config_file(world) }
.fmap(&method(:expand))
.bind { Bootstrap.apply(world, @config) }
.bind(&Runner.public_method(:apply))
.from_right { |error| world.stderr.puts(error); return false }
.success?
end

def expand(file_config)
@config = file_config.merge(@config)
end

def soft_fail(result)
result.either(
lambda do |message|
stderr = world.stderr
stderr.puts(message)
UNLICENSED.each { |line| stderr.puts(unlicensed(line)) }
world.kernel.sleep(SLEEP)
Either::Right.new(nil)
end,
->(_subscription) { Either::Right.new(nil) }
)
end

def unlicensed(message)
"[Mutant-License-Error]: #{message}"
end

def parse_remaining_arguments(arguments)
traverse(@config.expression_parser.public_method(:apply), arguments)
.fmap do |match_expressions|
Expand Down Expand Up @@ -158,7 +112,6 @@ def add_runner_options(parser)
end
end
end # Run
# rubocop:enable Metrics/ClassLength
end # Command
end # CLI
end # Mutant
58 changes: 58 additions & 0 deletions lib/mutant/cli/command/environment/run.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

module Mutant
module CLI
class Command
class Environment
class Run < self
NAME = 'run'
SHORT_DESCRIPTION = 'Run code analysis'

SLEEP = 40

UNLICENSED = <<~MESSAGE.lines.freeze
Soft fail, continuing in #{SLEEP} seconds
Next major version will enforce the license
See https://github.com/mbj/mutant#licensing
MESSAGE

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

private

def execute
soft_fail(License.apply(world))
.bind { Config.load_config_file(world) }
.fmap(&method(:expand))
.bind { Bootstrap.apply(world, @config) }
.bind(&Runner.public_method(:apply))
.from_right { |error| world.stderr.puts(error); return false }
.success?
end

def soft_fail(result)
result.either(
lambda do |message|
stderr = world.stderr
stderr.puts(message)
UNLICENSED.each { |line| stderr.puts(unlicensed(line)) }
world.kernel.sleep(SLEEP)
Either::Right.new(nil)
end,
->(_subscription) { Either::Right.new(nil) }
)
end

def unlicensed(message)
"[Mutant-License-Error]: #{message}"
end
end # Run
end # Environment
end # Command
end # CLI
end # Mutant
2 changes: 1 addition & 1 deletion lib/mutant/cli/command/root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Mutant
module CLI
class Command
class Root < self
SUBCOMMANDS = [Run, Subscription].freeze
SUBCOMMANDS = [Environment::Run, Subscription].freeze
SHORT_DESCRIPTION = 'mutation testing engine main command'
NAME = 'mutant'
end
Expand Down

0 comments on commit 6f7ac46

Please sign in to comment.