From 56d4914ce1ff9756ea44485c3754d56439a74fcf Mon Sep 17 00:00:00 2001 From: Yann Lugrin Date: Thu, 4 Nov 2010 14:10:58 +0100 Subject: [PATCH] restrict capacity to run_at_start to reload and run_all actions --- lib/guard.rb | 4 ++-- spec/guard_spec.rb | 31 ++++++++++++++++++------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index 0f443a6..a9131a6 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -41,8 +41,8 @@ module Guard UI.info "Guard is now watching at '#{Dir.pwd}'" guards.each do |guard| if supervised_task(guard, :start) - (guard.class.instance_methods(false) | ::Guard::Guard.instance_methods(false)).each do |m| - guard.send(:"#{$1}") if m.to_s =~ /^(.+)_at_start\?$/ && guard.send(:"#{$1}_at_start?") + %w[reload run_all].each do |m| + guard.send(m) if guard.respond_to?(:"#{m}_at_start?") && guard.send(:"#{m}_at_start?") end end end diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index 70283f1..1e0425e 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -61,6 +61,7 @@ describe Guard do @guard = mock(::Guard::Guard) @guard.stub!(:reload_at_start?).and_return(false) @guard.stub!(:run_all_at_start?).and_return(false) + @guard.stub!(:respond_to?) @listener = mock(::Guard::Polling) @listener.stub!(:on_change) @@ -93,27 +94,31 @@ describe Guard do subject.start end - it 'should call method definined to run at start' do - @guard.class.stub(:instance_methods).with(false).and_return([:test, :test_at_start?]) - - @guard.should_receive(:run_all_at_start?).and_return(true) - @guard.should_receive(:run_all).and_return(true) - - @guard.should_receive(:test_at_start?).and_return(true) - @guard.should_receive(:test).and_return(true) + it 'should call reload at start if needed' do + @guard.should_receive(:respond_to?).with(:reload_at_start?).and_return(true) + @guard.should_receive(:reload_at_start?).and_return(true) + @guard.should_receive(:reload).and_return(true) subject.start end - it 'should not call method definined to not run at start' do - @guard.class.stub(:instance_methods).with(false).and_return([:test, :test_at_start?]) + it 'should call run_all at start if needed' do + @guard.should_receive(:respond_to?).with(:run_all_at_start?).and_return(true) + @guard.should_receive(:run_all_at_start?).and_return(true) + @guard.should_receive(:run_all).and_return(true) + subject.start + end + + it 'should not call reload and run_all at start if not needed' do + @guard.should_receive(:respond_to?).with(:reload_at_start?).and_return(true) + @guard.should_receive(:reload_at_start?).and_return(false) + @guard.should_not_receive(:reload) + + @guard.should_receive(:respond_to?).with(:run_all_at_start?).and_return(true) @guard.should_receive(:run_all_at_start?).and_return(false) @guard.should_not_receive(:run_all) - @guard.should_receive(:test_at_start?).and_return(false) - @guard.should_not_receive(:test) - subject.start end