Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix silent exits when using the --bare option with the init task

  • Loading branch information...
commit 688100ab6ddede9689e5754f0db414cdf021110d 1 parent ec38f53
@Maher4Ever authored
View
8 lib/guard.rb
@@ -28,10 +28,16 @@ class << self
#
# @see Guard::CLI.init
#
- def create_guardfile
+ # @param [Hash] options The options for creating a Guardfile
+ # @option options [Boolean] :abort_on_existence Whether to abort or not when a Guardfile already exists
+ #
+ def create_guardfile(options = {})
if !File.exist?('Guardfile')
::Guard::UI.info "Writing new Guardfile to #{ Dir.pwd }/Guardfile"
FileUtils.cp(GUARDFILE_TEMPLATE, 'Guardfile')
+ elsif options[:abort_on_existence]
+ ::Guard::UI.error "Guardfile already exists at #{ Dir.pwd }/Guardfile"
+ abort
end
end
View
2  lib/guard/cli.rb
@@ -122,7 +122,7 @@ def version
def init(guard_name = nil)
verify_bundler_presence
- ::Guard.create_guardfile
+ ::Guard.create_guardfile(:abort_on_existence => options[:bare])
return if options[:bare]
View
7 spec/guard/cli_spec.rb
@@ -138,13 +138,16 @@
end
describe '#init' do
+ let(:options) { {:bare => false} }
+
before do
+ subject.stub(:options => options)
::Guard.stub(:create_guardfile)
::Guard.stub(:initialize_all_templates)
end
it 'creates a Guardfile by delegating to Guard.create_guardfile' do
- ::Guard.should_receive(:create_guardfile)
+ ::Guard.should_receive(:create_guardfile).with(:abort_on_existence => options[:bare])
subject.init
end
@@ -161,7 +164,7 @@
end
context 'with the bare option' do
- before { subject.should_receive(:options).and_return({:bare => true}) }
+ let(:options) { {:bare => true} }
it 'Only creates the Guardfile and does not initialize any Guard template' do
::Guard.should_receive(:create_guardfile)
View
18 spec/guard_spec.rb
@@ -14,11 +14,25 @@
before { File.should_receive(:exist?).and_return true }
it "does not copy the Guardfile template or notify the user" do
- ::Guard::UI.should_not_receive(:info).with('Writing new Guardfile to /home/user/Guardfile')
- FileUtils.should_not_receive(:cp).with(an_instance_of(String), 'Guardfile')
+ ::Guard::UI.should_not_receive(:info)
+ FileUtils.should_not_receive(:cp)
subject.create_guardfile
end
+
+ it "does not display any kind of error or abort" do
+ ::Guard::UI.should_not_receive(:error)
+ subject.should_not_receive(:abort)
+ subject.create_guardfile
+ end
+
+ context "with the :abort_on_existence option set to true" do
+ it "displays an error message and aborts the process" do
+ ::Guard::UI.should_receive(:error).with("Guardfile already exists at /home/user/Guardfile")
+ subject.should_receive(:abort)
+ subject.create_guardfile(:abort_on_existence => true)
+ end
+ end
end
context "without an existing Guardfile" do
Please sign in to comment.
Something went wrong with that request. Please try again.