Add @groups attributes to keep track of the groups, ordered as in the Guardfile

This commit is contained in:
Rémy Coutable 2011-08-17 00:39:45 +02:00
parent 6f7ce6feb7
commit dd86402109
2 changed files with 109 additions and 10 deletions

View File

@ -9,12 +9,13 @@ module Guard
autoload :Notifier, 'guard/notifier'
class << self
attr_accessor :options, :guards, :listener
attr_accessor :options, :guards, :groups, :listener
# initialize this singleton
def setup(options = {})
@options = options
@listener = Listener.select_and_init(@options[:watchdir] ? File.expand_path(@options[:watchdir]) : Dir.pwd)
@groups = [:default]
@guards = []
@options[:notify] && ENV["GUARD_NOTIFY"] != 'false' ? Notifier.turn_on : Notifier.turn_off
@ -81,21 +82,25 @@ module Guard
listener.start
end
def add_guard(name, watchers = [], options = {})
if name.downcase == 'ego'
def add_guard(name, watchers = [], options = {}, group = nil)
if name.to_sym == :ego
UI.deprecation("Guard::Ego is now part of Guard. You can remove it from your Guardfile.")
else
guard_class = get_guard_class(name)
@guards << guard_class.new(watchers, options)
guard = get_guard_class(name).new(watchers, options, group)
@guards << guard
end
end
def add_group(name)
@groups << name.to_sym unless name.nil?
end
def get_guard_class(name)
try_require = false
const_name = name.to_s.downcase.gsub('-', '')
begin
require "guard/#{name.to_s.downcase}" if try_require
self.const_get(self.constants.find {|c| c.to_s.downcase == const_name })
require "guard/#{name.downcase}" if try_require
self.const_get(self.constants.find { |c| c.to_s.downcase == const_name })
rescue TypeError
unless try_require
try_require = true

View File

@ -10,13 +10,17 @@ describe Guard do
subject.should be ::Guard
end
it "initializes the Guards" do
::Guard.guards.should be_kind_of(Array)
it "initializes @guards" do
subject.guards.should eql []
end
it "initializes @groups" do
Guard.groups.should eql [:default]
end
it "initializes the options" do
opts = { :my_opts => true }
::Guard.setup(opts).options.should include(:my_opts)
Guard.setup(opts).options.should include(:my_opts)
end
it "initializes the listener" do
@ -62,6 +66,96 @@ describe Guard do
end
end
describe ".add_guard" do
before(:each) do
@guard_rspec_class = double('Guard::RSpec')
@guard_rspec = double('Guard::RSpec')
Guard.stub!(:get_guard_class) { @guard_rspec_class }
Guard.setup
end
it "accepts guard name as string" do
@guard_rspec_class.should_receive(:new).and_return(@guard_rspec)
Guard.add_guard('rspec')
end
it "accepts guard name as symbol" do
@guard_rspec_class.should_receive(:new).and_return(@guard_rspec)
Guard.add_guard(:rspec)
end
it "adds guard to the @guards array" do
@guard_rspec_class.should_receive(:new).and_return(@guard_rspec)
Guard.add_guard(:rspec)
Guard.guards.should eql [@guard_rspec]
end
context "with no watchers given" do
it "gives an empty array of watchers" do
@guard_rspec_class.should_receive(:new).with([], {}, nil).and_return(@guard_rspec)
Guard.add_guard(:rspec, [])
end
end
context "with watchers given" do
it "give the watchers array" do
@guard_rspec_class.should_receive(:new).with([:foo], {}, nil).and_return(@guard_rspec)
Guard.add_guard(:rspec, [:foo])
end
end
context "with no options given" do
it "gives an empty hash of options" do
@guard_rspec_class.should_receive(:new).with([], {}, nil).and_return(@guard_rspec)
Guard.add_guard(:rspec, [], {})
end
end
context "with options given" do
it "give the options hash" do
@guard_rspec_class.should_receive(:new).with([], { :foo => true }, nil).and_return(@guard_rspec)
Guard.add_guard(:rspec, [], { :foo => true })
end
end
context "with the group :backend given" do
it "initialize the guard and pass it its group" do
@guard_rspec_class.should_receive(:new).with([], {}, :backend).and_return(@guard_rspec)
Guard.add_guard(:rspec, [], {}, :backend)
end
end
end
describe ".add_group" do
before(:each) do
Guard.setup
end
it "accepts group name as string" do
Guard.add_group('backend')
Guard.groups.should eql [:default, :backend]
end
it "accepts group name as symbol" do
Guard.add_group(:backend)
Guard.groups.should eql [:default, :backend]
end
end
describe ".get_guard_class" do
after do
[:Classname, :DashedClassName, :Inline].each do |const|