/
testrunner.rb
693 lines (614 loc) · 22.1 KB
/
testrunner.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
#--
#
# Author:: Nathaniel Talbott.
# Copyright::
# * Copyright (c) 2000-2003 Nathaniel Talbott. All rights reserved.
# * Copyright (c) 2008-2013 Kouhei Sutou <kou@clear-code.com>
# License:: Ruby license.
begin
require 'io/console'
rescue LoadError
end
require 'test/unit/color-scheme'
require 'test/unit/code-snippet-fetcher'
require 'test/unit/fault-location-detector'
require 'test/unit/diff'
require 'test/unit/ui/testrunner'
require 'test/unit/ui/testrunnermediator'
require 'test/unit/ui/console/outputlevel'
module Test
module Unit
module UI
module Console
# Runs a Test::Unit::TestSuite on the console.
class TestRunner < UI::TestRunner
include OutputLevel
# Creates a new TestRunner for running the passed
# suite. If quiet_mode is true, the output while
# running is limited to progress dots, errors and
# failures, and the final result. io specifies
# where runner output should go to; defaults to
# STDOUT.
def initialize(suite, options={})
super
@output_level = @options[:output_level] || NORMAL
@output = @options[:output] || STDOUT
@use_color = @options[:use_color]
@use_color = guess_color_availability if @use_color.nil?
@color_scheme = @options[:color_scheme] || ColorScheme.default
@reset_color = Color.new("reset")
@progress_row = 0
@progress_row_max = @options[:progress_row_max]
@progress_row_max ||= guess_progress_row_max
@show_detail_immediately = @options[:show_detail_immediately]
@show_detail_immediately = true if @show_detail_immediately.nil?
@already_outputted = false
@indent = 0
@top_level = true
@current_output_level = NORMAL
@faults = []
@code_snippet_fetcher = CodeSnippetFetcher.new
@test_suites = []
end
private
def change_output_level(level)
old_output_level = @current_output_level
@current_output_level = level
yield
@current_output_level = old_output_level
end
def setup_mediator
super
output_setup_end
end
def output_setup_end
suite_name = @suite.to_s
suite_name = @suite.name if @suite.kind_of?(Module)
output("Loaded suite #{suite_name}")
end
def attach_to_mediator
@mediator.add_listener(TestResult::FAULT,
&method(:add_fault))
@mediator.add_listener(TestRunnerMediator::STARTED,
&method(:started))
@mediator.add_listener(TestRunnerMediator::FINISHED,
&method(:finished))
@mediator.add_listener(TestCase::STARTED_OBJECT,
&method(:test_started))
@mediator.add_listener(TestCase::FINISHED_OBJECT,
&method(:test_finished))
@mediator.add_listener(TestSuite::STARTED_OBJECT,
&method(:test_suite_started))
@mediator.add_listener(TestSuite::FINISHED_OBJECT,
&method(:test_suite_finished))
end
def add_fault(fault)
@faults << fault
output_progress(fault.single_character_display,
fault_marker_color(fault))
output_progress_in_detail(fault) if @show_detail_immediately
@already_outputted = true if fault.critical?
end
def started(result)
@result = result
output_started
end
def output_started
output("Started")
end
def finished(elapsed_time)
nl if output?(NORMAL) and !output?(VERBOSE)
output_faults unless @show_detail_immediately
nl(PROGRESS_ONLY)
change_output_level(IMPORTANT_FAULTS_ONLY) do
output_statistics(elapsed_time)
end
end
def output_faults
categorized_faults = categorize_faults
change_output_level(IMPORTANT_FAULTS_ONLY) do
output_faults_in_detail(categorized_faults[:need_detail_faults])
end
output_faults_in_short("Omissions", Omission,
categorized_faults[:omissions])
output_faults_in_short("Notifications", Notification,
categorized_faults[:notifications])
end
def max_digit(max_number)
(Math.log10(max_number) + 1).truncate
end
def output_faults_in_detail(faults)
return if faults.nil?
digit = max_digit(faults.size)
faults.each_with_index do |fault, index|
nl
output_single("%#{digit}d) " % (index + 1))
output_fault_in_detail(fault)
end
end
def output_faults_in_short(label, fault_class, faults)
return if faults.nil?
digit = max_digit(faults.size)
nl
output_single(label, fault_class_color(fault_class))
output(":")
faults.each_with_index do |fault, index|
output_single("%#{digit}d) " % (index + 1))
output_fault_in_short(fault)
end
end
def categorize_faults
faults = {}
@faults.each do |fault|
category = categorize_fault(fault)
faults[category] ||= []
faults[category] << fault
end
faults
end
def categorize_fault(fault)
case fault
when Omission
:omissions
when Notification
:notifications
else
:need_detail_faults
end
end
def output_fault_in_detail(fault)
if fault.is_a?(Failure) and
fault.inspected_expected and fault.inspected_actual
output_single("#{fault.label}: ")
output(fault.test_name, fault_color(fault))
output_fault_backtrace(fault)
output_failure_message(fault)
else
if fault.is_a?(Error)
output_single("#{fault.label}: ")
output_single(fault.test_name, fault_color(fault))
output_fault_message(fault)
else
output_single(fault.label, fault_color(fault))
output_fault_message(fault)
output(fault.test_name)
end
output_fault_backtrace(fault)
end
end
def output_fault_message(fault)
message = fault.message
return if message.nil?
if message.include?("\n")
output(":")
message.each_line do |line|
output(" #{line.chomp}")
end
else
output(": #{message}")
end
end
def output_fault_backtrace(fault)
snippet_is_shown = false
detector = FaultLocationDetector.new(fault, @code_snippet_fetcher)
backtrace = fault.location
# workaround for test-spec. :<
# see also GitHub:#22
backtrace ||= []
backtrace.each_with_index do |entry, i|
output(entry)
next if snippet_is_shown
next unless detector.target?(entry)
file, line_number, = detector.split_backtrace_entry(entry)
snippet_is_shown = output_code_snippet(file, line_number,
fault_color(fault))
end
end
def output_code_snippet(file, line_number, target_line_color=nil)
lines = @code_snippet_fetcher.fetch(file, line_number)
return false if lines.empty?
max_n = lines.collect {|n, line, attributes| n}.max
digits = (Math.log10(max_n) + 1).truncate
lines.each do |n, line, attributes|
if attributes[:target_line?]
line_color = target_line_color
current_line_mark = "=>"
else
line_color = nil
current_line_mark = ""
end
output(" %2s %*d: %s" % [current_line_mark, digits, n, line],
line_color)
end
true
end
def output_failure_message(failure)
if failure.expected.respond_to?(:encoding) and
failure.actual.respond_to?(:encoding) and
failure.expected.encoding != failure.actual.encoding
need_encoding = true
else
need_encoding = false
end
output(failure.user_message) if failure.user_message
output_single("<")
output_single(failure.inspected_expected, color("pass"))
output_single(">")
if need_encoding
output_single("(")
output_single(failure.expected.encoding.name, color("pass"))
output_single(")")
end
output(" expected but was")
output_single("<")
output_single(failure.inspected_actual, color("failure"))
output_single(">")
if need_encoding
output_single("(")
output_single(failure.actual.encoding.name, color("failure"))
output_single(")")
end
output("")
from, to = prepare_for_diff(failure.expected, failure.actual)
if from and to
if need_encoding
unless from.valid_encoding?
from = from.dup.force_encoding("ASCII-8BIT")
end
unless to.valid_encoding?
to = to.dup.force_encoding("ASCII-8BIT")
end
end
from_lines = from.split(/\r?\n/)
to_lines = to.split(/\r?\n/)
if need_encoding
from_lines << ""
to_lines << ""
from_lines << "Encoding: #{failure.expected.encoding.name}"
to_lines << "Encoding: #{failure.actual.encoding.name}"
end
differ = ColorizedReadableDiffer.new(from_lines, to_lines, self)
if differ.need_diff?
output("")
output("diff:")
differ.diff
end
end
end
def output_fault_in_short(fault)
output_single(fault.message, fault_color(fault))
output(" [#{fault.test_name}]")
output(fault.location.first)
end
def format_fault(fault)
fault.long_display
end
def output_statistics(elapsed_time)
output("Finished in #{elapsed_time} seconds.")
output_summary_marker
output(@result)
output("%g%% passed" % @result.pass_percentage)
unless elapsed_time.zero?
output_summary_marker
test_throughput = @result.run_count / elapsed_time
assertion_throughput = @result.assertion_count / elapsed_time
throughput = [
"%.2f tests/s" % test_throughput,
"%.2f assertions/s" % assertion_throughput,
]
output(throughput.join(", "))
end
end
def output_summary_marker
term_width = guess_term_width
if term_width.zero?
marker_width = 6
else
marker_width = term_width
end
output("-" * marker_width, summary_marker_color)
end
def test_started(test)
return unless output?(VERBOSE)
name = test.name.sub(/\(.+?\)\z/, '')
right_space = 8 * 2
left_space = @progress_row_max - right_space
left_space = left_space - indent.size - name.size
tab_stop = "\t" * ([left_space - 1, 0].max / 8)
output_single("#{indent}#{name}:#{tab_stop}", nil, VERBOSE)
@test_start = Time.now
end
def test_finished(test)
unless @already_outputted
output_progress(".", color("pass-marker"))
end
@already_outputted = false
return unless output?(VERBOSE)
output(": (%f)" % (Time.now - @test_start), nil, VERBOSE)
end
def suite_name(prefix, suite)
name = suite.name
if name.nil?
"(anonymous)"
else
name.sub(/\A#{Regexp.escape(prefix)}/, "")
end
end
def test_suite_started(suite)
last_test_suite = @test_suites.last
@test_suites << suite
if @top_level
@top_level = false
return
end
output_single(indent, nil, VERBOSE)
if suite.test_case.nil?
_color = color("suite")
else
_color = color("case")
end
prefix = "#{last_test_suite.name}::"
output_single(suite_name(prefix, suite), _color, VERBOSE)
output(": ", nil, VERBOSE)
@indent += 2
end
def test_suite_finished(suite)
@indent -= 2
@test_suites.pop
end
def indent
if output?(VERBOSE)
" " * @indent
else
""
end
end
def nl(level=nil)
output("", nil, level)
end
def output(something, color=nil, level=nil)
return unless output?(level)
output_single(something, color, level)
@output.puts
end
def output_single(something, color=nil, level=nil)
return false unless output?(level)
if @use_color and color
something = "%s%s%s" % [color.escape_sequence,
something,
@reset_color.escape_sequence]
end
@output.write(something)
@output.flush
true
end
def output_progress(mark, color=nil)
if output_single(mark, color, PROGRESS_ONLY)
return unless @progress_row_max > 0
@progress_row += mark.size
if @progress_row >= @progress_row_max
nl unless @output_level == VERBOSE
@progress_row = 0
end
end
end
def output_progress_in_detail_marker(fault)
if @progress_row_max > 0
output("=" * @progress_row_max)
else
nl
end
end
def output_progress_in_detail(fault)
return if @output_level == SILENT
nl
output_progress_in_detail_marker(fault)
if categorize_fault(fault) == :need_detail_faults
output_fault_in_detail(fault)
else
output_fault_in_short(fault)
end
output_progress_in_detail_marker(fault)
@progress_row = 0
end
def output?(level)
(level || @current_output_level) <= @output_level
end
def color(name)
_color = @color_scheme[name]
_color ||= @color_scheme["success"] if name == "pass"
_color ||= ColorScheme.default[name]
_color
end
def fault_class_color_name(fault_class)
fault_class.name.split(/::/).last.downcase
end
def fault_color(fault)
color(fault_class_color_name(fault.class))
end
def fault_marker_color(fault)
color("#{fault_class_color_name(fault.class)}-marker")
end
def summary_marker_color
color("#{@result.status}-marker")
end
def guess_color_availability
return false unless @output.tty?
return true if windows? and ruby_2_0_or_later?
case ENV["TERM"]
when /(?:term|screen)(?:-(?:256)?color)?\z/
true
else
return true if ENV["EMACS"] == "t"
false
end
end
def windows?
/mswin|mingw/ === RUBY_PLATFORM
end
def ruby_2_0_or_later?
RUBY_VERSION >= "2.0.0"
end
def guess_progress_row_max
term_width = guess_term_width
if term_width.zero?
if ENV["EMACS"] == "t"
-1
else
79
end
else
term_width
end
end
def guess_term_width
guess_term_width_from_io || guess_term_width_from_env || 0
end
def guess_term_width_from_io
if @output.respond_to?(:winsize)
begin
@output.winsize[1]
rescue SystemCallError
nil
end
else
nil
end
end
def guess_term_width_from_env
env = ENV["COLUMNS"] || ENV["TERM_WIDTH"]
return nil if env.nil?
begin
Integer(env)
rescue ArgumentError
nil
end
end
end
class ColorizedReadableDiffer < Diff::ReadableDiffer
def initialize(from, to, runner)
@runner = runner
super(from, to)
end
def need_diff?(options={})
return false if one_line_all_change?
operations.each do |tag,|
return true if [:replace, :equal].include?(tag)
end
false
end
private
def one_line_all_change?
return false if operations.size != 1
tag, from_start, from_end, to_start, to_end = operations.first
return false if tag != :replace
return false if [from_start, from_end] != [0, 1]
return false if [from_start, from_end] != [to_start, to_end]
_, _, _line_operations = line_operations(@from.first, @to.first)
_line_operations.size == 1
end
def output_single(something, color=nil)
@runner.__send__(:output_single, something, color)
end
def output(something, color=nil)
@runner.__send__(:output, something, color)
end
def color(name)
@runner.__send__(:color, name)
end
def cut_off_ratio
0
end
def default_ratio
0
end
def tag(mark, color_name, contents)
_color = color(color_name)
contents.each do |content|
output_single(mark, _color)
output_single(" ")
output(content)
end
end
def tag_deleted(contents)
tag("-", "diff-deleted-tag", contents)
end
def tag_inserted(contents)
tag("+", "diff-inserted-tag", contents)
end
def tag_equal(contents)
tag(" ", "normal", contents)
end
def tag_difference(contents)
tag("?", "diff-difference-tag", contents)
end
def diff_line(from_line, to_line)
to_operations = []
from_line, to_line, _operations = line_operations(from_line, to_line)
no_replace = true
_operations.each do |tag,|
if tag == :replace
no_replace = false
break
end
end
output_single("?", color("diff-difference-tag"))
output_single(" ")
_operations.each do |tag, from_start, from_end, to_start, to_end|
from_width = compute_width(from_line, from_start, from_end)
to_width = compute_width(to_line, to_start, to_end)
case tag
when :replace
output_single(from_line[from_start...from_end],
color("diff-deleted"))
if (from_width < to_width)
output_single(" " * (to_width - from_width))
end
to_operations << Proc.new do
output_single(to_line[to_start...to_end],
color("diff-inserted"))
if (to_width < from_width)
output_single(" " * (from_width - to_width))
end
end
when :delete
output_single(from_line[from_start...from_end],
color("diff-deleted"))
unless no_replace
to_operations << Proc.new {output_single(" " * from_width)}
end
when :insert
if no_replace
output_single(to_line[to_start...to_end],
color("diff-inserted"))
else
output_single(" " * to_width)
to_operations << Proc.new do
output_single(to_line[to_start...to_end],
color("diff-inserted"))
end
end
when :equal
output_single(from_line[from_start...from_end])
unless no_replace
to_operations << Proc.new {output_single(" " * to_width)}
end
else
raise "unknown tag: #{tag}"
end
end
output("")
unless to_operations.empty?
output_single("?", color("diff-difference-tag"))
output_single(" ")
to_operations.each do |operation|
operation.call
end
output("")
end
end
end
end
end
end
end