Skip to content

Commit 6f7ac46

Browse files
committed
Change to separate Cli::Command::Environment abstraction
* This allows to add other subcommands that depend on the environment without adding duplication
1 parent 8edba09 commit 6f7ac46

File tree

5 files changed

+63
-51
lines changed

5 files changed

+63
-51
lines changed

lib/mutant.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,9 @@ module Mutant
167167
require 'mutant/config'
168168
require 'mutant/cli'
169169
require 'mutant/cli/command'
170-
require 'mutant/cli/command/run'
171170
require 'mutant/cli/command/subscription'
171+
require 'mutant/cli/command/environment'
172+
require 'mutant/cli/command/environment/run'
172173
require 'mutant/cli/command/root'
173174
require 'mutant/runner'
174175
require 'mutant/runner/sink'

lib/mutant/cli/command.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def full_name
6666
#
6767
# @return [Bool]
6868
def zombie?
69-
instance_of?(Run)
69+
false
7070
end
7171

7272
private

lib/mutant/cli/command/run.rb renamed to lib/mutant/cli/command/environment.rb

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33
module Mutant
44
module CLI
55
class Command
6-
# rubocop:disable Metrics/ClassLength
7-
class Run < self
8-
NAME = 'run'
9-
SHORT_DESCRIPTION = 'Run code analysis'
10-
6+
class Environment < self
117
OPTIONS =
128
%i[
139
add_environment_options
@@ -16,59 +12,17 @@ class Run < self
1612
add_matcher_options
1713
].freeze
1814

19-
SLEEP = 40
20-
21-
UNLICENSED = <<~MESSAGE.lines.freeze
22-
Soft fail, continuing in #{SLEEP} seconds
23-
Next major version will enforce the license
24-
See https://github.com/mbj/mutant#licensing
25-
MESSAGE
26-
27-
# Test if command needs to be executed in zombie environment
28-
#
29-
# @return [Bool]
30-
def zombie?
31-
@config.zombie
32-
end
33-
3415
private
3516

3617
def initialize(attributes)
3718
super(attributes)
3819
@config = Config.env
3920
end
4021

41-
def execute
42-
soft_fail(License.apply(world))
43-
.bind { Config.load_config_file(world) }
44-
.fmap(&method(:expand))
45-
.bind { Bootstrap.apply(world, @config) }
46-
.bind(&Runner.public_method(:apply))
47-
.from_right { |error| world.stderr.puts(error); return false }
48-
.success?
49-
end
50-
5122
def expand(file_config)
5223
@config = file_config.merge(@config)
5324
end
5425

55-
def soft_fail(result)
56-
result.either(
57-
lambda do |message|
58-
stderr = world.stderr
59-
stderr.puts(message)
60-
UNLICENSED.each { |line| stderr.puts(unlicensed(line)) }
61-
world.kernel.sleep(SLEEP)
62-
Either::Right.new(nil)
63-
end,
64-
->(_subscription) { Either::Right.new(nil) }
65-
)
66-
end
67-
68-
def unlicensed(message)
69-
"[Mutant-License-Error]: #{message}"
70-
end
71-
7226
def parse_remaining_arguments(arguments)
7327
traverse(@config.expression_parser.public_method(:apply), arguments)
7428
.fmap do |match_expressions|
@@ -158,7 +112,6 @@ def add_runner_options(parser)
158112
end
159113
end
160114
end # Run
161-
# rubocop:enable Metrics/ClassLength
162115
end # Command
163116
end # CLI
164117
end # Mutant
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
module Mutant
4+
module CLI
5+
class Command
6+
class Environment
7+
class Run < self
8+
NAME = 'run'
9+
SHORT_DESCRIPTION = 'Run code analysis'
10+
11+
SLEEP = 40
12+
13+
UNLICENSED = <<~MESSAGE.lines.freeze
14+
Soft fail, continuing in #{SLEEP} seconds
15+
Next major version will enforce the license
16+
See https://github.com/mbj/mutant#licensing
17+
MESSAGE
18+
19+
# Test if command needs to be executed in zombie environment
20+
#
21+
# @return [Bool]
22+
def zombie?
23+
@config.zombie
24+
end
25+
26+
private
27+
28+
def execute
29+
soft_fail(License.apply(world))
30+
.bind { Config.load_config_file(world) }
31+
.fmap(&method(:expand))
32+
.bind { Bootstrap.apply(world, @config) }
33+
.bind(&Runner.public_method(:apply))
34+
.from_right { |error| world.stderr.puts(error); return false }
35+
.success?
36+
end
37+
38+
def soft_fail(result)
39+
result.either(
40+
lambda do |message|
41+
stderr = world.stderr
42+
stderr.puts(message)
43+
UNLICENSED.each { |line| stderr.puts(unlicensed(line)) }
44+
world.kernel.sleep(SLEEP)
45+
Either::Right.new(nil)
46+
end,
47+
->(_subscription) { Either::Right.new(nil) }
48+
)
49+
end
50+
51+
def unlicensed(message)
52+
"[Mutant-License-Error]: #{message}"
53+
end
54+
end # Run
55+
end # Environment
56+
end # Command
57+
end # CLI
58+
end # Mutant

lib/mutant/cli/command/root.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Mutant
44
module CLI
55
class Command
66
class Root < self
7-
SUBCOMMANDS = [Run, Subscription].freeze
7+
SUBCOMMANDS = [Environment::Run, Subscription].freeze
88
SHORT_DESCRIPTION = 'mutation testing engine main command'
99
NAME = 'mutant'
1010
end

0 commit comments

Comments
 (0)