From debdecbbd726eb7f6d24d9147a1933efb97a4d5c Mon Sep 17 00:00:00 2001 From: Joshua Davey and Veezus Kreist Date: Tue, 24 May 2011 16:32:02 -0500 Subject: [PATCH 1/5] Allow user-based Guardfiles --- lib/guard/dsl.rb | 14 ++++++++++++-- spec/guard/dsl_spec.rb | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index fbbb2ec..e0f41b5 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -13,7 +13,7 @@ module Guard exit 1 end else - UI.error "No Guardfile in current folder, please create one." + UI.error "No Guardfile found, please create one." exit 1 end end @@ -23,7 +23,17 @@ module Guard end def guardfile_path - File.join(Dir.pwd, 'Guardfile') + return local_guardfile_path if File.exist? local_guardfile_path + home_guardfile_path + end + + private + def local_guardfile_path + File.join(Dir.pwd, "Guardfile") + end + + def home_guardfile_path + File.join(ENV['HOME'], "Guardfile") end end diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index f72ba85..140bd52 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -8,9 +8,8 @@ describe Guard::Dsl do end it "displays an error message when no Guardfile is found" do - Dir.stub!(:pwd).and_return("no_guardfile_here") - - Guard::UI.should_receive(:error).with("No Guardfile in current folder, please create one.") + subject.stub(:guardfile_path).and_return("no_guardfile_here") + Guard::UI.should_receive(:error).with("No Guardfile found, please create one.") lambda { subject.evaluate_guardfile }.should raise_error end @@ -21,6 +20,33 @@ describe Guard::Dsl do lambda { subject.evaluate_guardfile }.should raise_error end + describe ".guardfile_path" do + let(:local_path) { File.join(Dir.pwd, 'Guardfile') } + let(:user_path) { File.join(ENV["HOME"], 'Guardfile') } + before do + File.stub(:exist? => false) + end + 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) + subject.guardfile_path.should == local_path + end + end + 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) + subject.guardfile_path.should == user_path + end + end + 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) + subject.guardfile_path.should == local_path + end + end + end + describe ".guardfile_include?" do it "detects a Guard specified by a string with simple quotes" do mock_guardfile_content("guard 'test'") @@ -117,7 +143,7 @@ describe Guard::Dsl do private def mock_guardfile_content(content) - File.stub!(:read).with(File.expand_path('../../../Guardfile', __FILE__)) { content } + File.stub!(:read).with(subject.guardfile_path) { content } end end From add80d2831ac42d145d3d1e4cd4d701fbaf19ddf Mon Sep 17 00:00:00 2001 From: Aaron Kalin and Veezus Kreist Date: Wed, 25 May 2011 13:23:02 -0500 Subject: [PATCH 2/5] Conform to project standards --- lib/guard/dsl.rb | 5 ++--- spec/guard/dsl_spec.rb | 5 +++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index e0f41b5..9e993bd 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -23,8 +23,7 @@ module Guard end def guardfile_path - return local_guardfile_path if File.exist? local_guardfile_path - home_guardfile_path + File.exist?(local_guardfile_path) ? local_guardfile_path : home_guardfile_path end private @@ -33,7 +32,7 @@ module Guard end def home_guardfile_path - File.join(ENV['HOME'], "Guardfile") + File.expand_path(File.join("~", "Guardfile")) end end diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index 140bd52..b15ac84 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -23,21 +23,25 @@ describe Guard::Dsl do describe ".guardfile_path" do let(:local_path) { File.join(Dir.pwd, 'Guardfile') } let(:user_path) { File.join(ENV["HOME"], 'Guardfile') } + before do File.stub(:exist? => false) end + 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) subject.guardfile_path.should == local_path end end + 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) subject.guardfile_path.should == user_path end end + 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) @@ -45,6 +49,7 @@ describe Guard::Dsl do subject.guardfile_path.should == local_path end end + end describe ".guardfile_include?" do From b69aa7677ce100b3efdf423e08248be395af53ed Mon Sep 17 00:00:00 2001 From: Aaron Kalin and Veezus Kreist Date: Wed, 25 May 2011 13:23:08 -0500 Subject: [PATCH 3/5] Update README regarding Guardfile location --- README.markdown | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index a07aa6f..94e47da 100644 --- a/README.markdown +++ b/README.markdown @@ -36,6 +36,8 @@ Generate an empty Guardfile with: $ guard init ``` +You may optionally place this Guardfile in your home directory to use it across multiple projects. + Add the guards you need to your Guardfile (see the existing guards below). ### On Mac OS X @@ -101,6 +103,8 @@ or if you use Bundler, to run the Guard executable specific to your bundle: $ bundle exec guard ``` +Guard will look for a Guardfile in your current directory. If it does not find one, it will look in your home directory for one. + Command line options -------------------- @@ -310,4 +314,4 @@ Author Contributors ------ -https://github.com/guard/guard/contributors \ No newline at end of file +https://github.com/guard/guard/contributors From 9928f80c7690ca723b06fd759c13560f2d9dfb79 Mon Sep 17 00:00:00 2001 From: Aaron Kalin and Veezus Kreist Date: Wed, 25 May 2011 13:32:56 -0500 Subject: [PATCH 4/5] Fix guardfile_path spec for Windows support --- spec/guard/dsl_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index b15ac84..c46c002 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -22,7 +22,7 @@ describe Guard::Dsl do describe ".guardfile_path" do let(:local_path) { File.join(Dir.pwd, 'Guardfile') } - let(:user_path) { File.join(ENV["HOME"], 'Guardfile') } + let(:user_path) { File.expand_path(File.join("~", 'Guardfile')) } before do File.stub(:exist? => false) From a51afdf0e373fe9fd8cab1db31c72b1ee7ecbb69 Mon Sep 17 00:00:00 2001 From: Aaron Kalin and Veezus Kreist Date: Wed, 25 May 2011 14:08:11 -0500 Subject: [PATCH 5/5] Outdent private statement --- lib/guard/dsl.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index 9e993bd..f482eae 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -26,7 +26,8 @@ module Guard File.exist?(local_guardfile_path) ? local_guardfile_path : home_guardfile_path end - private + private + def local_guardfile_path File.join(Dir.pwd, "Guardfile") end