Skip to content

Commit

Permalink
Extract helper methods for options that do something and exit.
Browse files Browse the repository at this point in the history
This addresses a rubocop cyclomatic complexity failure.
  • Loading branch information
myronmarston committed Apr 6, 2015
1 parent 0b6acf3 commit 269dddc
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions lib/rspec/core/option_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ def parser(options)

parser.on('--bisect', 'Repeatedly runs the suite in order to isolates the failures to the ',
' smallest reproducible case.') do
RSpec::Support.require_rspec_core "bisect/coordinator"
success = Bisect::Coordinator.bisect_with(original_args, RSpec.configuration)
exit(success ? 0 : 1)
bisect_and_exit
end

parser.on('--[no-]fail-fast', 'Abort the run on first failure.') do |value|
Expand All @@ -92,9 +90,7 @@ def parser(options)
end

parser.on('--init', 'Initialize your project with RSpec.') do |_cmd|
RSpec::Support.require_rspec_core "project_initializer"
ProjectInitializer.new.run
exit
initialize_project_and_exit
end

parser.separator("\n **** Output ****\n\n")
Expand Down Expand Up @@ -227,8 +223,7 @@ def parser(options)
parser.separator("\n **** Utility ****\n\n")

parser.on('-v', '--version', 'Display the version.') do
puts RSpec::Core::Version::STRING
exit
print_version_and_exit
end

# These options would otherwise be confusing to users, so we forcibly
Expand All @@ -240,9 +235,7 @@ def parser(options)
invalid_options = %w[-d --I]

parser.on_tail('-h', '--help', "You're looking at it.") do
# Removing the blank invalid options from the output.
puts parser.to_s.gsub(/^\s+(#{invalid_options.join('|')})\s*$\n/, '')
exit
print_help_and_exit(parser, invalid_options)
end

# This prevents usage of the invalid_options.
Expand All @@ -268,5 +261,28 @@ def configure_only_failures(options)
options[:only_failures] = true
add_tag_filter(options, :inclusion_filter, :last_run_status, 'failed')
end

def initialize_project_and_exit
RSpec::Support.require_rspec_core "project_initializer"
ProjectInitializer.new.run
exit
end

def bisect_and_exit
RSpec::Support.require_rspec_core "bisect/coordinator"
success = Bisect::Coordinator.bisect_with(original_args, RSpec.configuration)
exit(success ? 0 : 1)
end

def print_version_and_exit
puts RSpec::Core::Version::STRING
exit
end

def print_help_and_exit(parser, invalid_options)
# Removing the blank invalid options from the output.
puts parser.to_s.gsub(/^\s+(#{invalid_options.join('|')})\s*$\n/, '')
exit
end
end
end

0 comments on commit 269dddc

Please sign in to comment.