diff --git a/README.markdown b/README.markdown index ca7979d..2e16e34 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 diff --git a/lib/guard/dsl.rb b/lib/guard/dsl.rb index 56f64b8..6742e33 100644 --- a/lib/guard/dsl.rb +++ b/lib/guard/dsl.rb @@ -54,7 +54,7 @@ module Guard if File.exist?(guardfile_default_path) read_guardfile(guardfile_default_path) else - UI.error "No Guardfile in current folder, please create one with `guard init`." + UI.error "No Guardfile found, please create one with `guard init`." exit 1 end end @@ -63,7 +63,7 @@ module Guard UI.error "The command file(#{@@options[:guardfile]}) seems to be empty." exit 1 end - + guardfile_contents end @@ -76,7 +76,17 @@ module Guard end def guardfile_default_path - File.join(Dir.pwd, 'Guardfile') + File.exist?(local_guardfile_path) ? local_guardfile_path : home_guardfile_path + end + + private + + def local_guardfile_path + File.join(Dir.pwd, "Guardfile") + end + + def home_guardfile_path + File.expand_path(File.join("~", "Guardfile")) end end diff --git a/spec/guard/dsl_spec.rb b/spec/guard/dsl_spec.rb index 89729a2..26ef5ef 100644 --- a/spec/guard/dsl_spec.rb +++ b/spec/guard/dsl_spec.rb @@ -3,7 +3,8 @@ require 'spec_helper' describe Guard::Dsl do subject { described_class } before(:each) do - @default_guardfile = File.join(Dir.pwd, 'Guardfile') + @local_guardfile_path = File.join(Dir.pwd, 'Guardfile') + @home_guardfile_path = File.expand_path(File.join("~", "Guardfile")) ::Guard.stub!(:options).and_return(:debug => true) end @@ -27,7 +28,7 @@ describe Guard::Dsl do end it "should use a default file if no other options are given" do - fake_guardfile(@default_guardfile, "guard :bar") + fake_guardfile(@local_guardfile_path, "guard :bar") Guard::UI.should_not_receive(:error) lambda { subject.evaluate_guardfile }.should_not raise_error @@ -36,7 +37,7 @@ describe Guard::Dsl do it "should use a string over any other method" do fake_guardfile('/abc/Guardfile', "guard :foo") - fake_guardfile(@default_guardfile, "guard :bar") + fake_guardfile(@local_guardfile_path, "guard :bar") Guard::UI.should_not_receive(:error) lambda { subject.evaluate_guardfile(:guardfile_contents => valid_guardfile_string) }.should_not raise_error @@ -45,7 +46,7 @@ describe Guard::Dsl do it "should use the given Guardfile over default Guardfile" do fake_guardfile('/abc/Guardfile', "guard :foo") - fake_guardfile(@default_guardfile, "guard :bar") + fake_guardfile(@local_guardfile_path, "guard :bar") Guard::UI.should_not_receive(:error) lambda { subject.evaluate_guardfile(:guardfile => '/abc/Guardfile') }.should_not raise_error @@ -53,6 +54,12 @@ describe Guard::Dsl do end end + it "displays an error message when no Guardfile is found" do + 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`.") + lambda { subject.evaluate_guardfile }.should raise_error + end + describe "it should correctly read data from its valid data source" do before(:each) do ::Guard::Dsl.stub!(:instance_eval_guardfile) @@ -99,9 +106,10 @@ describe Guard::Dsl do end it "should raise error when resorting to use default, finds no default" do - File.stub!(:exist?).with(@default_guardfile) { false } + File.stub!(:exist?).with(@local_guardfile_path) { false } + File.stub!(:exist?).with(@home_guardfile_path) { false } - Guard::UI.should_receive(:error).with(/No Guardfile in current folder/) + Guard::UI.should_receive(:error).with("No Guardfile found, please create one with `guard init`.") lambda { subject.evaluate_guardfile }.should raise_error end @@ -119,6 +127,38 @@ describe Guard::Dsl do lambda { subject.evaluate_guardfile(:guardfile_contents => invalid_guardfile_string ) }.should raise_error end + describe ".guardfile_default_path" do + let(:local_path) { File.join(Dir.pwd, 'Guardfile') } + let(:user_path) { File.expand_path(File.join("~", '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_default_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_default_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_default_path.should == local_path + end + end + + end + describe ".guardfile_include?" do it "detects a guard specified by a string with double quotes" do subject.stub(:guardfile_contents => 'guard "test" {watch("c")}') @@ -230,6 +270,10 @@ private end" end + def mock_guardfile_content(content) + File.stub!(:read).with(subject.guardfile_default_path) { content } + end + def invalid_guardfile_string "Bad Guardfile" end