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
- .display_errors(errors, logger) ⇒ Object
- .display_summary(tests_count, errors_count, start_time, logger) ⇒ Object
Instance Method Summary collapse
- #print_debug_catchable(exception) ⇒ Object
- #print_debug_message(method, params) ⇒ Object
- #print_error(error) ⇒ Object
- #print_failure(action, response, exception = nil) ⇒ Object
- #print_match_failure(action) ⇒ Object
- #print_success ⇒ Object
- #quiet? ⇒ Boolean
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 } }) = [] << "โถ Action: #{error[:action].first}" if error[:action] << "๐ฌ #{error[:error].class} - #{error[:error].}" << error[:error].backtrace.join("$/\n") if ENV['DEBUG'] == 'true' puts .join("\n") logger.error(.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')}" = <<~MSG #{summary} #{duration} MSG puts print TTY::Box.frame(, width: BOX_WIDTH, title: { top_left: '[SUMMARY]' }, style: { border: { fg: :cyan } }) logger.info duration end |
Instance Method Details
#print_debug_catchable(exception) ⇒ Object
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 |
#print_debug_message(method, params) ⇒ Object
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 (method, params) begin = [ "File: #{$test_file} | Action: #{method}", "Parameters: #{params}", '' ] if boolean_response? << "Response: #{@response}" else << "Response status: #{@response.status}" << 'Response body:' if @response.body.is_a?(String) .push(@response.body.empty? ? ' ""' : @response.body) elsif @response.body.is_a?(Hash) .push(*@response.body.map { |k, v| " #{k}: #{v}" }) end << 'Response headers:' .push(*@response.headers.map { |k, v| " #{k}: #{v}" }) end puts TTY::Box.frame(.join("\n"), width: BOX_WIDTH, title: { top_left: '[DEBUG]' }) rescue ArgumentError => e if e. == 'invalid byte sequence in UTF-8' @response.body.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') (method, params) end end end |
#print_error(error) ⇒ Object
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 |
#print_failure(action, response, exception = nil) ⇒ Object
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 = ["Expected result: #{action}"] if response&.body << response.body else << response end << "Exception: #{exception}" if exception end |
#print_match_failure(action) ⇒ Object
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 = <<~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() end |
#print_success ⇒ Object
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
43 44 45 |
# File 'lib/elasticsearch/tests/printer.rb', line 43 def quiet? !ENV['QUIET'].nil? && ![false, 'false'].include?(ENV['QUIET']) end |