Permalink
Browse files

Merge branch 'user_guardfile' of https://github.com/hashrocket/guard

…into hashrocket-user_guardfile

Conflicts:
	lib/guard/dsl.rb
	spec/guard/dsl_spec.rb
  • Loading branch information...
2 parents 6a77f01 + a51afdf commit 1747f66128b3f3d5506617fd3a03870b491059d2 @thibaudgg thibaudgg committed May 28, 2011
Showing with 68 additions and 10 deletions.
  1. +5 −1 README.markdown
  2. +13 −3 lib/guard/dsl.rb
  3. +50 −6 spec/guard/dsl_spec.rb
View
@@ -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
+https://github.com/guard/guard/contributors
View
@@ -54,7 +54,7 @@ def fetch_guardfile_contents
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 @@ def fetch_guardfile_contents
UI.error "The command file(#{@@options[:guardfile]}) seems to be empty."
exit 1
end
-
+
guardfile_contents
end
@@ -76,7 +76,17 @@ def guardfile_contents_usable?
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
View
@@ -3,7 +3,8 @@
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 @@
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 @@
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,14 +46,20 @@
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
subject.guardfile_contents.should == "guard :foo"
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 @@
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 @@
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 @@ def valid_guardfile_string
end"
end
+ def mock_guardfile_content(content)
+ File.stub!(:read).with(subject.guardfile_default_path) { content }
+ end
+
def invalid_guardfile_string
"Bad Guardfile"
end

0 comments on commit 1747f66

Please sign in to comment.