Class: Minitap::Reporter

Inherits:
Minitest::StatisticsReporter
  • Object
show all
Defined in:
lib/minitap/reporter.rb

Overview

Base class of the TAP-Y and TAP-J reporters. It turns Minitest's results into the records of a TAP-Y/J stream; the subclasses write them out.

While tests run, $stdout and $stderr are captured, so that what a test prints becomes part of its record instead of breaking the stream.

Direct Known Subclasses

TapJ, TapY

Constant Summary collapse

REVISION =

TAP-Y/J revision.

4
SNIPPET_RADIUS =

Number of lines of source to show on each side of a failure.

2
LIB_DIRECTORY =

Where Minitap's own code lives, to keep it out of backtraces.

File.expand_path('..', __dir__) + File::SEPARATOR

Instance Method Summary collapse

Constructor Details

#initialize(io = $stdout, options = {}) ⇒ Reporter

Returns a new instance of Reporter.



24
25
26
27
28
29
30
# File 'lib/minitap/reporter.rb', line 24

def initialize(io = $stdout, options = {})
  super
  # Tapout reads from a pipe, so nothing should wait in a buffer.
  io.sync = true if io.respond_to?(:sync=)
  @source_cache = {}
  @current_case = nil
end

Instance Method Details

#actual_error(failure) ⇒ Object (private)

Minitest wraps an error raised by a test in an UnexpectedError.



168
169
170
171
# File 'lib/minitap/reporter.rb', line 168

def actual_error(failure)
  return failure unless Minitest::UnexpectedError === failure
  failure.respond_to?(:error) ? failure.error : failure.exception
end

#capture_io ⇒ Object (private)



234
235
236
237
238
# File 'lib/minitap/reporter.rb', line 234

def capture_io
  @real_stdout, @real_stderr = $stdout, $stderr
  @stdout, @stderr = StringIO.new, StringIO.new
  $stdout, $stderr = @stdout, @stderr
end

#captured_output ⇒ Object (private)

What has been printed since the last record, as two strings.



247
248
249
250
251
252
253
254
# File 'lib/minitap/reporter.rb', line 247

def captured_output
  [@stdout, @stderr].map do |capture|
    text = capture.string.dup
    capture.truncate(0)
    capture.rewind
    text
  end
end

#case_name(result) ⇒ Object (private)

The name of the class the test belongs to.



142
143
144
# File 'lib/minitap/reporter.rb', line 142

def case_name(result)
  result.respond_to?(:klass) ? result.klass.to_s : result.class.name
end

#case_record(test_case) ⇒ Object (private)



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

def case_record(test_case)
  {
    'type'  => 'case',
    'label' => test_case.to_s,
    'level' => 0
  }
end

#clean_backtrace(backtrace) ⇒ Object (private)

The backtrace without the test framework, without the method names, and with paths relative to the working directory.



175
176
177
178
179
180
181
# File 'lib/minitap/reporter.rb', line 175

def clean_backtrace(backtrace)
  trace = Minitest.filter_backtrace(Array(backtrace))
  trace = trace.reject { |line| line.start_with?(LIB_DIRECTORY) }
  trace.map do |line|
    line.sub(/:in .*\z/m, '').sub(Dir.pwd + File::SEPARATOR, '')
  end
end

#emit(record) ⇒ Object (private)

Write a record to the stream. Subclasses define this.

Raises:

  • (NotImplementedError)


62
63
64
# File 'lib/minitap/reporter.rb', line 62

def emit(record)
  raise NotImplementedError
end

#exception_record(failure) ⇒ Object (private)



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/minitap/reporter.rb', line 147

def exception_record(failure)
  error     = actual_error(failure)
  backtrace = clean_backtrace(failure.backtrace || error.backtrace)
  file, line = location(backtrace)

  record = { 'message' => error.message.to_s.strip, 'class' => error.class.name }

  if file
    record['file'] = file
    record['line'] = line
    if lines = source(file)
      record['source']  = lines[line - 1].to_s.strip
      record['snippet'] = snippet(lines, line)
    end
  end

  record['backtrace'] = backtrace
  record
end

#final_record ⇒ Object (private)



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/minitap/reporter.rb', line 110

def final_record
  {
    'type'   => 'final',
    'time'   => Time.now - @suite_start,
    'counts' => {
      'total' => count,
      'pass'  => count - failures - errors - skips,
      'fail'  => failures,
      'error' => errors,
      'omit'  => skips,
      'todo'  => 0
    }
  }
end

#finish ⇒ Object (private)

Write whatever ends the stream.



67
68
# File 'lib/minitap/reporter.rb', line 67

def finish
end

#label(result) ⇒ Object (private)

The name of the test. Minitest::Spec names a test test_0001_does something; the label is the part that was written.



137
138
139
# File 'lib/minitap/reporter.rb', line 137

def label(result)
  result.name.to_s.sub(/\Atest_\d{4}_/, '')
end

#location(backtrace) ⇒ Object (private)

File and line number of the first line of a backtrace.



184
185
186
187
# File 'lib/minitap/reporter.rb', line 184

def location(backtrace)
  md = /\A(.+?):(\d+)\z/.match(backtrace.first.to_s)
  md ? [md[1], md[2].to_i] : [nil, nil]
end

#name_pattern(pattern) ⇒ Object (private)

A name option written /like this/ is a regular expression.



229
230
231
# File 'lib/minitap/reporter.rb', line 229

def name_pattern(pattern)
  String === pattern && pattern =~ %r{/(.*)/} ? Regexp.new($1) : pattern
end

#record(result) ⇒ Object

Minitest calls this with the result of each test.



41
42
43
44
45
46
47
48
49
# File 'lib/minitap/reporter.rb', line 41

def record(result)
  super
  test_case = case_name(result)
  if test_case != @current_case
    @current_case = test_case
    emit case_record(test_case)
  end
  emit test_record(result)
end

#release_io ⇒ Object (private)



241
242
243
244
# File 'lib/minitap/reporter.rb', line 241

def release_io
  $stdout = @real_stdout if $stdout.equal?(@stdout)
  $stderr = @real_stderr if $stderr.equal?(@stderr)
end

#report ⇒ Object

Minitest calls this when all tests have run.



52
53
54
55
56
57
# File 'lib/minitap/reporter.rb', line 52

def report
  super
  release_io
  emit final_record
  finish
end

#selected_methods(runnable) ⇒ Object (private)

The tests of a class that Minitest's name options select. The option is :include in Minitest 6 and :filter before.



217
218
219
220
221
222
223
224
225
226
# File 'lib/minitap/reporter.rb', line 217

def selected_methods(runnable)
  methods = runnable.runnable_methods
  wanted  = name_pattern(options[:include] || options[:filter])
  spurned = name_pattern(options[:exclude])
  methods = methods.select { |m| wanted === m  || wanted === "#{runnable}##{m}" } if wanted
  methods = methods.reject { |m| spurned === m || spurned === "#{runnable}##{m}" } if spurned
  methods
rescue NotImplementedError
  []
end

#snippet(lines, line) ⇒ Object (private)

The lines around a line of source, as a list of number => text.



196
197
198
199
200
# File 'lib/minitap/reporter.rb', line 196

def snippet(lines, line)
  first = [line - SNIPPET_RADIUS, 1].max
  last  = [line + SNIPPET_RADIUS, lines.size].min
  (first..last).map { |n| { n => lines[n - 1].chomp } }
end

#source(file) ⇒ Object (private)

The lines of a source file, or nil if it can't be read.



190
191
192
193
# File 'lib/minitap/reporter.rb', line 190

def source(file)
  return @source_cache[file] if @source_cache.key?(file)
  @source_cache[file] = File.file?(file) ? File.readlines(file) : nil
end

#start ⇒ Object

Minitest calls this just before the tests run.



33
34
35
36
37
38
# File 'lib/minitap/reporter.rb', line 33

def start
  super
  @suite_start = Time.now
  capture_io
  emit suite_record
end

#status(failure) ⇒ Object (private)

TAP-Y/J calls a skipped test omitted.



126
127
128
129
130
131
132
133
# File 'lib/minitap/reporter.rb', line 126

def status(failure)
  case failure
  when nil                        then 'pass'
  when Minitest::UnexpectedError  then 'error'
  when Minitest::Skip             then 'omit'
  else                                 'fail'
  end
end

#suite_record ⇒ Object (private)



71
72
73
74
75
76
77
78
79
# File 'lib/minitap/reporter.rb', line 71

def suite_record
  {
    'type'  => 'suite',
    'start' => @suite_start.strftime('%Y-%m-%d %H:%M:%S'),
    'count' => test_count,
    'seed'  => options[:seed],
    'rev'   => REVISION
  }
end

#test_count ⇒ Object (private)

How many tests are about to run.

Asking a class for its tests shuffles them, which draws on Ruby's random numbers. So they are seeded again afterwards, and the tests run in the order they would have without Minitap.



207
208
209
210
211
212
213
# File 'lib/minitap/reporter.rb', line 207

def test_count
  total = Minitest::Runnable.runnables.inject(0) do |sum, runnable|
    sum + selected_methods(runnable).size
  end
  srand options[:seed] if options[:seed]
  total
end

#test_record(result) ⇒ Object (private)



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/minitap/reporter.rb', line 91

def test_record(result)
  failure = result.failure

  record = {
    'type'   => 'test',
    'status' => status(failure),
    'label'  => label(result)
  }
  record['exception'] = exception_record(failure) if failure

  stdout, stderr = captured_output
  record['stdout'] = stdout unless stdout.empty?
  record['stderr'] = stderr unless stderr.empty?

  record['time'] = Time.now - @suite_start
  record
end