2010-10-30 16:26:09 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Guard::Dsl do
|
2011-05-27 15:56:18 +00:00
|
|
|
subject { described_class }
|
2010-10-30 20:42:17 +00:00
|
|
|
before(:each) do
|
2011-05-28 15:52:50 +00:00
|
|
|
@local_guardfile_path = File.join(Dir.pwd, 'Guardfile')
|
2011-06-21 18:43:30 +00:00
|
|
|
@home_guardfile_path = File.expand_path(File.join("~", ".Guardfile"))
|
2011-05-27 15:56:18 +00:00
|
|
|
::Guard.stub!(:options).and_return(:debug => true)
|
2011-07-02 08:01:45 +00:00
|
|
|
::Guard.stub!(:guards).and_return([mock('Guard')])
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
describe "it should select the correct data source for Guardfile" do
|
2011-07-02 08:01:45 +00:00
|
|
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
it "should use a string for initializing" do
|
|
|
|
Guard::UI.should_not_receive(:error)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
|
|
|
|
subject.guardfile_contents.should == valid_guardfile_string
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
2011-07-29 07:05:40 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
it "should use a -command file over the default loc" do
|
2011-05-27 15:56:18 +00:00
|
|
|
fake_guardfile('/abc/Guardfile', "guard :foo")
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
Guard::UI.should_not_receive(:error)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
|
|
|
|
subject.guardfile_contents.should == "guard :foo"
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
it "should use a default file if no other options are given" do
|
2011-05-28 15:52:50 +00:00
|
|
|
fake_guardfile(@local_guardfile_path, "guard :bar")
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
Guard::UI.should_not_receive(:error)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile }.should_not raise_error
|
|
|
|
subject.guardfile_contents.should == "guard :bar"
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
it "should use a string over any other method" do
|
|
|
|
fake_guardfile('/abc/Guardfile', "guard :foo")
|
2011-05-28 15:52:50 +00:00
|
|
|
fake_guardfile(@local_guardfile_path, "guard :bar")
|
2011-05-05 09:05:58 +00:00
|
|
|
|
|
|
|
Guard::UI.should_not_receive(:error)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
|
|
|
|
subject.guardfile_contents.should == valid_guardfile_string
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
it "should use the given Guardfile over default Guardfile" do
|
|
|
|
fake_guardfile('/abc/Guardfile', "guard :foo")
|
2011-05-28 15:52:50 +00:00
|
|
|
fake_guardfile(@local_guardfile_path, "guard :bar")
|
2011-05-05 09:05:58 +00:00
|
|
|
|
|
|
|
Guard::UI.should_not_receive(:error)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
|
|
|
|
subject.guardfile_contents.should == "guard :foo"
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
2010-10-30 16:26:09 +00:00
|
|
|
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
|
2011-05-28 15:52:50 +00:00
|
|
|
subject.stub(:guardfile_default_path).and_return("no_guardfile_here")
|
|
|
|
Guard::UI.should_receive(:error).with("No Guardfile found, please create one with `guard init`.")
|
2010-10-30 16:26:09 +00:00
|
|
|
lambda { subject.evaluate_guardfile }.should raise_error
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-07-02 08:01:45 +00:00
|
|
|
it "displays an error message when no guard are defined in Guardfile" do
|
|
|
|
::Guard::Dsl.stub!(:instance_eval_guardfile)
|
|
|
|
::Guard.stub!(:guards).and_return([])
|
|
|
|
Guard::UI.should_receive(:error)
|
|
|
|
subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string)
|
|
|
|
end
|
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
describe "it should correctly read data from its valid data source" do
|
2011-07-02 08:01:45 +00:00
|
|
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
it "should read correctly from a string" do
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error
|
2011-05-05 09:05:58 +00:00
|
|
|
subject.guardfile_contents.should == valid_guardfile_string
|
2010-12-17 17:37:24 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
it "should read correctly from a Guardfile" do
|
|
|
|
fake_guardfile('/abc/Guardfile', "guard :foo" )
|
2011-05-05 09:05:58 +00:00
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error
|
|
|
|
subject.guardfile_contents.should == "guard :foo"
|
2011-04-16 21:13:29 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
it "should read correctly from a Guardfile" do
|
|
|
|
fake_guardfile(File.join(Dir.pwd, 'Guardfile'), valid_guardfile_string)
|
|
|
|
|
|
|
|
lambda { subject.evaluate_guardfile }.should_not raise_error
|
2011-05-05 09:05:58 +00:00
|
|
|
subject.guardfile_contents.should == valid_guardfile_string
|
2010-12-17 17:37:24 +00:00
|
|
|
end
|
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
describe "It should correctly throw errors when initializing with invalid data" do
|
2011-07-02 08:01:45 +00:00
|
|
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
it "should raise error when there's a problem reading a file" do
|
|
|
|
File.stub!(:exist?).with('/def/Guardfile') { true }
|
2011-05-27 15:56:18 +00:00
|
|
|
File.stub!(:read).with('/def/Guardfile') { raise Errno::EACCES.new("permission error") }
|
2011-05-05 09:05:58 +00:00
|
|
|
|
|
|
|
Guard::UI.should_receive(:error).with(/^Error reading file/)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile => '/def/Guardfile') }.should raise_error
|
|
|
|
end
|
2011-05-05 09:05:58 +00:00
|
|
|
|
|
|
|
it "should raise error when -guardfile doesn't exist" do
|
|
|
|
File.stub!(:exist?).with('/def/Guardfile') { false }
|
|
|
|
|
|
|
|
Guard::UI.should_receive(:error).with(/No Guardfile exists at/)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile => '/def/Guardfile') }.should raise_error
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise error when resorting to use default, finds no default" do
|
2011-05-28 15:52:50 +00:00
|
|
|
File.stub!(:exist?).with(@local_guardfile_path) { false }
|
|
|
|
File.stub!(:exist?).with(@home_guardfile_path) { false }
|
2011-05-05 09:05:58 +00:00
|
|
|
|
2011-05-28 15:52:50 +00:00
|
|
|
Guard::UI.should_receive(:error).with("No Guardfile found, please create one with `guard init`.")
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile }.should raise_error
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should raise error when guardfile_content ends up empty or nil" do
|
2011-07-29 07:05:40 +00:00
|
|
|
Guard::UI.should_receive(:error).with(/The command file/)
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => "") }.should raise_error
|
2011-07-29 07:05:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not raise error when guardfile_content is nil (skipped)" do
|
|
|
|
Guard::UI.should_not_receive(:error)
|
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => nil) }.should_not raise_error
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
|
|
|
end
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
it "displays an error message when Guardfile is not valid" do
|
|
|
|
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:/)
|
2011-05-27 15:56:18 +00:00
|
|
|
|
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string ) }.should raise_error
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-07-28 22:22:53 +00:00
|
|
|
describe ".reevaluate_guardfile" do
|
2011-07-02 08:01:45 +00:00
|
|
|
before(:each) { ::Guard::Dsl.stub!(:instance_eval_guardfile) }
|
|
|
|
|
|
|
|
it "resets already definded guards before calling evaluate_guardfile" do
|
2011-07-15 06:33:13 +00:00
|
|
|
Guard::Notifier.turn_off
|
2011-07-02 08:01:45 +00:00
|
|
|
subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string)
|
|
|
|
::Guard.guards.should_not be_empty
|
|
|
|
::Guard::Dsl.should_receive(:evaluate_guardfile)
|
2011-07-28 22:22:53 +00:00
|
|
|
subject.reevaluate_guardfile
|
2011-07-02 08:01:45 +00:00
|
|
|
::Guard.guards.should be_empty
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-28 15:52:50 +00:00
|
|
|
describe ".guardfile_default_path" do
|
2011-05-24 21:32:02 +00:00
|
|
|
let(:local_path) { File.join(Dir.pwd, 'Guardfile') }
|
2011-06-21 18:43:30 +00:00
|
|
|
let(:user_path) { File.expand_path(File.join("~", '.Guardfile')) }
|
2011-07-02 08:01:45 +00:00
|
|
|
before(:each) { File.stub(:exist? => false) }
|
2011-05-25 18:23:02 +00:00
|
|
|
|
2011-05-24 21:32:02 +00:00
|
|
|
context "when there is a local Guardfile" do
|
|
|
|
it "returns the path to the local Guardfile" do
|
|
|
|
File.stub(:exist?).with(local_path).and_return(true)
|
2011-05-28 15:52:50 +00:00
|
|
|
subject.guardfile_default_path.should == local_path
|
2011-05-24 21:32:02 +00:00
|
|
|
end
|
|
|
|
end
|
2011-05-25 18:23:02 +00:00
|
|
|
|
2011-05-24 21:32:02 +00:00
|
|
|
context "when there is a Guardfile in the user's home directory" do
|
|
|
|
it "returns the path to the user Guardfile" do
|
|
|
|
File.stub(:exist?).with(user_path).and_return(true)
|
2011-05-28 15:52:50 +00:00
|
|
|
subject.guardfile_default_path.should == user_path
|
2011-05-24 21:32:02 +00:00
|
|
|
end
|
|
|
|
end
|
2011-05-25 18:23:02 +00:00
|
|
|
|
2011-05-24 21:32:02 +00:00
|
|
|
context "when there's both a local and user Guardfile" do
|
|
|
|
it "returns the path to the local Guardfile" do
|
|
|
|
File.stub(:exist?).with(local_path).and_return(true)
|
|
|
|
File.stub(:exist?).with(user_path).and_return(true)
|
2011-05-28 15:52:50 +00:00
|
|
|
subject.guardfile_default_path.should == local_path
|
2011-05-24 21:32:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-17 17:37:24 +00:00
|
|
|
describe ".guardfile_include?" do
|
2011-05-05 09:05:58 +00:00
|
|
|
it "detects a guard specified by a string with double quotes" do
|
2011-05-27 15:56:18 +00:00
|
|
|
subject.stub(:guardfile_contents => 'guard "test" {watch("c")}')
|
|
|
|
|
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-05-05 09:05:58 +00:00
|
|
|
it "detects a guard specified by a string with single quote" do
|
2011-05-27 15:56:18 +00:00
|
|
|
subject.stub(:guardfile_contents => 'guard \'test\' {watch("c")}')
|
|
|
|
|
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-05-05 09:05:58 +00:00
|
|
|
it "detects a guard specified by a symbol" do
|
2011-05-27 15:56:18 +00:00
|
|
|
subject.stub(:guardfile_contents => 'guard :test {watch("c")}')
|
|
|
|
|
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-05-05 09:05:58 +00:00
|
|
|
it "detects a guard wrapped in parentheses" do
|
2011-05-27 15:56:18 +00:00
|
|
|
subject.stub(:guardfile_contents => 'guard(:test) {watch("c")}')
|
|
|
|
|
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
|
2011-06-01 19:00:01 +00:00
|
|
|
it "should evaluates only the specified string group" do
|
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, {})
|
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['w']) }.should_not raise_error
|
|
|
|
end
|
|
|
|
it "should evaluates only the specified symbol group" do
|
2010-12-17 17:13:31 +00:00
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, {})
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['x']) }.should_not raise_error
|
2010-12-17 17:13:31 +00:00
|
|
|
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, {})
|
2011-05-27 15:56:18 +00:00
|
|
|
lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string, :group => ['x','y']) }.should_not raise_error
|
2010-12-17 17:13:31 +00:00
|
|
|
end
|
2010-12-17 15:31:39 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
# TODO: not sure if each seperate quoting/call type needs its own test
|
2010-12-17 17:13:31 +00:00
|
|
|
describe "#guard" do
|
2011-05-27 15:56:18 +00:00
|
|
|
it "should load a guard specified as a quoted string from the DSL" do
|
2010-12-17 17:13:31 +00:00
|
|
|
::Guard.should_receive(:add_guard).with('test', [], {})
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
subject.evaluate_guardfile(:guardfile_contents => "guard 'test'")
|
2010-12-17 17:13:31 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:37:24 +00:00
|
|
|
it "should load a guard specified as a symbol from the DSL" do
|
|
|
|
::Guard.should_receive(:add_guard).with(:test, [], {})
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
subject.evaluate_guardfile(:guardfile_contents => "guard :test")
|
2010-12-17 17:37:24 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
it "should load a guard specified as a symbol and called with parens from the DSL" do
|
|
|
|
::Guard.should_receive(:add_guard).with(:test, [], {})
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
subject.evaluate_guardfile(:guardfile_contents => "guard(:test)")
|
2010-12-17 17:37:24 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
it "should receive options when specified" do
|
|
|
|
::Guard.should_receive(:add_guard).with('test', anything, { :opt_a => 1, :opt_b => 'fancy' })
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
subject.evaluate_guardfile(:guardfile_contents => "guard 'test', :opt_a => 1, :opt_b => 'fancy'")
|
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
|
|
|
|
it "should receive watchers when specified" do
|
2011-05-27 15:56:18 +00:00
|
|
|
guardfile_with_watchers = "guard 'test' do
|
|
|
|
watch('a') { 'b' }
|
|
|
|
watch('c')
|
|
|
|
end"
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
::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'
|
2011-05-05 09:05:58 +00:00
|
|
|
watchers[1].action.should == nil
|
2010-12-17 17:13:31 +00:00
|
|
|
end
|
2011-05-27 15:56:18 +00:00
|
|
|
subject.evaluate_guardfile(:guardfile_contents => guardfile_with_watchers)
|
2010-12-17 17:13:31 +00:00
|
|
|
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
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
def fake_guardfile(name, contents)
|
|
|
|
File.stub!(:exist?).with(name) { true }
|
2011-05-27 15:56:18 +00:00
|
|
|
File.stub!(:read).with(name) { contents }
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
def valid_guardfile_string
|
2011-06-01 19:00:01 +00:00
|
|
|
"group 'w' do
|
|
|
|
guard 'test' do
|
|
|
|
watch('c')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
group :x do
|
2011-05-05 09:05:58 +00:00
|
|
|
guard 'test' do
|
|
|
|
watch('c')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
group 'y' do
|
|
|
|
guard 'another' do
|
|
|
|
watch('c')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
group 'z' do
|
|
|
|
guard 'another' do
|
|
|
|
watch('c')
|
|
|
|
end
|
|
|
|
end"
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2010-10-30 20:42:17 +00:00
|
|
|
def mock_guardfile_content(content)
|
2011-05-28 15:52:50 +00:00
|
|
|
File.stub!(:read).with(subject.guardfile_default_path) { content }
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-04-10 20:32:29 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
def invalid_guardfile_string
|
2011-05-27 15:56:18 +00:00
|
|
|
"Bad Guardfile"
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|