Debian Codegraph @

lib/minitest/reporters/default_reporter.rb

raw
1 module Minitest
2 module Reporters
3 # A reporter identical to the standard Minitest reporter except with more
4 # colors.
5 #
6 # Based upon Ryan Davis of Seattle.rb's Minitest (MIT License).
7 #
8 # @see https://github.com/seattlerb/minitest Minitest
9
10 class DefaultReporter < BaseReporter
11 include ANSI::Code
12 include RelativePosition
13
14 def initialize(options = {})
15 super
16 @detailed_skip = options.fetch(:detailed_skip, true)
17 @slow_count = options.fetch(:slow_count, 0)
18 @slow_suite_count = options.fetch(:slow_suite_count, 0)
19 @suite_times = []
20 @suite_start_times = {}
21 @fast_fail = options.fetch(:fast_fail, false)
22 @show_test_location = options.fetch(:location, false)
23 @options = options
24 end
25
26 def start
27 super
28 on_start
29 end
30
31 def on_start
32 puts
33 puts("# Running tests with run options %s:" % options[:args])
34 puts
35 end
36
37 def before_test(test)
38 super
39 print "\n#{test.class}##{test.name} " if options[:verbose]
40 end
41
42 def before_suite(suite)
43 @suite_start_times[suite] = Minitest::Reporters.clock_time
44 super
45 end
46
47 def after_suite(suite)
48 super
49 duration = suite_duration(suite)
50 @suite_times << [suite.name, duration]
51 end
52
53 def record(test)
54 super
55
56 on_record(test)
57 end
58
59 def on_record(test)
60 print "#{"%.2f" % test.time} = " if options[:verbose]
61
62 # Print the pass/skip/fail mark
63 print(if test.passed?
64 record_pass(test)
65 elsif test.skipped?
66 record_skip(test)
67 elsif test.failure
68 record_failure(test)
69 end)
70
71 # Print fast_fail information
72 if @fast_fail && (test.skipped? || test.failure)
73 print_failure(test)
74 end
75 end
76
77 def record_pass(record)
78 green(record.result_code)
79 end
80
81 def record_skip(record)
82 yellow(record.result_code)
83 end
84
85 def record_failure(record)
86 red(record.result_code)
87 end
88
89 def report
90 super
91 on_report
92 end
93
94 def on_report
95 status_line = "Finished tests in %.6fs, %.4f tests/s, %.4f assertions/s." %
96 [total_time, count / total_time, assertions / total_time]
97
98 puts
99 puts
100 puts colored_for(suite_result, status_line)
101 puts
102
103 unless @fast_fail
104 tests.reject(&:passed?).each do |test|
105 print_failure(test)
106 end
107 end
108
109 if @slow_count > 0
110 slow_tests = tests.sort_by(&:time).reverse.take(@slow_count)
111
112 puts
113 puts "Slowest tests:"
114 puts
115
116 slow_tests.each do |test|
117 puts "%.6fs %s#%s" % [test.time, test.name, test_class(test)]
118 end
119 end
120
121 if @slow_suite_count > 0
122 slow_suites = @suite_times.sort_by { |x| x[1] }.reverse.take(@slow_suite_count)
123
124 puts
125 puts "Slowest test classes:"
126 puts
127
128 slow_suites.each do |slow_suite|
129 puts "%.6fs %s" % [slow_suite[1], slow_suite[0]]
130 end
131 end
132
133 puts
134 print colored_for(suite_result, result_line)
135 puts
136 end
137
138 alias to_s report
139
140 def print_failure(test)
141 message = message_for(test)
142 unless message.nil? || message.strip == ''
143 puts
144 puts colored_for(result(test), message)
145 if @show_test_location
146 location = get_source_location(test)
147 puts "\n\n#{relative_path(location[0])}:#{location[1]}"
148 end
149
150 end
151 end
152
153 private
154
155 def relative_path(path)
156 Pathname.new(path).relative_path_from(Pathname.new(Dir.getwd))
157 end
158
159 def get_source_location(result)
160 if result.respond_to? :klass
161 result.source_location
162 else
163 result.method(result.name).source_location
164 end
165 end
166
167 def color?
168 return @color if defined?(@color)
169 @color = @options.fetch(:color) do
170 io.tty? && (
171 ENV["TERM"] =~ /^screen|color/ ||
172 ENV["EMACS"] == "t"
173 )
174 end
175 end
176
177 def green(string)
178 color? ? ANSI::Code.green(string) : string
179 end
180
181 def yellow(string)
182 color? ? ANSI::Code.yellow(string) : string
183 end
184
185 def red(string)
186 color? ? ANSI::Code.red(string) : string
187 end
188
189 def colored_for(result, string)
190 case result
191 when :fail, :error; red(string)
192 when :skip; yellow(string)
193 else green(string)
194 end
195 end
196
197 def suite_result
198 case
199 when failures > 0; :fail
200 when errors > 0; :error
201 when skips > 0; :skip
202 else :pass
203 end
204 end
205
206 def location(exception)
207 last_before_assertion = ''
208 exception.backtrace.reverse_each do |s|
209 break if s =~ /in .(assert|refute|flunk|pass|fail|raise|must|wont)/
210 last_before_assertion = s
211 end
212
213 last_before_assertion.sub(/:in .*$/, '')
214 end
215
216 def message_for(test)
217 e = test.failure
218
219 if test.skipped?
220 if @detailed_skip
221 "Skipped:\n#{test_class(test)}##{test.name} [#{location(e)}]:\n#{e.message}"
222 end
223 elsif test.error?
224 "Error:\n#{test_class(test)}##{test.name}:\n#{e.message}"
225 else
226 "Failure:\n#{test_class(test)}##{test.name} [#{test.failure.location}]\n#{e.class}: #{e.message}"
227 end
228 end
229
230 def result_line
231 '%d tests, %d assertions, %d failures, %d errors, %d skips' %
232 [count, assertions, failures, errors, skips]
233 end
234
235 def suite_duration(suite)
236 start_time = @suite_start_times.delete(suite)
237 if start_time.nil?
238 0
239 else
240 Minitest::Reporters.clock_time - start_time
241 end
242 end
243 end
244 end
245 end