2010-10-30 16:26:09 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Guard::Dsl do
|
2010-10-30 20:42:17 +00:00
|
|
|
subject { Guard::Dsl }
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-30 20:42:17 +00:00
|
|
|
before(:each) do
|
2010-10-30 16:26:09 +00:00
|
|
|
::Guard.stub!(:add_guard)
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:13:29 +00:00
|
|
|
it "displays an error message when no Guardfile is found" do
|
2010-10-30 20:42:17 +00:00
|
|
|
Dir.stub!(:pwd).and_return("no_guardfile_here")
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-30 16:26:09 +00:00
|
|
|
Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.")
|
|
|
|
lambda { subject.evaluate_guardfile }.should raise_error
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:13:29 +00:00
|
|
|
it "displays an error message when Guardfile is not valid" do
|
2010-10-30 20:42:17 +00:00
|
|
|
mock_guardfile_content("This Guardfile is invalid!")
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-30 16:26:09 +00:00
|
|
|
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/)
|
|
|
|
lambda { subject.evaluate_guardfile }.should raise_error
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:37:24 +00:00
|
|
|
describe ".guardfile_include?" do
|
2011-04-16 21:13:29 +00:00
|
|
|
it "detects a guard specified by a string with simple quotes" do
|
2010-12-17 17:37:24 +00:00
|
|
|
mock_guardfile_content("guard 'test'")
|
2011-04-16 21:13:29 +00:00
|
|
|
subject.guardfile_include?('test').should be_true
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:13:29 +00:00
|
|
|
it "detects a guard specified by a string with double quotes" do
|
|
|
|
mock_guardfile_content('guard "test"')
|
2010-12-17 17:37:24 +00:00
|
|
|
subject.guardfile_include?('test').should be_true
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:13:29 +00:00
|
|
|
it "detects a guard specified by a symbol" do
|
2010-12-17 17:37:24 +00:00
|
|
|
mock_guardfile_content("guard :test")
|
2011-04-16 21:13:29 +00:00
|
|
|
subject.guardfile_include?('test').should be_true
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:13:29 +00:00
|
|
|
it "detects a guard wrapped in parentheses" do
|
|
|
|
mock_guardfile_content("guard(:test)")
|
2010-12-17 17:37:24 +00:00
|
|
|
subject.guardfile_include?('test').should be_true
|
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
describe "#group" do
|
|
|
|
before do
|
|
|
|
mock_guardfile_content("
|
|
|
|
group 'x' do
|
|
|
|
guard 'test' do
|
|
|
|
watch('c')
|
|
|
|
end
|
2010-12-17 15:31:39 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
group 'y' do
|
|
|
|
guard 'another' do
|
|
|
|
watch('c')
|
|
|
|
end
|
|
|
|
end")
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
it "evaluates only the specified group" do
|
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, anything, {})
|
|
|
|
::Guard.should_not_receive(:add_guard).with('another', anything, anything, {})
|
2010-12-17 17:13:31 +00:00
|
|
|
subject.evaluate_guardfile(:group => ['x'])
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
it "evaluates only the specified groups" do
|
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, anything, {})
|
|
|
|
::Guard.should_receive(:add_guard).with('another', anything, anything, {})
|
2010-12-17 17:13:31 +00:00
|
|
|
subject.evaluate_guardfile(:group => ['x', 'y'])
|
|
|
|
end
|
2010-12-17 15:31:39 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
describe "#guard" do
|
2011-04-16 21:02:13 +00:00
|
|
|
it "loads a guard specified by a string" do
|
2010-12-17 17:13:31 +00:00
|
|
|
mock_guardfile_content("guard 'test'")
|
2011-04-16 21:02:13 +00:00
|
|
|
::Guard.should_receive(:add_guard).with('test', [], [], {})
|
2010-12-17 17:13:31 +00:00
|
|
|
subject.evaluate_guardfile
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
it "loads a guard specified as a symbol from the DSL" do
|
2010-12-17 17:37:24 +00:00
|
|
|
mock_guardfile_content("guard :test")
|
2011-04-16 21:02:13 +00:00
|
|
|
::Guard.should_receive(:add_guard).with(:test, [], [], {})
|
2010-12-17 17:37:24 +00:00
|
|
|
subject.evaluate_guardfile
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
it "accepts options" do
|
2010-12-17 17:13:31 +00:00
|
|
|
mock_guardfile_content("guard 'test', :opt_a => 1, :opt_b => 'fancy'")
|
2011-04-16 21:02:13 +00:00
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, anything, { :opt_a => 1, :opt_b => 'fancy' })
|
2010-12-17 17:13:31 +00:00
|
|
|
subject.evaluate_guardfile
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
describe "#watch" do
|
2011-04-16 21:02:13 +00:00
|
|
|
it "creates watchers for the guard" do
|
2010-12-17 17:13:31 +00:00
|
|
|
mock_guardfile_content("
|
|
|
|
guard 'test' do
|
|
|
|
watch('a') { 'b' }
|
|
|
|
watch('c')
|
|
|
|
end")
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, anything, {}) do |name, watchers, callbacks, options|
|
|
|
|
watchers.should have(2).items
|
|
|
|
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
|
|
|
|
|
|
|
|
describe "#callback" do
|
|
|
|
it "creates callbacks for the guard" do
|
|
|
|
class MyCustomCallback
|
|
|
|
end
|
|
|
|
|
|
|
|
mock_guardfile_content("
|
|
|
|
guard 'test' do
|
|
|
|
callback(:start_end) { 'Guard::Test started!' }
|
|
|
|
callback(MyCustomCallback, [:start_begin, :run_all_begin])
|
|
|
|
end")
|
|
|
|
|
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, anything, {}) do |name, watchers, callbacks, options|
|
|
|
|
callbacks.should have(2).items
|
|
|
|
callbacks[0][:events].should == :start_end
|
|
|
|
callbacks[0][:listener].call.should == proc { 'Guard::Test started!' }.call
|
|
|
|
callbacks[1][:events].should == [:start_begin, :run_all_begin]
|
|
|
|
callbacks[1][:listener].should == MyCustomCallback
|
2010-12-17 17:13:31 +00:00
|
|
|
end
|
|
|
|
subject.evaluate_guardfile
|
|
|
|
end
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-30 16:26:09 +00:00
|
|
|
private
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-30 20:42:17 +00:00
|
|
|
def mock_guardfile_content(content)
|
|
|
|
File.stub!(:read).with(File.expand_path('../../../Guardfile', __FILE__)) { content }
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|