Skip to content

Commit

Permalink
more cop
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaumik-Ashraf committed Aug 9, 2024
1 parent d44d408 commit e5670e4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 50 deletions.
102 changes: 55 additions & 47 deletions lib/inferno/apps/cli/execute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,46 +80,9 @@ def run(options)
Jobs.perform(Jobs::ExecuteTestRun, test_run.id, force_synchronous: true)

results = test_runs_repo.results_for_test_run(test_run.id).reverse
verbose_puts '=========================================='
verbose_puts 'JSON Test Results:'
verbose_puts '=========================================='
verbose_puts serialize(results)
verbose_puts '=========================================='

puts '=========================================='
puts 'Colored Test Results:'
puts '=========================================='
results.each do |result|
print format_id(result), ': '
case result.result
when 'pass'
print COLOR.bold.green(CHECKMARK, ' pass')
when 'fail'
print COLOR.bold.red 'X fail'
when 'skip'
print COLOR.yellow '* skip'
when 'omit'
print COLOR.blue '* omit'
when 'error'
print COLOR.magenta 'X error'
when 'wait'
# This may be dead code with synchronous test execution
print '. wait'
when 'cancel'
print COLOR.red 'X cancel'
else
# TODO: strict behavior or no?
# raise StandardError.new, "Unrecognized result #{result.result}" # strict
print '- unknown' # unstrict
end
puts ''
verbose_puts "\tsummary: ", result.result_message
verbose_puts "\tmessages: ", format_messages(result)
verbose_puts "\trequests: ", format_requests(result)
verbose_puts "\tinputs: ", format_inputs(result)
verbose_puts "\toutputs: ", format_outputs(result)
end
puts '=========================================='
verbose_print_json_results(results)
print_color_results(results)

exit(0) if results.find { |result| result.test_suite_id == options[:suite_id] }.result == 'pass'

Expand Down Expand Up @@ -151,7 +114,7 @@ def create_params(test_session, suite)
def serialize(entity)
case entity.class.to_s
when 'Array'
JSON.pretty_generate entity.map { |item| JSON.parse serialize(item) }
JSON.pretty_generate(entity.map { |item| JSON.parse serialize(item) })
when lambda { |x|
defined?(x.constantize) && defined?("Inferno::Web::Serializers::#{x.split('::').last}".constantize)
}
Expand All @@ -176,14 +139,14 @@ def format_id(result)

def format_messages(result)
result.messages.map do |message|
"\n\t\t" + message.type + ': ' + message.message
end.join('')
"\n\t\t#{message.type}: #{message.message}"
end.join
end

def format_requests(result)
result.requests.map do |req_res|
"\n\t\t" + req_res.status.to_s + ' ' + req_res.verb.upcase + ' ' + req_res.url
end.join('')
"\n\t\t#{req_res.status} #{req_res.verb.upcase} #{req_res.url}"
end.join
end

def format_inputs(result, attr = :input_json)
Expand All @@ -192,18 +155,63 @@ def format_inputs(result, attr = :input_json)

JSON.parse(input_json).map do |input|
"\n\t\t#{input['name']}: #{input['value']}"
end.join('')
end.join
end

def format_outputs(result)
format_inputs(result, :output_json)
end

def print_error_and_exit(e, code)
def format_result(result) # rubocop:disable Metrics/CyclomaticComplexity
case result.result
when 'pass'
COLOR.bold.green(CHECKMARK, ' pass')
when 'fail'
COLOR.bold.red 'X fail'
when 'skip'
COLOR.yellow '* skip'
when 'omit'
COLOR.blue '* omit'
when 'error'
COLOR.magenta 'X error'
when 'wait'
# This may be dead code with synchronous test execution
'. wait'
when 'cancel'
COLOR.red 'X cancel'
else
raise StandardError.new, "Unrecognized result #{result.result}"
end
end

def print_error_and_exit(err, code)
# TODO: use Application Logger for stderr?
warn COLOR.red "Error: #{e.full_message}"
warn COLOR.red "Error: #{err.full_message}"
exit(code)
end

def verbose_print_json_results(results)
verbose_puts '=========================================='
verbose_puts 'JSON Test Results:'
verbose_puts '=========================================='
verbose_puts serialize(results)
verbose_puts '=========================================='
end

def print_color_results(results)
puts '=========================================='
puts 'Colored Test Results:'
puts '=========================================='
results.each do |result|
print format_id(result), ': ', format_result(result), "\n"
verbose_puts "\tsummary: ", result.result_message
verbose_puts "\tmessages: ", format_messages(result)
verbose_puts "\trequests: ", format_requests(result)
verbose_puts "\tinputs: ", format_inputs(result)
verbose_puts "\toutputs: ", format_outputs(result)
end
puts '=========================================='
end
end
end
end
4 changes: 2 additions & 2 deletions lib/inferno/apps/cli/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def version
puts "Inferno Core v#{Inferno::VERSION}"
end

EXECUTE_HELP = <<~EOT
EXECUTE_HELP = <<~END_OF_HELP.freeze
Run Inferno tests in the command line. Exits with 0 only if test suite passes. Must be run from test kit as working directory.
You must have background services running: `bundle exec inferno services start`
Expand All @@ -77,7 +77,7 @@ def version
`bundle exec inferno execute --suite dev_validator --inputs "url:https://hapi.fhir.org/baseR4" patient_id:1234321`
=> Outputs test results
EOT
END_OF_HELP
desc 'execute', 'Run Inferno tests in command line'
long_desc EXECUTE_HELP, wrap: false
option :suite,
Expand Down
2 changes: 1 addition & 1 deletion lib/inferno/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Inferno
module Jobs
def self.perform(job_klass, *params, force_synchronous: false)
if force_synchronous || (Application['async_jobs'] === false)
if force_synchronous || (Application['async_jobs'] == false)
job_klass.new.perform(*params)
else
job_klass.perform_async(*params)
Expand Down

0 comments on commit e5670e4

Please sign in to comment.