Hook - 1) Send args to hooks from Guard.supervised_task
2) Pass args from hooks to callbacks 3) Suppress UI message from hooks unless in 'development'
This commit is contained in:
parent
5b8ae609da
commit
c82e1582f8
@ -60,9 +60,9 @@ module Guard
|
|||||||
# Let a guard execute its task but
|
# Let a guard execute its task but
|
||||||
# fire it if his work leads to a system failure
|
# fire it if his work leads to a system failure
|
||||||
def supervised_task(guard, task_to_supervise, *args)
|
def supervised_task(guard, task_to_supervise, *args)
|
||||||
guard.hook "#{task_to_supervise}_begin"
|
guard.hook("#{task_to_supervise}_begin", *args)
|
||||||
result = guard.send(task_to_supervise, *args)
|
result = guard.send(task_to_supervise, *args)
|
||||||
guard.hook "#{task_to_supervise}_end"
|
guard.hook("#{task_to_supervise}_end", result)
|
||||||
result
|
result
|
||||||
rescue Exception
|
rescue Exception
|
||||||
UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise}> command: #{$!}")
|
UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise}> command: #{$!}")
|
||||||
|
@ -9,7 +9,7 @@ module Guard
|
|||||||
# from the symbol and calling method name. When passed
|
# from the symbol and calling method name. When passed
|
||||||
# a string, #hook will turn the string into a symbol
|
# a string, #hook will turn the string into a symbol
|
||||||
# directly.
|
# directly.
|
||||||
def hook(event)
|
def hook(event, *args)
|
||||||
hook_name = if event.is_a? Symbol
|
hook_name = if event.is_a? Symbol
|
||||||
calling_method = caller[0][/`([^']*)'/, 1]
|
calling_method = caller[0][/`([^']*)'/, 1]
|
||||||
"#{calling_method}_#{event}".to_sym
|
"#{calling_method}_#{event}".to_sym
|
||||||
@ -17,8 +17,11 @@ module Guard
|
|||||||
event.to_sym
|
event.to_sym
|
||||||
end
|
end
|
||||||
|
|
||||||
UI.info "\nHook :#{hook_name} executed for #{self.class}"
|
if ENV["GUARD_ENV"] == "development"
|
||||||
Hook.notify(self.class, hook_name)
|
UI.info "\nHook :#{hook_name} executed for #{self.class}"
|
||||||
|
end
|
||||||
|
|
||||||
|
Hook.notify(self.class, hook_name, *args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -38,9 +41,9 @@ module Guard
|
|||||||
callbacks[[guard_class, event]].include?(listener)
|
callbacks[[guard_class, event]].include?(listener)
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify(guard_class, event)
|
def notify(guard_class, event, *args)
|
||||||
callbacks[[guard_class, event]].each do |listener|
|
callbacks[[guard_class, event]].each do |listener|
|
||||||
listener.call(guard_class, event)
|
listener.call(guard_class, event, *args)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -3,23 +3,16 @@ require 'spec_helper'
|
|||||||
describe Guard::Hook do
|
describe Guard::Hook do
|
||||||
subject { Guard::Hook }
|
subject { Guard::Hook }
|
||||||
|
|
||||||
class Guard::Dummy < Guard::Guard
|
class Guard::Dummy < Guard::Guard; end
|
||||||
include Guard::Hook
|
|
||||||
|
|
||||||
def run_all
|
|
||||||
hook :begin
|
|
||||||
hook :end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
let(:guard_class) { ::Guard::Dummy }
|
let(:guard_class) { ::Guard::Dummy }
|
||||||
let(:listener) { double('listener').as_null_object }
|
let(:listener) { double('listener').as_null_object }
|
||||||
|
|
||||||
|
after { subject.reset_callbacks! }
|
||||||
|
|
||||||
context "--module methods--" do
|
context "--module methods--" do
|
||||||
before { subject.add_callback(listener, guard_class, :start_begin) }
|
before { subject.add_callback(listener, guard_class, :start_begin) }
|
||||||
|
|
||||||
after { subject.reset_callbacks! }
|
|
||||||
|
|
||||||
describe ".add_callback" do
|
describe ".add_callback" do
|
||||||
it "can add a single callback" do
|
it "can add a single callback" do
|
||||||
subject.has_callback?(listener, guard_class, :start_begin).should be_true
|
subject.has_callback?(listener, guard_class, :start_begin).should be_true
|
||||||
@ -34,8 +27,8 @@ describe Guard::Hook do
|
|||||||
|
|
||||||
describe ".notify" do
|
describe ".notify" do
|
||||||
it "sends :call to the given Guard class's callbacks" do
|
it "sends :call to the given Guard class's callbacks" do
|
||||||
listener.should_receive(:call).with(guard_class, :start_begin)
|
listener.should_receive(:call).with(guard_class, :start_begin, "args")
|
||||||
subject.notify(guard_class, :start_begin)
|
subject.notify(guard_class, :start_begin, "args")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "runs only the given callbacks" do
|
it "runs only the given callbacks" do
|
||||||
@ -55,24 +48,55 @@ describe Guard::Hook do
|
|||||||
end
|
end
|
||||||
|
|
||||||
describe "#hook" do
|
describe "#hook" do
|
||||||
it "calls Guard::Hook.notify" do
|
before(:all) do
|
||||||
guard = guard_class.new
|
|
||||||
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_begin)
|
|
||||||
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_end)
|
|
||||||
guard.run_all
|
|
||||||
end
|
|
||||||
|
|
||||||
it "if passed a string parameter, will use that for the hook name" do
|
|
||||||
guard_class.class_eval do
|
guard_class.class_eval do
|
||||||
def start
|
def start
|
||||||
hook "my_hook"
|
hook "my_hook"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def run_all
|
||||||
|
hook :begin
|
||||||
|
hook :end
|
||||||
|
end
|
||||||
|
|
||||||
|
def stop
|
||||||
|
hook :begin, 'args'
|
||||||
|
hook 'special_sauce', 'first_arg', 'second_arg'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
guard = guard_class.new
|
@guard = guard_class.new
|
||||||
|
end
|
||||||
|
|
||||||
|
it "calls Guard::Hook.notify" do
|
||||||
|
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_begin)
|
||||||
|
Guard::Hook.should_receive(:notify).with(guard_class, :run_all_end)
|
||||||
|
@guard.run_all
|
||||||
|
end
|
||||||
|
|
||||||
|
it "if passed a string parameter, will use that for the hook name" do
|
||||||
Guard::Hook.should_receive(:notify).with(guard_class, :my_hook)
|
Guard::Hook.should_receive(:notify).with(guard_class, :my_hook)
|
||||||
guard.start
|
@guard.start
|
||||||
|
end
|
||||||
|
|
||||||
|
it "accepts extra args" do
|
||||||
|
Guard::Hook.should_receive(:notify).with(guard_class, :stop_begin, 'args')
|
||||||
|
Guard::Hook.should_receive(:notify).with(guard_class, :special_sauce, 'first_arg', 'second_arg')
|
||||||
|
@guard.stop
|
||||||
|
end
|
||||||
|
|
||||||
|
context "--UI message--" do
|
||||||
|
it "is sent when in development mode" do
|
||||||
|
ENV["GUARD_ENV"] = 'development'
|
||||||
|
Guard::UI.should_receive(:info)
|
||||||
|
@guard.start
|
||||||
|
ENV["GUARD_ENV"] = 'test'
|
||||||
|
end
|
||||||
|
|
||||||
|
it "is not sent when not in development mode" do
|
||||||
|
Guard::UI.should_not_receive(:info)
|
||||||
|
@guard.start
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -77,10 +77,14 @@ describe Guard do
|
|||||||
::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "i'm a success"
|
::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "i'm a success"
|
||||||
end
|
end
|
||||||
|
|
||||||
it "calls the default hooks" do
|
it "passes the args to the :begin hook" do
|
||||||
@g.should_receive(:hook).with("regular_begin")
|
@g.should_receive(:hook).with("regular_with_arg_begin", "given_path")
|
||||||
@g.should_receive(:hook).with("regular_end")
|
::Guard.supervised_task(@g, :regular_with_arg, "given_path")
|
||||||
::Guard.supervised_task(@g, :regular)
|
end
|
||||||
|
|
||||||
|
it "passes the result of the supervised method to the :end hook" do
|
||||||
|
@g.should_receive(:hook).with("regular_with_arg_end", "i'm a success")
|
||||||
|
::Guard.supervised_task(@g, :regular_with_arg, "given_path")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user