Class: PrettyTest::Reporter

Inherits:
Minitest::AbstractReporter
  • Object
show all
Defined in:
lib/pretty_test/reporter.rb

Constant Summary collapse

SKIP_FORMAT =
"\e[33m[SKIPPED] %s\e[0m\n%s\n%s"
FAILURE_FORMAT =
"\e[31m[FAILURE] %s\e[0m\n\e[31m%s: %s\e[0m\n%s"
ERROR_FORMAT =
"\e[31m[ERROR] %s\e[0m\n\e[31m%s: %s\e[0m\n%s"
STATUS_FORMAT =
"\e[2K\r\e[?7l\e[37m(%.1fs) \e[32m%d/%d tests (%d%%)\e[37m, \e[36m%d assertions\e[37m, \e[31m%d errors\e[37m, \e[31m%d failures\e[37m, \e[33m%d skips\e[?7h\e[0m"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Reporter

Returns a new instance of Reporter.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/pretty_test/reporter.rb', line 14

def initialize(options = {})
  super()

  self.io = options[:io] || $stdout

  self.started_at = nil
  self.completed = 0
  self.assertions = 0
  self.failures = 0
  self.errors = 0
  self.skips = 0
end

Instance Attribute Details

#assertionsObject

Returns the value of attribute assertions.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def assertions
  @assertions
end

#completedObject

Returns the value of attribute completed.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def completed
  @completed
end

#errorsObject

Returns the value of attribute errors.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def errors
  @errors
end

#failuresObject

Returns the value of attribute failures.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def failures
  @failures
end

#ioObject

Returns the value of attribute io.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def io
  @io
end

#skipsObject

Returns the value of attribute skips.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def skips
  @skips
end

#started_atObject

Returns the value of attribute started_at.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def started_at
  @started_at
end

#testsObject

Returns the value of attribute tests.



12
13
14
# File 'lib/pretty_test/reporter.rb', line 12

def tests
  @tests
end

Instance Method Details

#clean_trace_line(prefix, path, line, method = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/pretty_test/reporter.rb', line 111

def clean_trace_line(prefix, path, line, method = nil)
  case path
  when %r{^#{Dir.pwd}/([^/]+)/(.+)$} then "\e[37m#{prefix}[#{$1}] #{$2}:#{line} #{method}\e[0m"
  when %r{^.*/(ruby-[^/]+)/(bin/.+)$} then "\e[35m#{prefix}[#{$1}] #{$2}:#{line} #{method}\e[0m"
  when %r{^.*/gems/(minitap|minitest)-.+/(.+)$} then nil
  when %r{^.*/gems/([^/]+)/(.+)$} then "\e[36m#{prefix}[#{$1}] #{$2}:#{line} #{method}\e[0m"
  else "#{prefix}#{path}:#{line}\e[0m"
  end
end

#error(test_name, exception) ⇒ Object



82
83
84
85
86
87
# File 'lib/pretty_test/reporter.rb', line 82

def error(test_name, exception)
  self.errors += 1
  index = find_exception_index(exception)
  trace = pretty_trace(exception, index)
  print_error ERROR_FORMAT, test_name, exception.class, exception.message, trace
end

#failure(test_name, exception) ⇒ Object



75
76
77
78
79
80
# File 'lib/pretty_test/reporter.rb', line 75

def failure(test_name, exception)
  self.failures += 1
  index = find_assertion_index(exception)
  trace = pretty_trace(exception, index)
  print_error FAILURE_FORMAT, test_name, exception.class, exception.message, trace
end

#find_assertion_index(error) ⇒ Object



89
90
91
92
# File 'lib/pretty_test/reporter.rb', line 89

def find_assertion_index(error)
  index = error.backtrace.rindex { |trace| trace =~ /:in .(assert|refute|flunk|pass|fail|raise|must|wont)/ }
  index ? index + 1 : find_exception_index(error)
end

#find_exception_index(error) ⇒ Object



94
95
96
# File 'lib/pretty_test/reporter.rb', line 94

def find_exception_index(error)
  error.backtrace.index { |trace| trace.index(Dir.pwd) }
end

#passObject



65
66
# File 'lib/pretty_test/reporter.rb', line 65

def pass
end

#passed?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/pretty_test/reporter.rb', line 61

def passed?
  true
end

#pretty_trace(error, location_index) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pretty_test/reporter.rb', line 98

def pretty_trace(error, location_index)
  lines = []
  backtrace = error.backtrace
  entry_point = backtrace.reverse.detect { |trace| trace.starts_with?(Dir.pwd) }
  backtrace.each_with_index do |trace, index|
    prefix = index == location_index ? "\e[1m-> " : "   "
    trace_file, trace_line, trace_method = trace.split(":", 3)
    lines << clean_trace_line(prefix, trace_file, trace_line, trace_method)
    break if trace == entry_point
  end
  lines.compact.join("\n")
end


121
122
123
124
125
# File 'lib/pretty_test/reporter.rb', line 121

def print_error(format, *args)
  remove_status
  io.puts format % args
  io.puts
end

#record(result) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/pretty_test/reporter.rb', line 36

def record(result)
  super
  test_name = "#{result.class.name}##{result.name}"
  @completed += 1
  @assertions += result.assertions
  case exception = result.failure
  when nil then pass
  when ::MiniTest::Skip then skip(test_name, exception)
  when ::MiniTest::Assertion then failure(test_name, exception)
  else error(test_name, exception)
  end
  update_status
end

#remove_statusObject



133
134
135
# File 'lib/pretty_test/reporter.rb', line 133

def remove_status
  io.print "\e[2K\r"
end

#reportObject



50
51
52
53
54
55
56
57
58
59
# File 'lib/pretty_test/reporter.rb', line 50

def report
  if tests > 0
    update_status
  end
  if errors + failures == 0
    io.puts "\n\n\e[32m----- PASSED! -----\e[0m\n\n"
  else
    io.puts "\n\n\e[31m----- FAILED! -----\e[0m\n\n"
  end
end

#skip(test_name, exception) ⇒ Object



68
69
70
71
72
73
# File 'lib/pretty_test/reporter.rb', line 68

def skip(test_name, exception)
  self.skips += 1
  index = find_assertion_index(exception)
  trace = pretty_trace(exception, index)
  print_error SKIP_FORMAT, test_name, exception.message, trace
end

#startObject



27
28
29
30
31
32
33
34
# File 'lib/pretty_test/reporter.rb', line 27

def start
  self.started_at = Time.now
  suites = ::Minitest::Runnable.runnables
  self.tests = 0
  suites.each do |suite|
    self.tests += suite.runnable_methods.count
  end
end

#update_statusObject



127
128
129
130
131
# File 'lib/pretty_test/reporter.rb', line 127

def update_status
  running_time = Time.now - started_at
  progress = 100.0 * completed / tests
  io.print STATUS_FORMAT % [running_time, completed, tests, progress, assertions, errors, failures, skips]
end