From dd1100b4ae421c31c58149a20e38d50beda0c1ab Mon Sep 17 00:00:00 2001 From: Olivier Amblet Date: Sat, 30 Oct 2010 18:26:09 +0200 Subject: [PATCH] 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 :) --- lib/guard/dsl.rb | 17 ++++-- spec/fixtures/dsl/invalid_guardfile/Guardfile | 1 + spec/fixtures/dsl/options/Guardfile | 1 + spec/fixtures/dsl/simple/Guardfile | 1 + spec/fixtures/dsl/watchers/Guardfile | 4 ++ spec/guard/dsl_spec.rb | 55 +++++++++++++++++++ 6 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 spec/fixtures/dsl/invalid_guardfile/Guardfile create mode 100644 spec/fixtures/dsl/options/Guardfile create mode 100644 spec/fixtures/dsl/simple/Guardfile create mode 100644 spec/fixtures/dsl/watchers/Guardfile create mode 100644 spec/guard/dsl_spec.rb diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index c9ebe5c..35ae282 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -3,11 +3,18 @@ module Guard 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) diff --git a/spec/fixtures/dsl/invalid_guardfile/Guardfile b/spec/fixtures/dsl/invalid_guardfile/Guardfile new file mode 100644 index 0000000..954a2fe --- /dev/null +++ b/spec/fixtures/dsl/invalid_guardfile/Guardfile @@ -0,0 +1 @@ +Balasfllsd fsdadsg hsla kjsdh jakh kajlsh klsajhgs dklhj \ No newline at end of file diff --git a/spec/fixtures/dsl/options/Guardfile b/spec/fixtures/dsl/options/Guardfile new file mode 100644 index 0000000..d87a0f6 --- /dev/null +++ b/spec/fixtures/dsl/options/Guardfile @@ -0,0 +1 @@ +guard 'test', :opt_a => 1, :opt_b => 'test' \ No newline at end of file diff --git a/spec/fixtures/dsl/simple/Guardfile b/spec/fixtures/dsl/simple/Guardfile new file mode 100644 index 0000000..ed2fd58 --- /dev/null +++ b/spec/fixtures/dsl/simple/Guardfile @@ -0,0 +1 @@ +guard 'test' \ No newline at end of file diff --git a/spec/fixtures/dsl/watchers/Guardfile b/spec/fixtures/dsl/watchers/Guardfile new file mode 100644 index 0000000..6127008 --- /dev/null +++ b/spec/fixtures/dsl/watchers/Guardfile @@ -0,0 +1,4 @@ +guard 'test' do + watch('a') { 'b' } + watch('c') +end \ No newline at end of file diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb new file mode 100644 index 0000000..3564f0f --- /dev/null +++ b/spec/guard/dsl_spec.rb @@ -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