Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group side effects in own class #69

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions spec/cli_options_spec.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "../src/crytic/cli_options"
require "../src/crytic/side_effects"
require "./spec_helper"

module Crytic
Expand All @@ -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."
Expand All @@ -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
Expand Down Expand Up @@ -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
5 changes: 3 additions & 2 deletions src/crytic.cr
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 3 additions & 7 deletions src/crytic/cli.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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

Expand Down
27 changes: 11 additions & 16 deletions src/crytic/cli_options.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require "./generator/generator"
require "./mutant/possibilities"
require "./reporter/*"
require "./side_effects"
require "option_parser"

module Crytic
Expand All @@ -13,23 +14,17 @@ 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)
OptionParser.parse(args) do |parser|
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|
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions src/crytic/side_effects.cr
Original file line number Diff line number Diff line change
@@ -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