Merge branch 'master' into hook

Conflicts:
	lib/guard.rb
	lib/guard/dsl.rb
	spec/guard/dsl_spec.rb
	spec/guard_spec.rb
This commit is contained in:
Rémy Coutable 2011-08-17 10:45:20 +02:00
commit 652c3d8661
5 changed files with 38 additions and 45 deletions

View File

@ -86,13 +86,13 @@ module Guard
listener.start listener.start
end end
def add_guard(name, watchers = [], callbacks = [], options = {}, group = nil) def add_guard(name, watchers = [], callbacks = [], options = {})
if name.to_sym == :ego if name.to_sym == :ego
UI.deprecation("Guard::Ego is now part of Guard. You can remove it from your Guardfile.") UI.deprecation("Guard::Ego is now part of Guard. You can remove it from your Guardfile.")
else else
guard_class = get_guard_class(name) guard_class = get_guard_class(name)
callbacks.each { |callback| ::Guard::Hook.add_callback(callback[:listener], guard_class, callback[:events]) } callbacks.each { |callback| ::Guard::Hook.add_callback(callback[:listener], guard_class, callback[:events]) }
@guards << guard_class.new(watchers, options, group) @guards << guard_class.new(watchers, options)
end end
end end

View File

@ -119,11 +119,12 @@ module Guard
@watchers = [] @watchers = []
@callbacks = [] @callbacks = []
watch_and_callback_definition.call if watch_and_callback_definition watch_and_callback_definition.call if watch_and_callback_definition
::Guard.add_guard(name.to_s.downcase.to_sym, @watchers, @callbacks, options, @current_group || :default) options.update(:group => (@current_group || :default))
::Guard.add_guard(name.to_s.downcase.to_sym, @watchers, @callbacks, options)
end end
def watch(pattern, &action) def watch(pattern, &action)
@watchers << { :pattern => pattern, :action => action } @watchers << ::Guard::Watcher.new(pattern, action)
end end
def callback(*args, &listener) def callback(*args, &listener)

View File

@ -4,9 +4,9 @@ module Guard
attr_accessor :watchers, :options, :group attr_accessor :watchers, :options, :group
def initialize(watchers = [], options = {}, group = nil) def initialize(watchers = [], options = {})
@group = options.delete(:group) || :default
@watchers, @options = watchers, options @watchers, @options = watchers, options
@group ||= :default
end end
# Guardfile template needed inside guard gem # Guardfile template needed inside guard gem

View File

@ -204,41 +204,41 @@ describe Guard::Dsl do
describe "#group" do describe "#group" do
it "evaluates only the specified string group" do it "evaluates only the specified string group" do
::Guard.should_receive(:add_guard).with(:pow, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:pow, [], [], { :group => :default })
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :w) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :w })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['w']) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['w'])
end end
it "evaluates only the specified symbol group" do it "evaluates only the specified symbol group" do
::Guard.should_receive(:add_guard).with(:pow, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:pow, [], [], { :group => :default })
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :w) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :w })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w]) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
end end
it "evaluates only the specified groups" do it "evaluates only the specified groups" do
::Guard.should_receive(:add_guard).with(:pow, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:pow, [], [], { :group => :default })
::Guard.should_receive(:add_guard).with(:rspec, [], [], {}, :x) ::Guard.should_receive(:add_guard).with(:rspec, [], [], { :group => :x })
::Guard.should_receive(:add_guard).with(:ronn, [], [], {}, :x) ::Guard.should_receive(:add_guard).with(:ronn, [], [], { :group => :x })
::Guard.should_receive(:add_guard).with(:less, [], [], {}, :y) ::Guard.should_receive(:add_guard).with(:less, [], [], { :group => :y })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:x, :y]) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:x, :y])
end end
it "evaluates always guard outside any group (even when a group is given)" do it "evaluates always guard outside any group (even when a group is given)" do
::Guard.should_receive(:add_guard).with(:pow, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:pow, [], [], { :group => :default })
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :w) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :w })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w]) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => [:w])
end end
it "evaluates all groups when no group option is specified" do it "evaluates all groups when no group option is specified" do
::Guard.should_receive(:add_guard).with(:pow, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:pow, [], [], { :group => :default })
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :w) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :w })
::Guard.should_receive(:add_guard).with(:rspec, [], [], {}, :x) ::Guard.should_receive(:add_guard).with(:rspec, [], [], { :group => :x })
::Guard.should_receive(:add_guard).with(:ronn, [], [], {}, :x) ::Guard.should_receive(:add_guard).with(:ronn, [], [], { :group => :x })
::Guard.should_receive(:add_guard).with(:less, [], [], {}, :y) ::Guard.should_receive(:add_guard).with(:less, [], [], { :group => :y })
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
end end
@ -246,31 +246,31 @@ describe Guard::Dsl do
describe "#guard" do describe "#guard" do
it "loads a guard specified as a quoted string from the DSL" do it "loads a guard specified as a quoted string from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard 'test'") subject.evaluate_guardfile(:guardfile_contents => "guard 'test'")
end end
it "loads a guard specified as a double quoted string from the DSL" do it "loads a guard specified as a double quoted string from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => 'guard "test"') subject.evaluate_guardfile(:guardfile_contents => 'guard "test"')
end end
it "loads a guard specified as a symbol from the DSL" do it "loads a guard specified as a symbol from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard :test") subject.evaluate_guardfile(:guardfile_contents => "guard :test")
end end
it "loads a guard specified as a symbol and called with parens from the DSL" do it "loads a guard specified as a symbol and called with parens from the DSL" do
::Guard.should_receive(:add_guard).with(:test, [], [], {}, :default) ::Guard.should_receive(:add_guard).with(:test, [], [], { :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard(:test)") subject.evaluate_guardfile(:guardfile_contents => "guard(:test)")
end end
it "receives options when specified, from normal arg" do it "receives options when specified, from normal arg" do
::Guard.should_receive(:add_guard).with(:test, [], [], { :opt_a => 1, :opt_b => 'fancy' }, :default) ::Guard.should_receive(:add_guard).with(:test, [], [], { :opt_a => 1, :opt_b => 'fancy', :group => :default })
subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'") subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'")
end end
@ -278,12 +278,12 @@ describe Guard::Dsl do
describe "#watch" do describe "#watch" do
it "should receive watchers when specified" do it "should receive watchers when specified" do
::Guard.should_receive(:add_guard).with(:dummy, anything, anything, {}, :default) do |name, watchers, callbacks, options, group| ::Guard.should_receive(:add_guard).with(:dummy, anything, anything, { :group => :default }) do |name, watchers, callbacks, options|
watchers.size.should == 2 watchers.size.should == 2
watchers[0][:pattern].should == 'a' watchers[0].pattern.should == 'a'
watchers[0][:action].call.should == proc { 'b' }.call watchers[0].action.call.should == proc { 'b' }.call
watchers[1][:pattern].should == 'c' watchers[1].pattern.should == 'c'
watchers[1][:action].should == nil watchers[1].action.should == nil
end end
subject.evaluate_guardfile(:guardfile_contents => " subject.evaluate_guardfile(:guardfile_contents => "
guard :dummy do guard :dummy do
@ -301,7 +301,7 @@ describe Guard::Dsl do
end end
end end
::Guard.should_receive(:add_guard).with(:dummy, anything, anything, {}, :default) do |name, watchers, callbacks, options, group| ::Guard.should_receive(:add_guard).with(:dummy, anything, anything, { :group => :default }) do |name, watchers, callbacks, options|
callbacks.should have(2).items callbacks.should have(2).items
callbacks[0][:events].should == :start_end callbacks[0][:events].should == :start_end
callbacks[0][:listener].call(Guard::Dummy, :start_end, 'foo').should == "Guard::Dummy executed 'start_end' hook with foo!" callbacks[0][:listener].call(Guard::Dummy, :start_end, 'foo').should == "Guard::Dummy executed 'start_end' hook with foo!"

View File

@ -98,7 +98,7 @@ describe Guard do
context "with no watchers given" do context "with no watchers given" do
it "gives an empty array of watchers" do it "gives an empty array of watchers" do
@guard_rspec_class.should_receive(:new).with([], {}, nil).and_return(@guard_rspec) @guard_rspec_class.should_receive(:new).with([], {}).and_return(@guard_rspec)
Guard.add_guard(:rspec, []) Guard.add_guard(:rspec, [])
end end
@ -106,7 +106,7 @@ describe Guard do
context "with watchers given" do context "with watchers given" do
it "give the watchers array" do it "give the watchers array" do
@guard_rspec_class.should_receive(:new).with([:foo], {}, nil).and_return(@guard_rspec) @guard_rspec_class.should_receive(:new).with([:foo], {}).and_return(@guard_rspec)
Guard.add_guard(:rspec, [:foo]) Guard.add_guard(:rspec, [:foo])
end end
@ -114,7 +114,7 @@ describe Guard do
context "with no options given" do context "with no options given" do
it "gives an empty hash of options" do it "gives an empty hash of options" do
@guard_rspec_class.should_receive(:new).with([], {}, nil).and_return(@guard_rspec) @guard_rspec_class.should_receive(:new).with([], {}).and_return(@guard_rspec)
Guard.add_guard(:rspec, [], [], {}) Guard.add_guard(:rspec, [], [], {})
end end
@ -122,17 +122,9 @@ describe Guard do
context "with options given" do context "with options given" do
it "give the options hash" do it "give the options hash" do
@guard_rspec_class.should_receive(:new).with([], { :foo => true }, nil).and_return(@guard_rspec) @guard_rspec_class.should_receive(:new).with([], { :foo => true, :group => :backend }).and_return(@guard_rspec)
Guard.add_guard(:rspec, [], [], { :foo => true }) Guard.add_guard(:rspec, [], [], { :foo => true, :group => :backend })
end
end
context "with the group :backend given" do
it "initialize the guard and pass it its group" do
@guard_rspec_class.should_receive(:new).with([], {}, :backend).and_return(@guard_rspec)
Guard.add_guard(:rspec, [], [], {}, :backend)
end end
end end
end end