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

Implement error handling for invalid arguments #8

Closed
wants to merge 2 commits into from
Closed
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
66 changes: 40 additions & 26 deletions bin/morse
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,47 @@
require 'open-uri'
require 'json'

VALID_ARGS = { run_examples: '--run', file_output: '--files' }

def main
process_args(ARGV)
ARGV.freeze
validate_args

if api_token.empty?
puts 'No semaphore token found'
#initialise
exit
output_error 'no semaphore token found'
exit_with_failure
end

@git_branch = git_current_branch #check current git branch


if !@git_branch.empty?
if project_id.nil?
puts 'Unable to locate project on semaphore'
exit
output_error 'unable to locate project on semaphore'
exit_with_failure
end
if branch_id.nil?
puts 'Unable to locate branch on semaphore'
exit
output_error 'unable to locate branch on semaphore'
exit_with_failure
end

examples = failed_examples
if examples.empty?
puts 'All tests passed'
$stdout.puts 'All Rspec tests passed'
elsif examples == 'pending'
puts 'Branch is currently building'
$stdout.puts 'Branch is currently building'
else
if @no_line_numbers
if file_output?
puts output_without_line_numbers(examples)
else
puts examples.join("\n")
end
run_examples(examples) if @do_run_examples
run_examples(examples) if run_examples?
end
else
puts 'No git branch detected'
output_error 'no git branch detected'
exit_with_failure
end

end

def git_current_project
Expand Down Expand Up @@ -144,22 +146,34 @@ def find_and_cache_project
end
end

def process_args(argv)
@do_run_examples = argv.include? '--run'
@no_line_numbers = argv.include? '--files'

argv.each do |arg|
unless %w(--run --files).include? arg
@exit_program = true
output_warning("unrecognised argument '#{arg}'")
end
def validate_args
fail ArgumentError unless (ARGV - VALID_ARGS.values).empty?
rescue ArgumentError
ARGV.each do |arg|
output_error("invalid argument '#{arg}'") unless VALID_ARGS.values.include? arg
end

exit if @exit_program
exit_with_failure
end

def run_examples?
arg_present? :run_examples
end

def file_output?
arg_present? :file_output
end

def arg_present?(arg)
!(Array(VALID_ARGS[arg.to_sym]) & ARGV).empty? if arg.respond_to? :to_sym
end

def output_error(message)
$stderr.puts "\e[31mError:\e[0m #{message}" if message.respond_to? :to_s
end

def output_warning(message)
$stderr.puts "\e[33mWarning:\e[0m #{message}" if message.respond_to? :to_s
def exit_with_failure
exit false
end

main