2010-10-18 19:45:31 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Guard do
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
describe ".setup" do
|
|
|
|
subject { ::Guard.setup }
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "returns itself for chaining" do
|
|
|
|
subject.should be ::Guard
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "initializes the Guards" do
|
|
|
|
::Guard.guards.should be_kind_of(Array)
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "initializes the options" do
|
|
|
|
opts = { :my_opts => true }
|
|
|
|
::Guard.setup(opts).options.should include(:my_opts)
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "initializes the listener" do
|
|
|
|
::Guard.listener.should be_kind_of(Guard::Listener)
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "turns on the notifier by default" do
|
|
|
|
ENV["GUARD_NOTIFY"] = nil
|
|
|
|
::Guard::Notifier.should_receive(:turn_on)
|
|
|
|
::Guard.setup(:notify => true)
|
|
|
|
end
|
2011-05-06 21:19:31 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "turns off the notifier if the notify option is false" do
|
|
|
|
::Guard::Notifier.should_receive(:turn_off)
|
|
|
|
::Guard.setup(:notify => false)
|
|
|
|
end
|
2011-05-06 21:19:31 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "turns off the notifier if environment variable GUARD_NOTIFY is false" do
|
|
|
|
ENV["GUARD_NOTIFY"] = 'false'
|
|
|
|
::Guard::Notifier.should_receive(:turn_off)
|
|
|
|
::Guard.setup(:notify => true)
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
2011-05-13 09:26:05 +00:00
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
describe ".get_guard_class" do
|
|
|
|
it "reports an error if the class is not found" do
|
|
|
|
::Guard::UI.should_receive(:error)
|
|
|
|
Guard.get_guard_class('notAGuardClass')
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
context 'with a nested Guard class' do
|
2011-05-27 15:56:46 +00:00
|
|
|
it "returns the Guard class when passed a Symbol" do
|
|
|
|
Guard.should_receive(:try_to_load_gem) { |classname|
|
|
|
|
classname.should == :classname
|
|
|
|
class Guard::Classname
|
|
|
|
end
|
|
|
|
}
|
|
|
|
Guard.get_guard_class(:classname).should == Guard::Classname
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the Guard class when passed a String" do
|
2011-05-13 09:26:05 +00:00
|
|
|
Guard.should_receive(:try_to_load_gem) { |classname|
|
|
|
|
classname.should == 'classname'
|
|
|
|
class Guard::Classname
|
|
|
|
end
|
|
|
|
}
|
|
|
|
Guard.get_guard_class('classname').should == Guard::Classname
|
2011-04-21 21:39:46 +00:00
|
|
|
end
|
2011-05-13 09:26:05 +00:00
|
|
|
end
|
2011-04-21 21:39:46 +00:00
|
|
|
|
2011-05-23 23:07:12 +00:00
|
|
|
context 'with a name with dashes' do
|
|
|
|
it "returns the Guard class" do
|
|
|
|
Guard.should_receive(:try_to_load_gem) { |classname|
|
|
|
|
classname.should == 'dashed-class-name'
|
|
|
|
class Guard::DashedClassName
|
|
|
|
end
|
|
|
|
}
|
|
|
|
Guard.get_guard_class('dashed-class-name').should == Guard::DashedClassName
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
context 'with an inline Guard class' do
|
|
|
|
it 'returns the Guard class' do
|
|
|
|
module Guard
|
|
|
|
class Inline < Guard
|
2011-04-21 21:39:46 +00:00
|
|
|
end
|
2011-02-18 18:53:05 +00:00
|
|
|
end
|
2011-05-13 09:26:05 +00:00
|
|
|
|
|
|
|
Guard.get_guard_class('inline').should == Guard::Inline
|
2011-02-18 18:53:05 +00:00
|
|
|
end
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
2011-05-13 09:26:05 +00:00
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
describe ".locate_guard" do
|
|
|
|
it "returns the path of a Guard gem" do
|
|
|
|
if Gem::Version.create(Gem::VERSION) >= Gem::Version.create('1.8.0')
|
|
|
|
gem_location = Gem::Specification.find_by_name("guard-rspec").full_gem_path
|
|
|
|
else
|
|
|
|
gem_location = Gem.source_index.find_name("guard-rspec").last.full_gem_path
|
2010-11-03 22:31:00 +00:00
|
|
|
end
|
2011-05-13 09:26:05 +00:00
|
|
|
|
|
|
|
Guard.locate_guard('rspec').should == gem_location
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
2011-05-13 09:26:05 +00:00
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
describe ".supervised_task" do
|
|
|
|
subject { ::Guard.setup }
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@g = mock(Guard::Guard).as_null_object
|
|
|
|
subject.guards.push(@g)
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
context "with a task that succeed" do
|
|
|
|
context 'without any arguments' do
|
2010-11-30 20:23:53 +00:00
|
|
|
before(:each) do
|
|
|
|
@g.stub!(:regular) { true }
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "doesn't fire the Guard" do
|
2010-11-30 20:23:53 +00:00
|
|
|
lambda { subject.supervised_task(@g, :regular) }.should_not change(subject.guards, :size)
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "returns the result of the task" do
|
2010-11-30 20:23:53 +00:00
|
|
|
::Guard.supervised_task(@g, :regular).should be_true
|
|
|
|
end
|
2010-11-03 22:31:00 +00:00
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
context 'with arguments' do
|
|
|
|
before(:each) do
|
|
|
|
@g.stub!(:regular_with_arg).with("given_path") { "I'm a success" }
|
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "doesn't fire the Guard" do
|
|
|
|
lambda { subject.supervised_task(@g, :regular_with_arg, "given_path") }.should_not change(subject.guards, :size)
|
2010-11-30 20:23:53 +00:00
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "returns the result of the task" do
|
|
|
|
::Guard.supervised_task(@g, :regular_with_arg, "given_path").should == "I'm a success"
|
2010-11-30 20:23:53 +00:00
|
|
|
end
|
2010-11-03 22:31:00 +00:00
|
|
|
end
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
2011-04-16 21:13:29 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
context "with a task that raises an exception" do
|
|
|
|
before(:each) { @g.stub!(:failing) { raise "I break your system" } }
|
|
|
|
|
|
|
|
it "fires the Guard" do
|
|
|
|
lambda { subject.supervised_task(@g, :failing) }.should change(subject.guards, :size).by(-1)
|
|
|
|
subject.guards.should_not include(@g)
|
|
|
|
end
|
2011-05-06 19:51:50 +00:00
|
|
|
|
2011-05-13 09:26:05 +00:00
|
|
|
it "returns the exception" do
|
|
|
|
failing_result = ::Guard.supervised_task(@g, :failing)
|
|
|
|
failing_result.should be_kind_of(Exception)
|
|
|
|
failing_result.message.should == 'I break your system'
|
2011-04-16 21:13:29 +00:00
|
|
|
end
|
|
|
|
end
|
2010-10-27 13:18:00 +00:00
|
|
|
end
|
2011-02-22 14:15:09 +00:00
|
|
|
|
2011-02-18 18:53:05 +00:00
|
|
|
end
|