Skip to content

Commit

Permalink
configuration option and features for Rake handler
Browse files Browse the repository at this point in the history
  • Loading branch information
leonid-shevtsov committed Jun 12, 2011
1 parent 9620bd6 commit 34521f9
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 6 deletions.
23 changes: 23 additions & 0 deletions features/rake.feature
@@ -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
17 changes: 17 additions & 0 deletions features/step_definitions/rake_steps.rb
@@ -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
57 changes: 57 additions & 0 deletions features/support/rake/Rakefile
@@ -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
7 changes: 6 additions & 1 deletion lib/hoptoad_notifier/configuration.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions lib/hoptoad_notifier/rake_handler.rb
Expand Up @@ -11,15 +11,17 @@ def standard_exception_handling
# Exit silently
exit(false)
rescue Exception => ex
if tty_output?
handle_exception_without_hoptoad(ex)
else
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

handle_exception_without_hoptoad(ex)
exit(false)
end
end

def handle_exception_without_hoptoad(ex)
# Exit with error message
$stderr.puts "#{name} aborted!"
Expand All @@ -31,7 +33,7 @@ def handle_exception_without_hoptoad(ex)
$stderr.puts "(See full trace by running task with --trace)"
end
end

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

0 comments on commit 34521f9

Please sign in to comment.