rspec-ontap 0.3.0 → 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,510 +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
- line = line.to_i
90
-
91
- doc = {
92
- 'type' => 'test',
93
- 'subtype' => 'it',
94
- 'status' => 'pass',
95
- #'setup': foo instance
96
- 'label' => "#{example.description}",
97
- #'expected' => 2
98
- #'returned' => 2
99
- 'file' => file,
100
- 'line' => line,
101
- 'source' => source(file)[line-1].strip,
102
- 'snippet' => code_snippet(file, line),
103
- #'coverage' => {
104
- # file: lib/foo.rb
105
- # line: 11..13
106
- # code: Foo#*
107
- #}
108
- 'time' => Time.now - @start_time
109
- }
110
-
111
- doc.update(captured_output)
112
-
113
- return doc
87
+ emit test_record(notification.example, 'pass')
114
88
  end
115
89
 
90
+ # Invoked for an example that was skipped or is pending.
116
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`.
117
94
  def example_pending(notification)
118
- #super(notification)
119
-
120
95
  example = notification.example
96
+ reason = example.execution_result.pending_message.to_s
121
97
 
122
- file, line = example.location.split(':')
123
- file = self.class.relative_path(file)
124
- line = line.to_i
125
-
126
- doc = {
127
- 'type' => 'test',
128
- 'subtype' => 'it',
129
- 'status' => 'todo',
130
- #'setup': foo instance
131
- 'label' => "#{example.description}",
132
- 'file' => file,
133
- 'line' => line,
134
- 'source' => source(file)[line-1].strip,
135
- 'snippet' => code_snippet(file, line),
136
- #'coverage' => {
137
- # file: lib/foo.rb
138
- # line: 11..13
139
- # code: Foo#*
140
- #}
141
- 'time' => Time.now - @start_time
142
- }
143
-
144
- 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
145
104
 
146
- return doc
105
+ record = test_record(example, status)
106
+ record['exception'] = { 'message' => reason } unless reason.empty?
107
+ emit record
147
108
  end
148
109
 
149
110
  #
150
111
  def example_failed(notification)
151
- #super(notification)
152
-
153
- example = notification.example
154
-
155
- file, line = example.location.split(':')
156
-
157
- file = relative_path(file)
158
- line = line.to_i
159
-
160
- if RSpec::Expectations::ExpectationNotMetError === example.exception
161
- status = 'fail'
162
- if md = /expected:\s*(.*?)\n\s*got:\s*(.*?)\s+/.match(example.exception.to_s)
163
- expected, returned = md[1], md[2]
164
- else
165
- expected, returned = nil, nil
166
- end
167
- else
168
- status = 'error'
169
- end
170
-
171
- backtrace = format_backtrace(example.exception.backtrace, example.metadata)
172
-
173
- efile, eline = parse_source_location(backtrace)
174
-
175
- doc = {
176
- 'type' => 'test',
177
- 'subtype' => 'it',
178
- 'status' => status,
179
- 'label' => "#{example.description}",
180
- #'setup' => "foo instance",
181
- 'file' => file,
182
- 'line' => line,
183
- 'source' => source(file)[line-1].strip,
184
- 'snippet' => code_snippet(file, line),
185
- #'coverage' =>
186
- #{
187
- # 'file' => lib/foo.rb
188
- # 'line' => 11..13
189
- # 'code' => Foo#*
190
- #}
191
- }
112
+ example = notification.example
113
+ exception = example.exception
114
+ record = test_record(example, failure?(exception) ? 'fail' : 'error')
192
115
 
193
- if expected or returned
194
- doc.update(
195
- 'expected' => expected,
196
- 'returned' => returned,
197
- )
116
+ if md = /expected:\s*(.*?)\n\s*got:\s*(.*?)\s+/.match(exception.to_s)
117
+ record['expected'] = md[1]
118
+ record['returned'] = md[2]
198
119
  end
199
120
 
200
- doc.update(
201
- 'exception' => {
202
- 'message' => example.exception.to_s.strip,
203
- 'class' => example.exception.class.name,
204
- 'file' => efile,
205
- 'line' => eline,
206
- 'source' => source(efile)[eline-1].strip,
207
- 'snippet' => code_snippet(efile, eline),
208
- 'backtrace' => backtrace
209
- },
210
- 'time' => Time.now - @start_time
211
- )
212
-
213
- doc.update(captured_output)
214
-
215
- return doc
121
+ record['exception'] = exception_record(example, exception)
122
+ emit record
216
123
  end
217
124
 
218
- # This method is invoked after the dumping of examples and failures.
219
- def dump_summary(summary_notification)
220
- #super(summary_notification)
221
-
222
- duration = summary_notification.duration
223
- example_count = summary_notification.examples.size
224
- failure_count = summary_notification.failed_examples.size
225
- pending_count = summary_notification.pending_examples.size
226
-
227
- failed_examples = summary_notification.failed_examples
228
-
229
- error_count = 0
230
-
231
- failed_examples.each do |e|
232
- if RSpec::Expectations::ExpectationNotMetError === e.exception
233
- #failure_count += 1
234
- else
235
- failure_count -= 1
236
- error_count += 1
237
- end
238
- 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
239
135
 
240
- 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
241
143
 
242
- doc = {
243
- 'type' => 'final',
244
- 'time' => duration,
144
+ emit(
145
+ 'type' => 'final',
146
+ 'time' => summary.duration,
245
147
  'counts' => {
246
- 'total' => example_count,
247
- 'pass' => passing_count,
248
- 'fail' => failure_count,
249
- 'error' => error_count,
250
- 'omit' => 0,
251
- '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
252
154
  }
253
- }
254
- return doc
255
- end
256
-
257
- # This gets invoked after the summary if option is set to do so.
258
- #def dump_pending
259
- #end
260
-
261
- def seed(notification)
262
- @seed = notification.seed
155
+ )
156
+ finish
263
157
  end
264
158
 
265
- # Add any messages as notes.
266
- def message(message_notification)
267
- doc = {
268
- 'type' => 'note',
269
- 'text' => message_notification.message
270
- }
271
- return doc
159
+ # Invoked last. Makes sure the real output streams are back.
160
+ def close(notification)
161
+ release_output
162
+ super
272
163
  end
273
164
 
274
- #
275
- # NOTE: None of the following are being used. If ever added, be sure
276
- # to activate in register calls below.
277
- #
165
+ private
278
166
 
279
- # (not used)
280
- def stop(examples_notification)
281
- super(examples_notification)
167
+ # Write a record to the stream. Subclasses define this.
168
+ def emit(record)
169
+ raise NotImplementedError
282
170
  end
283
171
 
284
- # (not used)
285
- def start_dump(null_notification)
172
+ # Write whatever ends the stream.
173
+ def finish
286
174
  end
287
175
 
288
- # (not used)
289
- def dump_pending(examples_notification)
290
- 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
+ }
291
185
 
292
- # (not used)
293
- def dump_failures(examples_notification)
294
- 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
295
192
 
296
- # (not used)
297
- def close(null_notification)
298
- # there is a super method for this
299
- super(null_notification)
193
+ record.update(captured_output)
194
+ record['time'] = Time.now - @start_time
195
+ record
300
196
  end
301
197
 
302
- private
198
+ #
199
+ def exception_record(example, exception)
200
+ backtrace = format_backtrace(exception.backtrace || [], example.metadata)
201
+ file, line = split_location(backtrace.first)
303
202
 
304
- # Returns a String of source code.
305
- def code_snippet(file, line)
306
- s = []
307
- if File.file?(file)
308
- source = source(file)
309
- radius = 2 # TODO: make customizable (number of surrounding lines to show)
310
- region = [line - radius, 1].max ..
311
- [line + radius, source.length].min
312
-
313
- s = region.map do |n|
314
- {n => source[n-1].chomp}
315
- 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)
316
211
  end
317
- return s
212
+ record['backtrace'] = backtrace
213
+ record
318
214
  end
319
215
 
320
- # Cache source file text. This is only used if the TAP-Y stream
321
- # doesn not provide a snippet and the test file is locatable.
322
- def source(file)
323
- @_source_cache ||= {}
324
- @_source_cache[file] ||= (
325
- File.readlines(file)
326
- )
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
327
225
  end
328
226
 
329
- # Parse source location from caller, caller[0] or an Exception object.
330
- def parse_source_location(caller)
331
- case caller
332
- when Exception
333
- trace = caller.backtrace #.reject{ |bt| bt =~ INTERNALS }
334
- caller = trace.first
335
- when Array
336
- caller = caller.first
337
- end
338
- caller =~ /(.+?):(\d+(?=:|\z))/ or return ""
339
- source_file, source_line = $1, $2.to_i
340
- 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]
341
231
  end
342
232
 
343
- #
344
- def reset_output
345
- @_oldout = $stdout
346
- @_olderr = $stderr
347
-
348
- @_newout = StringIO.new
349
- @_newerr = StringIO.new
350
-
351
- $stdout = @_newout
352
- $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)
353
238
  end
354
239
 
355
- #
356
- def captured_output
357
- return unless (@_newout && @_newerr)
358
-
359
- stdout = @_newout.string.chomp("\n")
360
- stderr = @_newerr.string.chomp("\n")
361
-
362
- doc = {}
363
- doc['stdout'] = stdout unless stdout.empty?
364
- 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
365
245
 
366
- $stdout = @_oldout
367
- $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
368
252
 
369
- 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
370
257
  end
371
258
 
372
259
  #
373
- def capture_io
374
- ostdout, ostderr = $stdout, $stderr
375
- cstdout, cstderr = StringIO.new, StringIO.new
376
- $stdout, $stderr = cstdout, cstderr
377
-
378
- yield
379
-
380
- return cstdout.string.chomp("\n"), cstderr.string.chomp("\n")
381
- ensure
382
- $stdout = ostdout
383
- $stderr = ostderr
260
+ def format_backtrace(backtrace, metadata)
261
+ configuration.backtrace_formatter.format_backtrace(backtrace, metadata)
384
262
  end
385
263
 
386
264
  #
387
- def relative_path_regex
388
- @relative_path_regex ||= /(\A|\s)#{File.expand_path('.')}(#{File::SEPARATOR}|\s|\Z)/
265
+ def configuration
266
+ RSpec.configuration
389
267
  end
390
268
 
391
- # Get relative path of file.
392
- #
393
- # line - current code line [String]
394
269
  #
395
- # Returns relative path to line. [String]
396
- def relative_path(line)
397
- line = line.sub(relative_path_regex, "\\1.\\2".freeze)
398
- line = line.sub(/\A([^:]+:\d+)$/, '\\1'.freeze)
399
- return nil if line == '-e:1'.freeze
400
- line
401
- rescue SecurityError
402
- 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
403
275
  end
404
276
 
405
- #
406
- def format_backtrace(*args)
407
- 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
408
284
  end
409
285
 
410
- #
411
- def backtrace_formatter
412
- 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
413
298
  end
414
299
 
415
300
  end
416
301
 
302
+ # Formats in TAP-Y, a stream of YAML documents.
303
+ #
304
+ # $ rspec -f RSpec::TapY | tapout
417
305
  #
418
306
  class TapY < TapBaseFormatter
419
- ::RSpec::Core::Formatters.register self,
420
- :start,
421
- :example_group_started,
422
- :example_group_finished,
423
- :example_started,
424
- :example_passed,
425
- :example_failed,
426
- :example_pending,
427
- :dump_summary,
428
- :seed,
429
- :message,
430
- #:stop,
431
- #:start_dump,
432
- #:dump_pending,
433
- #:dump_failures,
434
- :close
435
-
436
- def initialize(*args)
307
+ Core::Formatters.register self, *NOTIFICATIONS
308
+
309
+ def initialize(output)
437
310
  require 'yaml'
438
- super(*args)
311
+ super
439
312
  end
440
- def start(*args)
441
- output.puts super(*args).to_yaml
442
- end
443
- def example_group_started(*args)
444
- output.puts super(*args).to_yaml
445
- end
446
- def example_passed(*args)
447
- output.puts super(*args).to_yaml
448
- end
449
- def example_pending(*args)
450
- output.puts super(*args).to_yaml
451
- end
452
- def example_failed(*args)
453
- output.puts super(*args).to_yaml
313
+
314
+ private
315
+
316
+ def emit(record)
317
+ output.puts record.to_yaml
454
318
  end
455
- #def dump_summary(duration, example_count, failure_count, pending_count)
456
- # output.puts super(duration, example_count, failure_count, pending_count).to_yaml
457
- # output.puts "..."
458
- #end
459
- def dump_summary(*args)
460
- output.puts super(*args).to_yaml
461
- output.puts "..."
319
+
320
+ def finish
321
+ output.puts '...'
462
322
  end
463
323
  end
464
324
 
465
- #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
+ #
466
329
  class TapJ < TapBaseFormatter
467
- ::RSpec::Core::Formatters.register self,
468
- :start,
469
- :example_group_started,
470
- :example_group_finished,
471
- :example_started,
472
- :example_passed,
473
- :example_failed,
474
- :example_pending,
475
- :dump_summary,
476
- :seed,
477
- :message,
478
- #:stop,
479
- #:start_dump,
480
- #:dump_pending,
481
- #:dump_failures,
482
- :close
483
-
484
- def initialize(*args)
330
+ Core::Formatters.register self, *NOTIFICATIONS
331
+
332
+ def initialize(output)
485
333
  require 'json'
486
- super(*args)
487
- end
488
- def start(*args)
489
- output.puts super(*args).to_json
490
- end
491
- def example_group_started(*args)
492
- output.puts super(*args).to_json
334
+ super
493
335
  end
494
- def example_passed(*args)
495
- output.puts super(*args).to_json
496
- end
497
- def example_pending(*args)
498
- output.puts super(*args).to_json
499
- end
500
- def example_failed(*args)
501
- output.puts super(*args).to_json
502
- end
503
- def dump_summary(*args)
504
- output.puts super(*args).to_json
336
+
337
+ private
338
+
339
+ def emit(record)
340
+ output.puts JSON.generate(record)
505
341
  end
506
342
  end
507
343
 
508
- #end
509
- #end
510
344
  end