Skip to content

Commit

Permalink
Shortened implementation of the new group DSL method and made it (+ s…
Browse files Browse the repository at this point in the history
…pecs) clearer
  • Loading branch information
Rémy Coutable committed Dec 17, 2010
1 parent 63af219 commit f90823a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 54 deletions.
1 change: 1 addition & 0 deletions lib/guard.rb
Expand Up @@ -25,6 +25,7 @@ def start(options = {})

Interactor.init_signal_traps
Dsl.evaluate_guardfile(options)

if guards.empty?
UI.error "No guards found in Guardfile, please add at least one."
else
Expand Down
7 changes: 4 additions & 3 deletions lib/guard/cli.rb
Expand Up @@ -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"
Expand Down
15 changes: 7 additions & 8 deletions lib/guard/dsl.rb
Expand Up @@ -5,7 +5,7 @@ class << self
attr_accessor :options

def evaluate_guardfile(options = {})
self.options = options
@@options = options

guardfile = "#{Dir.pwd}/Guardfile"
if File.exists?(guardfile)
Expand All @@ -26,20 +26,19 @@ def guardfile_included?(guard_name)
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
101 changes: 58 additions & 43 deletions spec/guard/dsl_spec.rb
Expand Up @@ -8,67 +8,82 @@
::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'])
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
end

it "receive watchers when specified" do
mock_guardfile_content("
guard 'test' do
watch('a') { 'b' }
watch('c')
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, {}) 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
::Guard.should_receive(:add_guard).with('test', anything, { :opt_a => 1, :opt_b => 'fancy' })
subject.evaluate_guardfile
end
subject.evaluate_guardfile
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
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
Expand Down

0 comments on commit f90823a

Please sign in to comment.