From c82e1582f84cb95f6c31662deb62a4659261c685 Mon Sep 17 00:00:00 2001 From: monocle Date: Sun, 17 Apr 2011 17:06:45 -0700 Subject: [PATCH] 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' --- lib/guard.rb | 4 +-- lib/guard/hook.rb | 13 +++++--- spec/guard/hook_spec.rb | 70 +++++++++++++++++++++++++++-------------- spec/guard_spec.rb | 12 ++++--- 4 files changed, 65 insertions(+), 34 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index 9be9a34..322441e 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -60,9 +60,9 @@ module Guard # Let a guard execute its task but # fire it if his work leads to a system failure 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) - guard.hook "#{task_to_supervise}_end" + guard.hook("#{task_to_supervise}_end", result) result rescue Exception UI.error("#{guard.class.name} guard failed to achieve its <#{task_to_supervise}> command: #{$!}") diff --git a/lib/guard/hook.rb b/lib/guard/hook.rb index 99657bc..950e7f5 100644 --- a/lib/guard/hook.rb +++ b/lib/guard/hook.rb @@ -9,7 +9,7 @@ module Guard # from the symbol and calling method name. When passed # a string, #hook will turn the string into a symbol # directly. - def hook(event) + def hook(event, *args) hook_name = if event.is_a? Symbol calling_method = caller[0][/`([^']*)'/, 1] "#{calling_method}_#{event}".to_sym @@ -17,8 +17,11 @@ module Guard event.to_sym end - UI.info "\nHook :#{hook_name} executed for #{self.class}" - Hook.notify(self.class, hook_name) + if ENV["GUARD_ENV"] == "development" + UI.info "\nHook :#{hook_name} executed for #{self.class}" + end + + Hook.notify(self.class, hook_name, *args) end end @@ -38,9 +41,9 @@ module Guard callbacks[[guard_class, event]].include?(listener) end - def notify(guard_class, event) + def notify(guard_class, event, *args) callbacks[[guard_class, event]].each do |listener| - listener.call(guard_class, event) + listener.call(guard_class, event, *args) end end diff --git a/spec/guard/hook_spec.rb b/spec/guard/hook_spec.rb index 901057b..997af61 100644 --- a/spec/guard/hook_spec.rb +++ b/spec/guard/hook_spec.rb @@ -3,23 +3,16 @@ require 'spec_helper' describe Guard::Hook do subject { Guard::Hook } - class Guard::Dummy < Guard::Guard - include Guard::Hook - - def run_all - hook :begin - hook :end - end - end + class Guard::Dummy < Guard::Guard; end let(:guard_class) { ::Guard::Dummy } let(:listener) { double('listener').as_null_object } + after { subject.reset_callbacks! } + context "--module methods--" do before { subject.add_callback(listener, guard_class, :start_begin) } - after { subject.reset_callbacks! } - describe ".add_callback" do it "can add a single callback" do subject.has_callback?(listener, guard_class, :start_begin).should be_true @@ -34,8 +27,8 @@ describe Guard::Hook do describe ".notify" do it "sends :call to the given Guard class's callbacks" do - listener.should_receive(:call).with(guard_class, :start_begin) - subject.notify(guard_class, :start_begin) + listener.should_receive(:call).with(guard_class, :start_begin, "args") + subject.notify(guard_class, :start_begin, "args") end it "runs only the given callbacks" do @@ -55,24 +48,55 @@ describe Guard::Hook do end describe "#hook" do - it "calls Guard::Hook.notify" 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 + before(:all) do guard_class.class_eval do def start hook "my_hook" end + + def run_all + hook :begin + hook :end + end + + def stop + hook :begin, 'args' + hook 'special_sauce', 'first_arg', 'second_arg' + 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.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 diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index c27a2c7..3a1b745 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -77,10 +77,14 @@ describe Guard do ::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "i'm a success" end - it "calls the default hooks" do - @g.should_receive(:hook).with("regular_begin") - @g.should_receive(:hook).with("regular_end") - ::Guard.supervised_task(@g, :regular) + it "passes the args to the :begin hook" do + @g.should_receive(:hook).with("regular_with_arg_begin", "given_path") + ::Guard.supervised_task(@g, :regular_with_arg, "given_path") + 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