Allow user-based Guardfiles #67

Merged
merged 5 commits into from May 28, 2011
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
@@ -13,7 +13,7 @@ def evaluate_guardfile(options = {})
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 @@ def guardfile_include?(guard_name)
end
def guardfile_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
@@ -8,9 +8,8 @@
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,38 @@
lambda { subject.evaluate_guardfile }.should raise_error
end
+ describe ".guardfile_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_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 +148,7 @@
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