Added DSL specs and improved error message.

DSL now have basics specs that confirm it works
I splitted file not found and invalid Guardfile
error message because it was confusing to know
in which case you are when it occurs :)
This commit is contained in:
Olivier Amblet 2010-10-30 18:26:09 +02:00
parent 4da879e849
commit dd1100b4ae
6 changed files with 74 additions and 5 deletions

View File

@ -3,12 +3,19 @@ module Guard
def self.evaluate_guardfile def self.evaluate_guardfile
guardfile = "#{Dir.pwd}/Guardfile" guardfile = "#{Dir.pwd}/Guardfile"
if File.exists? guardfile
begin
dsl = new dsl = new
dsl.instance_eval(File.read(guardfile.to_s), guardfile.to_s, 1) dsl.instance_eval(File.read(guardfile.to_s), guardfile.to_s, 1)
rescue rescue
UI.error "Guardfile not found or invalid" UI.error "Invalid Guardfile, original error is:\n#{$!}"
exit 1 exit 1
end end
else
UI.error "No Guardfile in current folder, please create one."
exit 1
end
end
def self.guardfile_included?(guard_name) def self.guardfile_included?(guard_name)
File.read('Guardfile').include?("guard '#{guard_name}'") File.read('Guardfile').include?("guard '#{guard_name}'")

View File

@ -0,0 +1 @@
Balasfllsd fsdadsg hsla kjsdh jakh kajlsh klsajhgs dklhj

1
spec/fixtures/dsl/options/Guardfile vendored Normal file
View File

@ -0,0 +1 @@
guard 'test', :opt_a => 1, :opt_b => 'test'

1
spec/fixtures/dsl/simple/Guardfile vendored Normal file
View File

@ -0,0 +1 @@
guard 'test'

4
spec/fixtures/dsl/watchers/Guardfile vendored Normal file
View File

@ -0,0 +1,4 @@
guard 'test' do
watch('a') { 'b' }
watch('c')
end

55
spec/guard/dsl_spec.rb Normal file
View File

@ -0,0 +1,55 @@
require 'spec_helper'
require 'guard/dsl'
describe Guard::Dsl do
subject {Guard::Dsl}
it "load a guard from the DSL" do
fixture :simple
::Guard.stub!(:add_guard)
::Guard.should_receive(:add_guard).with('test', [], {})
subject.evaluate_guardfile
end
it "write an error message when no Guardfile is found" do
fixture :no_guardfile
Guard::UI.stub!(:error)
Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.")
lambda { subject.evaluate_guardfile }.should raise_error
end
it "write an error message when Guardfile is not valid" do
fixture :invalid_guardfile
Guard::UI.stub!(:error)
Guard::UI.should_receive(:error).with(/Invalid Guardfile, original error is:\n/)
lambda { subject.evaluate_guardfile }.should raise_error
end
it "receive watchers when specified" do
fixture :watchers
::Guard.stub!(:add_guard)
::Guard.should_receive(:add_guard).with('test', anything(), {}) do |name, watchers, options|
watchers.size.should eql 2
end
subject.evaluate_guardfile
end
it "receive options when specified" do
fixture :options
::Guard.stub!(:add_guard)
::Guard.should_receive(:add_guard).with('test', anything(), hash_including(:opt_a, :opt_b))
subject.evaluate_guardfile
end
private
def fixture name
## Hack to make guard look into the correct fixture folder
Dir.stub!(:pwd).and_return("#{@fixture_path}/dsl/#{name}")
Dir.pwd.should == "#{@fixture_path}/dsl/#{name}"
end
end