C0 code coverage information
Generated on Wed Oct 07 08:34:02 -0700 2009 with rcov 0.8.2.1
Code reported as executed by Ruby looks like this...
and this: this line is also marked as covered.
Lines considered as run by rcov, but not reported by Ruby, look like this,
and this: these lines were inferred by rcov (using simple heuristics).
Finally, here's a line marked as not executed.
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements. See the NOTICE file distributed with this
3 # work for additional information regarding copyright ownership. The ASF
4 # licenses this file to you under the Apache License, Version 2.0 (the
5 # "License"); you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations under
14 # the License.
15
16 require 'fileutils'
17 module Buildr #:nodoc:
18 module TestFramework
19
20 # A class used by buildr for jruby based frameworks, so that buildr can know
21 # which tests succeeded/failed.
22 class TestResult
23
24 class Error < ::Exception
25 attr_reader :message, :backtrace
26 def initialize(message, backtrace)
27 @message = message
28 @backtrace = backtrace
29 set_backtrace backtrace
30 end
31
32 def self.dump_yaml(file, e)
33 FileUtils.mkdir_p File.dirname(file)
34 File.open(file, 'w') { |f| f.puts(YAML.dump(Error.new(e.message, e.backtrace))) }
35 end
36
37 def self.guard(file)
38 begin
39 yield
40 rescue => e
41 dump_yaml(file, e)
42 end
43 end
44 end
45
46 attr_accessor :failed, :succeeded
47
48 def initialize
49 @failed, @succeeded = [], []
50 end
51
52 # An Rspec formatter used by buildr
53 class YamlFormatter
54 attr_reader :result
55
56 attr_accessor :example_group, :options, :where
57
58 def initialize(options, where)
59 @options = options
60 @where = where
61 @result = Hash.new
62 @result[:succeeded] = []
63 @result[:failed] = []
64 end
65
66 %w[ example_started
67 start_dump dump_failure dump_summary dump_pending ].each do |meth|
68 module_eval "def #{meth}(*args); end"
69 end
70
71 def example_group_started(example_group)
72 @example_group = example_group
73 end
74
75 def example_passed(example)
76 result.succeeded << example_group.location.gsub(/:\d+$/, '')
77 end
78
79 def example_pending(example, counter, failure)
80 result.succeeded << example_group.location.gsub(/:\d+$/, '')
81 end
82
83 def example_failed(example, counter, failure)
84 result.failed << example_group.location.gsub(/:\d+$/, '')
85 end
86
87 def start(example_count)
88 @result = TestResult.new
89 end
90
91 def close
92 result.succeeded = result.succeeded - result.failed
93 FileUtils.mkdir_p File.dirname(where)
94 File.open(where, 'w') { |f| f.puts YAML.dump(result) }
95 end
96 end # YamlFormatter
97
98 # Rspec formatter used for JtestR
99 # (JtestR provides its own version of rspec)
100 class JtestRYamlFormatter
101 attr_reader :result
102
103 attr_accessor :example_group, :options, :where
104
105 def initialize(options, where)
106 @options = options
107 @where = where
108 @result = Hash.new
109 @result[:succeeded] = []
110 @result[:failed] = []
111 end
112
113 %w[ example_started
114 start_dump dump_failure dump_summary dump_pending ].each do |meth|
115 module_eval "def #{meth}(*args); end"
116 end
117
118 def add_example_group(example_group)
119 @example_group = example_group
120 end
121
122 def example_passed(example)
123 end
124
125 def example_pending(example, counter, failure)
126 end
127
128 def example_failed(example, counter, failure)
129 if example_group.respond_to?(:spec_path)
130 result.failed << example_group.spec_path.gsub(/:\d+$/, '')
131 else
132 path = path_from_bt(failure.exception.backtrace)
133 result.failed << path if path
134 end
135 end
136
137 def start(example_count)
138 @result = TestResult.new
139 end
140
141 def path_from_bt(ary)
142 files = options.files
143 test = nil
144 ary.find do |bt|
145 bt = bt.split(':').first.strip
146 test = bt if files.include?(bt)
147 end
148 test
149 end
150
151 def close
152 files = options.files
153 result.succeeded = files - result.failed
154
155 FileUtils.mkdir_p File.dirname(where)
156 File.open(where, 'w') { |f| f.puts YAML.dump(result) }
157 end
158 end # JtestRYamlFormatter
159
160 # A JtestR ResultHandler
161 # Using this handler we can use RSpec formatters, like html/ci_reporter with JtestR
162 # Created for YamlFormatter
163 class RSpecResultHandler
164
165 # Workaround for http://jira.codehaus.org/browse/JTESTR-68
166 module TestNGResultHandlerMixin
167 def onTestSuccess(test_result)
168 @result_handler.succeed_single(test_result.name)
169 end
170 end
171
172 class BacktraceTweaker
173 attr_reader :ignore_patterns
174 def initialize
175 @ignore_patterns = ::Spec::Runner::QuietBacktraceTweaker::IGNORE_PATTERNS.dup
176 # ignore jruby/jtestr backtrace
177 ignore_patterns << /org\.jruby\.javasupport\.JavaMethod\./
178 ignore_patterns << /jtestr.*\.jar!/i << /runner\.rb/
179 end
180
181 def clean_up_double_slashes(line)
182 line.gsub!('//','/')
183 end
184
185 def tweak_backtrace(error)
186 return if error.backtrace.nil?
187 error.backtrace.collect! do |line|
188 clean_up_double_slashes(line)
189 ignore_patterns.each do |ignore|
190 if line =~ ignore
191 line = nil
192 break
193 end
194 end
195 line
196 end
197 error.backtrace.compact!
198 end
199 end
200
201 class << self
202 # an rspec reporter used to proxy events to rspec formatters
203 attr_reader :reporter
204
205 def init(argv = [], out = STDOUT, err = STDERR)
206 ::JtestR::TestNGResultHandler.module_eval { include TestNGResultHandlerMixin }
207 rspec_parser = ::Spec::Runner::OptionParser.new(err, out)
208 rspec_parser.order!(argv)
209 rspec_parser.options.backtrace_tweaker = BacktraceTweaker.new
210 @reporter = Spec::Runner::Reporter.new(rspec_parser.options)
211 end
212
213 def before
214 reporter.start(reporter.options.files.size)
215 end
216
217 def after
218 reporter.end
219 reporter.dump
220 end
221 end
222
223 module ExampleMethods
224 attr_accessor :name, :description, :__full_description
225 end
226
227 def reporter
228 self.class.reporter
229 end
230
231 attr_accessor :example_group, :current_example, :current_failure
232
233 def initialize(name, desc, *args)
234 self.example_group = ::Spec::Example::ExampleGroup.new(desc)
235 example_group.extend ExampleMethods
236 example_group.name = name.to_s
237 if example_group.name[/Spec/]
238 example_group.description = desc.to_s
239 else
240 example_group.description = name.to_s
241 end
242 reporter.add_example_group(example_group)
243 end
244
245 def starting
246 end
247
248 def ending
249 end
250
251 def add_fault(fault)
252 self.current_failure = fault
253 end
254
255 def add_pending(pending)
256 end
257
258 def starting_single(name = nil)
259 self.current_failure = nil
260 self.current_example = Object.new
261 current_example.extend ::Spec::Example::ExampleMethods
262 current_example.extend ExampleMethods
263 name = name.to_s
264 name[/\((pen?ding|error|failure|success)\)?$/]
265 name = $`
266 current_example.description = name
267 if example_group.name[/Spec/]
268 current_example.__full_description = "#{example_group.description} #{name}"
269 else
270 current_example.__full_description = "#{example_group.name}: #{name}"
271 end
272 reporter.example_started(current_example)
273 #puts "STARTED #{name} #{current_example.__full_description}"
274 end
275
276 def succeed_single(name = nil)
277 #puts "SUCC SINGLE #{name}"
278 reporter.example_finished(current_example, nil)
279 end
280
281 def fail_single(name = nil)
282 #puts "FAIL SINGLE #{name}"
283 reporter.example_finished(current_example, current_error)
284 end
285
286 def error_single(name = nil)
287 #puts "ERR SINGLE #{name}"
288 reporter.example_finished(current_example, current_error)
289 end
290
291 def pending_single(name = nil)
292 #puts "PEND SINGLE #{name}"
293 error = ::Spec::Example::ExamplePendingError.new(name)
294 reporter.example_finished(current_example, error)
295 end
296
297 private
298 def current_error(fault = current_failure)
299 case fault
300 when nil
301 nil
302 when Test::Unit::Failure
303 Error.new(fault.message, fault.location)
304 when Test::Unit::Error
305 if fault.exception.is_a?(NativeException)
306 exception = fault.exception.cause
307 bt = exception.stack_trace.to_a
308 else
309 exception = fault.exception
310 bt = exception.backtrace
311 end
312 Error.new(exception.message, bt)
313 when Expectations::Results::Error
314 fault.exception
315 when Spec::Runner::Reporter::Failure
316 ex = fault.exception
317 fault.example.instance_variable_get(:@_implementation).to_s =~ /@(.+:\d+)/
318 Error.new(ex.message, [$1.to_s] + ex.backtrace)
319 when Expectations::Results
320 file = fault.file
321 line = fault.line
322 Error.new(fault.message, ["#{fault.file}:#{fault.line}"])
323 else
324 if fault.respond_to?(:test_header)
325 fault.test_header[/\((.+)\)/]
326 test_cls, test_meth = $1.to_s, $`.to_s
327 exception = fault.exception
328 (class << exception; self; end).module_eval do
329 define_method(:backtrace) do
330 (["#{test_cls}:in `#{test_meth}'"] + stackTrace).map { |s| s.to_s }
331 end
332 end
333 exception
334 elsif fault.respond_to?(:method)
335 test_cls, test_meth = fault.method.test_class.name, fault.method.method_name
336 exception = fault.throwable
337 (class << exception; self; end).module_eval do
338 define_method(:backtrace) do
339 (["#{test_cls}:in `#{test_meth}'"] + stackTrace).map { |s| s.to_s }
340 end
341 end
342 exception
343 else
344 raise "Cannot handle fault #{fault.class}: #{fault.inspect}"
345 end
346 end
347 end
348
349 end # RSpecResultHandler
350
351 end # TestResult
352 end
353 end
Generated using the rcov code coverage analysis tool for Ruby
version 0.8.2.1.