<commit_before># 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 'buildr/java/test_frameworks'


module Buildr

  # Mixin for test frameworks using src/spec/{lang}
  module TestFramework::JavaBDD #:nodoc:
    
    class << self
      def included(mod)
        mod.module_eval do
          include TestFramework::JavaTest
          include ClassMethods
        end
        mod.extend ClassMethods
        mod.bdd_dir = :spec
        mod.lang = :java
        super
      end
    end

    module ClassMethods
      attr_accessor :lang, :bdd_dir
    end

    def initialize(task, options)
      self.bdd_dir = self.class.bdd_dir
      project = task.project
      project.task('test:compile').tap do |comp| 
        comp.send :associate_with, project, bdd_dir
        self.lang = comp.language || self.class.lang
      end
      project.task('test:resources').tap do |res|
        res.send :associate_with, project, bdd_dir
        res.filter.clear
        project.path_to(:source, bdd_dir, :resources).tap { |dir| res.from dir if File.exist?(dir) }
      end
      super
    end
  end
  
  class RSpec < TestFramework::Base
    include TestFramework::JavaBDD
    self.lang = :ruby

    
    REQUIRES = []
    REQUIRES.unshift 'org.jruby:jruby-complete:jar:1.1.1' unless RUBY_PLATFORM =~ /java/
    
    TESTS_PATTERN = [ /_spec.rb$/ ]
    OPTIONS = [:properties, :java_args]

    def self.applies_to?(project) #:nodoc:
      !Dir[project.path_to(:source, bdd_dir, lang, '**/*_spec.rb')].empty?
    end

    def tests(dependencies) #:nodoc:
      Dir[task.project.path_to(:source, bdd_dir, lang, '**/*_spec.rb')].
        select do |name| 
        selector = ENV['SPEC']
        selector.nil? || Regexp.new(selector) === name
      end
    end

    def run(tests, dependencies) #:nodoc:
      cmd_options = task.options.only(:properties, :java_args)
      dependencies.push *Dir.glob(File.join(jruby_home, "lib/*.jar")) if RUBY_PLATFORM =~ /java/
      cmd_options.update :classpath => dependencies, :project => task.project

      # TODO:  Setting up JRuby is something to do before running Buildr.
      #install_gems(cmd_options)

      report_dir = task.report_to.to_s
      FileUtils.rm_rf report_dir
      ENV['CI_REPORTS'] = report_dir

      jruby("-Ilib", "-S", "spec",
      "--require", "ci/reporter/rake/rspec_loader",
      "--format", "CI::Reporter::RSpecDoc", tests,
      cmd_options.merge({:name => "RSpec"}))
      tests
    end

    private
    def jruby_home
      @jruby_home ||= RUBY_PLATFORM =~ /java/ ? Config::CONFIG['prefix'] : File.expand_path(".jruby", ENV['HOME'])
    end

    def gem_path(project, gem_name, *additional)
      dir = Dir["#{jruby_home(project)}/lib/ruby/gems/1.8/gems/#{gem_name}*"].to_a.first
      dir = File.join(dir, *additional) unless additional.empty?
      dir
    end

    def required_gems(options)
      ["ci_reporter", options[:required_gems]].flatten.compact
    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 install_gems(options)
    #  unless required_gems(options).all? {|g| gem_path(options[:project], g)}
    #    args = ["-S", "maybe_install_gems", *required_gems(options)]
    #    args << {:name => "JRuby Setup"}.merge(options)
    #    jruby(*args)
    #  end
    #end
    
  end

  class JtestR < TestFramework::Base
    include TestFramework::JavaBDD
    self.lang = :ruby
  end
  
  # JBehave is a Java BDD framework. To use in your project:
  #   test.using :jbehave
  # 
  # This framework will search in your project for:
  #   src/spec/java/**/*Behaviour.java
  # 
  # JMock libraries are included on runtime.
  #
  # Support the following options:
  # * :properties -- Hash of properties to the test suite
  # * :java_args -- Arguments passed to the JVM
  class JBehave < TestFramework::Base
    include TestFramework::JavaBDD

    VERSION = "1.0.1" unless const_defined?('VERSION')
    REQUIRES = ["org.jbehave:jbehave:jar:#{VERSION}",
                "jmock:jmock-cglib:jar:#{JMock::VERSION}",
                "cglib:cglib-full:jar:2.0.2",
               ] + JUnit::REQUIRES
    TESTS_PATTERN = [ /Behaviou?r$/ ]

    def self.applies_to?(project) #:nodoc:
      %w{
        **/*Behaviour.java **/*Behavior.java<commit_msg>RSpec test framework now expects you to either run with JRuby or have JRUBY_HOME set.<commit_after># 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 'buildr/java/test_frameworks'


module Buildr

  # Mixin for test frameworks using src/spec/{lang}
  module TestFramework::JavaBDD #:nodoc:
    
    class << self
      def included(mod)
        mod.module_eval do
          include TestFramework::JavaTest
          include ClassMethods
        end
        mod.extend ClassMethods
        mod.bdd_dir = :spec
        mod.lang = :java
        super
      end
    end

    module ClassMethods
      attr_accessor :lang, :bdd_dir
    end

    def initialize(task, options)
      self.bdd_dir = self.class.bdd_dir
      project = task.project
      project.task('test:compile').tap do |comp| 
        comp.send :associate_with, project, bdd_dir
        self.lang = comp.language || self.class.lang
      end
      project.task('test:resources').tap do |res|
        res.send :associate_with, project, bdd_dir
        res.filter.clear
        project.path_to(:source, bdd_dir, :resources).tap { |dir| res.from dir if File.exist?(dir) }
      end
      super
    end
  end
  
  class RSpec < TestFramework::Base
    include TestFramework::JavaBDD
    self.lang = :ruby

    
    REQUIRES = []
    # REQUIRES.unshift 'org.jruby:jruby-complete:jar:1.1.1' unless RUBY_PLATFORM =~ /java/
    
    TESTS_PATTERN = [ /_spec.rb$/ ]
    OPTIONS = [:properties, :java_args]

    def self.applies_to?(project) #:nodoc:
      !Dir[project.path_to(:source, bdd_dir, lang, '**/*_spec.rb')].empty?
    end

    def tests(dependencies) #:nodoc:
      Dir[task.project.path_to(:source, bdd_dir, lang, '**/*_spec.rb')].
        select do |name| 
        selector = ENV['SPEC']
        selector.nil? || Regexp.new(selector) === name
      end
    end

    def run(tests, dependencies) #:nodoc:
      jruby_home or fail "To use RSpec you must either run on JRuby or have JRUBY_HOME set"
      cmd_options = task.options.only(:properties, :java_args)
      dependencies.push *Dir.glob(File.join(jruby_home, "lib/*.jar")) if RUBY_PLATFORM =~ /java/
      cmd_options.update :classpath => dependencies, :project => task.project

      report_dir = task.report_to.to_s
      FileUtils.rm_rf report_dir
      ENV['CI_REPORTS'] = report_dir

      jruby '-Ilib', '-S', 'spec', '--require', 'ci/reporter/rake/rspec_loader',
        '--format', 'CI::Reporter::RSpecDoc', tests, cmd_options.merge(:name => 'RSpec')
      tests
    end

    private
    def jruby_home
      @jruby_home ||= RUBY_PLATFORM =~ /java/ ? Config::CONFIG['prefix'] : ENV['JRUBY_HOME']
    end

    def required_gems(options)
      ["ci_reporter", options[:required_gems]].flatten.compact
    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

  end

  class JtestR < TestFramework::Base
    include TestFramework::JavaBDD
    self.lang = :ruby
  end
  
  # JBehave is a Java BDD framework. To use in your project:
  #   test.using :jbehave
  # 
  # This framework will search in your project for:
  #   src/spec/java/**/*Behaviour.java
  # 
  # JMock libraries are included on runtime.
  #
  # Support the following options:
  # * :properties -- Hash of properties to the test suite
  # * :java_args -- Arguments passed to the JVM
  class JBehave < TestFramework::Base
    include TestFramework::JavaBDD

    VERSION = "1.0.1" unless const_defined?('VERSION')
    REQUIRES = ["org.jbehave:jbehave:jar:#{VERSION}",
                "jmock:jmock-cglib:jar:#{JMock::VERSION}",
                "cglib:cglib-full:jar:2.0.2",
               ] + JUnit::REQUIRES
    TESTS_PATTERN = [ /Behaviou?r$/ ]

    def self.applies_to?(project) #:nodoc:
      %w{
        **/*Behaviour.java **/*Behavior.java
{'fraction_non_alphanumeric': 0.1206108355218364, 'fraction_numerical': 0.003307071296566482, 'mean_word_length': 2.899127796738718, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6625378', 'n_tokens_mistral': 3402, 'n_tokens_neox': 3266, 'n_words': 846}