buildr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vbo...@apache.org
Subject svn commit: r698286 - in /incubator/buildr/trunk/lib/buildr/java: bdd.rb jtestr_result_handler.rb jtestr_runner.rb.erb
Date Tue, 23 Sep 2008 19:12:13 GMT
Author: vborja
Date: Tue Sep 23 12:12:12 2008
New Revision: 698286

URL: http://svn.apache.org/viewvc?rev=698286&view=rev
Log:
Support of testing with JtestR.

- JtestR configuration using Buildr classpath.
- TODO: allow to set other JtestR options with test.using
- TODO: support jtestr server/client mode.
* Currently only working when running Buildr with JRuby

Added:
    incubator/buildr/trunk/lib/buildr/java/jtestr_result_handler.rb
    incubator/buildr/trunk/lib/buildr/java/jtestr_runner.rb.erb
Modified:
    incubator/buildr/trunk/lib/buildr/java/bdd.rb

Modified: incubator/buildr/trunk/lib/buildr/java/bdd.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/bdd.rb?rev=698286&r1=698285&r2=698286&view=diff
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/bdd.rb (original)
+++ incubator/buildr/trunk/lib/buildr/java/bdd.rb Tue Sep 23 12:12:12 2008
@@ -16,7 +16,6 @@
 
 require 'buildr/java/tests'
 
-
 module Buildr
 
   # Mixin for test frameworks using src/spec/{lang}
@@ -42,7 +41,33 @@
       super
     end
   end
-  
+
+  module TestFramework::JRubyBased
+    def jruby_home
+      @jruby_home ||= RUBY_PLATFORM =~ /java/ ? Config::CONFIG['prefix'] : ENV['JRUBY_HOME']
+    end
+
+    def jruby(*args)
+      java_args = ["org.jruby.Main", *args]
+      java_args << {} unless Hash === args.last
+      cmd_options = java_args.last
+      project = cmd_options.delete(:project)
+      cmd_options[:java_args] ||= []
+      cmd_options[:java_args] << "-Xmx512m" unless cmd_options[:java_args].detect {|a|
a =~ /^-Xmx/}
+      cmd_options[:properties] ||= {}
+      cmd_options[:properties]['jruby.home'] = jruby_home
+      Java::Commands.java(*java_args)
+    end
+
+    def new_runtime(cfg = {})
+      config = org.jruby.RubyInstanceConfig.new
+      cfg.each_pair do |name, value|
+        config.send("#{name}=", value)
+      end
+      yield config if block_given?
+      org.jruby.Ruby.newInstance config
+    end
+  end
   
   class RSpec < TestFramework::JavaBDD
     @lang = :ruby
@@ -98,9 +123,91 @@
 
   end
 
+  # <a href="http://jtestr.codehaus.org/">JtestR</a> is a framework for BDD and
TDD using JRuby and ruby tools.
+  # To test your project with JtestR use:
+  #   test.using :jtestr
+  #
+  #
+  # Support the following options:
+  # * :output -- JtestR utput file. @false@ to supress output
   class JtestR < TestFramework::JavaBDD
     @lang = :ruby
     @bdd_dir = :spec    
+
+    include TestFramework::JRubyBased
+
+    VERSION = '0.3.1' unless const_defined?('VERSION')
+    JTESTR_ARTIFACT = "org.jtestr:jtestr:jar:#{VERSION}"
+    
+    REQUIRES = [JTESTR_ARTIFACT] + JUnit::REQUIRES + TestNG::REQUIRES
+
+    # pattern for rspec stories
+    STORY_PATTERN    = /_(steps|story)\.rb$/
+    # pattern for test_unit files
+    TESTUNIT_PATTERN = /(_test|Test)\.rb$|(tc|ts)[^\\\/]+\.rb$/
+    # pattern for test files using http://expectations.rubyforge.org/
+    EXPECT_PATTERN   = /_expect\.rb$/
+
+    TESTS_PATTERN = [STORY_PATTERN, TESTUNIT_PATTERN, EXPECT_PATTERN] + RSpec::TESTS_PATTERN
+    
+    def self.applies_to?(project) #:nodoc:
+      File.exist?(project.path_to(:source, bdd_dir, lang, 'jtestr_config.rb')) ||
+        Dir[project.path_to(:source, bdd_dir, lang, '**/*.rb')].any? { |f| TESTS_PATTERN.any?
{ |r| r === f } } ||
+        JUnit.applies_to?(project) || TestNG.applies_to?(project)
+    end
+
+    def initialize(task, options) #:nodoc:
+      super
+      [:test, :spec].each do |usage|
+        java_tests = task.project.path_to(:source, usage, :java)
+        task.compile.from java_tests if File.directory?(java_tests)
+        resources = task.project.path_to(:source, usage, :resources)
+        task.resources.from resources if File.directory?(resources)
+      end
+    end
+
+    def user_config
+      options[:config] || task.project.path_to(:source, bdd_dir, lang, 'jtestr_config.rb')
+    end
+
+    def tests(dependencies) #:nodoc:
+      dependencies += [task.compile.target]
+      types = { :story => STORY_PATTERN, :rspec => RSpec::TESTS_PATTERN,
+                :testunit => TESTUNIT_PATTERN, :expect => EXPECT_PATTERN }
+      tests = types.keys.inject({}) { |h, k| h[k] = []; h }
+      tests[:junit] = JUnit.new(task, {}).tests(dependencies)
+      tests[:testng] = TestNG.new(task, {}).tests(dependencies)
+      Dir[task.project.path_to(:source, bdd_dir, lang, '**/*.rb')].each do |rb|
+        type = types.find { |k, v| Array(v).any? { |r| r === rb } }
+        tests[type.first] << rb if type
+      end
+      @jtestr_tests = tests
+      tests = tests.values.flatten
+      tests << nil if File.exist?(user_config)
+      tests
+    end
+
+    def run(tests, dependencies) #:nodoc:
+      dependencies += [task.compile.target.to_s]
+      
+      yaml_report = File.join(task.report_to.to_s, 'result.yaml')
+      spec_dir = task.project.path_to(:source, :spec, :ruby)
+
+      runner_file = task.project.path_to(:target, :spec, 'jtestr_runner.rb')
+      runner_erb = File.join(File.dirname(__FILE__), 'jtestr_runner.rb.erb')
+      runner = ERB.new(File.read(runner_erb)).result(binding)
+      Buildr.write runner_file, runner
+      
+      mkpath task.report_to.to_s
+      
+      runtime = new_runtime :current_directory => runner_file.pathmap('%d')
+      runtime.load_service.require runner_file
+
+      report = YAML::load(File.read(yaml_report))
+      raise (Array(report[:error][:message]) + report[:error][:backtrace]).join("\n") if
report[:error]
+      report[:success]
+    end
+    
   end
 
   

Added: incubator/buildr/trunk/lib/buildr/java/jtestr_result_handler.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/jtestr_result_handler.rb?rev=698286&view=auto
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/jtestr_result_handler.rb (added)
+++ incubator/buildr/trunk/lib/buildr/java/jtestr_result_handler.rb Tue Sep 23 12:12:12 2008
@@ -0,0 +1,77 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership.  The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+require 'jtestr'
+require 'yaml'
+
+class JtestR::YAMLResultHandler < JtestR::GenericResultHandler
+
+  class << self
+    attr_reader :report_file, :data
+    attr_accessor :tests
+
+    def report_to(file)
+      @report_file = file
+      self
+    end
+
+    def before
+      @data = {}
+    end
+
+    def after
+      FileUtils.mkdir_p(File.dirname(report_file))
+      data[:success] = tests - Array(data[:failure])
+      File.open(report_file, 'w') { |f| f.puts YAML::dump(data) }
+    end
+
+    def add_failure(test)
+      data[:failure] ||= []
+      data[:failure] << test unless data[:failure].include? test
+    end
+  end
+
+  attr_reader :name, :type_name
+
+  def add_fault(fault)
+    super
+    failure_from_bt = lambda do |ary|
+      test = nil
+      ary.find do |bt|
+        bt = bt.split(':').first.strip
+        test = bt if self.class.tests.include?(bt)
+      end
+      self.class.add_failure(test)
+    end
+    case fault
+    when Test::Unit::Failure
+      failure_from_bt.call fault.location
+    when Test::Unit::Error, Expectations::Results::Error, Spec::Runner::Reporter::Failure
+      failure_from_bt.call fault.exception.backtrace
+    when Expectations::Results
+      self.class.add_failure(fault.file)
+    else
+      if fault.respond_to?(:test_header)
+        fault.test_header[/\((.+)\)/]
+        test = $1.to_s
+        self.class.add_failure(test)
+      elsif fault.respond_to?(:method)
+        test = fault.method.test_class.name
+        self.class.add_failure(test)
+      end
+    end
+  end
+  
+end

Added: incubator/buildr/trunk/lib/buildr/java/jtestr_runner.rb.erb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/lib/buildr/java/jtestr_runner.rb.erb?rev=698286&view=auto
==============================================================================
--- incubator/buildr/trunk/lib/buildr/java/jtestr_runner.rb.erb (added)
+++ incubator/buildr/trunk/lib/buildr/java/jtestr_runner.rb.erb Tue Sep 23 12:12:12 2008
@@ -0,0 +1,104 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with this
+# work for additional information regarding copyright ownership.  The ASF
+# licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations under
+# the License.
+
+#  ___   ___     _  _  ___ _____    ___ ___ ___ _____    _____ _  _ ___ ___ 
+# |   \ / _ \   | \| |/ _ \_   _|  | __|   \_ _|_   _|  |_   _| || |_ _/ __|
+# | |) | (_) |  | .` | (_) || |    | _|| |) | |  | |      | | | __ || |\__ \
+# |___/ \___/   |_|\_|\___/ |_|    |___|___/___| |_|      |_| |_||_|___|___/
+#                                                                          
+#  ___ ___ _    ___ 
+# | __|_ _| |  | __|  CHANGES TO THIS FILE WILL BE LOST
+# | _| | || |__| _|   AUTO-GENERATED BY BUILDR on <%= Time.now %>
+# |_| |___|____|___|  PREFER TO EDIT JtestR CONFIGURATION FILE:
+#
+# <%= user_config %>
+                  
+begin
+  require 'jruby'
+  <%= dependencies.inspect %>.each { |dep| $CLASSPATH << dep }
+  require '<%= File.join(File.dirname(runner_erb), "jtestr_result_handler") %>'
+
+  jtestr = JtestR::TestRunner.new
+
+  class << jtestr
+    def config(&block)
+      @config_blocks ||= []
+      @config_blocks << block if block
+      @config_blocks
+    end
+    
+    def load_configuration
+      super
+      config.each { |block| @configuration.instance_eval(&block) }
+    end
+  end
+
+  jtestr.config do 
+    classpath [] # already loaded
+    add_common_classpath false
+  
+    <% ts = ( @jtestr_tests[:junit] & tests ).map { |c| 'Java.' + c } %>
+    junit [<%= ts.join(', ') %>]
+  
+    <% ts = ( @jtestr_tests[:testng] & tests ).map { |c| 'Java.' + c } %>
+    testng [<%= ts.join(', ') %>]
+  
+    <% ts = @jtestr_tests[:testunit] & tests %>
+    test_unit <%= ts.inspect %>
+  
+    <% ts = @jtestr_tests[:story] & tests %>
+    story <%= ts.inspect %>
+  
+    <% ts = @jtestr_tests[:rspec] & tests %>
+    rspec <%= ts.inspect %>
+  
+    <% ts = @jtestr_tests[:expect] & tests %>
+    expectations <%= ts.inspect %>
+  
+    ignore __FILE__
+  
+    if File.file?(<%= user_config.inspect %>)
+      ignore <%= user_config.inspect %>
+      load <%= user_config.inspect %>
+    end
+  end # config
+
+  args =  [ '<%= spec_dir %>' ] # the directory to search for jtestr files
+  args << JtestR::SimpleLogger::ERR # log level
+  args << JtestR::GenericResultHandler::QUIET # verbose level
+  <% if options[:output] == false %>
+    args << StringIO.new # output
+  <% elsif options[:output].kind_of?(String) %>
+    args << File.open('<%= options[:output] %>')
+  <% else %>
+    args << STDOUT # output
+  <% end %>
+  args << [] # groups_to_run
+  args << JtestR::YAMLResultHandler # result handler
+
+  JtestR::YAMLResultHandler.report_to('<%= yaml_report %>')
+  JtestR::YAMLResultHandler.tests = <%= tests.inspect %>
+  jtestr.run *args
+
+rescue => e
+  File.open('<%= yaml_report %>', "w") do |f| 
+    f.write YAML.dump({:error => { :message => e.message, :backtrace => e.backtrace}})
+  end
+end
+
+
+# Local Variables:
+# mode: ruby
+# End:



Mime
View raw message