Module: Elasticsearch::Tests::Printer

Included in:
CodeRunner
Defined in:
lib/elasticsearch/tests/printer.rb

Overview

Functions to print out test results, errors, summary, etc.

Constant Summary collapse

BOX_WIDTH =
TTY::Screen.width

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.display_errors(errors, logger) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/elasticsearch/tests/printer.rb', line 88

def self.display_errors(errors, logger)
  puts
  print TTY::Box.frame("โŒ Errors/Failures: #{errors.count}", width: BOX_WIDTH, style: { border: { fg: :red } })

  errors.each do |error|
    print TTY::Box.frame("๐Ÿงช Test: #{error[:file]}", width: BOX_WIDTH * 0.6, style: { border: { fg: :red } })
    message = []
    message << "โ–ถ Action: #{error[:action].first}" if error[:action]
    message << "๐Ÿ”ฌ #{error[:error].class} - #{error[:error].message}"
    message << error[:error].backtrace.join("$/\n") if ENV['DEBUG'] == 'true'
    puts message.join("\n")
    logger.error(message.join("\n"))
  end
end

.display_summary(tests_count, errors_count, start_time, logger) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/elasticsearch/tests/printer.rb', line 103

def self.display_summary(tests_count, errors_count, start_time, logger)
  summary = "๐Ÿงช Tests: #{tests_count} | Passed: #{tests_count - errors_count} | Failed: #{errors_count}"
  logger.info summary
  duration = "โฒ  Elapsed time: #{Time.at(Time.now - start_time).utc.strftime('%H:%M:%S')}"
  message = <<~MSG
               #{summary}
               #{duration}
  MSG
  puts
  print TTY::Box.frame(message, width: BOX_WIDTH, title: { top_left: '[SUMMARY]' }, style: { border: { fg: :cyan } })
  logger.info duration
end

Instance Method Details



145
146
147
148
149
150
151
# File 'lib/elasticsearch/tests/printer.rb', line 145

def print_debug_catchable(exception)
  puts TTY::Box.frame(
         "Catchable: #{exception}\nResponse: #{@response}\n",
         width: BOX_WIDTH,
         title: { top_left: '[DEBUG]' }
       )
end


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/elasticsearch/tests/printer.rb', line 116

def print_debug_message(method, params)
  begin
    message = [
      "File: #{$test_file} | Action: #{method}",
      "Parameters: #{params}",
      ''
    ]
    if boolean_response?
      message << "Response: #{@response}"
    else
      message << "Response status: #{@response.status}"
      message << 'Response body:'
      if @response.body.is_a?(String)
        message.push(@response.body.empty? ? '  ""' : @response.body)
      elsif @response.body.is_a?(Hash)
        message.push(*@response.body.map { |k, v| "  #{k}: #{v}" })
      end
      message << 'Response headers:'
      message.push(*@response.headers.map { |k, v| "  #{k}: #{v}" })
    end
    puts TTY::Box.frame(message.join("\n"), width: BOX_WIDTH, title: { top_left: '[DEBUG]' })
  rescue ArgumentError => e
    if e.message == 'invalid byte sequence in UTF-8'
      @response.body.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
      print_debug_message(method, params)
    end
  end
end


80
81
82
83
84
85
86
# File 'lib/elasticsearch/tests/printer.rb', line 80

def print_error(error)
  print TTY::Box.error("โŒ ERROR: #{@short_name} #{@title} failed", width: BOX_WIDTH)
  logger.error error.display
  backtrace = error.backtrace.join("\n")
  logger.error "#{backtrace}\n"
  raise error
end


47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/elasticsearch/tests/printer.rb', line 47

def print_failure(action, response, exception = nil)
  if quiet?
    print '๐Ÿ”ด '
  else
    puts "๐Ÿ”ด \e[33m#{@short_name}\e[0m - #{@action}  \e[31mfailed\e[0m"
  end
  message = ["Expected result: #{action}"]
  if response&.body
    message << response.body
  else
    message << response
  end
  message << "Exception: #{exception}" if exception
end


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/elasticsearch/tests/printer.rb', line 62

def print_match_failure(action)
  keys = action['match'].keys.first
  value = action['match'].values.first

  if quiet?
    print '๐Ÿ”ด '
  else
    puts "๐Ÿ”ด \e[33m#{@short_name} (match)\e[0m - \e[32;9m#{keys}: #{value}\e[0m  \e[95m#{keys}: #{search_in_response(action['match'].keys.first)}\e[0m \e[31mfailed\e[0m"
  end
  message = <<~MSG
    #{@short_name} #{@title} failed
    \e[31m\e[1mExpected: { #{keys}: #{value} }\e[0m
    \e[32m\e[1mGot     : { #{keys}: #{search_in_response(action['match'].keys.first)} }\e[0m
    \e[1mResponse\e[0m:\n\e[36m #{@response}\e[0m
  MSG
  raise Elasticsearch::Tests::TestFailure.new(message)
end


29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/elasticsearch/tests/printer.rb', line 29

def print_success
  if quiet?
    print '๐ŸŸข '
  else
    msg = "๐ŸŸข \e[33m#{@short_name}\e[0m - #{@action} \e[32mpassed\e[0m"
    if @response.nil?
      puts msg
    else
      status = boolean_response? ? @response : @response.status
      puts "#{msg} [#{status}]"
    end
  end
end

#quiet?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/elasticsearch/tests/printer.rb', line 43

def quiet?
  !ENV['QUIET'].nil? && ![false, 'false'].include?(ENV['QUIET'])
end