diff --git a/lib/guard/guardfile.rb b/lib/guard/guardfile.rb index b3cfbe62f..51afcfa7a 100644 --- a/lib/guard/guardfile.rb +++ b/lib/guard/guardfile.rb @@ -30,6 +30,24 @@ def create_guardfile(options = {}) end end + # Opens an existing guardfile and searches for redundant definitions + # if extraneous defintions are found, it warns the user + # + # @see Guard::CLI.init + # + # @param [String] class name of gem definition that you would like to search for in the Guardfile + # @param [String] contents of existing guardfile + # + def duplicate_definitions?(guard_class, guard_file) + matches = guard_file.to_s.scan(/guard\s[\'|\"]#{guard_class}[\'|\"]\sdo/) + if matches.count > 1 + ::Guard::UI.info "There are #{matches.count.to_s} definitions in your Guardfile for '#{guard_class}', you may want to clean up your Guardfile as this could cause issues." + return true + else + return false + end + end + # Adds the Guardfile template of a Guard implementation # to an existing Guardfile. # @@ -42,6 +60,9 @@ def initialize_template(guard_name) if guard_class guard_class.init(guard_name) + guardfile_name = 'Guardfile' + guard_file = File.read(guardfile_name) if File.exists?(guardfile_name) + duplicate_definitions?(guard_name, guard_file) elsif File.exist?(File.join(HOME_TEMPLATES, guard_name)) content = File.read('Guardfile') template = File.read(File.join(HOME_TEMPLATES, guard_name)) diff --git a/spec/guard/guardfile_spec.rb b/spec/guard/guardfile_spec.rb index 0dbf285bc..231de7892 100644 --- a/spec/guard/guardfile_spec.rb +++ b/spec/guard/guardfile_spec.rb @@ -46,6 +46,24 @@ end end + describe ".duplicate_defintions?" do + context "that finds an existing Guardfile" do + context "that has duplicate definitions" do + it "should return true" do + io = StringIO.new("guard 'rspec' do\nend\nguard 'rspec' do\nend\n") + Guard::Guardfile.duplicate_definitions?('rspec', io.string).should == true + end + end + + context "that doesn't have duplicate definitions" do + it "should return false" do + io = StringIO.new("guard 'rspec' do\nend\n") + Guard::Guardfile.duplicate_definitions?('rspec', io.string).should == false + end + end + end + end + describe ".initialize_template" do context 'with an installed Guard implementation' do let(:foo_guard) { double('Guard::Foo').as_null_object }