| 1 |
require 'ruby-progressbar' |
| 2 |
|
| 3 |
module Minitest |
| 4 |
module Reporters |
| 5 |
# Fuubar-like reporter with a progress bar. |
| 6 |
# |
| 7 |
# Based upon Jeff Kreefmeijer's Fuubar (MIT License) and paydro's |
| 8 |
# monkey-patch. |
| 9 |
# |
| 10 |
# @see https://github.com/jeffkreeftmeijer/fuubar Fuubar |
| 11 |
# @see https://gist.github.com/356945 paydro's monkey-patch |
| 12 |
class ProgressReporter < BaseReporter |
| 13 |
include RelativePosition |
| 14 |
include ANSI::Code |
| 15 |
|
| 16 |
PROGRESS_MARK = '='.freeze |
| 17 |
|
| 18 |
def initialize(options = {}) |
| 19 |
super |
| 20 |
@detailed_skip = options.fetch(:detailed_skip, true) |
| 21 |
|
| 22 |
@progress = ProgressBar.create( |
| 23 |
total: total_count, |
| 24 |
starting_at: count, |
| 25 |
progress_mark: green(PROGRESS_MARK), |
| 26 |
remainder_mark: ' ', |
| 27 |
format: options.fetch(:format, ' %c/%C: [%B] %p%% %a, %e'), |
| 28 |
autostart: false |
| 29 |
) |
| 30 |
end |
| 31 |
|
| 32 |
def start |
| 33 |
super |
| 34 |
puts('Started with run options %s' % options[:args]) |
| 35 |
puts |
| 36 |
@progress.start |
| 37 |
@progress.total = total_count |
| 38 |
show |
| 39 |
end |
| 40 |
|
| 41 |
def before_test(test) |
| 42 |
super |
| 43 |
if options[:verbose] |
| 44 |
puts |
| 45 |
puts("\n%s#%s" % [test_class(test), test.name]) |
| 46 |
end |
| 47 |
end |
| 48 |
|
| 49 |
def record(test) |
| 50 |
super |
| 51 |
return show if test.skipped? && !@detailed_skip |
| 52 |
|
| 53 |
# In minitest 6, we need to handle skipped tests explicitly |
| 54 |
# because test.failure might not trigger the same way |
| 55 |
if test.failure || (test.skipped? && @detailed_skip) |
| 56 |
print "\e[0m\e[1000D\e[K" |
| 57 |
print_colored_status(test) |
| 58 |
print_test_with_time(test) |
| 59 |
puts |
| 60 |
print_info(test.failure, test.error?) if test.failure |
| 61 |
puts |
| 62 |
end |
| 63 |
|
| 64 |
if test.skipped? && color != "red" |
| 65 |
self.color = "yellow" |
| 66 |
elsif test.failure |
| 67 |
self.color = "red" |
| 68 |
end |
| 69 |
|
| 70 |
show |
| 71 |
end |
| 72 |
|
| 73 |
def report |
| 74 |
super |
| 75 |
@progress.finish |
| 76 |
|
| 77 |
puts |
| 78 |
puts('Finished in %.5fs' % total_time) |
| 79 |
print('%d tests, %d assertions, ' % [count, assertions]) |
| 80 |
color = failures.zero? && errors.zero? ? :green : :red |
| 81 |
print(send(color) { '%d failures, %d errors, ' } % [failures, errors]) |
| 82 |
print(yellow { '%d skips' } % skips) |
| 83 |
puts |
| 84 |
end |
| 85 |
|
| 86 |
private |
| 87 |
|
| 88 |
def show |
| 89 |
@progress.increment unless count == 0 |
| 90 |
end |
| 91 |
|
| 92 |
def print_test_with_time(test) |
| 93 |
print(" %s#%s (%.2fs)" % [test_class(test), test.name, total_time]) |
| 94 |
end |
| 95 |
|
| 96 |
def color |
| 97 |
@color ||= "green" |
| 98 |
end |
| 99 |
|
| 100 |
def color=(color) |
| 101 |
@color = color |
| 102 |
@progress.progress_mark = send(color, PROGRESS_MARK) |
| 103 |
end |
| 104 |
end |
| 105 |
end |
| 106 |
end |