<commit_before>#--
# Copyright (c) 2010-2011 Engine Yard, Inc.
# Copyright (c) 2007-2009 Sun Microsystems, Inc.
# This source code is available under the MIT license.
# See the file LICENSE.txt for details.
#++

require 'java'
require 'rspec'
Maven.set_classpath

java_import org.jruby.rack.RackContext
java_import org.jruby.rack.RackConfig
java_import org.jruby.rack.RackServletContextListener
java_import org.jruby.rack.servlet.ServletRackContext
java_import javax.servlet.ServletContext
java_import javax.servlet.ServletConfig

module SharedHelpers

  def mock_servlet_context
    @rack_config ||= RackConfig.impl {}
    @rack_context ||= ServletRackContext.impl {}
    @servlet_context ||= ServletContext.impl {}
    [@rack_context, @servlet_context].each do |context|
      context.stub!(:log)
      context.stub!(:getInitParameter).and_return nil
      context.stub!(:getRealPath).and_return "/"
      context.stub!(:getResource).and_return nil
      context.stub!(:getContextPath).and_return "/"
    end
    @rack_context.stub!(:getConfig).and_return @rack_config
    @servlet_config ||= ServletConfig.impl {}
    @servlet_config.stub!(:getServletName).and_return "A Servlet"
    @servlet_config.stub!(:getServletContext).and_return @servlet_context
  end

  def create_booter(booter_class = JRuby::Rack::Booter)
    require 'jruby/rack'
    @booter = booter_class.new @rack_context
    yield @booter if block_given?
    @booter
  end

end

begin
  require 'rails' # attempt to load rails - for "real life" testing
  require 'rails/version' # use Rails::VERSION to detect current env
rescue LoadError
  # add to load path for stubbed out action_controller, railtie etc
  $LOAD_PATH.unshift File.expand_path('../rails/stub', __FILE__) 
end

WD_START = Dir.getwd

RSpec.configure do |config|

  config.include SharedHelpers
  
  config.before :each do
    @env_save = ENV.to_hash
    mock_servlet_context
  end

  config.after :each do
    (ENV.keys - @env_save.keys).each {|k| ENV.delete k}
    @env_save.each {|k,v| ENV[k] = v}
    Dir.chdir(WD_START) unless Dir.getwd == WD_START
    $servlet_context = nil
  end
  
  current_lib = 
    if defined?(Rails::VERSION)
      case Rails::VERSION::STRING
        when /2.3/ then :rails23
        when /3.0/ then :rails30
        when /3.1/ then :rails31
        # TODO setup rails 3.2 !
        else :stub
      end
    else
      :stub
    end
  
  config.filter_run_excluding :lib => lambda { |lib|
    return false if lib.nil? # no :lib => specified run with all
    ! ( lib.is_a?(Array) ? lib : [ lib ] ).include?(current_lib)
  }
  <commit_msg>better detection of current rails env in specs<commit_after>#--
# Copyright (c) 2010-2011 Engine Yard, Inc.
# Copyright (c) 2007-2009 Sun Microsystems, Inc.
# This source code is available under the MIT license.
# See the file LICENSE.txt for details.
#++

require 'java'
require 'rspec'
Maven.set_classpath

java_import org.jruby.rack.RackContext
java_import org.jruby.rack.RackConfig
java_import org.jruby.rack.RackServletContextListener
java_import org.jruby.rack.servlet.ServletRackContext
java_import javax.servlet.ServletContext
java_import javax.servlet.ServletConfig

module SharedHelpers

  def mock_servlet_context
    @rack_config ||= RackConfig.impl {}
    @rack_context ||= ServletRackContext.impl {}
    @servlet_context ||= ServletContext.impl {}
    [@rack_context, @servlet_context].each do |context|
      context.stub!(:log)
      context.stub!(:getInitParameter).and_return nil
      context.stub!(:getRealPath).and_return "/"
      context.stub!(:getResource).and_return nil
      context.stub!(:getContextPath).and_return "/"
    end
    @rack_context.stub!(:getConfig).and_return @rack_config
    @servlet_config ||= ServletConfig.impl {}
    @servlet_config.stub!(:getServletName).and_return "A Servlet"
    @servlet_config.stub!(:getServletContext).and_return @servlet_context
  end

  def create_booter(booter_class = JRuby::Rack::Booter)
    require 'jruby/rack'
    @booter = booter_class.new @rack_context
    yield @booter if block_given?
    @booter
  end

end

begin
  require 'rails' # attempt to load rails - for "real life" testing
  require 'rails/version' # use Rails::VERSION to detect current env
rescue LoadError
  # add to load path for stubbed out action_controller, railtie etc
  $LOAD_PATH.unshift File.expand_path('../rails/stub', __FILE__) 
end

WD_START = Dir.getwd

RSpec.configure do |config|

  config.include SharedHelpers
  
  config.before :each do
    @env_save = ENV.to_hash
    mock_servlet_context
  end

  config.after :each do
    (ENV.keys - @env_save.keys).each {|k| ENV.delete k}
    @env_save.each {|k,v| ENV[k] = v}
    Dir.chdir(WD_START) unless Dir.getwd == WD_START
    $servlet_context = nil
  end
  
  current_lib = 
    if defined?(Rails::VERSION)
      :"rails#{Rails::VERSION::MAJOR}#{Rails::VERSION::MINOR}" # e.g :rails31
    else
      :stub
    end
  
  config.filter_run_excluding :lib => lambda { |lib|
    return false if lib.nil? # no :lib => specified run with all
    ! ( lib.is_a?(Array) ? lib : [ lib ] ).include?(current_lib)
  }
  
{'fraction_non_alphanumeric': 0.11176240719030871, 'fraction_numerical': 0.009378663540445486, 'mean_word_length': 3.7398148148148147, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3489702', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1718, 'n_words': 399}