Permalink
Browse files

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 :)
  • Loading branch information...
1 parent 4da879e commit dd1100b4ae421c31c58149a20e38d50beda0c1ab @oliamb oliamb committed Oct 30, 2010
View
@@ -3,11 +3,18 @@ class Dsl
def self.evaluate_guardfile
guardfile = "#{Dir.pwd}/Guardfile"
- dsl = new
- dsl.instance_eval(File.read(guardfile.to_s), guardfile.to_s, 1)
- rescue
- UI.error "Guardfile not found or invalid"
- exit 1
+ if File.exists? guardfile
+ begin
+ dsl = new
+ dsl.instance_eval(File.read(guardfile.to_s), guardfile.to_s, 1)
+ rescue
+ UI.error "Invalid Guardfile, original error is:\n#{$!}"
+ exit 1
+ end
+ else
+ UI.error "No Guardfile in current folder, please create one."
+ exit 1
+ end
end
def self.guardfile_included?(guard_name)
@@ -0,0 +1 @@
+Balasfllsd fsdadsg hsla kjsdh jakh kajlsh klsajhgs dklhj
@@ -0,0 +1 @@
+guard 'test', :opt_a => 1, :opt_b => 'test'
@@ -0,0 +1 @@
+guard 'test'
@@ -0,0 +1,4 @@
+guard 'test' do
+ watch('a') { 'b' }
+ watch('c')
+end
View
@@ -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

0 comments on commit dd1100b

Please sign in to comment.