Shortened implementation of the new group DSL method and made it (+ specs) clearer

This commit is contained in:
Rémy Coutable 2010-12-17 18:13:31 +01:00
parent 63af219490
commit f90823ae90
4 changed files with 71 additions and 55 deletions

View File

@ -25,6 +25,7 @@ module Guard
Interactor.init_signal_traps Interactor.init_signal_traps
Dsl.evaluate_guardfile(options) Dsl.evaluate_guardfile(options)
if guards.empty? if guards.empty?
UI.error "No guards found in Guardfile, please add at least one." UI.error "No guards found in Guardfile, please add at least one."
else else

View File

@ -5,21 +5,22 @@ module Guard
class CLI < Thor class CLI < Thor
default_task :start default_task :start
desc "start", "Starts guard"
method_option :clear, :type => :boolean, :default => false, :aliases => '-c', :banner => "Auto clear shell before each change/run_all/reload" method_option :clear, :type => :boolean, :default => false, :aliases => '-c', :banner => "Auto clear shell before each change/run_all/reload"
method_option :debug, :type => :boolean, :default => false, :aliases => '-d', :banner => "Print debug messages" method_option :debug, :type => :boolean, :default => false, :aliases => '-d', :banner => "Print debug messages"
method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups" method_option :group, :type => :array, :default => [], :aliases => '-g', :banner => "Run only the passed groups"
desc "start", "Starts Guard"
def start def start
::Guard.start(options) ::Guard.start(options)
end end
desc "version", "Prints the guard's version information" desc "version", "Prints Guard's version information"
def version def version
::Guard::UI.info "Guard version #{Guard::VERSION}" ::Guard::UI.info "Guard version #{Guard::VERSION}"
end end
map %w(-v --version) => :version map %w(-v --version) => :version
desc "init [GUARD]", "Generates a Guardfile into the current working directory, or add it given guard" desc "init [GUARD]", "Generates a Guardfile into the current working directory, or insert the given GUARD"
def init(guard_name = nil) def init(guard_name = nil)
if !File.exist?("Guardfile") if !File.exist?("Guardfile")
puts "Writing new Guardfile to #{Dir.pwd}/Guardfile" puts "Writing new Guardfile to #{Dir.pwd}/Guardfile"

View File

@ -5,7 +5,7 @@ module Guard
attr_accessor :options attr_accessor :options
def evaluate_guardfile(options = {}) def evaluate_guardfile(options = {})
self.options = options @@options = options
guardfile = "#{Dir.pwd}/Guardfile" guardfile = "#{Dir.pwd}/Guardfile"
if File.exists?(guardfile) if File.exists?(guardfile)
@ -26,20 +26,19 @@ module Guard
end end
end end
def group(name, &definition) def group(name, &guard_definition)
options = self.class.options guard_definition.call if guard_definition && (@@options[:group].empty? || @@options[:group].include?(name))
definition.call if definition && (options[:group].empty? || options[:group].include?(name))
end end
def guard(name, options = {}, &definition) def guard(name, options = {}, &watch_definition)
@watchers = [] @watchers = []
definition.call if definition watch_definition.call if watch_definition
::Guard.add_guard(name, @watchers, options) ::Guard.add_guard(name, @watchers, options)
end end
def watch(pattern, &action) def watch(pattern, &action)
@watchers << ::Guard::Watcher.new(pattern, action) @watchers << ::Guard::Watcher.new(pattern, action)
end end
end end
end end

View File

@ -8,67 +8,82 @@ describe Guard::Dsl do
::Guard.stub!(:add_guard) ::Guard.stub!(:add_guard)
end end
it "write an error message when no Guardfile is found" do it "should write an error message when no Guardfile is found" do
Dir.stub!(:pwd).and_return("no_guardfile_here") Dir.stub!(:pwd).and_return("no_guardfile_here")
Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.") Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.")
lambda { subject.evaluate_guardfile }.should raise_error lambda { subject.evaluate_guardfile }.should raise_error
end end
it "write an error message when Guardfile is not valid" do it "should write an error message when Guardfile is not valid" do
mock_guardfile_content("This Guardfile is invalid!") mock_guardfile_content("This Guardfile is invalid!")
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/) Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/)
lambda { subject.evaluate_guardfile }.should raise_error lambda { subject.evaluate_guardfile }.should raise_error
end end
it "load a guard from the DSL" do describe "#group" do
mock_guardfile_content("guard 'test'") before do
mock_guardfile_content("
::Guard.should_receive(:add_guard).with('test', [], {}) group 'x' do
subject.evaluate_guardfile guard 'test' do
end watch('c')
end
it "evaluates only the specified groups" do
mock_guardfile_content("
group 'x' do
guard 'test' do
watch('c')
end end
end
group 'y' do group 'y' do
guard 'another' do guard 'another' do
watch('c') watch('c')
end end
end") end")
end
::Guard.should_receive(:add_guard).with('test', anything, {})
::Guard.should_not_receive(:add_guard).with('another', anything, {}) it "should evaluates only the specified group" do
subject.evaluate_guardfile(:group => ['x']) ::Guard.should_receive(:add_guard).with('test', anything, {})
end ::Guard.should_not_receive(:add_guard).with('another', anything, {})
subject.evaluate_guardfile(:group => ['x'])
it "receive watchers when specified" do end
mock_guardfile_content("
guard 'test' do it "should evaluates only the specified groups" do
watch('a') { 'b' } ::Guard.should_receive(:add_guard).with('test', anything, {})
watch('c') ::Guard.should_receive(:add_guard).with('another', anything, {})
end") subject.evaluate_guardfile(:group => ['x', 'y'])
::Guard.should_receive(:add_guard).with('test', anything, {}) do |name, watchers, options|
watchers.size.should == 2
watchers[0].pattern.should == 'a'
watchers[0].action.call.should == proc { 'b' }.call
watchers[1].pattern.should == 'c'
watchers[1].action.should be_nil
end end
subject.evaluate_guardfile
end end
it "receive options when specified" do describe "#guard" do
mock_guardfile_content("guard 'test', :opt_a => 1, :opt_b => 'fancy'") it "should load a guard from the DSL" do
mock_guardfile_content("guard 'test'")
::Guard.should_receive(:add_guard).with('test', [], {})
subject.evaluate_guardfile
end
::Guard.should_receive(:add_guard).with('test', anything, { :opt_a => 1, :opt_b => 'fancy' }) it "should receive options when specified" do
subject.evaluate_guardfile mock_guardfile_content("guard 'test', :opt_a => 1, :opt_b => 'fancy'")
::Guard.should_receive(:add_guard).with('test', anything, { :opt_a => 1, :opt_b => 'fancy' })
subject.evaluate_guardfile
end
end
describe "#watch" do
it "should receive watchers when specified" do
mock_guardfile_content("
guard 'test' do
watch('a') { 'b' }
watch('c')
end")
::Guard.should_receive(:add_guard).with('test', anything, {}) do |name, watchers, options|
watchers.size.should == 2
watchers[0].pattern.should == 'a'
watchers[0].action.call.should == proc { 'b' }.call
watchers[1].pattern.should == 'c'
watchers[1].action.should be_nil
end
subject.evaluate_guardfile
end
end end
private private