From dd864021090a3e779cb12ed37cce60f94c153bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Re=CC=81my=20Coutable?= Date: Wed, 17 Aug 2011 00:39:45 +0200 Subject: [PATCH] Add @groups attributes to keep track of the groups, ordered as in the Guardfile --- lib/guard.rb | 19 +++++---- spec/guard_spec.rb | 100 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 10 deletions(-) diff --git a/lib/guard.rb b/lib/guard.rb index 72a8518..4fc8407 100644 --- a/lib/guard.rb +++ b/lib/guard.rb @@ -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 diff --git a/spec/guard_spec.rb b/spec/guard_spec.rb index d85b8fb..461c157 100644 --- a/spec/guard_spec.rb +++ b/spec/guard_spec.rb @@ -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|