Permalink
Browse files

Merge branch 'master' of https://github.com/leonid-shevtsov/hoptoad_n…

  • Loading branch information...
mike-burns committed Jun 30, 2011
2 parents 676c36e + 95571c4 commit e0023007abf9266b0a04e885d4293338fa04a640
@@ -0,0 +1,23 @@
Feature: Use the Gem to catch errors in a Rake application
Background:
Given I have built and installed the "hoptoad_notifier" gem

Scenario: Catching exceptions in Rake
When I run rake with hoptoad
Then Hoptoad should catch the exception

Scenario: Disabling Rake exception catcher
When I run rake with hoptoad disabled
Then Hoptoad should not catch the exception

Scenario: Autodetect, running from terminal
When I run rake with hoptoad autodetect from terminal
Then Hoptoad should not catch the exception

Scenario: Autodetect, not running from terminal
When I run rake with hoptoad autodetect not from terminal
Then Hoptoad should catch the exception

Scenario: Sendind the correct component name
When I run rake with hoptoad
Then Hoptoad should send the rake command line as the component name
@@ -0,0 +1,17 @@
When /I run rake with (.+)/ do |command|
@rake_command = "rake #{command.gsub(' ','_')}"
@rake_result = `cd features/support/rake && GEM_HOME=#{BUILT_GEM_ROOT} #{@rake_command} 2>&1`
end

Then /Hoptoad should (|not) ?catch the exception/ do |condition|
if condition=='not'
@rake_result.should_not =~ /^hoptoad/
else
@rake_result.should =~ /^hoptoad/
end
end

Then /Hoptoad should send the rake command line as the component name/ do
component = @rake_result.match(/^hoptoad (.*)$/)[1]
component.should == @rake_command
end
@@ -0,0 +1,57 @@
# A test harness for RakeHandler
#
require 'rake'
require 'rubygems'
require 'hoptoad_notifier'
require 'hoptoad_notifier/rake_handler'

HoptoadNotifier.configure do |c|
end

# Should catch exception
task :hoptoad do
HoptoadNotifier.configuration.rescue_rake_exceptions = true
stub_tty_output(true)
raise_exception
end

# Should not catch exception
task :hoptoad_disabled do
HoptoadNotifier.configuration.rescue_rake_exceptions = false
stub_tty_output(true)
raise_exception
end

# Should not catch exception as tty_output is true
task :hoptoad_autodetect_from_terminal do
HoptoadNotifier.configuration.rescue_rake_exceptions = nil
stub_tty_output(true)
raise_exception
end

# Should catch exception as tty_output is false
task :hoptoad_autodetect_not_from_terminal do
HoptoadNotifier.configuration.rescue_rake_exceptions = nil
stub_tty_output(false)
raise_exception
end

module HoptoadNotifier
def self.notify(*args)
# TODO if you need to check more params, you'll have to use json.dump or something
$stderr.puts "hoptoad #{args[1][:component]}"
end
end

def stub_tty_output(value)
Rake.application.instance_eval do
@tty_output_stub = value
def tty_output?
@tty_output_stub
end
end
end

def raise_exception
raise 'TEST'
end
@@ -8,7 +8,7 @@ class Configuration
:ignore_user_agent, :notifier_name, :notifier_url, :notifier_version,
:params_filters, :project_root, :port, :protocol, :proxy_host,
:proxy_pass, :proxy_port, :proxy_user, :secure, :framework,
:user_information].freeze
:user_information, :rescue_rake_exceptions].freeze

# The API key for your project, found on the project edit form.
attr_accessor :api_key
@@ -87,6 +87,10 @@ class Configuration
# The framework HoptoadNotifier is configured to use
attr_accessor :framework

# Should HoptoadNotifier catch exceptions from Rake tasks?
# (boolean or nil; set to nil to catch exceptions when rake isn't running from a terminal; default is nil)
attr_accessor :rescue_rake_exceptions

DEFAULT_PARAMS_FILTERS = %w(password password_confirmation).freeze

DEFAULT_BACKTRACE_FILTERS = [
@@ -134,6 +138,7 @@ def initialize
@notifier_url = 'http://hoptoadapp.com'
@framework = 'Standalone'
@user_information = 'Hoptoad Error {{error_id}}'
@rescue_rake_exceptions = nil
end

# Takes a block and adds it to the list of backtrace filters. When the filters
@@ -4,6 +4,7 @@
module HoptoadNotifier
class Railtie < Rails::Railtie
rake_tasks do
require 'hoptoad_notifier/rake_handler'
require "hoptoad_notifier/rails3_tasks"
end

@@ -0,0 +1,65 @@
# Patch Rake::Application to handle errors with Hoptoad
module HoptoadNotifier::RakeHandler
def self.included(klass)
klass.class_eval do
include Rake087Methods unless defined?(Rake::VERSION) && Rake::VERSION >= '0.9.0'
alias_method :display_error_message_without_hoptoad, :display_error_message
alias_method :display_error_message, :display_error_message_with_hoptoad
end
end

def display_error_message_with_hoptoad(ex)
if HoptoadNotifier.configuration.rescue_rake_exceptions ||
(HoptoadNotifier.configuration.rescue_rake_exceptions===nil && !self.tty_output?)

HoptoadNotifier.notify(ex, :component => reconstruct_command_line, :cgi_data => ENV)
end

display_error_message_without_hoptoad(ex)
end

def reconstruct_command_line
"rake #{ARGV.join( ' ' )}"
end

# This module brings Rake 0.8.7 error handling to 0.9.0 standards
module Rake087Methods
# Method taken from Rake 0.9.0 source
#
# Provide standard exception handling for the given block.
def standard_exception_handling
begin
yield
rescue SystemExit => ex
# Exit silently with current status
raise
rescue OptionParser::InvalidOption => ex
$stderr.puts ex.message
exit(false)
rescue Exception => ex
# Exit with error message
display_error_message(ex)
exit(false)
end
end

# Method extracted from Rake 0.8.7 source
def display_error_message(ex)
$stderr.puts "#{name} aborted!"
$stderr.puts ex.message
if options.trace
$stderr.puts ex.backtrace.join("\n")
else
$stderr.puts ex.backtrace.find {|str| str =~ /#{@rakefile}/ } || ""
$stderr.puts "(See full trace by running task with --trace)"
end
end
end
end

Rake.application.instance_eval do
class << self
include HoptoadNotifier::RakeHandler
end
end

0 comments on commit e002300

Please sign in to comment.