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