Class: Inferno::CLI::Execute::ConsoleOutputter

Inherits:
Object
  • Object
show all
Defined in:
lib/inferno/apps/cli/execute/console_outputter.rb

Constant Summary collapse

COLOR =
Pastel.new
CHECKMARK =
"\u2713".freeze
BAR =
('=' * 80).freeze

Instance Method Summary collapse

Instance Method Details

#format_inputs(result) ⇒ Object



98
99
100
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 98

def format_inputs(result)
  format_session_data(result, :input_json)
end

#format_messages(result) ⇒ Object



77
78
79
80
81
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 77

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

#format_outputs(result) ⇒ Object



102
103
104
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 102

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

#format_requests(result) ⇒ Object



83
84
85
86
87
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 83

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

#format_result(result) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 106

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'
    COLOR.bold '. wait'
  when 'cancel'
    COLOR.red 'X cancel'
  when 'running'
    COLOR.bold '- running'
  else
    raise StandardError.new, "Unrecognized result #{result.result}"
  end
end

#format_session_data(result, attr) ⇒ Object



89
90
91
92
93
94
95
96
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 89

def format_session_data(result, attr)
  json = result.send(attr)
  return '' if json.nil?

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

#format_tag(result) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 65

def format_tag(result)
  if result.runnable.respond_to?(:short_id)
    "#{result.runnable.short_id} #{format_tag_suffix(result)}"
  else
    format_tag_suffix(result)
  end
end

#format_tag_suffix(result) ⇒ Object



73
74
75
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 73

def format_tag_suffix(result)
  result.runnable.short_title.presence || result.runnable.title.presence || result.runnable.id
end


24
25
26
27
28
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 24

def print_around_run(_options)
  puts 'Running tests. This may take a while...'
  # TODO: spinner/progress bar
  yield
end


47
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 47

def print_end_message(options); end


49
50
51
52
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 49

def print_error(options, exception)
  puts COLOR.red "Error: #{exception.full_message}"
  verbose_print(options, exception.backtrace&.join('\n'))
end


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 30

def print_results(options, results)
  verbose_print_json_results(options, results)

  puts BAR
  puts 'Test Results:'
  puts BAR
  results.each do |result|
    print format_tag(result), ': ', format_result(result), "\n"
    verbose_puts(options, "\tsummary: ",     result.result_message)
    verbose_puts(options, "\tmessages: ",    format_messages(result))
    verbose_puts(options, "\trequests: ",    format_requests(result))
    verbose_puts(options, "\tinputs: ",      format_inputs(result))
    verbose_puts(options, "\toutputs: ",     format_outputs(result))
  end
  puts BAR
end


14
15
16
17
18
19
20
21
22
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 14

def print_start_message(options)
  puts ''
  puts BAR
  puts "Testing #{options[:suite]}"
  verbose_puts options, "Running Groups #{options[:groups]}" unless options[:groups].blank?
  verbose_puts options, "Running Tests #{options[:tests]}" unless options[:tests].blank?
  verbose_puts options, "Running Entities #{options[:short_ids]}" unless options[:short_ids].blank?
  puts BAR
end

#serialize(entity) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 137

def serialize(entity)
  case entity.class.to_s
  when 'Array'
    JSON.pretty_generate(entity.map { |item| JSON.parse serialize(item) })
  when lambda { |x|
         defined?(x.constantize) && defined?("Inferno::Web::Serializers::#{x.split('::').last}".constantize)
       }
    "Inferno::Web::Serializers::#{entity.class.to_s.split('::').last}".constantize.render(entity)
  else
    raise StandardError, "CLI does not know how to serialize #{entity.class}"
  end
end

#verbose_print(options, *args) ⇒ Object

private



56
57
58
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 56

def verbose_print(options, *args)
  print(COLOR.dim(*args)) if options[:verbose]
end

#verbose_print_json_results(options, results) ⇒ Object



129
130
131
132
133
134
135
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 129

def verbose_print_json_results(options, results)
  verbose_puts(options, BAR)
  verbose_puts(options, 'JSON Test Results:')
  verbose_puts(options, BAR)
  verbose_puts(options, serialize(results))
  verbose_puts(options, BAR)
end

#verbose_puts(options, *args) ⇒ Object



60
61
62
63
# File 'lib/inferno/apps/cli/execute/console_outputter.rb', line 60

def verbose_puts(options, *args)
  args.push("\n")
  verbose_print(options, *args)
end