rspec-ontap 0.3.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/rspec/ontap.rb CHANGED
@@ -1,512 +1,344 @@
1
+ require 'rspec/core'
1
2
  require 'rspec/core/formatters/base_formatter'
3
+ require 'rspec/ontap/version'
4
+ require 'stringio'
2
5
 
3
6
  module RSpec
4
- #module Core
5
- #module Formatters
6
7
 
7
- #
8
+ # Base class of the TAP-Y and TAP-J formatters. It turns RSpec's
9
+ # notifications into the records of a TAP-Y/J stream; the subclasses
10
+ # write them out.
11
+ #
12
+ # While an example runs, `$stdout` and `$stderr` are captured, so that
13
+ # what it prints becomes part of its record instead of breaking the
14
+ # stream.
8
15
  class TapBaseFormatter < Core::Formatters::BaseFormatter
9
16
 
10
- # TAP-Y/J Revision
17
+ # TAP-Y/J revision.
11
18
  REVISION = 4
12
19
 
13
- #
14
- attr_accessor :example_group_stack
20
+ # Number of lines of source to show on each side of a line.
21
+ SNIPPET_RADIUS = 2
22
+
23
+ # The notifications the formatters ask RSpec for.
24
+ NOTIFICATIONS = [
25
+ :start,
26
+ :example_group_started,
27
+ :example_group_finished,
28
+ :example_started,
29
+ :example_passed,
30
+ :example_failed,
31
+ :example_pending,
32
+ :message,
33
+ :dump_summary,
34
+ :close
35
+ ]
15
36
 
16
- #
17
37
  def initialize(output)
18
38
  super(output)
19
- @example_group_stack = []
39
+ # Tapout reads from a pipe, so nothing should wait in a buffer.
40
+ output.sync = true if output.respond_to?(:sync=)
41
+ @group_level = 0
42
+ @source_cache = {}
43
+ @omit_count = 0
44
+ @early_notes = []
20
45
  end
21
46
 
22
- #
23
- # This method is invoked before any examples are run, right after
24
- # they have all been collected. This can be useful for special
25
- # formatters that need to provide progress on feedback (graphical ones)
26
- #
27
- # This will only be invoked once, and the next one to be invoked
28
- # is #example_group_started
29
- #
47
+ # Invoked once, before any example runs.
30
48
  def start(notification)
31
- # there is a super method for this
32
- super(notification)
33
-
49
+ super
34
50
  @start_time = Time.now
35
-
36
- doc = {
51
+ @seed = configuration.seed if configuration.seed_used?
52
+ emit(
37
53
  'type' => 'suite',
38
54
  'start' => @start_time.strftime('%Y-%m-%d %H:%M:%S'),
39
55
  'count' => notification.count,
40
56
  'seed' => @seed,
41
57
  'rev' => REVISION
42
- }
43
- return doc
58
+ )
59
+ @early_notes.each { |note| emit note }
60
+ @early_notes = nil
44
61
  end
45
62
 
46
- #
47
- # This method is invoked at the beginning of the execution of each example group.
48
- # +example_group+ is the example_group.
49
- #
50
- # The next method to be invoked after this is +example_passed+,
51
- # +example_pending+, or +example_finished+
52
- #
63
+ # Invoked at the start of each example group.
53
64
  def example_group_started(notification)
54
- # there is a super method for this
55
- super(notification)
56
- doc = {
65
+ super
66
+ emit(
57
67
  'type' => 'case',
58
68
  'subtype' => 'describe',
59
- 'label' => "#{notification.group.description}",
60
- 'level' => @example_group_stack.size
61
- }
62
- @example_group_stack << example_group
63
- return doc
69
+ 'label' => notification.group.description.to_s,
70
+ 'level' => @group_level
71
+ )
72
+ @group_level += 1
64
73
  end
65
74
 
66
- # This method is invoked at the end of the execution of each example group.
67
- # +example_group+ is the example_group.
75
+ # Invoked at the end of each example group.
68
76
  def example_group_finished(notification)
69
- #super(notification)
70
- @example_group_stack.pop
77
+ @group_level -= 1
71
78
  end
72
79
 
73
- # Set up stdout and stderr to be captured.
74
- #
75
- # IMPORTANT: Comment out the `reset_output` line to debug!!!!!!!!!
76
- #
80
+ # Invoked before each example. Starts capturing its output.
77
81
  def example_started(notification)
78
- reset_output
82
+ capture_output
79
83
  end
80
84
 
81
85
  #
82
86
  def example_passed(notification)
83
- #super(notification)
84
-
85
- example = notification.example
86
-
87
- file, line = example.location.split(':')
88
- #file = self.class.relative_path(file)
89
- file = relative_path(file)
90
- line = line.to_i
91
-
92
- doc = {
93
- 'type' => 'test',
94
- 'subtype' => 'it',
95
- 'status' => 'pass',
96
- #'setup': foo instance
97
- 'label' => "#{example.description}",
98
- #'expected' => 2
99
- #'returned' => 2
100
- 'file' => file,
101
- 'line' => line,
102
- 'source' => source(file)[line-1].strip,
103
- 'snippet' => code_snippet(file, line),
104
- #'coverage' => {
105
- # file: lib/foo.rb
106
- # line: 11..13
107
- # code: Foo#*
108
- #}
109
- 'time' => Time.now - @start_time
110
- }
111
-
112
- doc.update(captured_output)
113
-
114
- return doc
87
+ emit test_record(notification.example, 'pass')
115
88
  end
116
89
 
90
+ # Invoked for an example that was skipped or is pending.
117
91
  #
92
+ # An example that was skipped (`skip`, `xit`) is an `omit` in
93
+ # TAP-Y/J. One that is pending, or has no body yet, is a `todo`.
118
94
  def example_pending(notification)
119
- #super(notification)
120
-
121
95
  example = notification.example
96
+ reason = example.execution_result.pending_message.to_s
122
97
 
123
- file, line = example.location.split(':')
124
- #file = self.class.relative_path(file)
125
- file = relative_path(file)
126
- line = line.to_i
127
-
128
- doc = {
129
- 'type' => 'test',
130
- 'subtype' => 'it',
131
- 'status' => 'todo',
132
- #'setup': foo instance
133
- 'label' => "#{example.description}",
134
- 'file' => file,
135
- 'line' => line,
136
- 'source' => source(file)[line-1].strip,
137
- 'snippet' => code_snippet(file, line),
138
- #'coverage' => {
139
- # file: lib/foo.rb
140
- # line: 11..13
141
- # code: Foo#*
142
- #}
143
- 'time' => Time.now - @start_time
144
- }
145
-
146
- doc.update(captured_output)
98
+ if example.skipped? && reason != Core::Pending::NOT_YET_IMPLEMENTED
99
+ status = 'omit'
100
+ @omit_count += 1
101
+ else
102
+ status = 'todo'
103
+ end
147
104
 
148
- return doc
105
+ record = test_record(example, status)
106
+ record['exception'] = { 'message' => reason } unless reason.empty?
107
+ emit record
149
108
  end
150
109
 
151
110
  #
152
111
  def example_failed(notification)
153
- #super(notification)
154
-
155
- example = notification.example
156
-
157
- file, line = example.location.split(':')
158
-
159
- file = relative_path(file)
160
- line = line.to_i
161
-
162
- if RSpec::Expectations::ExpectationNotMetError === example.exception
163
- status = 'fail'
164
- if md = /expected:\s*(.*?)\n\s*got:\s*(.*?)\s+/.match(example.exception.to_s)
165
- expected, returned = md[1], md[2]
166
- else
167
- expected, returned = nil, nil
168
- end
169
- else
170
- status = 'error'
171
- end
172
-
173
- backtrace = format_backtrace(example.exception.backtrace, example.metadata)
174
-
175
- efile, eline = parse_source_location(backtrace)
176
-
177
- doc = {
178
- 'type' => 'test',
179
- 'subtype' => 'it',
180
- 'status' => status,
181
- 'label' => "#{example.description}",
182
- #'setup' => "foo instance",
183
- 'file' => file,
184
- 'line' => line,
185
- 'source' => source(file)[line-1].strip,
186
- 'snippet' => code_snippet(file, line),
187
- #'coverage' =>
188
- #{
189
- # 'file' => lib/foo.rb
190
- # 'line' => 11..13
191
- # 'code' => Foo#*
192
- #}
193
- }
112
+ example = notification.example
113
+ exception = example.exception
114
+ record = test_record(example, failure?(exception) ? 'fail' : 'error')
194
115
 
195
- if expected or returned
196
- doc.update(
197
- 'expected' => expected,
198
- 'returned' => returned,
199
- )
116
+ if md = /expected:\s*(.*?)\n\s*got:\s*(.*?)\s+/.match(exception.to_s)
117
+ record['expected'] = md[1]
118
+ record['returned'] = md[2]
200
119
  end
201
120
 
202
- doc.update(
203
- 'exception' => {
204
- 'message' => example.exception.to_s.strip,
205
- 'class' => example.exception.class.name,
206
- 'file' => efile,
207
- 'line' => eline,
208
- 'source' => source(efile)[eline-1].strip,
209
- 'snippet' => code_snippet(efile, eline),
210
- 'backtrace' => backtrace
211
- },
212
- 'time' => Time.now - @start_time
213
- )
214
-
215
- doc.update(captured_output)
216
-
217
- return doc
121
+ record['exception'] = exception_record(example, exception)
122
+ emit record
218
123
  end
219
124
 
220
- # This method is invoked after the dumping of examples and failures.
221
- def dump_summary(summary_notification)
222
- #super(summary_notification)
223
-
224
- duration = summary_notification.duration
225
- example_count = summary_notification.examples.size
226
- failure_count = summary_notification.failed_examples.size
227
- pending_count = summary_notification.pending_examples.size
228
-
229
- failed_examples = summary_notification.failed_examples
230
-
231
- error_count = 0
232
-
233
- failed_examples.each do |e|
234
- if RSpec::Expectations::ExpectationNotMetError === e.exception
235
- #failure_count += 1
236
- else
237
- failure_count -= 1
238
- error_count += 1
239
- end
240
- end
125
+ # Invoked for anything RSpec has to say outside of an example, such
126
+ # as an error while loading a spec file. It becomes a note.
127
+ def message(notification)
128
+ text = notification.message.to_s.strip
129
+ return if text.empty?
130
+ note = { 'type' => 'note', 'text' => text }
131
+ # The stream has to begin with the suite, so a note that comes
132
+ # before it waits.
133
+ @early_notes ? @early_notes << note : emit(note)
134
+ end
241
135
 
242
- passing_count = example_count - failure_count - error_count - pending_count
136
+ # Invoked when all examples have run.
137
+ def dump_summary(summary)
138
+ total = summary.examples.size
139
+ failed = summary.failed_examples
140
+ failures = failed.count { |example| failure?(example.exception) }
141
+ errors = failed.size - failures
142
+ pending = summary.pending_examples.size
243
143
 
244
- doc = {
245
- 'type' => 'final',
246
- 'time' => duration,
144
+ emit(
145
+ 'type' => 'final',
146
+ 'time' => summary.duration,
247
147
  'counts' => {
248
- 'total' => example_count,
249
- 'pass' => passing_count,
250
- 'fail' => failure_count,
251
- 'error' => error_count,
252
- 'omit' => 0,
253
- 'todo' => pending_count
148
+ 'total' => total,
149
+ 'pass' => total - failed.size - pending,
150
+ 'fail' => failures,
151
+ 'error' => errors,
152
+ 'omit' => @omit_count,
153
+ 'todo' => pending - @omit_count
254
154
  }
255
- }
256
- return doc
257
- end
258
-
259
- # This gets invoked after the summary if option is set to do so.
260
- #def dump_pending
261
- #end
262
-
263
- def seed(notification)
264
- @seed = notification.seed
155
+ )
156
+ finish
265
157
  end
266
158
 
267
- # Add any messages as notes.
268
- def message(message_notification)
269
- doc = {
270
- 'type' => 'note',
271
- 'text' => message_notification.message
272
- }
273
- return doc
159
+ # Invoked last. Makes sure the real output streams are back.
160
+ def close(notification)
161
+ release_output
162
+ super
274
163
  end
275
164
 
276
- #
277
- # NOTE: None of the following are being used. If ever added, be sure
278
- # to activate in register calls below.
279
- #
165
+ private
280
166
 
281
- # (not used)
282
- def stop(examples_notification)
283
- super(examples_notification)
167
+ # Write a record to the stream. Subclasses define this.
168
+ def emit(record)
169
+ raise NotImplementedError
284
170
  end
285
171
 
286
- # (not used)
287
- def start_dump(null_notification)
172
+ # Write whatever ends the stream.
173
+ def finish
288
174
  end
289
175
 
290
- # (not used)
291
- def dump_pending(examples_notification)
292
- end
176
+ # What every test record has: where the example is and what it
177
+ # printed.
178
+ def test_record(example, status)
179
+ record = {
180
+ 'type' => 'test',
181
+ 'subtype' => 'it',
182
+ 'status' => status,
183
+ 'label' => example.description.to_s
184
+ }
293
185
 
294
- # (not used)
295
- def dump_failures(examples_notification)
296
- end
186
+ file, line = split_location(example.location)
187
+ if file
188
+ record['file'] = file
189
+ record['line'] = line
190
+ add_source(record, file, line)
191
+ end
297
192
 
298
- # (not used)
299
- def close(null_notification)
300
- # there is a super method for this
301
- super(null_notification)
193
+ record.update(captured_output)
194
+ record['time'] = Time.now - @start_time
195
+ record
302
196
  end
303
197
 
304
- private
198
+ #
199
+ def exception_record(example, exception)
200
+ backtrace = format_backtrace(exception.backtrace || [], example.metadata)
201
+ file, line = split_location(backtrace.first)
305
202
 
306
- # Returns a String of source code.
307
- def code_snippet(file, line)
308
- s = []
309
- if File.file?(file)
310
- source = source(file)
311
- radius = 2 # TODO: make customizable (number of surrounding lines to show)
312
- region = [line - radius, 1].max ..
313
- [line + radius, source.length].min
314
-
315
- s = region.map do |n|
316
- {n => source[n-1].chomp}
317
- end
203
+ record = {
204
+ 'message' => exception.to_s.strip,
205
+ 'class' => exception.class.name
206
+ }
207
+ if file
208
+ record['file'] = file
209
+ record['line'] = line
210
+ add_source(record, file, line)
318
211
  end
319
- return s
212
+ record['backtrace'] = backtrace
213
+ record
320
214
  end
321
215
 
322
- # Cache source file text. This is only used if the TAP-Y stream
323
- # doesn not provide a snippet and the test file is locatable.
324
- def source(file)
325
- @_source_cache ||= {}
326
- @_source_cache[file] ||= (
327
- File.readlines(file)
328
- )
216
+ # An expectation that wasn't met is a failure, as is a pending
217
+ # example that passed. Anything else raised is an error.
218
+ def failure?(exception)
219
+ return true if Core::Pending::PendingExampleFixedError === exception
220
+ return true if defined?(RSpec::Expectations::ExpectationNotMetError) &&
221
+ RSpec::Expectations::ExpectationNotMetError === exception
222
+ return true if defined?(RSpec::Mocks::MockExpectationError) &&
223
+ RSpec::Mocks::MockExpectationError === exception
224
+ false
329
225
  end
330
226
 
331
- # Parse source location from caller, caller[0] or an Exception object.
332
- def parse_source_location(caller)
333
- case caller
334
- when Exception
335
- trace = caller.backtrace #.reject{ |bt| bt =~ INTERNALS }
336
- caller = trace.first
337
- when Array
338
- caller = caller.first
339
- end
340
- caller =~ /(.+?):(\d+(?=:|\z))/ or return ""
341
- source_file, source_line = $1, $2.to_i
342
- return source_file, source_line
227
+ # Split `file:line`, with or without more after it, in two.
228
+ def split_location(location)
229
+ md = /\A(.+?):(\d+)(?::|\z)/.match(location.to_s)
230
+ md ? [relative_path(md[1]), md[2].to_i] : [nil, nil]
343
231
  end
344
232
 
345
- #
346
- def reset_output
347
- @_oldout = $stdout
348
- @_olderr = $stderr
349
-
350
- @_newout = StringIO.new
351
- @_newerr = StringIO.new
352
-
353
- $stdout = @_newout
354
- $stderr = @_newerr
233
+ # Add the line of source, and the lines around it, to a record.
234
+ def add_source(record, file, line)
235
+ lines = source(file) or return
236
+ record['source'] = lines[line - 1].to_s.strip
237
+ record['snippet'] = snippet(lines, line)
355
238
  end
356
239
 
357
- #
358
- def captured_output
359
- return unless (@_newout && @_newerr)
360
-
361
- stdout = @_newout.string.chomp("\n")
362
- stderr = @_newerr.string.chomp("\n")
363
-
364
- doc = {}
365
- doc['stdout'] = stdout unless stdout.empty?
366
- doc['stderr'] = stderr unless stderr.empty?
240
+ # The lines of a source file, or nil if it can't be read.
241
+ def source(file)
242
+ return @source_cache[file] if @source_cache.key?(file)
243
+ @source_cache[file] = File.file?(file) ? File.readlines(file) : nil
244
+ end
367
245
 
368
- $stdout = @_oldout
369
- $stderr = @_olderr
246
+ # The lines around a line of source, as a list of number => text.
247
+ def snippet(lines, line)
248
+ first = [line - SNIPPET_RADIUS, 1].max
249
+ last = [line + SNIPPET_RADIUS, lines.size].min
250
+ (first..last).map { |n| { n => lines[n - 1].chomp } }
251
+ end
370
252
 
371
- return doc
253
+ # A path under the working directory, written from `./`.
254
+ def relative_path(path)
255
+ directory = File.expand_path('.') + File::SEPARATOR
256
+ path.start_with?(directory) ? './' + path[directory.size..-1] : path
372
257
  end
373
258
 
374
259
  #
375
- def capture_io
376
- ostdout, ostderr = $stdout, $stderr
377
- cstdout, cstderr = StringIO.new, StringIO.new
378
- $stdout, $stderr = cstdout, cstderr
379
-
380
- yield
381
-
382
- return cstdout.string.chomp("\n"), cstderr.string.chomp("\n")
383
- ensure
384
- $stdout = ostdout
385
- $stderr = ostderr
260
+ def format_backtrace(backtrace, metadata)
261
+ configuration.backtrace_formatter.format_backtrace(backtrace, metadata)
386
262
  end
387
263
 
388
264
  #
389
- def relative_path_regex
390
- @relative_path_regex ||= /(\A|\s)#{File.expand_path('.')}(#{File::SEPARATOR}|\s|\Z)/
265
+ def configuration
266
+ RSpec.configuration
391
267
  end
392
268
 
393
- # Get relative path of file.
394
- #
395
- # line - current code line [String]
396
269
  #
397
- # Returns relative path to line. [String]
398
- def relative_path(line)
399
- line = line.sub(relative_path_regex, "\\1.\\2".freeze)
400
- line = line.sub(/\A([^:]+:\d+)$/, '\\1'.freeze)
401
- return nil if line == '-e:1'.freeze
402
- line
403
- rescue SecurityError
404
- nil
270
+ def capture_output
271
+ release_output
272
+ @real_stdout, @real_stderr = $stdout, $stderr
273
+ @stdout, @stderr = StringIO.new, StringIO.new
274
+ $stdout, $stderr = @stdout, @stderr
405
275
  end
406
276
 
407
- #
408
- def format_backtrace(*args)
409
- backtrace_formatter.format_backtrace(*args)
277
+ # Put back the real output streams, unless the example changed them
278
+ # itself.
279
+ def release_output
280
+ return unless @stdout
281
+ $stdout = @real_stdout if $stdout.equal?(@stdout)
282
+ $stderr = @real_stderr if $stderr.equal?(@stderr)
283
+ @stdout = @stderr = nil
410
284
  end
411
285
 
412
- #
413
- def backtrace_formatter
414
- RSpec.configuration.backtrace_formatter
286
+ # What the example printed, as part of a record.
287
+ def captured_output
288
+ return {} unless @stdout
289
+
290
+ stdout = @stdout.string.chomp("\n")
291
+ stderr = @stderr.string.chomp("\n")
292
+ release_output
293
+
294
+ record = {}
295
+ record['stdout'] = stdout unless stdout.empty?
296
+ record['stderr'] = stderr unless stderr.empty?
297
+ record
415
298
  end
416
299
 
417
300
  end
418
301
 
302
+ # Formats in TAP-Y, a stream of YAML documents.
303
+ #
304
+ # $ rspec -f RSpec::TapY | tapout
419
305
  #
420
306
  class TapY < TapBaseFormatter
421
- ::RSpec::Core::Formatters.register self,
422
- :start,
423
- :example_group_started,
424
- :example_group_finished,
425
- :example_started,
426
- :example_passed,
427
- :example_failed,
428
- :example_pending,
429
- :dump_summary,
430
- :seed,
431
- :message,
432
- #:stop,
433
- #:start_dump,
434
- #:dump_pending,
435
- #:dump_failures,
436
- :close
437
-
438
- def initialize(*args)
307
+ Core::Formatters.register self, *NOTIFICATIONS
308
+
309
+ def initialize(output)
439
310
  require 'yaml'
440
- super(*args)
311
+ super
441
312
  end
442
- def start(*args)
443
- output.puts super(*args).to_yaml
444
- end
445
- def example_group_started(*args)
446
- output.puts super(*args).to_yaml
447
- end
448
- def example_passed(*args)
449
- output.puts super(*args).to_yaml
450
- end
451
- def example_pending(*args)
452
- output.puts super(*args).to_yaml
453
- end
454
- def example_failed(*args)
455
- output.puts super(*args).to_yaml
313
+
314
+ private
315
+
316
+ def emit(record)
317
+ output.puts record.to_yaml
456
318
  end
457
- #def dump_summary(duration, example_count, failure_count, pending_count)
458
- # output.puts super(duration, example_count, failure_count, pending_count).to_yaml
459
- # output.puts "..."
460
- #end
461
- def dump_summary(*args)
462
- output.puts super(*args).to_yaml
463
- output.puts "..."
319
+
320
+ def finish
321
+ output.puts '...'
464
322
  end
465
323
  end
466
324
 
467
- #rspec -f RSpec::TapY spec/*.rb | tapout progress
325
+ # Formats in TAP-J, a JSON document on each line.
326
+ #
327
+ # $ rspec -f RSpec::TapJ | tapout
328
+ #
468
329
  class TapJ < TapBaseFormatter
469
- ::RSpec::Core::Formatters.register self,
470
- :start,
471
- :example_group_started,
472
- :example_group_finished,
473
- :example_started,
474
- :example_passed,
475
- :example_failed,
476
- :example_pending,
477
- :dump_summary,
478
- :seed,
479
- :message,
480
- #:stop,
481
- #:start_dump,
482
- #:dump_pending,
483
- #:dump_failures,
484
- :close
485
-
486
- def initialize(*args)
330
+ Core::Formatters.register self, *NOTIFICATIONS
331
+
332
+ def initialize(output)
487
333
  require 'json'
488
- super(*args)
489
- end
490
- def start(*args)
491
- output.puts super(*args).to_json
492
- end
493
- def example_group_started(*args)
494
- output.puts super(*args).to_json
334
+ super
495
335
  end
496
- def example_passed(*args)
497
- output.puts super(*args).to_json
498
- end
499
- def example_pending(*args)
500
- output.puts super(*args).to_json
501
- end
502
- def example_failed(*args)
503
- output.puts super(*args).to_json
504
- end
505
- def dump_summary(*args)
506
- output.puts super(*args).to_json
336
+
337
+ private
338
+
339
+ def emit(record)
340
+ output.puts JSON.generate(record)
507
341
  end
508
342
  end
509
343
 
510
- #end
511
- #end
512
344
  end