-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor error containment into its own class for resue
- Loading branch information
Showing
3 changed files
with
55 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |