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,14 +26,13 @@ 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

View File

@ -8,46 +8,67 @@ 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
::Guard.should_receive(:add_guard).with('test', [], {})
subject.evaluate_guardfile
end
it "evaluates only the specified groups" do
mock_guardfile_content(" mock_guardfile_content("
group 'x' do group 'x' do
guard 'test' do guard 'test' do
watch('c') watch('c')
end end
end end
group 'y' do group 'y' do
guard 'another' do guard 'another' do
watch('c') watch('c')
end end
end") end")
end
it "should evaluates only the specified group" do
::Guard.should_receive(:add_guard).with('test', anything, {}) ::Guard.should_receive(:add_guard).with('test', anything, {})
::Guard.should_not_receive(:add_guard).with('another', anything, {}) ::Guard.should_not_receive(:add_guard).with('another', anything, {})
subject.evaluate_guardfile(:group => ['x']) subject.evaluate_guardfile(:group => ['x'])
end end
it "receive watchers when specified" do it "should evaluates only the specified groups" do
::Guard.should_receive(:add_guard).with('test', anything, {})
::Guard.should_receive(:add_guard).with('another', anything, {})
subject.evaluate_guardfile(:group => ['x', 'y'])
end
end
describe "#guard" do
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
it "should receive options when specified" do
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(" mock_guardfile_content("
guard 'test' do guard 'test' do
watch('a') { 'b' } watch('a') { 'b' }
@ -63,12 +84,6 @@ describe Guard::Dsl do
end end
subject.evaluate_guardfile subject.evaluate_guardfile
end end
it "receive options when specified" do
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
private private