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
Dsl.evaluate_guardfile(options)
if guards.empty?
UI.error "No guards found in Guardfile, please add at least one."
else

View File

@ -5,21 +5,22 @@ module Guard
class CLI < Thor
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 :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"
desc "start", "Starts Guard"
def start
::Guard.start(options)
end
desc "version", "Prints the guard's version information"
desc "version", "Prints Guard's version information"
def version
::Guard::UI.info "Guard version #{Guard::VERSION}"
end
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)
if !File.exist?("Guardfile")
puts "Writing new Guardfile to #{Dir.pwd}/Guardfile"

View File

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

View File

@ -8,67 +8,82 @@ describe Guard::Dsl do
::Guard.stub!(:add_guard)
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")
Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.")
lambda { subject.evaluate_guardfile }.should raise_error
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!")
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/)
lambda { subject.evaluate_guardfile }.should raise_error
end
it "load a guard from the DSL" do
mock_guardfile_content("guard 'test'")
::Guard.should_receive(:add_guard).with('test', [], {})
subject.evaluate_guardfile
end
it "evaluates only the specified groups" do
mock_guardfile_content("
group 'x' do
guard 'test' do
watch('c')
describe "#group" do
before do
mock_guardfile_content("
group 'x' do
guard 'test' do
watch('c')
end
end
end
group 'y' do
guard 'another' do
watch('c')
end
end")
::Guard.should_receive(:add_guard).with('test', anything, {})
::Guard.should_not_receive(:add_guard).with('another', anything, {})
subject.evaluate_guardfile(:group => ['x'])
end
it "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
group 'y' do
guard 'another' do
watch('c')
end
end")
end
it "should evaluates only the specified group" do
::Guard.should_receive(:add_guard).with('test', anything, {})
::Guard.should_not_receive(:add_guard).with('another', anything, {})
subject.evaluate_guardfile(:group => ['x'])
end
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
subject.evaluate_guardfile
end
it "receive options when specified" do
mock_guardfile_content("guard 'test', :opt_a => 1, :opt_b => 'fancy'")
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
::Guard.should_receive(:add_guard).with('test', anything, { :opt_a => 1, :opt_b => 'fancy' })
subject.evaluate_guardfile
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("
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
private