class Honeybadger::CLI::Test

def run_rails_test

def run_rails_test
  # Suppress error logging in Rails' exception handling middleware. Rails 3.0
  # uses ActionDispatch::ShowExceptions to rescue/show exceptions, but does
  # not log anything but application trace. Rails 3.2 now falls back to
  # logging the framework trace (moved to ActionDispatch::DebugExceptions),
  # which caused cluttered output while running the test task.
  defined?(::ActionDispatch::DebugExceptions) and
    ::ActionDispatch::DebugExceptions.class_eval { def logger(*args) ; @logger ||= Logger.new(nil) ; end }
  defined?(::ActionDispatch::ShowExceptions) and
    ::ActionDispatch::ShowExceptions.class_eval { def logger(*args) ; @logger ||= Logger.new(nil) ; end }
  # Detect and disable the better_errors gem
  if defined?(::BetterErrors::Middleware)
    say('Better Errors detected: temporarily disabling middleware.', :yellow)
    ::BetterErrors::Middleware.class_eval { def call(env) @app.call(env); end }
  end
  begin
    require './app/controllers/application_controller'
  rescue LoadError
    nil
  end
  unless defined?(::ApplicationController)
    say('Error: No ApplicationController found.', :red)
    return false
  end
  eval(<<-CONTROLLER)
  class Honeybadger::TestController < ApplicationController
    # This is to bypass any filters that may prevent access to the action.
    if respond_to?(:prepend_before_action)
      prepend_before_action :test_honeybadger
    else
      prepend_before_filter :test_honeybadger
    end
    def test_honeybadger
      puts "Raising '#{Honeybadger::CLI::Test::TEST_EXCEPTION.class.name}' to simulate application failure."
      raise Honeybadger::CLI::Test::TEST_EXCEPTION
    end
    # Ensure we actually have an action to go to.
    def verify; end
  end
  CONTROLLER
  ::Rails.application.routes.tap do |r|
    # RouteSet#disable_clear_and_finalize prevents existing routes from
    # being cleared. We'll set it back to the original value when we're
    # done so not to mess with Rails state.
    d = r.disable_clear_and_finalize
    begin
      r.disable_clear_and_finalize = true
      r.clear!
      r.draw do
        match 'verify' => 'honeybadger/test#verify', :as => "verify_#{SecureRandom.hex}", :via => :get
      end
      ::Rails.application.routes_reloader.paths.each{ |path| load(path) }
      ::ActiveSupport.on_load(:action_controller) { r.finalize! }
    ensure
      r.disable_clear_and_finalize = d
    end
  end
  ssl = defined?(::Rails.configuration.force_ssl) && ::Rails.configuration.force_ssl
  env = ::Rack::MockRequest.env_for("http#{ ssl ? 's' : nil }://www.example.com/verify", 'REMOTE_ADDR' => '127.0.0.1', 'HTTP_HOST' => 'localhost')
  ::Rails.application.call(env)
end