C0 code coverage information
Generated on Wed Oct 07 08:34:01 -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
17 require 'buildr/java/tests'
18 require 'buildr/java/test_result'
19
20
21 module Buildr
22
23 # Mixin for test frameworks using src/spec/{lang}
24 class TestFramework::JavaBDD < TestFramework::Java #:nodoc:
25
26 class << self
27 attr_reader :lang, :bdd_dir
28 end
29 attr_accessor :lang, :bdd_dir
30
31 def initialize(task, options)
32 self.bdd_dir = self.class.bdd_dir
33 project = task.project
34 project.task('test:compile').tap do |comp|
35 comp.send :associate_with, project, bdd_dir
36 self.lang = comp.language || self.class.lang
37 end
38 project.task('test:resources').tap do |res|
39 res.send :associate_with, project, bdd_dir
40 res.filter.clear
41 project.path_to(:source, bdd_dir, :resources).tap { |dir| res.from dir if File.exist?(dir) }
42 end
43 super
44 end
45
46 end
47
48 module TestFramework::JRubyBased
49 extend self
50
51 VERSION = '1.3.1'
52
53 class << self
54 def version
55 Buildr.settings.build['jruby'] || VERSION
56 end
57
58 def jruby_artifact
59 "org.jruby:jruby-complete:jar:#{version}"
60 end
61
62 def dependencies
63 [jruby_artifact]
64 end
65
66 def included(mod)
67 mod.extend ClassMethods
68 super
69 end
70 end
71
72 module ClassMethods
73 def dependencies
74 deps = super
75 unless RUBY_PLATFORM[/java/] && TestFramework::JRubyBased.jruby_installed?
76 deps |= TestFramework::JRubyBased.dependencies
77 end
78 deps
79 end
80 end
81
82 def run(tests, dependencies)
83 maybe_install_jruby
84 dependencies |= [task.compile.target.to_s]
85
86 spec_dir = task.project.path_to(:source, :spec, :ruby)
87 report_dir = task.report_to.to_s
88 rm_rf report_dir
89 mkdir_p report_dir
90 ENV['CI_REPORTS'] = report_dir
91
92 runner = runner_config
93 runner.content = runner_content(binding)
94
95 Buildr.write(runner.file, runner.content)
96 rm_f runner.result
97
98 if RUBY_PLATFORM[/java/] && !options.fork
99 runtime = new_runtime
100 runtime.getObject.java.lang.System.getProperties().putAll(options[:properties] || {})
101 runtime.getLoadService.require runner.file
102 else
103 cmd_options = task.options.only(:properties, :java_args)
104 cmd_options.update(:classpath => dependencies, :project => task.project)
105 jruby runner.file, tests, cmd_options
106 end
107
108 result = YAML.load(File.read(runner.result))
109 if Exception === result
110 raise [result.message, result.backtrace].flatten.join("\n")
111 end
112 tests - result.failed
113 end
114
115 def jruby_home
116 @jruby_home ||= RUBY_PLATFORM =~ /java/ ? Config::CONFIG['prefix'] :
117 ( ENV['JRUBY_HOME'] || File.expand_path('~/.jruby') )
118 end
119
120 def jruby_installed?
121 !Dir.glob(File.join(jruby_home, 'lib', 'jruby*.jar')).empty?
122 end
123
124 protected
125 def maybe_install_jruby
126 unless jruby_installed?
127 jruby_artifact = Buildr.artifact(TestFramework::JRubyBased.jruby_artifact)
128 msg = "JRUBY_HOME is not correctly set or points to an invalid JRuby installation: #{jruby_home}"
129 say msg
130 say ''
131 say "You need to install JRuby version #{jruby_artifact.version} using your system package manager."
132 say 'Or you can just execute the following command: '
133 say ''
134 say " java -jar #{jruby_artifact} -S extract '#{jruby_home}'"
135 say ''
136 if agree('Do you want me to execute it for you? [y/N]', false)
137 jruby_artifact.invoke
138 Java::Commands.java('-jar', jruby_artifact.to_s, '-S', 'extract', jruby_home)
139 end
140
141 fail msg unless jruby_installed?
142 end
143 end
144
145 def jruby(*args)
146 java_args = ['org.jruby.Main', *args]
147 java_args << {} unless Hash === args.last
148 cmd_options = java_args.last
149 project = cmd_options.delete(:project)
150 cmd_options[:classpath] ||= []
151 Dir.glob(File.join(jruby_home, 'lib', '*.jar')) { |jar| cmd_options[:classpath] << jar }
152 cmd_options[:java_args] ||= []
153 cmd_options[:java_args] << '-Xmx512m' unless cmd_options[:java_args].detect {|a| a =~ /^-Xmx/}
154 cmd_options[:properties] ||= {}
155 cmd_options[:properties]['jruby.home'] = jruby_home
156 Java::Commands.java(*java_args)
157 end
158
159 def new_runtime(cfg = {})
160 config = Java.org.jruby.RubyInstanceConfig.new
161 cfg.each_pair do |name, value|
162 config.send("#{name}=", value)
163 end
164 yield config if block_given?
165 Java.org.jruby.Ruby.newInstance config
166 end
167
168 def jruby_gem
169 %{
170 require 'jruby'
171 def JRuby.gem(name, version = '>0', *args)
172 require 'rbconfig'
173 jruby_home = Config::CONFIG['prefix']
174 expected_version = '#{TestFramework::JRubyBased.version}'
175 unless JRUBY_VERSION >= expected_version
176 fail "Expected JRuby version \#{expected_version} installed at \#{jruby_home} but got \#{JRUBY_VERSION}"
177 end
178 require 'rubygems'
179 begin
180 Kernel.send :gem, name, version
181 rescue LoadError, Gem::LoadError => e
182 require 'rubygems/gem_runner'
183 Gem.manage_gems
184 args = ['install', name, '--version', version] + args
185 Gem::GemRunner.new.run(args)
186 Kernel.send :gem, name, version
187 end
188 end
189 }
190 end
191
192 def runner_config(runner = OpenStruct.new)
193 [:requires, :gems, :output, :format].each do |key|
194 runner.send("#{key}=", options[key])
195 end
196 runner.html_report ||= File.join(task.report_to.to_s, 'report.html')
197 runner.result ||= task.project.path_to(:target, :spec, 'result.yaml')
198 runner.file ||= task.project.path_to(:target, :spec, 'runner.rb')
199 runner.requires ||= []
200 runner.requires.unshift File.join(File.dirname(__FILE__), 'test_result')
201 runner.gems ||= {}
202 runner.rspec ||= ['--format', 'progress', '--format', "html:#{runner.html_report}"]
203 runner.format.each { |format| runner.rspec << '--format' << format } if runner.format
204 runner.rspec.push '--format', "#{runner_formatter}:#{runner.result}"
205 runner
206 end
207
208 def runner_formatter
209 "Buildr::TestFramework::TestResult::YamlFormatter"
210 end
211
212 end
213
214 # <a href="http://rspec.info">RSpec</a> is the defacto BDD framework for ruby.
215 # To test your project with RSpec use:
216 # test.using :rspec
217 #
218 #
219 # Support the following options:
220 # * :gems -- A hash of gems to install before running the tests.
221 # The keys of this hash are the gem name, the value must be the required version.
222 # * :requires -- A list of ruby files to require before running the specs
223 # Mainly used if an rspec format needs to require some file.
224 # * :format -- A list of valid Rspec --format option values. (defaults to 'progress')
225 # * :output -- File path to output dump. @false@ to supress output
226 # * :fork -- Create a new JavaVM to run the tests on
227 # * :properties -- Hash of properties passed to the test suite.
228 # * :java_args -- Arguments passed to the JVM.
229 class RSpec < TestFramework::JavaBDD
230 @lang = :ruby
231 @bdd_dir = :spec
232
233 include TestFramework::JRubyBased
234
235 TESTS_PATTERN = [ /_spec.rb$/ ]
236 OPTIONS = [:properties, :java_args]
237
238 def self.applies_to?(project) #:nodoc:
239 !Dir[project.path_to(:source, bdd_dir, lang, '**/*_spec.rb')].empty?
240 end
241
242 def tests(dependencies) #:nodoc:
243 Dir[task.project.path_to(:source, bdd_dir, lang, '**/*_spec.rb')].select do |name|
244 selector = ENV['SPEC']
245 selector.nil? || Regexp.new(selector) === name
246 end
247 end
248
249 def runner_config
250 runner = super
251 runner.gems.update 'rspec' => '>0'
252 runner.requires.unshift 'spec'
253 runner
254 end
255
256 def runner_content(binding)
257 runner_erb = %q{
258 <%= jruby_gem %>
259 <%= dependencies.inspect %>.each { |dep| $CLASSPATH << dep }
260 <%= runner.gems.inspect %>.each { |ary| JRuby.gem(*ary.flatten) }
261 <%= runner.requires.inspect %>.each { |rb| Kernel.require rb }
262 <% if runner.output == false %>
263 output = StringIO.new
264 <% elsif runner.output.kind_of?(String) %>
265 output = File.open(<%= result.output.inspect %>, 'w')
266 <% else %>
267 output = STDOUT
268 <% end %>
269 parser = ::Spec::Runner::OptionParser.new(output, output)
270 argv = <%= runner.rspec.inspect %> || []
271 argv.push *<%= tests.inspect %>
272 parser.order!(argv)
273 $rspec_options = parser.options
274
275 Buildr::TestFramework::TestResult::Error.guard('<%= runner.result %>') do
276 ::Spec::Runner::CommandLine.run($rspec_options)
277 end
278 exit 0 # let buildr figure the result from the yaml file
279 }
280 Filter::Mapper.new(:erb, binding).transform(runner_erb)
281 end
282
283 end
284
285 # <a href="http://jtestr.codehaus.org/">JtestR</a> is a framework for BDD and TDD using JRuby and ruby tools.
286 # To test your project with JtestR use:
287 # test.using :jtestr
288 #
289 #
290 # Support the following options:
291 # * :config -- path to JtestR config file. defaults to @spec/ruby/jtestr_config.rb@
292 # * :gems -- A hash of gems to install before running the tests.
293 # The keys of this hash are the gem name, the value must be the required version.
294 # * :requires -- A list of ruby files to require before running the specs
295 # Mainly used if an rspec format needs to require some file.
296 # * :format -- A list of valid Rspec --format option values. (defaults to 'progress')
297 # * :output -- File path to output dump. @false@ to supress output
298 # * :fork -- Create a new JavaVM to run the tests on
299 # * :properties -- Hash of properties passed to the test suite.
300 # * :java_args -- Arguments passed to the JVM.
301 class JtestR < TestFramework::JavaBDD
302 @lang = :ruby
303 @bdd_dir = :spec
304
305 include TestFramework::JRubyBased
306
307 VERSION = '0.3.1'
308
309 # pattern for rspec stories
310 STORY_PATTERN = /_(steps|story)\.rb$/
311 # pattern for test_unit files
312 TESTUNIT_PATTERN = /(_test|Test)\.rb$|(tc|ts)[^\\\/]+\.rb$/
313 # pattern for test files using http://expectations.rubyforge.org/
314 EXPECT_PATTERN = /_expect\.rb$/
315
316 TESTS_PATTERN = [STORY_PATTERN, TESTUNIT_PATTERN, EXPECT_PATTERN] + RSpec::TESTS_PATTERN
317
318 class << self
319
320 def version
321 Buildr.settings.build['jtestr'] || VERSION
322 end
323
324 def dependencies
325 @dependencies ||= Array(super) + ["org.jtestr:jtestr:jar:#{version}"] +
326 JUnit.dependencies + TestNG.dependencies
327 end
328
329 def applies_to?(project) #:nodoc:
330 File.exist?(project.path_to(:source, bdd_dir, lang, 'jtestr_config.rb')) ||
331 Dir[project.path_to(:source, bdd_dir, lang, '**/*.rb')].any? { |f| TESTS_PATTERN.any? { |r| r === f } } ||
332 JUnit.applies_to?(project) || TestNG.applies_to?(project)
333 end
334
335 private
336 def const_missing(const)
337 return super unless const == :REQUIRES # TODO: remove in 1.5
338 Buildr.application.deprecated 'Please use JtestR.dependencies/.version instead of JtestR::REQUIRES/VERSION'
339 dependencies
340 end
341
342 end
343
344 def initialize(task, options) #:nodoc:
345 super
346 [:test, :spec].each do |usage|
347 java_tests = task.project.path_to(:source, usage, :java)
348 task.compile.from java_tests if File.directory?(java_tests)
349 resources = task.project.path_to(:source, usage, :resources)
350 task.resources.from resources if File.directory?(resources)
351 end
352 end
353
354 def user_config
355 options[:config] || task.project.path_to(:source, bdd_dir, lang, 'jtestr_config.rb')
356 end
357
358 def tests(dependencies) #:nodoc:
359 dependencies |= [task.compile.target.to_s]
360 types = { :story => STORY_PATTERN, :rspec => RSpec::TESTS_PATTERN,
361 :testunit => TESTUNIT_PATTERN, :expect => EXPECT_PATTERN }
362 tests = types.keys.inject({}) { |h, k| h[k] = []; h }
363 tests[:junit] = JUnit.new(task, {}).tests(dependencies)
364 tests[:testng] = TestNG.new(task, {}).tests(dependencies)
365 Dir[task.project.path_to(:source, bdd_dir, lang, '**/*.rb')].each do |rb|
366 type = types.find { |k, v| Array(v).any? { |r| r === rb } }
367 tests[type.first] << rb if type
368 end
369 @jtestr_tests = tests
370 tests.values.flatten
371 end
372
373 def runner_config
374 runner = super
375 # JtestR 0.3.1 comes with rspec 1.1.4 (and any other jtestr dependency) included,
376 # so the rspec version used depends on the jtestr jar.
377 runner.requires.unshift 'jtestr'
378 runner
379 end
380
381 def runner_content(binding)
382 runner_erb = File.join(File.dirname(__FILE__), 'jtestr_runner.rb.erb')
383 Filter::Mapper.new(:erb, binding).transform(File.read(runner_erb), runner_erb)
384 end
385
386 def runner_formatter
387 'Buildr::TestFramework::TestResult::JtestRYamlFormatter'
388 end
389
390 end
391
392
393 # JBehave is a Java BDD framework. To use in your project:
394 # test.using :jbehave
395 #
396 # This framework will search in your project for:
397 # src/spec/java/**/*Behaviour.java
398 #
399 # JMock libraries are included on runtime.
400 #
401 # Support the following options:
402 # * :properties -- Hash of properties to the test suite
403 # * :java_args -- Arguments passed to the JVM
404 class JBehave < TestFramework::JavaBDD
405 @lang = :java
406 @bdd_dir = :spec
407
408 VERSION = '1.0.1'
409 TESTS_PATTERN = [ /Behaviou?r$/ ] #:nodoc:
410
411 class << self
412 def version
413 Buildr.settings.build['jbehave'] || VERSION
414 end
415
416 def dependencies
417 @dependencies ||= ["org.jbehave:jbehave:jar:#{version}", 'cglib:cglib-full:jar:2.0.2'] +
418 JMock.dependencies + JUnit.dependencies
419 end
420
421 def applies_to?(project) #:nodoc:
422 %w{
423 **/*Behaviour.java **/*Behavior.java
424 }.any? { |glob| !Dir[project.path_to(:source, bdd_dir, lang, glob)].empty? }
425 end
426
427 private
428 def const_missing(const)
429 return super unless const == :REQUIRES # TODO: remove in 1.5
430 Buildr.application.deprecated 'Please use JBehave.dependencies/.version instead of JBehave::REQUIRES/VERSION'
431 dependencies
432 end
433 end
434
435 def tests(dependencies) #:nodoc:
436 filter_classes(dependencies, :class_names => TESTS_PATTERN,
437 :interfaces => %w{ org.jbehave.core.behaviour.Behaviours })
438 end
439
440 def run(tests, dependencies) #:nodoc:
441 cmd_args = ['org.jbehave.core.BehaviourRunner']
442 cmd_options = { :properties=>options[:properties], :java_args=>options[:java_args], :classpath=>dependencies }
443 tests.inject([]) do |passed, test|
444 begin
445 Java::Commands.java cmd_args, test, cmd_options
446 passed << test
447 rescue
448 passed
449 end
450 end
451 end
452
453 end
454
455 end
456
457 Buildr::TestFramework << Buildr::RSpec
458 Buildr::TestFramework << Buildr::JtestR
459 Buildr::TestFramework << Buildr::JBehave
Generated using the rcov code coverage analysis tool for Ruby
version 0.8.2.1.