Skip to content

Commit ebc8809

Browse files
authoredMar 11, 2019
Merge pull request #69 from hanneskaeufler/hk-cluster-side-effects
Group side effects in own class
2 parents 6be9810 + 0c4fc14 commit ebc8809

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed
 

‎spec/cli_options_spec.cr

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "../src/crytic/cli_options"
2+
require "../src/crytic/side_effects"
23
require "./spec_helper"
34

45
module Crytic
@@ -10,7 +11,7 @@ module Crytic
1011

1112
cli_options_parser(
1213
std_err: std_err,
13-
exit_fun: ->(code : Int32) { exit_code = code })
14+
exit_fun: ->(code : Int32) { exit_code = code; nil })
1415
.parse(["-unknown"])
1516

1617
std_err.to_s.lines.first.should eq "ERROR: -unknown is not a valid option."
@@ -31,7 +32,7 @@ module Crytic
3132
it "exits when showing the help" do
3233
exit_code : Int32? = nil
3334

34-
cli_options_parser(exit_fun: ->(code : Int32) { exit_code = code })
35+
cli_options_parser(exit_fun: ->(code : Int32) { exit_code = code; nil })
3536
.parse(["--help"])
3637

3738
exit_code.should eq 0
@@ -140,5 +141,6 @@ private def cli_options_parser(
140141
env = fake_env,
141142
spec_files_glob = Crytic::CliOptions::DEFAULT_SPEC_FILES_GLOB
142143
)
143-
Crytic::CliOptions.new(std_out, std_err, exit_fun, env, spec_files_glob)
144+
Crytic::CliOptions.new(Crytic::SideEffects.new(
145+
std_out, std_err, exit_fun, env), spec_files_glob)
144146
end

‎src/crytic.cr

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
require "./crytic/cli"
2+
require "./crytic/side_effects"
23

34
success = !Crytic::Cli
4-
.new(STDOUT, STDERR, ->(code : Int32) { exit(code) }, {
5+
.new(Crytic::SideEffects.new(STDOUT, STDERR, ->(code : Int32) { exit(code) }, {
56
# manually map from ENV to a Hash because I am unable to conform ENV
67
# to anything that I can replace with a stub in the tests
78
"CIRCLE_BRANCH" => ENV["CIRCLE_BRANCH"]? || "",
89
"CIRCLE_PROJECT_REPONAME" => ENV["CIRCLE_PROJECT_REPONAME"]? || "",
910
"CIRCLE_PROJECT_USERNAME" => ENV["CIRCLE_PROJECT_USERNAME"]? || "",
1011
"STRYKER_DASHBOARD_API_KEY" => ENV["STRYKER_DASHBOARD_API_KEY"]? || "",
11-
})
12+
}))
1213
.run(ARGV)
1314

1415
exit(success.to_unsafe)

‎src/crytic/cli.cr

+3-7
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@ require "./cli_options"
22
require "./generator/in_memory_generator"
33
require "./generator/isolated_mutation_factory"
44
require "./runner/sequential"
5+
require "./side_effects"
56
require "./subject"
67

78
module Crytic
89
class Cli
9-
def initialize(
10-
@std_out : IO,
11-
@std_err : IO,
12-
@exit_fun : (Int32) ->,
13-
@env : Hash(String, String)
14-
)
10+
def initialize(@side_effects : SideEffects)
1511
end
1612

1713
def run(args)
@@ -25,7 +21,7 @@ module Crytic
2521

2622
private def parse_options(args)
2723
Crytic::CliOptions
28-
.new(@std_out, @std_err, @exit_fun, @env, Crytic::CliOptions::DEFAULT_SPEC_FILES_GLOB)
24+
.new(@side_effects, Crytic::CliOptions::DEFAULT_SPEC_FILES_GLOB)
2925
.parse(args)
3026
end
3127

‎src/crytic/cli_options.cr

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "./generator/generator"
22
require "./mutant/possibilities"
33
require "./reporter/*"
4+
require "./side_effects"
45
require "option_parser"
56

67
module Crytic
@@ -13,23 +14,17 @@ module Crytic
1314
@spec_files = [] of String
1415
@subject = [] of String
1516

16-
def initialize(
17-
@std_out : IO,
18-
@std_err : IO,
19-
@exit_fun : (Int32) ->,
20-
@env : Hash(String, String),
21-
@spec_files_glob : String
22-
)
23-
@reporters << Reporter::IoReporter.new(@std_out)
17+
def initialize(@side_effects : SideEffects, @spec_files_glob : String)
18+
@reporters << Reporter::IoReporter.new(@side_effects.std_out)
2419
end
2520

2621
def parse(args)
2722
OptionParser.parse(args) do |parser|
2823
parser.banner = "Usage: crytic [arguments]"
2924

3025
parser.on("-h", "--help", "Show this help") do
31-
@std_out.puts parser
32-
@exit_fun.call(0)
26+
@side_effects.std_out.puts parser
27+
@side_effects.exit_fun.call(0)
3328
end
3429

3530
parser.on("-m", "--min-msi=THRESHOLD", "Crytic will exit with zero if this threshold is reached.") do |threshold|
@@ -57,9 +52,9 @@ module Crytic
5752
end
5853

5954
parser.invalid_option do |flag|
60-
@std_err.puts "ERROR: #{flag} is not a valid option."
61-
@std_err.puts parser
62-
@exit_fun.call(1)
55+
@side_effects.std_err.puts "ERROR: #{flag} is not a valid option."
56+
@side_effects.std_err.puts parser
57+
@side_effects.exit_fun.call(1)
6358
end
6459
end
6560

@@ -87,16 +82,16 @@ module Crytic
8782
end
8883

8984
private def console_reporter
90-
Reporter::IoReporter.new(@std_out)
85+
Reporter::IoReporter.new(@side_effects.std_out)
9186
end
9287

9388
private def stryker_reporter
9489
client = Reporter::DefaultHttpClient.new
95-
Reporter::StrykerBadgeReporter.new(client, @env, @std_out)
90+
Reporter::StrykerBadgeReporter.new(client, @side_effects.env, @side_effects.std_out)
9691
end
9792

9893
private def file_summary_reporter
99-
Reporter::FileSummaryIoReporter.new(@std_out)
94+
Reporter::FileSummaryIoReporter.new(@side_effects.std_out)
10095
end
10196
end
10297
end

‎src/crytic/side_effects.cr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Crytic::SideEffects
2+
getter std_out : IO
3+
getter std_err : IO
4+
getter exit_fun : (Int32) ->
5+
getter env : Hash(String, String)
6+
7+
def initialize(@std_out, @std_err, @exit_fun, @env)
8+
end
9+
end

0 commit comments

Comments
 (0)
Please sign in to comment.