Skip to content

Commit

Permalink
Refactor error containment into its own class for resue
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbus committed May 29, 2013
1 parent 739ddfe commit 97ca58c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
7 changes: 1 addition & 6 deletions lib/guard/jruby-rspec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,13 @@ def reload_paths(paths)
# end
else
# reload the file
begin
Containment.new.protect do
load p
rescue
UI.error $!.message
UI.error $!.backtrace.join "\n"
throw :task_has_failed
end
end
end
end
end

end
end

25 changes: 25 additions & 0 deletions lib/guard/jruby-rspec/containment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Guard
class JRubyRSpec
class Containment
def initialize(options = {})
@error_handler = options.fetch(:error_handler, method(:output_as_guard_error))
end

def protect
yield
rescue Exception => e
error_handler.call e
throw :task_has_failed
end

private

attr_reader :error_handler

def output_as_guard_error(exception)
UI.error $!.message
UI.error $!.backtrace.join "\n"
end
end
end
end
29 changes: 29 additions & 0 deletions spec/guard/jruby-rspec/containment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'spec_helper'
require 'guard/jruby-rspec/containment'

class Guard::JRubyRSpec
describe Containment do
subject(:containment) { described_class.new }

describe '#protect' do
it 'runs the block that is passed to it' do
expect { |block| containment.protect &block }.to yield_control
end

it 'uses a default error_handler' do
Guard::UI.should_receive(:error).at_least(1).times
expect { containment.protect { raise 'busted' } }.to throw_symbol(:task_has_failed)
end

context 'with a custom error_handler' do
subject(:containment) { described_class.new(:error_handler => lambda { @custom_handler_called = true }) }

it 'calls the custom error_handler' do
Guard::UI.should_receive(:error).never
expect { containment.protect { raise 'busted' } }.to throw_symbol(:task_has_failed)
@custom_handler_called.should be_true
end
end
end
end
end

0 comments on commit 97ca58c

Please sign in to comment.