From f90823ae90bf9c06d82791002311340828e9698c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81my=20Coutable?= Date: Fri, 17 Dec 2010 18:13:31 +0100 Subject: [PATCH] Shortened implementation of the new group DSL method and made it (+ specs) clearer --- lib/guard.rb | 1 + lib/guard/cli.rb | 7 +-- lib/guard/dsl.rb | 15 +++--- spec/guard/dsl_spec.rb | 103 +++++++++++++++++++++++------------------ 4 files changed, 71 insertions(+), 55 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index d71b6f4..5c6460f 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -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 diff --git a/lib/guard/cli.rb b/lib/guard/cli.rb index 7f19862..2103a45 100644 --- a/lib/guard/cli.rb +++ b/lib/guard/cli.rb @@ -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" diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index c806966..804f8e1 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -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 diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index 1fb5fe4..2e45cfa 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -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