class Honeybadger::CLI::Test

def call(env) @app.call(env); end }

def call(env) @app.call(env); end }

def generate_success_message(response)

def generate_success_message(response)
  notice_id = JSON.parse(response.body)['id']
  notice_url = "https://app.honeybadger.io/notice/#{notice_id}"
  unless options[:install]
    return "âš¡ Success: #{notice_url}"
  end
  <<-MSG
Honeybadger is installed! -----------------------------------------------
ews: You're one deploy away from seeing all of your exceptions in
adger. For now, we've generated a test exception for you:
tice_url}
al steps:
ow a feedback form on your error page:
tps://docs.honeybadger.io/gem-feedback
ow a UUID or link to Honeybadger on your error page:
tps://docs.honeybadger.io/gem-informer
ack deployments (if you're using Capistrano, we already did this):
tps://docs.honeybadger.io/gem-deploys
 ever need help:
ad the gem troubleshooting guide: https://docs.honeybadger.io/gem-troubleshooting
eck out our documentation: https://docs.honeybadger.io/
ail the founders: support@honeybadger.io
eople don't realize that Honeybadger is a small, bootstrapped company. We
 couldn't do this without you. Thank you for allowing us to do what we
making developers awesome.
'badgering!
ely,
neybadger Crew
//www.honeybadger.io/about/
End --------------------------------------------------------------------
end

def initialize(options)

def initialize(options)
  @options = options
  @shell = ::Thor::Base.shell.new
end

def logger(*args) ; @logger ||= Logger.new(nil) ; end }

def logger(*args) ; @logger ||= Logger.new(nil) ; end }

def logger(*args) ; @logger ||= Logger.new(nil) ; end }

def logger(*args) ; @logger ||= Logger.new(nil) ; end }

def run

def run
  begin
    require File.join(Dir.pwd, 'config', 'environment.rb')
    raise LoadError unless defined?(::Rails.application)
    say("Detected Rails #{Rails::VERSION::STRING}")
  rescue LoadError
    require 'honeybadger/init/ruby'
  end
  if Honeybadger.config.get(:api_key).to_s =~ BLANK
    say("Unable to send test: Honeybadger API key is missing.", :red)
    exit(1)
  end
  Honeybadger.config.set(:report_data, !options[:dry_run])
  test_backend = TestBackend.new(Honeybadger.config.backend)
  Honeybadger.config.backend = test_backend
  at_exit do
    # Exceptions will already be reported when exiting.
    verify_test unless $!
  end
  run_test
end

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

def run_standalone_test

def run_standalone_test
  Honeybadger.notify(TEST_EXCEPTION)
end

def run_test

def run_test
  if defined?(::Rails.application) && ::Rails.application
    run_rails_test
  else
    run_standalone_test
  end
end

def test_exception_class

def test_exception_class
  exception_name = ENV['EXCEPTION'] || 'HoneybadgerTestingException'
  Object.const_get(exception_name)
rescue
  Object.const_set(exception_name, Class.new(Exception))
end

def verify_test

def verify_test
  Honeybadger.flush
  if calling = TestBackend.callings[:notices].find {|c| c[0].exception.eql?(TEST_EXCEPTION) }
    notice, response = *calling
    if !response.success?
      host = Honeybadger.config.get(:'connection.host')
      say(<<-MSG, :red)
 Honeybadger test failed ------------------------------------------------ !!
ror notifier is installed, but we encountered an error:
sponse.error_message}
 this issue, please try the following:
ke sure the gem is configured properly.
try executing this command a few times.
ke sure you can connect to #{host} (`curl https://#{host}/v1/notices`).
ail support@honeybadger.io for help. Include as much debug info as you
n for a faster resolution!
 End -------------------------------------------------------------------- !!
      exit(1)
    end
    say(generate_success_message(response), :green)
    exit(0)
  end
  say(<<-MSG, :red)
 Honeybadger test failed ------------------------------------------------ !!
 The test exception was not reported; the application may not be
ured properly.
s usually caused by one of the following issues:
ere was a problem loading your application. Check your logs to see if a
fferent exception is being raised.
e exception is being rescued before it reaches our Rack middleware. If
u're using `rescue` or `rescue_from` you may need to notify Honeybadger
nually: `Honeybadger.notify(exception)`.
e honeybadger gem is misconfigured. Check the settings in your
neybadger.yml file.
  notices = TestBackend.callings[:notices].map(&:first)
  unless notices.empty?
    say("\nThe following errors were reported:", :red)
    notices.each {|n| say("\n  - #{n.error_class}: #{n.error_message}", :red) }
  end
  say("\nSee https://docs.honeybadger.io/gem-troubleshooting for more troubleshooting help.\n\n", :red)
  say("!! --- End -------------------------------------------------------------------- !!", :red)
  exit(1)
end