diff --git a/spec/cli_options_spec.cr b/spec/cli_options_spec.cr index b7b54a74..9f7b0ccf 100644 --- a/spec/cli_options_spec.cr +++ b/spec/cli_options_spec.cr @@ -1,4 +1,5 @@ require "../src/crytic/cli_options" +require "../src/crytic/side_effects" require "./spec_helper" module Crytic @@ -10,7 +11,7 @@ module Crytic cli_options_parser( std_err: std_err, - exit_fun: ->(code : Int32) { exit_code = code }) + exit_fun: ->(code : Int32) { exit_code = code; nil }) .parse(["-unknown"]) std_err.to_s.lines.first.should eq "ERROR: -unknown is not a valid option." @@ -31,7 +32,7 @@ module Crytic it "exits when showing the help" do exit_code : Int32? = nil - cli_options_parser(exit_fun: ->(code : Int32) { exit_code = code }) + cli_options_parser(exit_fun: ->(code : Int32) { exit_code = code; nil }) .parse(["--help"]) exit_code.should eq 0 @@ -140,5 +141,6 @@ private def cli_options_parser( env = fake_env, spec_files_glob = Crytic::CliOptions::DEFAULT_SPEC_FILES_GLOB ) - Crytic::CliOptions.new(std_out, std_err, exit_fun, env, spec_files_glob) + Crytic::CliOptions.new(Crytic::SideEffects.new( + std_out, std_err, exit_fun, env), spec_files_glob) end diff --git a/src/crytic.cr b/src/crytic.cr index f9ecd107..0a377810 100644 --- a/src/crytic.cr +++ b/src/crytic.cr @@ -1,14 +1,15 @@ require "./crytic/cli" +require "./crytic/side_effects" success = !Crytic::Cli - .new(STDOUT, STDERR, ->(code : Int32) { exit(code) }, { + .new(Crytic::SideEffects.new(STDOUT, STDERR, ->(code : Int32) { exit(code) }, { # manually map from ENV to a Hash because I am unable to conform ENV # to anything that I can replace with a stub in the tests "CIRCLE_BRANCH" => ENV["CIRCLE_BRANCH"]? || "", "CIRCLE_PROJECT_REPONAME" => ENV["CIRCLE_PROJECT_REPONAME"]? || "", "CIRCLE_PROJECT_USERNAME" => ENV["CIRCLE_PROJECT_USERNAME"]? || "", "STRYKER_DASHBOARD_API_KEY" => ENV["STRYKER_DASHBOARD_API_KEY"]? || "", -}) +})) .run(ARGV) exit(success.to_unsafe) diff --git a/src/crytic/cli.cr b/src/crytic/cli.cr index 1de8d315..4bbd7c65 100644 --- a/src/crytic/cli.cr +++ b/src/crytic/cli.cr @@ -2,16 +2,12 @@ require "./cli_options" require "./generator/in_memory_generator" require "./generator/isolated_mutation_factory" require "./runner/sequential" +require "./side_effects" require "./subject" module Crytic class Cli - def initialize( - @std_out : IO, - @std_err : IO, - @exit_fun : (Int32) ->, - @env : Hash(String, String) - ) + def initialize(@side_effects : SideEffects) end def run(args) @@ -25,7 +21,7 @@ module Crytic private def parse_options(args) Crytic::CliOptions - .new(@std_out, @std_err, @exit_fun, @env, Crytic::CliOptions::DEFAULT_SPEC_FILES_GLOB) + .new(@side_effects, Crytic::CliOptions::DEFAULT_SPEC_FILES_GLOB) .parse(args) end diff --git a/src/crytic/cli_options.cr b/src/crytic/cli_options.cr index fc67e11a..91b1b838 100644 --- a/src/crytic/cli_options.cr +++ b/src/crytic/cli_options.cr @@ -1,6 +1,7 @@ require "./generator/generator" require "./mutant/possibilities" require "./reporter/*" +require "./side_effects" require "option_parser" module Crytic @@ -13,14 +14,8 @@ module Crytic @spec_files = [] of String @subject = [] of String - def initialize( - @std_out : IO, - @std_err : IO, - @exit_fun : (Int32) ->, - @env : Hash(String, String), - @spec_files_glob : String - ) - @reporters << Reporter::IoReporter.new(@std_out) + def initialize(@side_effects : SideEffects, @spec_files_glob : String) + @reporters << Reporter::IoReporter.new(@side_effects.std_out) end def parse(args) @@ -28,8 +23,8 @@ module Crytic parser.banner = "Usage: crytic [arguments]" parser.on("-h", "--help", "Show this help") do - @std_out.puts parser - @exit_fun.call(0) + @side_effects.std_out.puts parser + @side_effects.exit_fun.call(0) end parser.on("-m", "--min-msi=THRESHOLD", "Crytic will exit with zero if this threshold is reached.") do |threshold| @@ -57,9 +52,9 @@ module Crytic end parser.invalid_option do |flag| - @std_err.puts "ERROR: #{flag} is not a valid option." - @std_err.puts parser - @exit_fun.call(1) + @side_effects.std_err.puts "ERROR: #{flag} is not a valid option." + @side_effects.std_err.puts parser + @side_effects.exit_fun.call(1) end end @@ -87,16 +82,16 @@ module Crytic end private def console_reporter - Reporter::IoReporter.new(@std_out) + Reporter::IoReporter.new(@side_effects.std_out) end private def stryker_reporter client = Reporter::DefaultHttpClient.new - Reporter::StrykerBadgeReporter.new(client, @env, @std_out) + Reporter::StrykerBadgeReporter.new(client, @side_effects.env, @side_effects.std_out) end private def file_summary_reporter - Reporter::FileSummaryIoReporter.new(@std_out) + Reporter::FileSummaryIoReporter.new(@side_effects.std_out) end end end diff --git a/src/crytic/side_effects.cr b/src/crytic/side_effects.cr new file mode 100644 index 00000000..3a87d7f3 --- /dev/null +++ b/src/crytic/side_effects.cr @@ -0,0 +1,9 @@ +class Crytic::SideEffects + getter std_out : IO + getter std_err : IO + getter exit_fun : (Int32) -> + getter env : Hash(String, String) + + def initialize(@std_out, @std_err, @exit_fun, @env) + end +end