<commit_before>require 'active_support'
require 'json'
require 'helpers/rails_server'

module RailsHelpers
  extend ActiveSupport::Concern

  attr_reader :rails_server

  def rails_template_name
    @rails_template_name || raise("No Rails template name!")
  end

  def full_path(subpath)
    "#{rails_template_name}/#{subpath}"
  end

  def get(subpath, options = { })
    @rails_server.get(full_path(subpath), options)
  end

  def get_response(subpath, options = { })
    @rails_server.get_response(full_path(subpath), options)
  end

  def get_success(subpath, options = { })
    data = get(subpath, options)
    data.should match(/rails_spec_application/i) unless options[:no_layout]
    data
  end

  def expect_match(subpath, *args)
    options = args.extract_options!
    regexes = args

    data = get_success(subpath, options)
    regexes.each do |regex|
      data.should match(regex)
    end

    data
  end

  def expect_exception(subpath, class_name, message)
    data = get(subpath)

    json = begin
      JSON.parse(data)
    rescue => e
      raise %{Expected a JSON response from '#{subpath}' (because we expected an exception),
but we couldn't parse it as JSON; when we tried, we got:

(#{e.class.name}) #{e.message}

The data is:

#{data.inspect}}
    end

    json['exception'].should be
    json['exception']['class'].should == class_name.to_s
    json['exception']['message'].should match(message)
  end

  module ClassMethods
    def rails_template_name
      @rails_template_name || raise("No Rails template name!")
    end

    def uses_rails_with_template(template_name, options = { })
      before :all do
        @rails_template_name = template_name

        gem_root = File.expand_path(File.join(File.dirname(__FILE__), '../..'))
        templates = [ 'base', template_name ].map do |t|
          File.expand_path(File.join(gem_root, "spec/rails/templates/#{t}"))
        end
        runtime_base_directory = File.join(gem_root, 'tmp/spec/rails')
        additional_gemfile_lines = [ "gem 'fortitude', :path => '#{gem_root}'" ]
        additional_gemfile_lines += Array(options[:additional_gemfile_lines] || [ ])

        @rails_server = Spec::Helpers::RailsServer.new(
          :name => template_name, :template_paths => templates, :runtime_base_directory => runtime_base_directory,
          :rails_version => (ENV['FORTITUDE_SPECS_RAILS_VERSION'] || options[:rails_version]),
          :rails_env => options[:rails_env], :additional_gemfile_lines => additional_gemfile_lines)

        @rails_server.start!
      end

      after :all do<commit_msg>Modularize directory paths better.<commit_after>require 'active_support'
require 'json'
require 'helpers/rails_server'

module RailsHelpers
  extend ActiveSupport::Concern

  attr_reader :rails_server


  def full_path(subpath)
    "#{rails_template_name}/#{subpath}"
  end

  def get(subpath, options = { })
    rails_server.get(full_path(subpath), options)
  end

  def get_response(subpath, options = { })
    rails_server.get_response(full_path(subpath), options)
  end

  def get_success(subpath, options = { })
    data = get(subpath, options)
    data.should match(/rails_spec_application/i) unless options[:no_layout]
    data
  end

  def expect_match(subpath, *args)
    options = args.extract_options!
    regexes = args

    data = get_success(subpath, options)
    regexes.each do |regex|
      data.should match(regex)
    end

    data
  end

  def expect_exception(subpath, class_name, message)
    data = get(subpath)

    json = begin
      JSON.parse(data)
    rescue => e
      raise %{Expected a JSON response from '#{subpath}' (because we expected an exception),
but we couldn't parse it as JSON; when we tried, we got:

(#{e.class.name}) #{e.message}

The data is:

#{data.inspect}}
    end

    json['exception'].should be
    json['exception']['class'].should == class_name.to_s
    json['exception']['message'].should match(message)
  end

  def rails_template_name
    @rails_template_name || raise("No Rails template name!")
  end

  def rails_server_project_root
    @rails_server_project_root ||= File.expand_path(File.join(File.dirname(__FILE__), '../..'))
  end

  def rails_server_templates_root
    @rails_server_templates_root ||= File.join(rails_server_project_root, "spec/rails/templates")
  end

  def rails_server_runtime_base_directory
    @rails_server_runtime_base_directory ||= File.join(rails_server_project_root, "tmp/spec/rails")
  end

  module ClassMethods
    def uses_rails_with_template(template_name, options = { })
      before :all do
        @rails_template_name = template_name

        templates = [ 'base', template_name ].map do |t|
          File.join(rails_server_templates_root, t.to_s)
        end
        additional_gemfile_lines = [ "gem 'fortitude', :path => '#{rails_server_project_root}'" ]
        additional_gemfile_lines += Array(options[:additional_gemfile_lines] || [ ])

        @rails_server = Spec::Helpers::RailsServer.new(
          :name => template_name, :template_paths => templates, :runtime_base_directory => rails_server_runtime_base_directory,
          :rails_version => (ENV['FORTITUDE_SPECS_RAILS_VERSION'] || options[:rails_version]),
          :rails_env => options[:rails_env], :additional_gemfile_lines => additional_gemfile_lines)

        @rails_server.start!
      end

      after :all do
{'fraction_non_alphanumeric': 0.1157286619455833, 'fraction_numerical': 0.0, 'mean_word_length': 3.7919642857142857, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '4306890', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1666, 'n_words': 354}